Kannel: Open Source WAP and SMS gateway  svn-r5335
xidris.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*----------------------------------------------------------------
58  * 3united.com (formerly Xidris) - An austrian (AT) SMS aggregator
59  * Implementing version 1.3, 2003-05-06
60  * Updating to version 1.9.1, 2004-09-28
61  *
62  * Stipe Tolj <stolj@wapme.de>
63  */
64 
65 #include "gwlib/gwlib.h"
66 #include "smscconn.h"
67 #include "smscconn_p.h"
68 #include "bb_smscconn_cb.h"
69 #include "msg.h"
70 #include "sms.h"
71 #include "dlr.h"
72 #include "urltrans.h"
73 #include "meta_data.h"
74 
75 #include "../smsc_http_p.h"
76 
77 /*
78  * Parse for an parameter of an given XML tag and return it as Octstr
79  */
80 static Octstr *parse_xml_tag(Octstr *body, Octstr *tag)
81 {
82  Octstr *stag, *etag, *ret;
83  int spos, epos;
84 
85  stag = octstr_format("<%s>", octstr_get_cstr(tag));
86  if ((spos = octstr_search(body, stag, 0)) == -1) {
87  octstr_destroy(stag);
88  return NULL;
89  }
90  etag = octstr_format("</%s>", octstr_get_cstr(tag));
91  if ((epos = octstr_search(body, etag, spos+octstr_len(stag))) == -1) {
92  octstr_destroy(stag);
93  octstr_destroy(etag);
94  return NULL;
95  }
96 
97  ret = octstr_copy(body, spos+octstr_len(stag), epos+1 - (spos+octstr_len(etag)));
99  octstr_strip_crlfs(ret);
100 
101  octstr_destroy(stag);
102  octstr_destroy(etag);
103 
104  return ret;
105 }
106 
107 /* MT related function */
108 static int xidris_send_sms(SMSCConn *conn, Msg *sms)
109 {
110  ConnData *conndata = conn->data;
111  Octstr *url, *new_msg;
112  List *headers;
113  int dcs, esm_class;
114 
115  url = new_msg = NULL;
116  dcs = esm_class = 0;
117 
118  /* format the URL for call */
119  url = octstr_format("%S?"
120  "app_id=%E&key=%E&dest_addr=%E&source_addr=%E",
122  conndata->password, sms->sms.receiver, sms->sms.sender);
123 
124  if (octstr_len(sms->sms.udhdata)) {
125  /* RAW additions for binary (8bit) msgs */
126 
127  /* set the data coding scheme (DCS) and ESM class fields */
128  dcs = fields_to_dcs(sms, sms->sms.alt_dcs);
129  /* ESM_CLASS_SUBMIT_STORE_AND_FORWARD_MODE |
130  ESM_CLASS_SUBMIT_UDH_INDICATOR */
131  esm_class = 0x03 | 0x40;
132 
133  /* prepend UDH header to message block */
134  new_msg = octstr_duplicate(sms->sms.udhdata);
135  octstr_append(new_msg, sms->sms.msgdata);
136 
137  octstr_format_append(url, "&type=200&dcs=%d&esm=%d&message=%H",
138  dcs, esm_class, new_msg);
139  } else {
140  /* additions for text (7bit) msgs */
141 
142  octstr_format_append(url, "&type=%E&message=%E",
143  (sms->sms.mclass ? octstr_imm("1") : octstr_imm("0")),
144  sms->sms.msgdata);
145  }
146 
147  /*
148  * We use &account=<foobar> from sendsms interface to encode any additionaly
149  * proxied parameters, ie. billing information.
150  */
151  if (octstr_len(sms->sms.account)) {
152  octstr_url_decode(sms->sms.account);
153  octstr_format_append(url, "&%s", octstr_get_cstr(sms->sms.account));
154  }
155 
156  headers = gwlist_create();
157  debug("smsc.http.xidris", 0, "HTTP[%s]: Sending request <%s>",
159 
161  NULL, 0, sms, NULL);
162 
164  octstr_destroy(new_msg);
165  http_destroy_headers(headers);
166 
167  return 0;
168 }
169 
170 
171 static void xidris_parse_reply(SMSCConn *conn, Msg *msg, int status,
172  List *headers, Octstr *body)
173 {
174  Octstr *code, *desc, *mid;
175 
176  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
177  /* now parse the XML document for error code */
178  code = parse_xml_tag(body, octstr_imm("status"));
179  desc = parse_xml_tag(body, octstr_imm("description"));
180 
181  /* The following parsing assumes we get only *one* message id in the
182  * response XML. Which is ok, since we garantee via previous concat
183  * splitting, that we only pass PDUs of 1 SMS size to SMSC. */
184  mid = parse_xml_tag(body, octstr_imm("message_id"));
185 
186  if (octstr_case_compare(code, octstr_imm("0")) == 0 && mid != NULL) {
187  /* ensure the message id gets logged */
188  msg->sms.binfo = octstr_duplicate(mid);
189 
190  /* SMSC ACK.. now we have the message id. */
191  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
192  dlr_add(conn->id, mid, msg, 0);
193 
194  octstr_destroy(mid);
195  bb_smscconn_sent(conn, msg, NULL);
196 
197  } else {
198  error(0, "HTTP[%s]: Message not accepted. Status code <%s> "
199  "description `%s'.", octstr_get_cstr(conn->id),
203  }
204  } else {
205  error(0, "HTTP[%s]: Message was rejected. SMSC response was:",
206  octstr_get_cstr(conn->id));
207  octstr_dump(body, 0);
210  }
211 }
212 
213 /* MO related function */
215  List *headers, Octstr *body, List *cgivars)
216 {
217  ConnData *conndata = conn->data;
218  Octstr *user, *pass, *from, *to, *text, *account, *binfo;
219  Octstr *state, *mid, *dest;
220  Octstr *retmsg;
221  int mclass, mwi, coding, validity, deferred;
222  List *reply_headers;
223  int ret, status;
224 
225  mclass = mwi = coding = validity = deferred = 0;
226  retmsg = NULL;
227 
228  /* generic values */
229  user = http_cgi_variable(cgivars, "app_id");
230  pass = http_cgi_variable(cgivars, "key");
231 
232  /* MO specific values */
233  from = http_cgi_variable(cgivars, "source_addr");
234  to = http_cgi_variable(cgivars, "dest_addr");
235  text = http_cgi_variable(cgivars, "message");
236  account = http_cgi_variable(cgivars, "operator");
237  binfo = http_cgi_variable(cgivars, "tariff");
238 
239  /* DLR (callback) specific values */
240  state = http_cgi_variable(cgivars, "state");
241  mid = http_cgi_variable(cgivars, "message_id");
242  dest = http_cgi_variable(cgivars, "dest_addr");
243 
244  debug("smsc.http.xidris", 0, "HTTP[%s]: Received a request",
245  octstr_get_cstr(conn->id));
246 
247  if (user == NULL || pass == NULL ||
248  octstr_compare(user, conndata->username) != 0 ||
249  octstr_compare(pass, conndata->password) != 0) {
250  error(0, "HTTP[%s]: Authorization failure. username was <%s>.",
251  octstr_get_cstr(conn->id), octstr_get_cstr(user));
252  retmsg = octstr_create("Authorization failed for MO submission.");
254  }
255  else if (state != NULL && mid != NULL && dest != NULL) { /* a DLR message */
256  Msg *dlrmsg;
257  int dlrstat = -1;
258 
259  if (octstr_compare(state, octstr_imm("DELIVRD")) == 0)
260  dlrstat = DLR_SUCCESS;
261  else if (octstr_compare(state, octstr_imm("ACCEPTD")) == 0)
262  dlrstat = DLR_BUFFERED;
263  else
264  dlrstat = DLR_FAIL;
265 
266  dlrmsg = dlr_find(conn->id,
267  mid, /* smsc message id */
268  dest, /* destination */
269  dlrstat, 0);
270 
271  if (dlrmsg != NULL) {
272  dlrmsg->sms.msgdata = octstr_duplicate(mid);
273  dlrmsg->sms.sms_type = report_mo;
274 
275  ret = bb_smscconn_receive(conn, dlrmsg);
276  status = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
277  } else {
278  error(0,"HTTP[%s]: got DLR but could not find message or was not interested "
279  "in it id<%s> dst<%s>, type<%d>",
280  octstr_get_cstr(conn->id), octstr_get_cstr(mid),
281  octstr_get_cstr(dest), dlrstat);
282  status = HTTP_OK;
283  }
284 
285  }
286  else if (from == NULL || to == NULL || text == NULL) {
287  error(0, "HTTP[%s]: Insufficient args.",
288  octstr_get_cstr(conn->id));
289  retmsg = octstr_create("Insufficient arguments, rejected.");
291  }
292  else {
293  Msg *msg;
294  msg = msg_create(sms);
295 
296  debug("smsc.http.xidris", 0, "HTTP[%s]: Received new MO SMS.",
297  octstr_get_cstr(conn->id));
298 
299  msg->sms.sender = octstr_duplicate(from);
300  msg->sms.receiver = octstr_duplicate(to);
301  msg->sms.msgdata = octstr_duplicate(text);
302  msg->sms.account = octstr_duplicate(account);
303  msg->sms.binfo = octstr_duplicate(binfo);
304 
305  msg->sms.smsc_id = octstr_duplicate(conn->id);
306  msg->sms.time = time(NULL);
307  msg->sms.mclass = mclass;
308  msg->sms.mwi = mwi;
309  msg->sms.coding = coding;
310  msg->sms.validity = time(NULL) + validity * 60;
311  msg->sms.deferred = time(NULL) + deferred * 60;
312 
313  ret = bb_smscconn_receive(conn, msg);
314  status = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
315  }
316 
317  reply_headers = gwlist_create();
318  debug("smsc.http.xidris", 0, "HTTP[%s]: Sending reply with HTTP status <%d>.",
319  octstr_get_cstr(conn->id), status);
320 
321  http_send_reply(client, status, reply_headers, retmsg);
322 
323  octstr_destroy(retmsg);
324  http_destroy_headers(reply_headers);
325 }
326 
327 static int xidris_init(SMSCConn *conn, CfgGroup *cfg)
328 {
329  ConnData *conndata = conn->data;
330 
331  if (conndata->username == NULL || conndata->password == NULL) {
332  error(0, "HTTP[%s]: 'username' and 'password' required for Xidris http smsc",
333  octstr_get_cstr(conn->id));
334  return -1;
335  }
336 
337  return 0;
338 }
339 
341  .init = xidris_init,
342  .send_sms = xidris_send_sms,
343  .parse_reply = xidris_parse_reply,
344  .receive_sms = xidris_receive_sms,
345 };
void error(int err, const char *fmt,...)
Definition: log.c:648
struct smsc_http_fn_callbacks smsc_http_xidris_callback
Definition: xidris.c:340
static int coding
Definition: mtbatch.c:102
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1504
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
static void client(int port)
Definition: test_udp.c:77
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
int octstr_url_decode(Octstr *ostr)
Definition: octstr.c:1746
int code
Definition: smsc_cimd2.c:346
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1070
#define msg_create(type)
Definition: msg.h:136
static Cfg * cfg
Definition: opensmppbox.c:95
void octstr_strip_blanks(Octstr *text)
Definition: octstr.c:1346
Msg * dlr_find(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int typ, int use_dst)
Definition: dlr.c:387
void dlr_add(const Octstr *smsc, const Octstr *ts, Msg *msg, int use_dst)
Definition: dlr.c:330
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
static void xidris_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: xidris.c:214
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2836
void http_destroy_headers(List *headers)
Definition: http.c:2879
#define DLR_SUCCESS
Definition: dlr.h:72
static Octstr * from
Definition: mtbatch.c:95
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1760
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2695
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: http.h:142
Definition: msg.h:79
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:477
char * text
Definition: smsc_cimd2.c:921
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:903
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
int fields_to_dcs(Msg *msg, int mode)
Definition: sms.c:73
static int xidris_send_sms(SMSCConn *conn, Msg *sms)
Definition: xidris.c:108
void octstr_strip_crlfs(Octstr *text)
Definition: octstr.c:1378
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
int(* init)(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_http_p.h:74
Definition: octstr.c:118
void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
Definition: bb_smscconn.c:281
Octstr * password
Definition: smsc_http_p.h:98
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
Definition: cfg.c:73
static Octstr * parse_xml_tag(Octstr *body, Octstr *tag)
Definition: xidris.c:80
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2507
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http_p.h:97
#define DLR_BUFFERED
Definition: dlr.h:74
static void xidris_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: xidris.c:171
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:329
Octstr * send_url
Definition: smsc_http_p.h:93
static Octstr * url
Definition: test_xmlrpc.c:84
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
static int xidris_init(SMSCConn *conn, CfgGroup *cfg)
Definition: xidris.c:327
Definition: list.c:102
static Octstr * account
Definition: mtbatch.c:94
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
#define DLR_FAIL
Definition: dlr.h:73
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.