Harald Welte | 7f12954 | 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 | 0e3e88e | 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 | 7f12954 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 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 | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 23 | #include <osmocore/talloc.h> |
Harald Welte | 7f12954 | 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) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 29 | #include <openbsc/chan_alloc.h> |
| 30 | #include <openbsc/db.h> |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 31 | |
Harald Welte (local) | db0bba4 | 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) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 34 | |
| 35 | static char *build_sms_string(struct gsm_subscriber *subscr, u_int32_t token) |
| 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) | db0bba4 | 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) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 47 | sms_str[len-1] = '\0'; |
| 48 | |
| 49 | return sms_str; |
| 50 | } |
Harald Welte | 7f12954 | 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) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 57 | int rc = 0; |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 58 | |
Harald Welte | bcc2bef | 2009-12-13 12:56:47 +0100 | [diff] [blame] | 59 | if (signal != S_SUBSCR_ATTACHED) |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | |
Harald Welte | bcc2bef | 2009-12-13 12:56:47 +0100 | [diff] [blame] | 62 | if (subscr->net->auth_policy != GSM_AUTH_POLICY_TOKEN) |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 63 | return 0; |
| 64 | |
| 65 | if (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT) { |
| 66 | u_int32_t token; |
| 67 | char *sms_str; |
| 68 | |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 69 | /* we've seen this subscriber for the first time. */ |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 70 | 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 Freyther | 3fb6bf9 | 2011-01-10 15:34:09 +0100 | [diff] [blame^] | 82 | sms = sms_from_text(subscr, 0, sms_str); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 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 */ |
| 92 | unauth: |
| 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 Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 98 | struct gsm_subscriber_connection *conn = connection_for_subscr(subscr); |
| 99 | if (conn) { |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 100 | u_int8_t auth_rand[16]; |
| 101 | /* kick the subscriber off the network */ |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 102 | gsm48_tx_mm_auth_req(conn, auth_rand, 0); |
| 103 | gsm48_tx_mm_auth_rej(conn); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 104 | /* FIXME: close the channel early ?*/ |
| 105 | //gsm48_send_rr_Release(lchan); |
| 106 | } |
| 107 | } |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 110 | return rc; |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int token_sms_cb(unsigned int subsys, unsigned int signal, |
| 114 | void *handler_data, void *signal_data) |
| 115 | { |
Holger Hans Peter Freyther | ed3c204 | 2010-12-24 10:15:55 +0100 | [diff] [blame] | 116 | struct sms_signal_data *sig = signal_data; |
| 117 | struct gsm_sms *sms = sig->sms;; |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 118 | struct gsm_subscriber_connection *conn; |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 119 | u_int8_t auth_rand[16]; |
| 120 | |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 121 | |
| 122 | if (signal != S_SMS_DELIVERED) |
| 123 | return 0; |
| 124 | |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 125 | |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 126 | /* 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) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 131 | |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 132 | if (sms->receiver->net->auth_policy != GSM_AUTH_POLICY_TOKEN) |
| 133 | return 0; |
| 134 | |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 135 | |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 136 | conn = connection_for_subscr(sms->receiver); |
| 137 | if (conn) { |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 138 | /* kick the subscriber off the network */ |
Holger Hans Peter Freyther | 2f4af77 | 2010-06-16 13:23:55 +0800 | [diff] [blame] | 139 | gsm48_tx_mm_auth_req(conn, auth_rand, 0); |
| 140 | gsm48_tx_mm_auth_rej(conn); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 141 | /* FIXME: close the channel early ?*/ |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 142 | //gsm48_send_rr_Release(lchan); |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 148 | //static __attribute__((constructor)) void on_dso_load_token(void) |
| 149 | void on_dso_load_token(void) |
Harald Welte | 7f12954 | 2009-08-13 00:57:54 +0200 | [diff] [blame] | 150 | { |
| 151 | register_signal_handler(SS_SUBSCR, token_subscr_cb, NULL); |
| 152 | register_signal_handler(SS_SMS, token_sms_cb, NULL); |
| 153 | } |