Debian Codegraph @

gw/smsc/http/xidris.c

raw
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 [email protected].
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 <[email protected]>
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)));
88 octstr_strip_blanks(ret);
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,
192 SMSCCONN_FAILED_MALFORMED, octstr_duplicate(desc));
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,
199 SMSCCONN_FAILED_REJECTED, octstr_create("REJECTED"));
200 }
201 }
202
203 /* MO related function */
204 static void xidris_receive_sms(SMSCConn *conn, HTTPClient *client,
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
330 struct smsc_http_fn_callbacks smsc_http_xidris_callback = {
331 .init = xidris_init,
332 .send_sms = xidris_send_sms,
333 .parse_reply = xidris_parse_reply,
334 .receive_sms = xidris_receive_sms,
335 };