blob: 695b55243a533739409c47fffc5dfb22fc517211 [file] [log] [blame]
Harald Welteccceef82009-08-13 00:57:54 +02001/* SMS based token authentication for ad-hoc GSM networks */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte (local)50d12712009-08-13 13:52:14 +020023#include <stdio.h>
24#include <openbsc/talloc.h>
Harald Welteccceef82009-08-13 00:57:54 +020025#include <openbsc/signal.h>
26#include <openbsc/gsm_data.h>
27#include <openbsc/gsm_04_11.h>
28#include <openbsc/gsm_04_08.h>
29#include <openbsc/gsm_subscriber.h>
Harald Welte (local)50d12712009-08-13 13:52:14 +020030#include <openbsc/chan_alloc.h>
31#include <openbsc/db.h>
Harald Welteccceef82009-08-13 00:57:54 +020032
Harald Welte (local)2928bc02009-08-13 20:43:58 +020033#define TOKEN_SMS_TEXT "HAR 2009 GSM. Register at http://har2009.gnumonks.org/ " \
34 "Your IMSI is %s, auth token is %08X, phone no is %s."
Harald Welte (local)50d12712009-08-13 13:52:14 +020035
36static char *build_sms_string(struct gsm_subscriber *subscr, u_int32_t token)
37{
38 char *sms_str;
39 unsigned int len;
40
41 len = strlen(subscr->imsi) + 8 + strlen(TOKEN_SMS_TEXT);
42 sms_str = talloc_size(tall_bsc_ctx, len);
43 if (!sms_str)
44 return NULL;
45
Harald Welte (local)2928bc02009-08-13 20:43:58 +020046 snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token,
47 subscr->extension);
Harald Welte (local)50d12712009-08-13 13:52:14 +020048 sms_str[len-1] = '\0';
49
50 return sms_str;
51}
Harald Welteccceef82009-08-13 00:57:54 +020052
53static int token_subscr_cb(unsigned int subsys, unsigned int signal,
54 void *handler_data, void *signal_data)
55{
56 struct gsm_subscriber *subscr = signal_data;
57 struct gsm_sms *sms;
Harald Welte (local)50d12712009-08-13 13:52:14 +020058 int rc = 0;
Harald Welteccceef82009-08-13 00:57:54 +020059
60 if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
61 return 0;
62
Harald Welte (local)50d12712009-08-13 13:52:14 +020063 if (signal != S_SUBSCR_ATTACHED)
64 return 0;
65
66 if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) {
67 u_int32_t token;
68 char *sms_str;
69
Harald Welteccceef82009-08-13 00:57:54 +020070 /* we've seen this subscriber for the first time. */
Harald Welte (local)50d12712009-08-13 13:52:14 +020071 rc = db_subscriber_alloc_token(subscr, &token);
72 if (rc != 0) {
73 rc = -EIO;
74 goto unauth;
75 }
76
77 sms_str = build_sms_string(subscr, token);
78 if (!sms_str) {
79 rc = -ENOMEM;
80 goto unauth;
81 }
82
83 sms = sms_from_text(subscr, sms_str);
84 talloc_free(sms_str);
85 if (!sms) {
86 rc = -ENOMEM;
87 goto unauth;
88 }
89
90 rc = gsm411_send_sms_subscr(subscr, sms);
91
92 /* FIXME: else, delete the subscirber from database */
93unauth:
94
95 /* make sure we don't allow him in again unless he clicks the web UI */
96 subscr->authorized = 0;
97 db_sync_subscriber(subscr);
98 if (rc) {
99 struct gsm_lchan *lchan = lchan_for_subscr(subscr);
100 if (lchan) {
101 u_int8_t auth_rand[16];
102 /* kick the subscriber off the network */
103 gsm48_tx_mm_auth_req(lchan, auth_rand);
104 gsm48_tx_mm_auth_rej(lchan);
105 /* FIXME: close the channel early ?*/
106 //gsm48_send_rr_Release(lchan);
107 }
108 }
Harald Welteccceef82009-08-13 00:57:54 +0200109 }
110
Harald Welte (local)50d12712009-08-13 13:52:14 +0200111 return rc;
Harald Welteccceef82009-08-13 00:57:54 +0200112}
113
114static int token_sms_cb(unsigned int subsys, unsigned int signal,
115 void *handler_data, void *signal_data)
116{
117 struct gsm_sms *sms = signal_data;
118 struct gsm_lchan *lchan;
Harald Welte (local)50d12712009-08-13 13:52:14 +0200119 u_int8_t auth_rand[16];
120
Harald Welteccceef82009-08-13 00:57:54 +0200121
122 if (signal != S_SMS_DELIVERED)
123 return 0;
124
Harald Welte (local)50d12712009-08-13 13:52:14 +0200125
Harald Welteccceef82009-08-13 00:57:54 +0200126 /* these are not the droids we've been looking for */
127 if (!sms->receiver ||
128 !(sms->receiver->flags & GSM_SUBSCRIBER_FIRST_CONTACT))
129 return 0;
130
Harald Welte (local)50d12712009-08-13 13:52:14 +0200131
Harald Welteccceef82009-08-13 00:57:54 +0200132 if (sms->receiver->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
133 return 0;
134
Harald Welte (local)50d12712009-08-13 13:52:14 +0200135
Harald Welteccceef82009-08-13 00:57:54 +0200136 lchan = lchan_for_subscr(sms->receiver);
137 if (lchan) {
138 /* kick the subscriber off the network */
Harald Welte (local)50d12712009-08-13 13:52:14 +0200139 gsm48_tx_mm_auth_req(lchan, auth_rand);
Harald Welteccceef82009-08-13 00:57:54 +0200140 gsm48_tx_mm_auth_rej(lchan);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200141 /* FIXME: close the channel early ?*/
Harald Welteccceef82009-08-13 00:57:54 +0200142 //gsm48_send_rr_Release(lchan);
Harald Welteccceef82009-08-13 00:57:54 +0200143 }
144
Harald Welteccceef82009-08-13 00:57:54 +0200145 return 0;
146}
147
Harald Welte (local)50d12712009-08-13 13:52:14 +0200148//static __attribute__((constructor)) void on_dso_load_token(void)
149void on_dso_load_token(void)
Harald Welteccceef82009-08-13 00:57:54 +0200150{
151 register_signal_handler(SS_SUBSCR, token_subscr_cb, NULL);
152 register_signal_handler(SS_SMS, token_sms_cb, NULL);
153}