Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
xidris.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 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 
67 /*
68  * Parse for an parameter of an given XML tag and return it as Octstr
69  */
70 static Octstr *parse_xml_tag(Octstr *body, Octstr *tag)
71 {
72  Octstr *stag, *etag, *ret;
73  int spos, epos;
74 
75  stag = octstr_format("<%s>", octstr_get_cstr(tag));
76  if ((spos = octstr_search(body, stag, 0)) == -1) {
77  octstr_destroy(stag);
78  return NULL;
79  }
80  etag = octstr_format("</%s>", octstr_get_cstr(tag));
81  if ((epos = octstr_search(body, etag, spos+octstr_len(stag))) == -1) {
82  octstr_destroy(stag);
83  octstr_destroy(etag);
84  return NULL;
85  }
86 
87  ret = octstr_copy(body, spos+octstr_len(stag), epos+1 - (spos+octstr_len(etag)));
89  octstr_strip_crlfs(ret);
90 
91  octstr_destroy(stag);
92  octstr_destroy(etag);
93 
94  return ret;
95 }
96 
97 /* MT related function */
98 static int xidris_send_sms(SMSCConn *conn, Msg *sms)
99 {
100  ConnData *conndata = conn->data;
101  Octstr *url, *new_msg;
102  List *headers;
103  int dcs, esm_class;
104 
105  url = new_msg = NULL;
106  dcs = esm_class = 0;
107 
108  /* format the URL for call */
109  url = octstr_format("%S?"
110  "app_id=%E&key=%E&dest_addr=%E&source_addr=%E",
111  conndata->send_url, conndata->username,
112  conndata->password, sms->sms.receiver, sms->sms.sender);
113 
114  if (octstr_len(sms->sms.udhdata)) {
115  /* RAW additions for binary (8bit) msgs */
116 
117  /* set the data coding scheme (DCS) and ESM class fields */
118  dcs = fields_to_dcs(sms, sms->sms.alt_dcs);
119  /* ESM_CLASS_SUBMIT_STORE_AND_FORWARD_MODE |
120  ESM_CLASS_SUBMIT_UDH_INDICATOR */
121  esm_class = 0x03 | 0x40;
122 
123  /* prepend UDH header to message block */
124  new_msg = octstr_duplicate(sms->sms.udhdata);
125  octstr_append(new_msg, sms->sms.msgdata);
126 
127  octstr_format_append(url, "&type=200&dcs=%d&esm=%d&message=%H",
128  dcs, esm_class, new_msg);
129  } else {
130  /* additions for text (7bit) msgs */
131 
132  octstr_format_append(url, "&type=%E&message=%E",
133  (sms->sms.mclass ? octstr_imm("1") : octstr_imm("0")),
134  sms->sms.msgdata);
135  }
136 
137  /*
138  * We use &account=<foobar> from sendsms interface to encode any additionaly
139  * proxied parameters, ie. billing information.
140  */
141  if (octstr_len(sms->sms.account)) {
142  octstr_url_decode(sms->sms.account);
143  octstr_format_append(url, "&%s", octstr_get_cstr(sms->sms.account));
144  }
145 
146  headers = gwlist_create();
147  debug("smsc.http.xidris", 0, "HTTP[%s]: Sending request <%s>",
148  octstr_get_cstr(conn->id), octstr_get_cstr(url));
149 
150  http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,
151  NULL, 0, sms, NULL);
152 
153  octstr_destroy(url);
154  octstr_destroy(new_msg);
155  http_destroy_headers(headers);
156 
157  return 0;
158 }
159 
160 
161 static void xidris_parse_reply(SMSCConn *conn, Msg *msg, int status,
162  List *headers, Octstr *body)
163 {
164  Octstr *code, *desc, *mid;
165 
166  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
167  /* now parse the XML document for error code */
168  code = parse_xml_tag(body, octstr_imm("status"));
169  desc = parse_xml_tag(body, octstr_imm("description"));
170 
171  /* The following parsing assumes we get only *one* message id in the
172  * response XML. Which is ok, since we garantee via previous concat
173  * splitting, that we only pass PDUs of 1 SMS size to SMSC. */
174  mid = parse_xml_tag(body, octstr_imm("message_id"));
175 
176  if (octstr_case_compare(code, octstr_imm("0")) == 0 && mid != NULL) {
177  /* ensure the message id gets logged */
178  msg->sms.binfo = octstr_duplicate(mid);
179 
180  /* SMSC ACK.. now we have the message id. */
181  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
182  dlr_add(conn->id, mid, msg, 0);
183 
184  octstr_destroy(mid);
185  bb_smscconn_sent(conn, msg, NULL);
186 
187  } else {
188  error(0, "HTTP[%s]: Message not accepted. Status code <%s> "
189  "description `%s'.", octstr_get_cstr(conn->id),
190  octstr_get_cstr(code), octstr_get_cstr(desc));
191  bb_smscconn_send_failed(conn, msg,
193  }
194  } else {
195  error(0, "HTTP[%s]: Message was rejected. SMSC response was:",
196  octstr_get_cstr(conn->id));
197  octstr_dump(body, 0);
198  bb_smscconn_send_failed(conn, msg,
200  }
201 }
202 
203 /* MO related function */
205  List *headers, Octstr *body, List *cgivars)
206 {
207  ConnData *conndata = conn->data;
208  Octstr *user, *pass, *from, *to, *text, *account, *binfo;
209  Octstr *state, *mid, *dest;
210  Octstr *retmsg;
211  int mclass, mwi, coding, validity, deferred;
212  List *reply_headers;
213  int ret, status;
214 
215  mclass = mwi = coding = validity = deferred = 0;
216  retmsg = NULL;
217 
218  /* generic values */
219  user = http_cgi_variable(cgivars, "app_id");
220  pass = http_cgi_variable(cgivars, "key");
221 
222  /* MO specific values */
223  from = http_cgi_variable(cgivars, "source_addr");
224  to = http_cgi_variable(cgivars, "dest_addr");
225  text = http_cgi_variable(cgivars, "message");
226  account = http_cgi_variable(cgivars, "operator");
227  binfo = http_cgi_variable(cgivars, "tariff");
228 
229  /* DLR (callback) specific values */
230  state = http_cgi_variable(cgivars, "state");
231  mid = http_cgi_variable(cgivars, "message_id");
232  dest = http_cgi_variable(cgivars, "dest_addr");
233 
234  debug("smsc.http.xidris", 0, "HTTP[%s]: Received a request",
235  octstr_get_cstr(conn->id));
236 
237  if (user == NULL || pass == NULL ||
238  octstr_compare(user, conndata->username) != 0 ||
239  octstr_compare(pass, conndata->password) != 0) {
240  error(0, "HTTP[%s]: Authorization failure. username was <%s>.",
241  octstr_get_cstr(conn->id), octstr_get_cstr(user));
242  retmsg = octstr_create("Authorization failed for MO submission.");
243  status = HTTP_UNAUTHORIZED;
244  }
245  else if (state != NULL && mid != NULL && dest != NULL) { /* a DLR message */
246  Msg *dlrmsg;
247  int dlrstat = -1;
248 
249  if (octstr_compare(state, octstr_imm("DELIVRD")) == 0)
250  dlrstat = DLR_SUCCESS;
251  else if (octstr_compare(state, octstr_imm("ACCEPTD")) == 0)
252  dlrstat = DLR_BUFFERED;
253  else
254  dlrstat = DLR_FAIL;
255 
256  dlrmsg = dlr_find(conn->id,
257  mid, /* smsc message id */
258  dest, /* destination */
259  dlrstat, 0);
260 
261  if (dlrmsg != NULL) {
262  dlrmsg->sms.msgdata = octstr_duplicate(mid);
263  dlrmsg->sms.sms_type = report_mo;
264 
265  ret = bb_smscconn_receive(conn, dlrmsg);
266  status = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
267  } else {
268  error(0,"HTTP[%s]: got DLR but could not find message or was not interested "
269  "in it id<%s> dst<%s>, type<%d>",
270  octstr_get_cstr(conn->id), octstr_get_cstr(mid),
271  octstr_get_cstr(dest), dlrstat);
272  status = HTTP_OK;
273  }
274 
275  }
276  else if (from == NULL || to == NULL || text == NULL) {
277  error(0, "HTTP[%s]: Insufficient args.",
278  octstr_get_cstr(conn->id));
279  retmsg = octstr_create("Insufficient arguments, rejected.");
280  status = HTTP_BAD_REQUEST;
281  }
282  else {
283  Msg *msg;
284  msg = msg_create(sms);
285 
286  debug("smsc.http.xidris", 0, "HTTP[%s]: Received new MO SMS.",
287  octstr_get_cstr(conn->id));
288 
289  msg->sms.sender = octstr_duplicate(from);
290  msg->sms.receiver = octstr_duplicate(to);
291  msg->sms.msgdata = octstr_duplicate(text);
292  msg->sms.account = octstr_duplicate(account);
293  msg->sms.binfo = octstr_duplicate(binfo);
294 
295  msg->sms.smsc_id = octstr_duplicate(conn->id);
296  msg->sms.time = time(NULL);
297  msg->sms.mclass = mclass;
298  msg->sms.mwi = mwi;
299  msg->sms.coding = coding;
300  msg->sms.validity = time(NULL) + validity * 60;
301  msg->sms.deferred = time(NULL) + deferred * 60;
302 
303  ret = bb_smscconn_receive(conn, msg);
304  status = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
305  }
306 
307  reply_headers = gwlist_create();
308  debug("smsc.http.xidris", 0, "HTTP[%s]: Sending reply with HTTP status <%d>.",
309  octstr_get_cstr(conn->id), status);
310 
311  http_send_reply(client, status, reply_headers, retmsg);
312 
313  octstr_destroy(retmsg);
314  http_destroy_headers(reply_headers);
315 }
316 
317 static int xidris_init(SMSCConn *conn, CfgGroup *cfg)
318 {
319  ConnData *conndata = conn->data;
320 
321  if (conndata->username == NULL || conndata->password == NULL) {
322  error(0, "HTTP[%s]: 'username' and 'password' required for Xidris http smsc",
323  octstr_get_cstr(conn->id));
324  return -1;
325  }
326 
327  return 0;
328 }
329 
331  .init = xidris_init,
332  .send_sms = xidris_send_sms,
333  .parse_reply = xidris_parse_reply,
334  .receive_sms = xidris_receive_sms,
335 };
void error(int err, const char *fmt,...)
Definition: log.c:612
struct smsc_http_fn_callbacks smsc_http_xidris_callback
Definition: xidris.c:330
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
static void client(int port)
Definition: test_udp.c:77
HTTPCaller * http_ref
Definition: smsc_http.c:143
int octstr_url_decode(Octstr *ostr)
Definition: octstr.c:1744
int code
Definition: smsc_cimd2.c:346
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
#define msg_create(type)
Definition: msg.h:136
void octstr_strip_blanks(Octstr *text)
Definition: octstr.c:1344
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:204
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2813
void http_destroy_headers(List *headers)
Definition: http.c:2856
#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:1745
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2671
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
Definition: http.h:142
Definition: msg.h:79
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:478
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:901
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#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:98
void octstr_strip_crlfs(Octstr *text)
Definition: octstr.c:1376
static Cfg * cfg
Definition: smsbox.c:115
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
int(* init)(SMSCConn *conn, CfgGroup *cfg)
Definition: smsc_http.c:135
Definition: octstr.c:118
void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
Definition: bb_smscconn.c:279
Octstr * password
Definition: smsc_http.c:155
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
Definition: cfg.c:73
static Octstr * parse_xml_tag(Octstr *body, Octstr *tag)
Definition: xidris.c:70
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http.c:154
#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:161
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
Octstr * send_url
Definition: smsc_http.c:150
static Octstr * url
Definition: test_xmlrpc.c:84
static int xidris_init(SMSCConn *conn, CfgGroup *cfg)
Definition: xidris.c:317
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:869
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.