Debian Codegraph @

gw/smsc/http/brunet.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 * Brunet - A german aggregator (mainly doing T-Mobil D1 connections)
59 *
60 * o bruHTT v1.3L (for MO traffic)
61 * o bruHTP v2.1 (date 22.04.2003) (for MT traffic)
62 *
63 * Stipe Tolj <[email protected]>
64 * Tobias Weber <[email protected]>
65 */
66
67 #include "gwlib/gwlib.h"
68
69
70 /* MT related function */
71 static int brunet_send_sms(SMSCConn *conn, Msg *sms)
72 {
73 ConnData *conndata = conn->data;
74 Octstr *url, *tid, *xser;
75 List *headers;
76 char id[UUID_STR_LEN + 1];
77 int dcs;
78
79 /*
80 * Construct TransactionId.
81 * Beware that brunet needs an "clean" octstr representation,
82 * without the dashes in the string. So remove them.
83 */
84 uuid_unparse(sms->sms.id, id);
85 tid = octstr_create(id);
86 octstr_replace(tid, octstr_imm("-"), octstr_imm(""));
87
88 /* form the basic URL */
89 url = octstr_format("%S?MsIsdn=%E&Originator=%E",
90 conndata->send_url, sms->sms.receiver, sms->sms.sender);
91
92 /*
93 * We use &binfo=<foobar> from sendsms interface to encode
94 * additional paramters. If a mandatory value is not set,
95 * a default value is applied
96 */
97 if (octstr_len(sms->sms.binfo)) {
98 octstr_url_decode(sms->sms.binfo);
99 octstr_format_append(url, "&%S", sms->sms.binfo);
100 }
101 /* CustomerId */
102 if (octstr_search(url, octstr_create("CustomerId="), 0) == -1) {
103 octstr_format_append(url, "&CustomerId=%S", conndata->username);
104 }
105 /* TransactionId */
106 if (octstr_search(url, octstr_create("TransactionId="), 0) == -1) {
107 octstr_format_append(url, "&TransactionId=%S", tid);
108 }
109 /* SMSCount */
110 if (octstr_search(url, octstr_create("SMSCount="), 0) == -1) {
111 octstr_format_append(url, "&%s", "SMSCount=1");
112 }
113 /* ActionType */
114 if (octstr_search(url, octstr_create("ActionType="), 0) == -1) {
115 octstr_format_append(url, "&%s", "ActionType=A");
116 }
117 /* ServiceDeliveryType */
118 if (octstr_search(url, octstr_create("ServiceDeliveryType="), 0) == -1) {
119 octstr_format_append(url, "&%s", "ServiceDeliveryType=P");
120 }
121
122 /* if coding is not set and UDH exists, assume DC_8BIT
123 * else default to DC_7BIT */
124 if (sms->sms.coding == DC_UNDEF)
125 sms->sms.coding = octstr_len(sms->sms.udhdata) > 0 ? DC_8BIT : DC_7BIT;
126
127 if (sms->sms.coding == DC_8BIT)
128 octstr_format_append(url, "&MessageType=B&Text=%H", sms->sms.msgdata);
129 else
130 octstr_format_append(url, "&MessageType=S&Text=%E", sms->sms.msgdata);
131
132 dcs = fields_to_dcs(sms,
133 (sms->sms.alt_dcs != SMS_PARAM_UNDEFINED ? sms->sms.alt_dcs : 0));
134
135 /* XSer processing */
136 xser = octstr_create("");
137 /* XSer DCS values */
138 if (dcs != 0 && dcs != 4)
139 octstr_format_append(xser, "0201%02x", dcs & 0xff);
140 /* add UDH header */
141 if (octstr_len(sms->sms.udhdata)) {
142 octstr_format_append(xser, "01%02x%H", octstr_len(sms->sms.udhdata),
143 sms->sms.udhdata);
144 }
145 if (octstr_len(xser) > 0)
146 octstr_format_append(url, "&XSer=%S", xser);
147 octstr_destroy(xser);
148
149
150 headers = http_create_empty_headers();
151 debug("smsc.http.brunet", 0, "HTTP[%s]: Sending request <%s>",
152 octstr_get_cstr(conn->id), octstr_get_cstr(url));
153
154 /*
155 * Brunet requires an SSL-enabled HTTP client call, this is handled
156 * transparently by the Kannel HTTP layer module.
157 */
158 http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers,
159 NULL, 0, sms, NULL);
160
161 octstr_destroy(url);
162 octstr_destroy(tid);
163 http_destroy_headers(headers);
164
165 return 0;
166 }
167
168
169 /*
170 * Parse a line in the format: <name=value name=value ...>
171 * and return a Dict with the name as key and the value as value,
172 * otherwise return NULL if a parsing error occures.
173 */
174 static Dict *brunet_parse_body(Octstr *body)
175 {
176 Dict *param = NULL;
177 List *words = NULL;
178 long len;
179 Octstr *word;
180
181 words = octstr_split_words(body);
182 if ((len = gwlist_len(words)) > 0) {
183 param = dict_create(4, (void(*)(void *)) octstr_destroy);
184 while ((word = gwlist_extract_first(words)) != NULL) {
185 List *l = octstr_split(word, octstr_imm("="));
186 Octstr *key = gwlist_extract_first(l);
187 Octstr *value = gwlist_extract_first(l);
188 if (octstr_len(key))
189 dict_put(param, key, value);
190 octstr_destroy(key);
191 octstr_destroy(word);
192 gwlist_destroy(l, (void(*)(void *)) octstr_destroy);
193 }
194 }
195 gwlist_destroy(words, (void(*)(void *)) octstr_destroy);
196
197 return param;
198 }
199
200
201 static void brunet_parse_reply(SMSCConn *conn, Msg *msg, int status,
202 List *headers, Octstr *body)
203 {
204 if (status == HTTP_OK || status == HTTP_ACCEPTED) {
205 Dict *param;
206 Octstr *status;
207
208 if ((param = brunet_parse_body(body)) != NULL &&
209 (status = dict_get(param, octstr_imm("Status"))) != NULL &&
210 octstr_case_compare(status, octstr_imm("0")) == 0) {
211 Octstr *msg_id;
212
213 /* pass the MessageId for this MT to the logging facility */
214 if ((msg_id = dict_get(param, octstr_imm("MessageId"))) != NULL)
215 msg->sms.binfo = octstr_duplicate(msg_id);
216
217 bb_smscconn_sent(conn, msg, NULL);
218
219 } else {
220 error(0, "HTTP[%s]: Message was malformed. SMSC response `%s'.",
221 octstr_get_cstr(conn->id), octstr_get_cstr(body));
222 bb_smscconn_send_failed(conn, msg,
223 SMSCCONN_FAILED_MALFORMED, octstr_duplicate(body));
224 }
225 dict_destroy(param);
226
227 } else {
228 error(0, "HTTP[%s]: Message was rejected. SMSC response `%s'.",
229 octstr_get_cstr(conn->id), octstr_get_cstr(body));
230 bb_smscconn_send_failed(conn, msg,
231 SMSCCONN_FAILED_REJECTED, octstr_duplicate(body));
232 }
233 }
234
235 /* MO related function */
236 static void brunet_receive_sms(SMSCConn *conn, HTTPClient *client,
237 List *headers, Octstr *body, List *cgivars)
238 {
239 ConnData *conndata = conn->data;
240 Octstr *user, *from, *to, *text, *udh;
241 Octstr *retmsg;
242 int mclass, mwi, coding, validity, deferred;
243 List *reply_headers;
244 int ret;
245
246 mclass = mwi = coding = validity = deferred = 0;
247
248 user = http_cgi_variable(cgivars, "CustomerId");
249 from = http_cgi_variable(cgivars, "MsIsdn");
250 to = http_cgi_variable(cgivars, "Recipient");
251 text = http_cgi_variable(cgivars, "SMMO");
252 udh = http_cgi_variable(cgivars, "XSer");
253
254 debug("smsc.http.brunet", 0, "HTTP[%s]: Received a request",
255 octstr_get_cstr(conn->id));
256
257 if (user == NULL || octstr_compare(user, conndata->username) != 0) {
258 error(0, "HTTP[%s]: Authorization failure. CustomerId was <%s>.",
259 octstr_get_cstr(conn->id), octstr_get_cstr(user));
260 retmsg = octstr_create("Authorization failed for MO submission.");
261 }
262 else if (from == NULL || to == NULL || text == NULL) {
263 error(0, "HTTP[%s]: Insufficient args.",
264 octstr_get_cstr(conn->id));
265 retmsg = octstr_create("Insufficient arguments, rejected.");
266 }
267 else {
268 Msg *msg;
269 msg = msg_create(sms);
270
271 debug("smsc.http.brunet", 0, "HTTP[%s]: Received new MO SMS.",
272 octstr_get_cstr(conn->id));
273
274 msg->sms.sender = octstr_duplicate(from);
275 msg->sms.receiver = octstr_duplicate(to);
276 msg->sms.msgdata = octstr_duplicate(text);
277 msg->sms.udhdata = octstr_duplicate(udh);
278
279 msg->sms.smsc_id = octstr_duplicate(conn->id);
280 msg->sms.time = time(NULL); /* XXX maybe extract from DateReceived */
281 msg->sms.mclass = mclass;
282 msg->sms.mwi = mwi;
283 msg->sms.coding = coding;
284 msg->sms.validity = time(NULL) + validity * 60;
285 msg->sms.deferred = time(NULL) + deferred * 60;
286
287 ret = bb_smscconn_receive(conn, msg);
288 if (ret == -1)
289 retmsg = octstr_create("Status=1");
290 else
291 retmsg = octstr_create("Status=0");
292 }
293
294 reply_headers = gwlist_create();
295 http_header_add(reply_headers, "Content-Type", "text/plain");
296 debug("smsc.http.brunet", 0, "HTTP[%s]: Sending reply `%s'.",
297 octstr_get_cstr(conn->id), octstr_get_cstr(retmsg));
298 http_send_reply(client, HTTP_OK, reply_headers, retmsg);
299
300 octstr_destroy(retmsg);
301 http_destroy_headers(reply_headers);
302 }
303
304 static int brunet_init(SMSCConn *conn, CfgGroup *cfg)
305 {
306 ConnData *conndata = conn->data;
307
308 if (conndata->username == NULL) {
309 error(0, "HTTP[%s]: 'username' (=CustomerId) required for Brunet http smsc",
310 octstr_get_cstr(conn->id));
311 return -1;
312 }
313
314 return 0;
315 }
316
317 struct smsc_http_fn_callbacks smsc_http_brunet_callback = {
318 .init = brunet_init,
319 .send_sms = brunet_send_sms,
320 .parse_reply = brunet_parse_reply,
321 .receive_sms = brunet_receive_sms,
322 };
323
324