Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 23 | #include <osmocom/core/talloc.h> |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 24 | #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) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 29 | #include <openbsc/chan_alloc.h> |
| 30 | #include <openbsc/db.h> |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 31 | |
Harald Welte (local) | 2928bc0 | 2009-08-13 20:43:58 +0200 | [diff] [blame] | 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." |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 35 | static char *build_sms_string(struct gsm_subscriber *subscr, uint32_t token) |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 36 | { |
| 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) | 2928bc0 | 2009-08-13 20:43:58 +0200 | [diff] [blame] | 45 | snprintf(sms_str, len, TOKEN_SMS_TEXT, subscr->imsi, token, |
| 46 | subscr->extension); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 47 | sms_str[len-1] = '\0'; |
| 48 | |
| 49 | return sms_str; |
| 50 | } |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 51 | |
| 52 | static 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) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 57 | int rc = 0; |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 58 | |
Harald Welte | 37600be | 2009-12-13 12:56:47 +0100 | [diff] [blame] | 59 | if (signal != S_SUBSCR_ATTACHED) |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 62 | if (subscr->group->net->auth_policy != GSM_AUTH_POLICY_TOKEN) |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 63 | return 0; |
| 64 | |
| 65 | if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) { |
Sylvain Munaut | 01c13a3 | 2012-12-28 00:49:01 +0100 | [diff] [blame] | 66 | struct gsm_subscriber *sender; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 67 | uint32_t token; |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 68 | char *sms_str; |
| 69 | |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 70 | /* we've seen this subscriber for the first time. */ |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 71 | 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 Munaut | 01c13a3 | 2012-12-28 00:49:01 +0100 | [diff] [blame] | 83 | |
| 84 | /* FIXME: don't use ID 1 static */ |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 85 | sender = subscr_get_by_id(subscr->group, 1); |
Sylvain Munaut | 01c13a3 | 2012-12-28 00:49:01 +0100 | [diff] [blame] | 86 | |
| 87 | sms = sms_from_text(subscr, sender, 0, sms_str); |
| 88 | |
| 89 | subscr_put(sender); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 90 | 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 */ |
| 99 | unauth: |
| 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 Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 105 | struct gsm_subscriber_connection *conn = connection_for_subscr(subscr); |
| 106 | if (conn) { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 107 | uint8_t auth_rand[16]; |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 108 | /* kick the subscriber off the network */ |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 109 | gsm48_tx_mm_auth_req(conn, auth_rand, 0); |
| 110 | gsm48_tx_mm_auth_rej(conn); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 111 | /* FIXME: close the channel early ?*/ |
| 112 | //gsm48_send_rr_Release(lchan); |
| 113 | } |
| 114 | } |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 117 | return rc; |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int token_sms_cb(unsigned int subsys, unsigned int signal, |
| 121 | void *handler_data, void *signal_data) |
| 122 | { |
Holger Hans Peter Freyther | 04144c1 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 123 | struct sms_signal_data *sig = signal_data; |
| 124 | struct gsm_sms *sms = sig->sms;; |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 125 | struct gsm_subscriber_connection *conn; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 126 | uint8_t auth_rand[16]; |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 127 | |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 128 | |
| 129 | if (signal != S_SMS_DELIVERED) |
| 130 | return 0; |
| 131 | |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 132 | |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 133 | /* 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) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 138 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 139 | if (sms->receiver->group->net->auth_policy != GSM_AUTH_POLICY_TOKEN) |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 140 | return 0; |
| 141 | |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 142 | |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 143 | conn = connection_for_subscr(sms->receiver); |
| 144 | if (conn) { |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 145 | /* kick the subscriber off the network */ |
Holger Hans Peter Freyther | b2be195 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 146 | gsm48_tx_mm_auth_req(conn, auth_rand, 0); |
| 147 | gsm48_tx_mm_auth_rej(conn); |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 148 | /* FIXME: close the channel early ?*/ |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 149 | //gsm48_send_rr_Release(lchan); |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Harald Welte (local) | 50d1271 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 155 | //static __attribute__((constructor)) void on_dso_load_token(void) |
| 156 | void on_dso_load_token(void) |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 157 | { |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 158 | osmo_signal_register_handler(SS_SUBSCR, token_subscr_cb, NULL); |
| 159 | osmo_signal_register_handler(SS_SMS, token_sms_cb, NULL); |
Harald Welte | ccceef8 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 160 | } |