blob: cf26ba8d8aa009872548d63e7f74997482964b78 [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>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010023#include <osmocom/core/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 Freytherc42ad8b2011-04-18 17:04:00 +020035static char *build_sms_string(struct gsm_subscriber *subscr, uint32_t token)
Harald Welte (local)50d12712009-08-13 13:52:14 +020036{
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
Harald Welte (local)2928bc02009-08-13 20:43:58 +020045 snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token,
46 subscr->extension);
Harald Welte (local)50d12712009-08-13 13:52:14 +020047 sms_str[len-1] = '\0';
48
49 return sms_str;
50}
Harald Welteccceef82009-08-13 00:57:54 +020051
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;
Harald Welte (local)50d12712009-08-13 13:52:14 +020057 int rc = 0;
Harald Welteccceef82009-08-13 00:57:54 +020058
Harald Welte37600be2009-12-13 12:56:47 +010059 if (signal != S_SUBSCR_ATTACHED)
Harald Welteccceef82009-08-13 00:57:54 +020060 return 0;
61
Harald Welte37600be2009-12-13 12:56:47 +010062 if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
Harald Welte (local)50d12712009-08-13 13:52:14 +020063 return 0;
64
65 if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020066 uint32_t token;
Harald Welte (local)50d12712009-08-13 13:52:14 +020067 char *sms_str;
68
Harald Welteccceef82009-08-13 00:57:54 +020069 /* we've seen this subscriber for the first time. */
Harald Welte (local)50d12712009-08-13 13:52:14 +020070 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
Holger Hans Peter Freythercdfcbee2011-01-10 15:34:09 +010082 sms = sms_from_text(subscr, 0, sms_str);
Harald Welte (local)50d12712009-08-13 13:52:14 +020083 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) {
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +080098 struct gsm_subscriber_connection *conn = connection_for_subscr(subscr);
99 if (conn) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200100 uint8_t auth_rand[16];
Harald Welte (local)50d12712009-08-13 13:52:14 +0200101 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800102 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
103 gsm48_tx_mm_auth_rej(conn);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200104 /* FIXME: close the channel early ?*/
105 //gsm48_send_rr_Release(lchan);
106 }
107 }
Harald Welteccceef82009-08-13 00:57:54 +0200108 }
109
Harald Welte (local)50d12712009-08-13 13:52:14 +0200110 return rc;
Harald Welteccceef82009-08-13 00:57:54 +0200111}
112
113static int token_sms_cb(unsigned int subsys, unsigned int signal,
114 void *handler_data, void *signal_data)
115{
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100116 struct sms_signal_data *sig = signal_data;
117 struct gsm_sms *sms = sig->sms;;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800118 struct gsm_subscriber_connection *conn;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200119 uint8_t auth_rand[16];
Harald Welte (local)50d12712009-08-13 13:52:14 +0200120
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
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800136 conn = connection_for_subscr(sms->receiver);
137 if (conn) {
Harald Welteccceef82009-08-13 00:57:54 +0200138 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800139 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
140 gsm48_tx_mm_auth_rej(conn);
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{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200151 osmo_signal_register_handler(SS_SUBSCR, token_subscr_cb, NULL);
152 osmo_signal_register_handler(SS_SMS, token_sms_cb, NULL);
Harald Welteccceef82009-08-13 00:57:54 +0200153}