blob: 6d4f14b2ebcebf18454c5415f4708f35f1d3625c [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)50d12712009-08-13 13:52:14 +020033#define TOKEN_SMS_TEXT "HAR 2009 GSM. Please visit http://har2009.gnumonks.org/ to" \
34 "register. Your IMSI is %s, your auth token is %08X."
35
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
46 snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token);
47 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
59 if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
60 return 0;
61
Harald Welte (local)50d12712009-08-13 13:52:14 +020062 if (signal != S_SUBSCR_ATTACHED)
63 return 0;
64
65 if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) {
66 u_int32_t token;
67 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
82 sms = sms_from_text(subscr, 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_lchan *lchan = lchan_for_subscr(subscr);
99 if (lchan) {
100 u_int8_t auth_rand[16];
101 /* kick the subscriber off the network */
102 gsm48_tx_mm_auth_req(lchan, auth_rand);
103 gsm48_tx_mm_auth_rej(lchan);
104 /* 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{
116 struct gsm_sms *sms = signal_data;
117 struct gsm_lchan *lchan;
Harald Welte (local)50d12712009-08-13 13:52:14 +0200118 u_int8_t auth_rand[16];
119
Harald Welteccceef82009-08-13 00:57:54 +0200120
121 if (signal != S_SMS_DELIVERED)
122 return 0;
123
Harald Welte (local)50d12712009-08-13 13:52:14 +0200124
Harald Welteccceef82009-08-13 00:57:54 +0200125 /* these are not the droids we've been looking for */
126 if (!sms->receiver ||
127 !(sms->receiver->flags & GSM_SUBSCRIBER_FIRST_CONTACT))
128 return 0;
129
Harald Welte (local)50d12712009-08-13 13:52:14 +0200130
Harald Welteccceef82009-08-13 00:57:54 +0200131 if (sms->receiver->net->auth_policy != GSM_AUTH_POLICY_TOKEN)
132 return 0;
133
Harald Welte (local)50d12712009-08-13 13:52:14 +0200134
Harald Welteccceef82009-08-13 00:57:54 +0200135 lchan = lchan_for_subscr(sms->receiver);
136 if (lchan) {
137 /* kick the subscriber off the network */
Harald Welte (local)50d12712009-08-13 13:52:14 +0200138 gsm48_tx_mm_auth_req(lchan, auth_rand);
Harald Welteccceef82009-08-13 00:57:54 +0200139 gsm48_tx_mm_auth_rej(lchan);
Harald Welte (local)50d12712009-08-13 13:52:14 +0200140 /* FIXME: close the channel early ?*/
Harald Welteccceef82009-08-13 00:57:54 +0200141 //gsm48_send_rr_Release(lchan);
Harald Welteccceef82009-08-13 00:57:54 +0200142 }
143
Harald Welteccceef82009-08-13 00:57:54 +0200144 return 0;
145}
146
Harald Welte (local)50d12712009-08-13 13:52:14 +0200147//static __attribute__((constructor)) void on_dso_load_token(void)
148void on_dso_load_token(void)
Harald Welteccceef82009-08-13 00:57:54 +0200149{
150 register_signal_handler(SS_SUBSCR, token_subscr_cb, NULL);
151 register_signal_handler(SS_SMS, token_sms_cb, NULL);
152}