blob: cf26ba8d8aa009872548d63e7f74997482964b78 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <stdio.h>
Jonathan Santos5a45b152011-08-17 15:33:57 -040023#include <osmocom/core/talloc.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040024#include <openbsc/signal.h>
25#include <openbsc/gsm_data.h>
26#include <openbsc/gsm_04_11.h>
27#include <openbsc/gsm_04_08.h>
28#include <openbsc/gsm_subscriber.h>
29#include <openbsc/chan_alloc.h>
30#include <openbsc/db.h>
31
32#define TOKEN_SMS_TEXT "HAR 2009 GSM. Register at http://har2009.gnumonks.org/ " \
33 "Your IMSI is %s, auth token is %08X, phone no is %s."
34
Jonathan Santos5a45b152011-08-17 15:33:57 -040035static char *build_sms_string(struct gsm_subscriber *subscr, uint32_t token)
Jonathan Santos03fd8d02011-05-25 13:54:02 -040036{
37 char *sms_str;
38 unsigned int len;
39
40 len = strlen(subscr->imsi) + 8 + strlen(TOKEN_SMS_TEXT);
41 sms_str = talloc_size(tall_bsc_ctx, len);
42 if (!sms_str)
43 return NULL;
44
45 snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token,
46 subscr->extension);
47 sms_str[len-1] = '\0';
48
49 return sms_str;
50}
51
52static int token_subscr_cb(unsigned int subsys, unsigned int signal,
53 void *handler_data, void *signal_data)
54{
55 struct gsm_subscriber *subscr = signal_data;
56 struct gsm_sms *sms;
57 int rc = 0;
58
59 if (signal != S_SUBSCR_ATTACHED)
60 return 0;
61
62 if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
63 return 0;
64
65 if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) {
Jonathan Santos5a45b152011-08-17 15:33:57 -040066 uint32_t token;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040067 char *sms_str;
68
69 /* we've seen this subscriber for the first time. */
70 rc = db_subscriber_alloc_token(subscr, &token);
71 if (rc != 0) {
72 rc = -EIO;
73 goto unauth;
74 }
75
76 sms_str = build_sms_string(subscr, token);
77 if (!sms_str) {
78 rc = -ENOMEM;
79 goto unauth;
80 }
81
82 sms = sms_from_text(subscr, 0, sms_str);
83 talloc_free(sms_str);
84 if (!sms) {
85 rc = -ENOMEM;
86 goto unauth;
87 }
88
89 rc = gsm411_send_sms_subscr(subscr, sms);
90
91 /* FIXME: else, delete the subscirber from database */
92unauth:
93
94 /* make sure we don't allow him in again unless he clicks the web UI */
95 subscr->authorized = 0;
96 db_sync_subscriber(subscr);
97 if (rc) {
98 struct gsm_subscriber_connection *conn = connection_for_subscr(subscr);
99 if (conn) {
Jonathan Santos5a45b152011-08-17 15:33:57 -0400100 uint8_t auth_rand[16];
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400101 /* kick the subscriber off the network */
102 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
103 gsm48_tx_mm_auth_rej(conn);
104 /* FIXME: close the channel early ?*/
105 //gsm48_send_rr_Release(lchan);
106 }
107 }
108 }
109
110 return rc;
111}
112
113static int token_sms_cb(unsigned int subsys, unsigned int signal,
114 void *handler_data, void *signal_data)
115{
116 struct sms_signal_data *sig = signal_data;
117 struct gsm_sms *sms = sig->sms;;
118 struct gsm_subscriber_connection *conn;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400119 uint8_t auth_rand[16];
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400120
121
122 if (signal != S_SMS_DELIVERED)
123 return 0;
124
125
126 /* 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
131
132 if (sms->receiver->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
133 return 0;
134
135
136 conn = connection_for_subscr(sms->receiver);
137 if (conn) {
138 /* kick the subscriber off the network */
139 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
140 gsm48_tx_mm_auth_rej(conn);
141 /* FIXME: close the channel early ?*/
142 //gsm48_send_rr_Release(lchan);
143 }
144
145 return 0;
146}
147
148//static __attribute__((constructor)) void on_dso_load_token(void)
149void on_dso_load_token(void)
150{
Jonathan Santos5a45b152011-08-17 15:33:57 -0400151 osmo_signal_register_handler(SS_SUBSCR, token_subscr_cb, NULL);
152 osmo_signal_register_handler(SS_SMS, token_sms_cb, NULL);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400153}