blob: f9a583954ce8ee375899387f0b6b5a197767fba3 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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
Harald Welteccceef82009-08-13 00:57:54 +020010 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welteccceef82009-08-13 00:57:54 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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/>.
Harald Welteccceef82009-08-13 00:57:54 +020019 *
20 */
21
Harald Welte (local)50d12712009-08-13 13:52:14 +020022#include <stdio.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010023#include <osmocore/talloc.h>
Harald Welteccceef82009-08-13 00:57:54 +020024#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>
Harald Welte (local)50d12712009-08-13 13:52:14 +020029#include <openbsc/chan_alloc.h>
30#include <openbsc/db.h>
Harald Welteccceef82009-08-13 00:57:54 +020031
Harald Welte (local)2928bc02009-08-13 20:43:58 +020032#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."
Harald Welte (local)50d12712009-08-13 13:52:14 +020034
Holger Hans Peter Freyther65fb0fd2009-10-22 15:39:37 +020035extern struct gsm_sms *sms_from_text(struct gsm_subscriber *receiver,
36 const char *text);
37
Harald Welte (local)50d12712009-08-13 13:52:14 +020038static char *build_sms_string(struct gsm_subscriber *subscr, u_int32_t token)
39{
40 char *sms_str;
41 unsigned int len;
42
43 len = strlen(subscr->imsi) + 8 + strlen(TOKEN_SMS_TEXT);
44 sms_str = talloc_size(tall_bsc_ctx, len);
45 if (!sms_str)
46 return NULL;
47
Harald Welte (local)2928bc02009-08-13 20:43:58 +020048 snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token,
49 subscr->extension);
Harald Welte (local)50d12712009-08-13 13:52:14 +020050 sms_str[len-1] = '\0';
51
52 return sms_str;
53}
Harald Welteccceef82009-08-13 00:57:54 +020054
55static int token_subscr_cb(unsigned int subsys, unsigned int signal,
56 void *handler_data, void *signal_data)
57{
58 struct gsm_subscriber *subscr = signal_data;
59 struct gsm_sms *sms;
Harald Welte (local)50d12712009-08-13 13:52:14 +020060 int rc = 0;
Harald Welteccceef82009-08-13 00:57:54 +020061
Harald Welte37600be2009-12-13 12:56:47 +010062 if (signal != S_SUBSCR_ATTACHED)
Harald Welteccceef82009-08-13 00:57:54 +020063 return 0;
64
Harald Welte37600be2009-12-13 12:56:47 +010065 if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
Harald Welte (local)50d12712009-08-13 13:52:14 +020066 return 0;
67
68 if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) {
69 u_int32_t token;
70 char *sms_str;
71
Harald Welteccceef82009-08-13 00:57:54 +020072 /* we've seen this subscriber for the first time. */
Harald Welte (local)50d12712009-08-13 13:52:14 +020073 rc = db_subscriber_alloc_token(subscr, &token);
74 if (rc != 0) {
75 rc = -EIO;
76 goto unauth;
77 }
78
79 sms_str = build_sms_string(subscr, token);
80 if (!sms_str) {
81 rc = -ENOMEM;
82 goto unauth;
83 }
84
85 sms = sms_from_text(subscr, sms_str);
86 talloc_free(sms_str);
87 if (!sms) {
88 rc = -ENOMEM;
89 goto unauth;
90 }
91
92 rc = gsm411_send_sms_subscr(subscr, sms);
93
94 /* FIXME: else, delete the subscirber from database */
95unauth:
96
97 /* make sure we don't allow him in again unless he clicks the web UI */
98 subscr->authorized = 0;
99 db_sync_subscriber(subscr);
100 if (rc) {
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800101 struct gsm_subscriber_connection *conn = connection_for_subscr(subscr);
102 if (conn) {
Harald Welte (local)50d12712009-08-13 13:52:14 +0200103 u_int8_t auth_rand[16];
104 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800105 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
106 gsm48_tx_mm_auth_rej(conn);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200107 /* FIXME: close the channel early ?*/
108 //gsm48_send_rr_Release(lchan);
109 }
110 }
Harald Welteccceef82009-08-13 00:57:54 +0200111 }
112
Harald Welte (local)50d12712009-08-13 13:52:14 +0200113 return rc;
Harald Welteccceef82009-08-13 00:57:54 +0200114}
115
116static int token_sms_cb(unsigned int subsys, unsigned int signal,
117 void *handler_data, void *signal_data)
118{
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100119 struct sms_signal_data *sig = signal_data;
120 struct gsm_sms *sms = sig->sms;;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800121 struct gsm_subscriber_connection *conn;
Harald Welte (local)50d12712009-08-13 13:52:14 +0200122 u_int8_t auth_rand[16];
123
Harald Welteccceef82009-08-13 00:57:54 +0200124
125 if (signal != S_SMS_DELIVERED)
126 return 0;
127
Harald Welte (local)50d12712009-08-13 13:52:14 +0200128
Harald Welteccceef82009-08-13 00:57:54 +0200129 /* these are not the droids we've been looking for */
130 if (!sms->receiver ||
131 !(sms->receiver->flags & GSM_SUBSCRIBER_FIRST_CONTACT))
132 return 0;
133
Harald Welte (local)50d12712009-08-13 13:52:14 +0200134
Harald Welteccceef82009-08-13 00:57:54 +0200135 if (sms->receiver->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
136 return 0;
137
Harald Welte (local)50d12712009-08-13 13:52:14 +0200138
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800139 conn = connection_for_subscr(sms->receiver);
140 if (conn) {
Harald Welteccceef82009-08-13 00:57:54 +0200141 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800142 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
143 gsm48_tx_mm_auth_rej(conn);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200144 /* FIXME: close the channel early ?*/
Harald Welteccceef82009-08-13 00:57:54 +0200145 //gsm48_send_rr_Release(lchan);
Harald Welteccceef82009-08-13 00:57:54 +0200146 }
147
Harald Welteccceef82009-08-13 00:57:54 +0200148 return 0;
149}
150
Harald Welte (local)50d12712009-08-13 13:52:14 +0200151//static __attribute__((constructor)) void on_dso_load_token(void)
152void on_dso_load_token(void)
Harald Welteccceef82009-08-13 00:57:54 +0200153{
154 register_signal_handler(SS_SUBSCR, token_subscr_cb, NULL);
155 register_signal_handler(SS_SMS, token_sms_cb, NULL);
156}