2005-09-07 10:10:41 +00:00
|
|
|
/*
|
|
|
|
* Mbuni - Open Source MMS Gateway
|
|
|
|
*
|
|
|
|
* MMSBox Dlr: Dlr storage for MMSBox
|
|
|
|
*
|
2007-12-21 10:14:08 +00:00
|
|
|
* Copyright (C) 2003 - 2008, Digital Solutions Ltd. - http://www.dsmagic.com
|
2005-09-07 10:10:41 +00:00
|
|
|
*
|
|
|
|
* Paul Bagyenda <bagyenda@dsmagic.com>
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License, with a few exceptions granted (see LICENSE)
|
|
|
|
*/
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2007-12-21 10:14:08 +00:00
|
|
|
#ifdef SunOS
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-09-07 10:10:41 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "mmsbox.h"
|
|
|
|
#include "mms_queue.h"
|
|
|
|
|
|
|
|
#define MAXTRIES 10
|
2007-04-02 05:29:00 +00:00
|
|
|
static int dlr_entry_fname(char *msgid, char *rtype, Octstr *mmc_gid, Octstr **efname)
|
2005-09-07 10:10:41 +00:00
|
|
|
{
|
|
|
|
char d1[2], d2[3], fbuf[512], *p;
|
|
|
|
unsigned long h = _mshash(msgid);
|
|
|
|
Octstr *t, *s;
|
|
|
|
int i, fd;
|
|
|
|
|
|
|
|
/* Make toplevel dir. */
|
|
|
|
d1[0] = _TT[h%_TTSIZE];
|
|
|
|
d1[1] = '\0';
|
|
|
|
|
|
|
|
/* Then lower level. */
|
|
|
|
h /= _TTSIZE;
|
|
|
|
d2[0] = _TT[h%_TTSIZE];
|
|
|
|
h /= _TTSIZE;
|
|
|
|
d2[1] = _TT[h%_TTSIZE];
|
|
|
|
d2[2] = '\0';
|
|
|
|
|
|
|
|
/* Try and create the next level dir (first level was created by root_init) */
|
|
|
|
sprintf(fbuf, "%s/%s/%s", octstr_get_cstr(dlr_dir), d1, d2);
|
|
|
|
if (mkdir(fbuf,
|
|
|
|
S_IRWXU|S_IRWXG) < 0 &&
|
|
|
|
errno != EEXIST) {
|
2008-09-04 17:20:42 +00:00
|
|
|
mms_error(0, "mmsbox", NULL, "failed to create dir [%s] "
|
2005-09-07 10:10:41 +00:00
|
|
|
"while initialising dlr dir for %s: %s!",
|
|
|
|
fbuf, msgid, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-02 05:29:00 +00:00
|
|
|
t = octstr_format("%S-%s_%s", mmc_gid, msgid, rtype); /* Put mmc id into name. */
|
2005-09-07 10:10:41 +00:00
|
|
|
|
|
|
|
octstr_replace(t, octstr_imm("/"), octstr_imm("$")); /* XXX safe in all cases?? */
|
|
|
|
s = octstr_format("%s/%S", fbuf, t);
|
|
|
|
octstr_destroy(t);
|
|
|
|
|
|
|
|
|
|
|
|
p = octstr_get_cstr(s);
|
|
|
|
i = 0;
|
|
|
|
do
|
|
|
|
if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)) < 0) {
|
2008-09-04 17:20:42 +00:00
|
|
|
mms_error(0, "mmsbox", NULL, "Failed to open DLR URL store file [%s], error: %s!",
|
2005-09-07 10:10:41 +00:00
|
|
|
p, strerror(errno));
|
|
|
|
break;
|
|
|
|
} else if (mm_lockfile(fd, p, 1) != 0) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
while (i++ < MAXTRIES && fd < 0);
|
|
|
|
|
|
|
|
if (efname)
|
|
|
|
*efname = s;
|
|
|
|
else
|
|
|
|
octstr_destroy(s);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-08-06 11:57:15 +00:00
|
|
|
void mms_dlr_url_put(Octstr *msgid, char *rtype, Octstr *mmc_gid, Octstr *dlr_url, Octstr *transid)
|
2005-09-07 10:10:41 +00:00
|
|
|
{
|
2007-04-02 05:29:00 +00:00
|
|
|
int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_gid, NULL);
|
2005-09-07 10:10:41 +00:00
|
|
|
|
|
|
|
if (fd >= 0) {
|
2007-08-06 11:57:15 +00:00
|
|
|
Octstr *x = octstr_format("%S %S", transid ? transid : octstr_imm("x"), dlr_url); /* better have no spaces in transid! */
|
|
|
|
octstr_write_data(x, fd, 0);
|
2005-09-07 10:10:41 +00:00
|
|
|
close(fd);
|
2007-08-06 11:57:15 +00:00
|
|
|
octstr_destroy(x);
|
2005-09-07 10:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-06 11:57:15 +00:00
|
|
|
int mms_dlr_url_get(Octstr *msgid, char *rtype, Octstr *mmc_gid, Octstr **dlr_url, Octstr **transid)
|
2005-09-07 10:10:41 +00:00
|
|
|
{
|
2007-04-02 05:29:00 +00:00
|
|
|
int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_gid, NULL);
|
2005-09-07 10:10:41 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (fd >= 0 && (f = fdopen(fd, "r+")) != NULL) {
|
|
|
|
Octstr *s = octstr_read_pipe(f);
|
2007-08-06 11:57:15 +00:00
|
|
|
int i, ret;
|
|
|
|
|
2005-09-07 10:10:41 +00:00
|
|
|
fclose(f);
|
2005-09-12 09:51:31 +00:00
|
|
|
if (s && octstr_len(s) == 0) {
|
2007-08-06 11:57:15 +00:00
|
|
|
ret = -1;
|
|
|
|
} else if ((i = octstr_search_char(s, ' ', 0)) >= 0) {
|
|
|
|
*transid = octstr_copy(s, 0, i);
|
|
|
|
*dlr_url = octstr_copy(s, i+1, octstr_len(s));
|
|
|
|
ret = 0;
|
2005-09-12 09:51:31 +00:00
|
|
|
} else
|
2007-08-06 11:57:15 +00:00
|
|
|
ret = -1;
|
|
|
|
octstr_destroy(s);
|
|
|
|
return ret;
|
2005-09-07 10:10:41 +00:00
|
|
|
} else if (fd >= 0)
|
|
|
|
close(fd);
|
2007-08-06 11:57:15 +00:00
|
|
|
return -1;
|
2005-09-07 10:10:41 +00:00
|
|
|
}
|
|
|
|
|
2007-04-02 05:29:00 +00:00
|
|
|
void mms_dlr_url_remove(Octstr *msgid, char *rtype, Octstr *mmc_gid)
|
2005-09-07 10:10:41 +00:00
|
|
|
{
|
|
|
|
Octstr *fname = NULL;
|
2007-04-02 05:29:00 +00:00
|
|
|
int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_gid, &fname);
|
2005-09-07 10:10:41 +00:00
|
|
|
|
2008-09-13 06:37:24 +00:00
|
|
|
if (fname) {
|
2005-09-07 10:10:41 +00:00
|
|
|
unlink(octstr_get_cstr(fname));
|
2008-09-13 06:37:24 +00:00
|
|
|
octstr_destroy(fname);
|
|
|
|
}
|
2005-09-07 10:10:41 +00:00
|
|
|
if (fd >= 0)
|
2008-09-13 06:37:24 +00:00
|
|
|
close(fd);
|
|
|
|
|
2005-09-07 10:10:41 +00:00
|
|
|
}
|