/* * Mbuni - Open Source MMS Gateway * * MMS Global Queue Runner, routes messages generally. * * Copyright (C) 2003 - 2005, Digital Solutions Ltd. - http://www.dsmagic.com * * Paul Bagyenda * * This program is free software, distributed under the terms of * the GNU General Public License, with a few exceptions granted (see LICENSE) */ #include "mmsrelay.h" #define NMAX 256 static char mobile_qdir[NMAX]; static char mm4_qdir[NMAX]; static char sendmail_cmd[NMAX]; /* Set the queue directory for messages going to mobile. */ static int mms_setmobile_queuedir(char *mqdir); /* Set the queue directory for messages going to remote proxies. */ static int mms_setmm4_queuedir(char *mqdir); /* Send command for sending mail. It will be called as with * headers and message on stdin. * * The following % formatting characters are allowed: * f - from address * t - recipient * s - subject * m - message id * */ static int mms_setsendmail_cmd(char *sendmail); /* Queue this message for delivery to mobile terminal. */ static int mms_sendtomobile(Octstr *from, Octstr *to, Octstr *subject, Octstr *fromproxy, Octstr *msgid, time_t expires, MmsMsg *m, int dlr, Octstr **error); /* Send this message via an intermediate proxy (MM4 interface). * The caller must modify the MmsMsg sender and recipient address if necessary. */ static int mms_sendtoproxy(Octstr *from, Octstr *to, Octstr *subject, Octstr *proxy, Octstr *msgid, time_t expires, MmsMsg *m, int dlr, Octstr **error); static int mms_sendtovasp(MmsVasp *vasp, Octstr *from, Octstr *to, Octstr *msgId, MmsMsg *m, Octstr **error); static int _x_octstr_int_compare(int n, Octstr *s); /* Send errors */ #define MMS_SEND_OK 0 #define MMS_SEND_ERROR_TRANSIENT -1 #define MMS_SEND_ERROR_FATAL -2 #define NMAX 256 static char qdir[NMAX]; static List *cdr_list; /* List for cdr as used by cdr consumer thread. */ static int sendMsg(MmsEnvelope *e) { int i, n; MmsMsg *msg = NULL; if (e->msgtype == MMS_MSGTYPE_SEND_REQ && !e->bill.billed) { /* Attempt to bill if not already billed */ List *l = gwlist_create(); double amt; for (i = 0, n = gwlist_len(e->to); i < n; i++) { MmsEnvelopeTo *to = gwlist_get(e->to, i); gwlist_append(l, to->rcpt); } amt = settings->mms_billfuncs->mms_billmsg(e->from, l, e->msize, e->vaspid, settings->mms_bill_module_data); gwlist_destroy(l, NULL); info(0, "Global Queue MMS Bill: From %s, to_count=%ld, msgid=%s, msgsize=%ld: returned=%.2f", octstr_get_cstr(e->from), gwlist_len(e->to), e->msgId ? octstr_get_cstr(e->msgId) : "", e->msize, amt); if (amt == -1) { /* Delete message. */ for (i = 0, n = gwlist_len(e->to); i < n; i++) { MmsEnvelopeTo *to = gwlist_get(e->to, i); to->process = 0; } } else if (amt >= 0) { e->bill.billed = 1; e->bill.amt = amt; } if (amt >= -1) if (mms_queue_update(e) == 1) /* Write queue just in case we crash. */ e = NULL; if (e == NULL || !e->bill.billed) goto done2; /* If queue is gone, or we didn't manage to bill, go away */ } msg = mms_queue_getdata(e); #if 0 if (msg) mms_msgdump(msg,1); #endif for (i = 0, n = gwlist_len(e->to); i < n; i++) { Octstr *err = NULL; int res = MMS_SEND_OK, m; MmsEnvelopeTo *to = gwlist_get(e->to, i); time_t tnow = time(NULL); if (!to || !to->process) /* Already processed. */ continue; if (e->expiryt != 0 && /* Handle message expiry. */ e->expiryt < tnow) { err = octstr_format("MMSC error: Message expired while sending to %S!", to->rcpt); res = MMS_SEND_ERROR_FATAL; goto done; } else if (e->attempts >= settings->maxsendattempts) { err = octstr_format("MMSC error: Failed to deliver to %S after %ld attempts. (max attempts allowed is %ld)!", to->rcpt, e->attempts, settings->maxsendattempts); res = MMS_SEND_ERROR_FATAL; goto done; } /* - first check if it is an email address. * - Next check if proxy to send through is already set, use it. * - else we have a number of IP, try to deliver... */ if (octstr_search_char(to->rcpt, '@', 0) > 0) { if (settings->mms2email) res = mms_sendtovasp(settings->mms2email, e->from, to->rcpt, e->msgId, msg, &err); else res = mms_sendtoemail(e->from, to->rcpt, e->subject ? e->subject : settings->mms_email_subject, e->msgId, msg, e->dlr, &err, sendmail_cmd, settings->hostname, 1, 1, octstr_get_cstr(settings->mms_email_txt), octstr_get_cstr(settings->mms_email_html), 1); if (res == MMS_SEND_OK) mms_log2("Sent", e->from, to->rcpt, -1, e->msgId, NULL, NULL, "MM3", NULL,NULL); } else if (e->viaproxy && octstr_len(e->viaproxy) > 0) res = mms_sendtoproxy(e->from, to->rcpt, e->subject, e->viaproxy, e->msgId, e->expiryt, msg, e->dlr, &err); else { int j = octstr_case_search(to->rcpt, octstr_imm("/TYPE=PLMN"), 0); int k = octstr_case_search(to->rcpt, octstr_imm("/TYPE=IPv"), 0); int len = octstr_len(to->rcpt); Octstr *phonenum = NULL; Octstr *mmsc; int sent = 0; MmsVasp *vasp; /* If it is an IP, send to mobile handler, else if number, look for recipient. */ if (j > 0 && j - 1 + sizeof "/TYPE=PLMN" == len) phonenum = octstr_copy(to->rcpt, 0, j); else if (k > 0 && k + sizeof "/TYPE=IPv" == len) { if (settings->mms2mobile && e->msgtype == MMS_MSGTYPE_SEND_REQ) { /* Send a copy to this VASP. */ Octstr *xerr = NULL; int res = mms_sendtovasp(settings->mms2mobile, e->from, to->rcpt, e->msgId, msg, &err); info(0, "%s Global Queue MMS Send: Local Msg copy to VASP (%s) - " "From %s, to %s, msgsize=%ld: err=%s", SEND_ERROR_STR(res), octstr_get_cstr(settings->mms2mobile->id), octstr_get_cstr(e->from), octstr_get_cstr(to->rcpt), e->msize, xerr ? octstr_get_cstr(xerr) : "(null)"); if (xerr) octstr_destroy(xerr); } res = mms_sendtomobile(e->from, to->rcpt, e->subject, e->fromproxy, e->msgId, e->expiryt, msg, e->dlr, &err); sent = 1; goto done; } else { /* We don't handle other types for now. */ err = octstr_format("MMSC error: Unsupported recipient type %S", to->rcpt); res = MMS_SEND_ERROR_FATAL; goto done; } /* Search VASP list, see what you can find... */ for (j = 0, m = gwlist_len(settings->vasp_list); j < m; j++) if ((vasp = gwlist_get(settings->vasp_list, j)) != NULL && _x_octstr_int_compare(vasp->short_code, phonenum) == 0) { res = mms_sendtovasp(vasp, e->from, to->rcpt, e->msgId, msg, &err); sent = 1; break; } if (sent != 1) { /* Not yet, sent, find the receiver MMSC. */ /* Normalise the number, then see if we can resolve home MMSC for this recipient. */ normalize_number(octstr_get_cstr(settings->unified_prefix), &phonenum); if ((mmsc = settings->mms_resolvefuncs->mms_resolve(phonenum, settings->mms_resolver_module_data, settings, proxyrelays))) { info(0, "mmsc for \"%s\" resolved to: \"%s\"", octstr_get_cstr(phonenum), octstr_get_cstr(mmsc)); if (octstr_compare(mmsc, settings->hostname) == 0) { if (settings->mms2mobile && e->msgtype == MMS_MSGTYPE_SEND_REQ) { /* Send a copy to this VASP. */ Octstr *xerr = NULL; int res = mms_sendtovasp(settings->mms2mobile, e->from, to->rcpt, e->msgId, msg, &err); info(0, "%s Global Queue MMS Send: Local Msg copy to VASP (%s) - " "From %s, to %s, msgsize=%ld: err=%s", SEND_ERROR_STR(res), octstr_get_cstr(settings->mms2mobile->id), octstr_get_cstr(e->from), octstr_get_cstr(to->rcpt), e->msize, xerr ? octstr_get_cstr(xerr) : "(null)"); if (xerr) octstr_destroy(xerr); } res = mms_sendtomobile(e->from, to->rcpt, e->subject, e->fromproxy, e->msgId, e->expiryt, msg, e->dlr, &err); sent = 1; } else if (proxyrelays && gwlist_len(proxyrelays) > 0) /* Step through proxies. */ for (j = 0, m = gwlist_len(proxyrelays); jhost, mmsc)) { res = mms_sendtoproxy(e->from, to->rcpt, e->subject, mp->host, e->msgId, e->expiryt, msg, e->dlr, &err); sent = 1; break; } } octstr_destroy(mmsc); } } if (!sent) { res = MMS_SEND_ERROR_FATAL; err = octstr_format("MMSC error: Don't know how to deliver to %S !", to->rcpt); } if (phonenum) octstr_destroy(phonenum); } done: if (res == MMS_SEND_OK) to->process = 0; else { /* If there was a report request, queue it. */ if (e->dlr) { Octstr *qfs; MmsMsg *m = mms_deliveryreport(e->msgId, to->rcpt, tnow, (e->expiryt != 0 && e->expiryt < tnow) ? octstr_imm("Expired") : octstr_imm("Rejected")); List *l = gwlist_create(); gwlist_append(l, octstr_duplicate(e->from)); /* Add to queue, switch via proxy to be from proxy. */ qfs = mms_queue_add(settings->system_user, l, err, NULL, e->fromproxy, tnow, tnow+settings->default_msgexpiry, m, NULL, NULL, NULL, NULL, NULL, NULL, 0, qdir, settings->host_alias); octstr_destroy(qfs); gwlist_destroy(l, NULL); mms_destroy(m); } if (res == MMS_SEND_ERROR_FATAL) to->process = 0; /* No more attempts. */ } /* Write to log */ info(0, "%s Global Queue MMS Send: From %s, to %s, msgsize=%ld: err=%s", SEND_ERROR_STR(res), octstr_get_cstr(e->from), octstr_get_cstr(to->rcpt), e->msize, err ? octstr_get_cstr(err) : "(null)"); if (res == MMS_SEND_OK) { /* Do CDR writing. */ MmsCdrStruct *cdr = gw_malloc(sizeof *cdr); cdr->module_data = settings->mms_bill_module_data; cdr->sdate = e->created; strncpy(cdr->from, octstr_get_cstr(e->from), sizeof cdr->from); strncpy(cdr->to, octstr_get_cstr(to->rcpt), sizeof cdr->to); strncpy(cdr->msgid, e->msgId ? octstr_get_cstr(e->msgId) : "", sizeof cdr->msgid); strncpy(cdr->vaspid, e->vaspid ? octstr_get_cstr(e->vaspid) : "", sizeof cdr->vaspid); cdr->msg_size = e->msize; gwlist_produce(cdr_list, cdr); /* Put it on list so sending thread sends it. */ } /* Update queue entry so that we know which ones have been processed. */ e->lasttry = tnow; if (mms_queue_update(e) == 1) { e = NULL; break; /* Queue entry gone. */ } } done2: if (msg) mms_destroy(msg); if (e) { /* Update the queue if it is still valid (e.g. recipients not handled) XXX can this happen here??... */ e->lasttry = time(NULL); e->attempts++; /* Update count of number of delivery attempts. */ e->sendt = e->lasttry + settings->send_back_off * e->attempts; if (mms_queue_update(e) != 1) mms_queue_free_env(e); } return 1; /* Always deletes the queue entry. */ } static void cdr_thread(void *unused) { MmsCdrStruct *cdr; while ((cdr = gwlist_consume(cdr_list)) != NULL) { settings->mms_billfuncs->mms_logcdr(cdr); /* We should probably write to log here... */ gw_free(cdr); } } void mbuni_global_queue_runner(int *rstop) { mms_setmobile_queuedir(octstr_get_cstr(settings->mm1_queuedir)); mms_setmm4_queuedir(octstr_get_cstr(settings->mm4_queuedir)); mms_setsendmail_cmd(octstr_get_cstr(settings->sendmail)); strncpy(qdir, octstr_get_cstr(settings->global_queuedir), sizeof qdir); /* Start the thread for CDR */ cdr_list = gwlist_create(); gwlist_add_producer(cdr_list); gwthread_create(cdr_thread, NULL); mms_queue_run(qdir, sendMsg, settings->queue_interval, settings->maxthreads, rstop); /* When it ends, wait a little for other stuff to stop... */ sleep(2); gwlist_remove_producer(cdr_list); /* Stop CDR thread. */ return; } static int mms_setmobile_queuedir(char *mqdir) { strncpy(mobile_qdir, mqdir, sizeof mobile_qdir); return 0; } static int mms_setmm4_queuedir(char *mqdir) { strncpy(mm4_qdir, mqdir, sizeof mm4_qdir); return 0; } static int mms_setsendmail_cmd(char *sendmail) { strncpy(sendmail_cmd, sendmail, -1 + sizeof sendmail_cmd); return 0; } /* * Queue this message for delivery to mobile terminal. * A queue thread will handle sending of notifications to phone. * When a deliver is received, another thread will remove the queue entry. */ int mms_sendtomobile(Octstr *from, Octstr *to, Octstr *subject, Octstr *fromproxy, Octstr *msgid, time_t expires, MmsMsg *m, int dlr, Octstr **error) { Octstr *ret, *x; List *l = gwlist_create(); char tokenstr[128]; gwlist_append(l, to); /* We generate a special token that will be added to message ID to make * stealing messages a bit harder. */ snprintf(tokenstr, -1 + sizeof tokenstr, "wx%ld", random() % 100); x = octstr_create(tokenstr); ret = mms_queue_add(from, l, subject, fromproxy, NULL, 0, expires, m, x, NULL, NULL, NULL, NULL, NULL, dlr, mobile_qdir, settings->host_alias); octstr_destroy(x); gwlist_destroy(l, NULL); octstr_destroy(ret); if (ret == NULL) return MMS_SEND_ERROR_TRANSIENT; else return MMS_SEND_OK; } /* Send this message via an intermediate proxy (MM4 interface). * The way it works: We email the message to the proxy but we also keep a local copy in the queue * so we can handle delivery receipts and such. */ static int mms_sendtoproxy(Octstr *from, Octstr *to, Octstr *subject, Octstr *proxy, Octstr *msgid, time_t expires, MmsMsg *msg, int dlr, Octstr **error) { Octstr *pto; int x; if (!to || octstr_search_char(to, '@', 0) >= 0) { *error = octstr_format("Bad recipient address sent to MM4 interface, addresss is %S!", to); return MMS_SEND_ERROR_FATAL; } if (mms_messagetype(msg) == MMS_MSGTYPE_SEND_REQ) { /* Only queue these ones for future response. */ List *l = gwlist_create(); Octstr *ret; gwlist_append(l, to); ret = mms_queue_add(from, l, subject, NULL, proxy, 0, expires, msg,NULL, NULL, NULL, NULL, NULL, NULL, dlr, mm4_qdir, settings->host_alias); gwlist_destroy(l, NULL); if (ret == NULL) { *error = octstr_format("MM4: Failed to queue message to %S for future tracking. ", to); return MMS_SEND_ERROR_TRANSIENT; } octstr_destroy(ret); } pto = octstr_format("%S@%S", to, proxy); x = mms_sendtoemail(from, pto, subject ? subject : settings->mms_email_subject, msgid, msg, 0, error, sendmail_cmd, settings->hostname, 0, 0,NULL,NULL,0); mms_log2("Sent", from, to, -1, msgid, NULL, proxy, "MM4", NULL,NULL); octstr_destroy(pto); return x; } static int _x_octstr_int_compare(int n, Octstr *s) { char x[64]; sprintf(x, "%d", n); return octstr_str_compare(s,x); } static int mm7soap_send(MmsVasp *vasp, Octstr *from, Octstr *to, Octstr *msgId, MmsMsg *m, Octstr **error) { int ret = MMS_SEND_ERROR_TRANSIENT; int mtype = mms_messagetype(m); int hstatus = HTTP_OK, tstatus; List *xto = gwlist_create(); MSoapMsg_t *mreq = NULL, *mresp = NULL; List *rh = NULL, *ph = NULL; Octstr *body = NULL, *rbody = NULL, *url = NULL; HTTPCaller *caller = http_caller_create(); void *xx; Octstr *s; info(0, "MMS Relay: Send[soap] to VASP[%s], msg type [%s], from %s, to %s", vasp ? octstr_get_cstr(vasp->id) : "", mms_message_type_to_cstr(mtype), octstr_get_cstr(from), octstr_get_cstr(to)); gwlist_append(xto, to); if ((mreq = mm7_mmsmsg_to_soap(m, from, xto, msgId, settings->host_alias, msgId, 0, NULL, NULL)) == NULL) { *error = octstr_format("Failed to convert Msg[%s] 2 SOAP message!", mms_message_type_to_cstr(mtype)); goto done1; } if (mm7_soapmsg_to_httpmsg(mreq, &rh, &body) < 0) { *error = octstr_format("Failed to convert SOAP message 2 HTTP Msg!"); goto done1; } http_start_request(caller, HTTP_METHOD_POST, vasp->vasp_url, rh, body, 1, NULL, NULL); if ((xx = http_receive_result_real(caller, &hstatus, &url, &ph, &rbody,1)) == NULL || http_status_class(hstatus) != HTTP_STATUS_SUCCESSFUL) { *error = octstr_format("Failed to contact VASP[url=%s] => HTTP returned status = %d, id=%s !", octstr_get_cstr(vasp->vasp_url), hstatus, xx ? "Ok" : "not OK"); goto done1; } if ((mresp = mm7_parse_soap(ph, rbody)) == NULL) { *error = octstr_format("Failed to parse VASP[url=%s, id=%s] response!", octstr_get_cstr(vasp->vasp_url), octstr_get_cstr(vasp->id)); goto done1; } /* Now look at response code and use it to tell you what you want. */ if ((s = mm7_soap_header_value(mresp, octstr_imm("StatusCode"))) != NULL) { tstatus = atoi(octstr_get_cstr(s)); octstr_destroy(s); } else tstatus = MM7_SOAP_FORMAT_CORRUPT; if (!MM7_SOAP_STATUS_OK(tstatus)) { Octstr *detail = mm7_soap_header_value(mresp, octstr_imm("Details")); ret = MMS_SEND_ERROR_FATAL; info(0, "Send to VASP[%s], failed, code=[%d=>%s], detail=%s", vasp ? octstr_get_cstr(vasp->id) : "", tstatus, mms_soap_status_to_cstr(tstatus), detail ? octstr_get_cstr(detail) : ""); *error = octstr_format("Failed to deliver to VASP[url=%s, id=%s], status=[%d=>%s]!", octstr_get_cstr(vasp->vasp_url), octstr_get_cstr(vasp->id), tstatus, mms_soap_status_to_cstr(tstatus)); if (detail) octstr_destroy(detail); } else ret = MMS_SEND_OK; info(0, "Sent to VASP[%s], code=[%d=>%s]", octstr_get_cstr(vasp->id), tstatus, mms_soap_status_to_cstr(tstatus)); done1: if (mreq) mm7_soap_destroy(mreq); if (mresp) mm7_soap_destroy(mresp); if (rh) http_destroy_headers(rh); if (body) octstr_destroy(body); if (ph) http_destroy_headers(ph); if (rbody) octstr_destroy(rbody); if (url) octstr_destroy(url); http_caller_destroy(caller); gwlist_destroy(xto, NULL); return ret; } static int mm7eaif_send(MmsVasp *vasp, Octstr *from, Octstr *to, Octstr *msgid, MmsMsg *m, Octstr **error) { int ret = MMS_SEND_ERROR_TRANSIENT; int mtype = mms_messagetype(m); int hstatus = HTTP_OK; List *rh = http_create_empty_headers(), *ph = NULL; Octstr *body = NULL, *rbody = NULL, *url = NULL; HTTPCaller *caller = http_caller_create(); void *xx; char *msgtype; info(0, "MMS Relay: Send [eaif] to VASP[%s], msg type [%s], from %s, to %s", vasp ? octstr_get_cstr(vasp->id) : "", mms_message_type_to_cstr(mtype), octstr_get_cstr(from), octstr_get_cstr(to)); http_header_add(rh, "X-NOKIA-MMSC-To", octstr_get_cstr(to)); http_header_add(rh, "X-NOKIA-MMSC-From", octstr_get_cstr(from)); if (msgid) http_header_add(rh, "X-NOKIA-MMSC-Message-Id", octstr_get_cstr(msgid)); http_header_add(rh, "X-NOKIA-MMSC-Version", EAIF_VERSION); if (mtype == MMS_MSGTYPE_SEND_REQ || mtype == MMS_MSGTYPE_RETRIEVE_CONF) { msgtype = "MultiMediaMessage"; mms_make_sendreq(m); /* ensure it is a sendreq. */ } else if (mtype == MMS_MSGTYPE_DELIVERY_IND) msgtype = "DeliveryReport"; else msgtype = "ReadReply"; http_header_add(rh, "X-NOKIA-MMSC-Message-Type", msgtype); http_header_add(rh, "Content-Type", "application/vnd.wap.mms-message"); body = mms_tobinary(m); http_start_request(caller, HTTP_METHOD_POST, vasp->vasp_url, rh, body, 1, NULL, NULL); if ((xx = http_receive_result_real(caller, &hstatus, &url, &ph, &rbody,1)) == NULL || http_status_class(hstatus) != HTTP_STATUS_SUCCESSFUL) { *error = octstr_format("Failed to contact VASP[url=%s] => HTTP returned status = %d, id=%s !", octstr_get_cstr(vasp->vasp_url), hstatus, xx ? "Ok" : "not OK"); } else info(0, "Sent to VASP[%s], code=[%d]", octstr_get_cstr(vasp->id), hstatus); if (hstatus < 0) ret = MMS_SEND_ERROR_TRANSIENT; else { hstatus = http_status_class(hstatus); if (hstatus == HTTP_STATUS_CLIENT_ERROR) ret = MMS_SEND_ERROR_TRANSIENT; else if (hstatus == HTTP_STATUS_SERVER_ERROR) ret = MMS_SEND_ERROR_FATAL; else ret = MMS_SEND_OK; } if (rh) http_destroy_headers(rh); if (body) octstr_destroy(body); if (ph) http_destroy_headers(ph); if (rbody) octstr_destroy(rbody); if (url) octstr_destroy(url); http_caller_destroy(caller); return ret; } static int mms_sendtovasp(MmsVasp *vasp, Octstr *from, Octstr *to, Octstr *msgid, MmsMsg *m, Octstr **err) { if (vasp->type == SOAP_VASP) return mm7soap_send(vasp, from, to, msgid, m, err); else if (vasp->type == EAIF_VASP) return mm7eaif_send(vasp, from, to, msgid, m, err); else { error(0, "Vasp[%s] of unknown type, can't send!", vasp->id ? octstr_get_cstr(vasp->id) : ""); return MMS_SEND_ERROR_FATAL; } }