blob: 95fd9b345ad73fd357cee238d4a14d68294e29ee [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
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010062 if (subscr->group->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) {
Sylvain Munaut01c13a32012-12-28 00:49:01 +010066 struct gsm_subscriber *sender;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020067 uint32_t token;
Harald Welte (local)50d12712009-08-13 13:52:14 +020068 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
Sylvain Munaut01c13a32012-12-28 00:49:01 +010083
84 /* FIXME: don't use ID 1 static */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010085 sender = subscr_get_by_id(subscr->group, 1);
Sylvain Munaut01c13a32012-12-28 00:49:01 +010086
87 sms = sms_from_text(subscr, sender, 0, sms_str);
88
89 subscr_put(sender);
Harald Welte (local)50d12712009-08-13 13:52:14 +020090 talloc_free(sms_str);
91 if (!sms) {
92 rc = -ENOMEM;
93 goto unauth;
94 }
95
96 rc = gsm411_send_sms_subscr(subscr, sms);
97
98 /* FIXME: else, delete the subscirber from database */
99unauth:
100
101 /* make sure we don't allow him in again unless he clicks the web UI */
102 subscr->authorized = 0;
103 db_sync_subscriber(subscr);
104 if (rc) {
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800105 struct gsm_subscriber_connection *conn = connection_for_subscr(subscr);
106 if (conn) {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107 uint8_t auth_rand[16];
Harald Welte (local)50d12712009-08-13 13:52:14 +0200108 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800109 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
110 gsm48_tx_mm_auth_rej(conn);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200111 /* FIXME: close the channel early ?*/
112 //gsm48_send_rr_Release(lchan);
113 }
114 }
Harald Welteccceef82009-08-13 00:57:54 +0200115 }
116
Harald Welte (local)50d12712009-08-13 13:52:14 +0200117 return rc;
Harald Welteccceef82009-08-13 00:57:54 +0200118}
119
120static int token_sms_cb(unsigned int subsys, unsigned int signal,
121 void *handler_data, void *signal_data)
122{
Holger Hans Peter Freyther04144c12010-12-24 10:15:55 +0100123 struct sms_signal_data *sig = signal_data;
124 struct gsm_sms *sms = sig->sms;;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800125 struct gsm_subscriber_connection *conn;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200126 uint8_t auth_rand[16];
Harald Welte (local)50d12712009-08-13 13:52:14 +0200127
Harald Welteccceef82009-08-13 00:57:54 +0200128
129 if (signal != S_SMS_DELIVERED)
130 return 0;
131
Harald Welte (local)50d12712009-08-13 13:52:14 +0200132
Harald Welteccceef82009-08-13 00:57:54 +0200133 /* these are not the droids we've been looking for */
134 if (!sms->receiver ||
135 !(sms->receiver->flags & GSM_SUBSCRIBER_FIRST_CONTACT))
136 return 0;
137
Harald Welte (local)50d12712009-08-13 13:52:14 +0200138
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100139 if (sms->receiver->group->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
Harald Welteccceef82009-08-13 00:57:54 +0200140 return 0;
141
Harald Welte (local)50d12712009-08-13 13:52:14 +0200142
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800143 conn = connection_for_subscr(sms->receiver);
144 if (conn) {
Harald Welteccceef82009-08-13 00:57:54 +0200145 /* kick the subscriber off the network */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +0800146 gsm48_tx_mm_auth_req(conn, auth_rand, 0);
147 gsm48_tx_mm_auth_rej(conn);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200148 /* FIXME: close the channel early ?*/
Harald Welteccceef82009-08-13 00:57:54 +0200149 //gsm48_send_rr_Release(lchan);
Harald Welteccceef82009-08-13 00:57:54 +0200150 }
151
Harald Welteccceef82009-08-13 00:57:54 +0200152 return 0;
153}
154
Harald Welte (local)50d12712009-08-13 13:52:14 +0200155//static __attribute__((constructor)) void on_dso_load_token(void)
156void on_dso_load_token(void)
Harald Welteccceef82009-08-13 00:57:54 +0200157{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200158 osmo_signal_register_handler(SS_SUBSCR, token_subscr_cb, NULL);
159 osmo_signal_register_handler(SS_SMS, token_sms_cb, NULL);
Harald Welteccceef82009-08-13 00:57:54 +0200160}