blob: 3e81661222bec6fae581cdb080c20ffc6c4004d5 [file] [log] [blame]
Max372868b2017-03-02 12:12:00 +01001/* OsmoHLR Control Interface implementation */
2
3/* (C) 2017 sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Max Suraev <msuraev@sysmocom.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdbool.h>
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +020024#include <errno.h>
25#include <inttypes.h>
26#include <string.h>
Max372868b2017-03-02 12:12:00 +010027
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +020028#include <osmocom/gsm/gsm23003.h>
Max372868b2017-03-02 12:12:00 +010029#include <osmocom/ctrl/ports.h>
30
Max372868b2017-03-02 12:12:00 +010031#include "hlr.h"
Max372868b2017-03-02 12:12:00 +010032#include "ctrl.h"
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +020033#include "db.h"
Max372868b2017-03-02 12:12:00 +010034
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +020035#define SEL_BY "by-"
36#define SEL_BY_IMSI SEL_BY "imsi-"
37#define SEL_BY_MSISDN SEL_BY "msisdn-"
38#define SEL_BY_ID SEL_BY "id-"
39
40#define hexdump_buf(buf) osmo_hexdump_nospc((void*)buf, sizeof(buf))
41
42static bool startswith(const char *str, const char *start)
43{
44 return strncmp(str, start, strlen(start)) == 0;
45}
46
47static int _get_subscriber(struct db_context *dbc,
48 const char *by_selector,
49 struct hlr_subscriber *subscr)
50{
51 const char *val;
52 if (startswith(by_selector, SEL_BY_IMSI)) {
53 val = by_selector + strlen(SEL_BY_IMSI);
54 if (!osmo_imsi_str_valid(val))
55 return -EINVAL;
56 return db_subscr_get_by_imsi(dbc, val, subscr);
57 }
58 if (startswith(by_selector, SEL_BY_MSISDN)) {
59 val = by_selector + strlen(SEL_BY_MSISDN);
60 if (!osmo_msisdn_str_valid(val))
61 return -EINVAL;
62 return db_subscr_get_by_msisdn(dbc, val, subscr);
63 }
64 if (startswith(by_selector, SEL_BY_ID)) {
65 int64_t id;
66 char *endptr;
67 val = by_selector + strlen(SEL_BY_ID);
68 if (*val == '+')
69 return -EINVAL;
70 errno = 0;
71 id = strtoll(val, &endptr, 10);
72 if (errno || *endptr)
73 return -EINVAL;
74 return db_subscr_get_by_id(dbc, id, subscr);
75 }
76 return -ENOTSUP;
77}
78
79static bool get_subscriber(struct db_context *dbc,
80 const char *by_selector,
81 struct hlr_subscriber *subscr,
82 struct ctrl_cmd *cmd)
83{
84 int rc = _get_subscriber(dbc, by_selector, subscr);
85 switch (rc) {
86 case 0:
87 return true;
88 case -ENOTSUP:
89 cmd->reply = "Not a known subscriber 'by-xxx-' selector.";
90 return false;
91 case -EINVAL:
92 cmd->reply = "Invalid value part of 'by-xxx-value' selector.";
93 return false;
94 case -ENOENT:
95 cmd->reply = "No such subscriber.";
96 return false;
97 default:
98 cmd->reply = "An unknown error has occured during get_subscriber().";
99 return false;
100 }
101}
102
103/* Optimization: if a subscriber operation is requested by-imsi, just return
104 * the IMSI right back. */
105static const char *get_subscriber_imsi(struct db_context *dbc,
106 const char *by_selector,
107 struct ctrl_cmd *cmd)
108{
109 static struct hlr_subscriber subscr;
110
111 if (startswith(by_selector, SEL_BY_IMSI))
112 return by_selector + strlen(SEL_BY_IMSI);
113 if (!get_subscriber(dbc, by_selector, &subscr, cmd))
114 return NULL;
115 return subscr.imsi;
116}
117
118/* printf fmt and arg to completely omit a string if it is empty. */
119#define FMT_S "%s%s%s%s"
120#define ARG_S(name, val) \
121 (val) && *(val) ? "\n" : "", \
122 (val) && *(val) ? name : "", \
123 (val) && *(val) ? "\t" : "", \
124 (val) && *(val) ? (val) : "" \
125
126/* printf fmt and arg to completely omit bool of given value. */
127#define FMT_BOOL "%s"
128#define ARG_BOOL(name, val) \
129 val ? "\n" name "\t1" : "\n" name "\t0"
130
131static void print_subscr_info(struct ctrl_cmd *cmd,
132 struct hlr_subscriber *subscr)
133{
134 ctrl_cmd_reply_printf(cmd,
135 "\nid\t%"PRIu64
136 FMT_S
137 FMT_S
138 FMT_BOOL
139 FMT_BOOL
140 FMT_S
141 FMT_S
142 FMT_S
143 FMT_BOOL
144 FMT_BOOL
145 "\nperiodic_lu_timer\t%u"
146 "\nperiodic_rau_tau_timer\t%u"
147 "\nlmsi\t%08x"
148 ,
149 subscr->id,
150 ARG_S("imsi", subscr->imsi),
151 ARG_S("msisdn", subscr->msisdn),
152 ARG_BOOL("nam_cs", subscr->nam_cs),
153 ARG_BOOL("nam_ps", subscr->nam_ps),
154 ARG_S("vlr_number", subscr->vlr_number),
155 ARG_S("sgsn_number", subscr->sgsn_number),
156 ARG_S("sgsn_address", subscr->sgsn_address),
157 ARG_BOOL("ms_purged_cs", subscr->ms_purged_cs),
158 ARG_BOOL("ms_purged_ps", subscr->ms_purged_ps),
159 subscr->periodic_lu_timer,
160 subscr->periodic_rau_tau_timer,
161 subscr->lmsi
162 );
163}
164
165static void print_subscr_info_aud2g(struct ctrl_cmd *cmd, struct osmo_sub_auth_data *aud)
166{
167 if (aud->algo == OSMO_AUTH_ALG_NONE)
168 return;
169 ctrl_cmd_reply_printf(cmd,
170 "\naud2g.algo\t%s"
171 "\naud2g.ki\t%s"
172 ,
173 osmo_auth_alg_name(aud->algo),
174 hexdump_buf(aud->u.gsm.ki));
175}
176
177static void print_subscr_info_aud3g(struct ctrl_cmd *cmd, struct osmo_sub_auth_data *aud)
178{
179 if (aud->algo == OSMO_AUTH_ALG_NONE)
180 return;
181 ctrl_cmd_reply_printf(cmd,
182 "\naud3g.algo\t%s"
183 "\naud3g.k\t%s"
184 ,
185 osmo_auth_alg_name(aud->algo),
186 hexdump_buf(aud->u.umts.k));
187 /* hexdump uses a static string buffer, hence only one hexdump per
188 * printf(). */
189 ctrl_cmd_reply_printf(cmd,
190 "\naud3g.%s\t%s"
191 "\naud3g.ind_bitlen\t%u"
192 "\naud3g.sqn\t%"PRIu64
193 ,
194 aud->u.umts.opc_is_op? "op" : "opc",
195 hexdump_buf(aud->u.umts.opc),
196 aud->u.umts.ind_bitlen,
197 aud->u.umts.sqn);
198}
199
200CTRL_CMD_DEFINE_RO(subscr_info, "info");
201static int get_subscr_info(struct ctrl_cmd *cmd, void *data)
Max9cacb6f2017-02-20 17:22:56 +0100202{
Neels Hofmeyr00b1d432017-10-17 01:43:48 +0200203 struct hlr_subscriber subscr;
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200204 struct hlr *hlr = data;
205 const char *by_selector = cmd->node;
Max9cacb6f2017-02-20 17:22:56 +0100206
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200207 if (!get_subscriber(hlr->dbc, by_selector, &subscr, cmd))
208 return CTRL_CMD_ERROR;
209
210 print_subscr_info(cmd, &subscr);
211
212 return CTRL_CMD_REPLY;
213}
214
215CTRL_CMD_DEFINE_RO(subscr_info_aud, "info-aud");
216static int get_subscr_info_aud(struct ctrl_cmd *cmd, void *data)
217{
218 const char *imsi;
219 struct osmo_sub_auth_data aud2g;
220 struct osmo_sub_auth_data aud3g;
221 struct hlr *hlr = data;
222 const char *by_selector = cmd->node;
223 int rc;
224
225 imsi = get_subscriber_imsi(hlr->dbc, by_selector, cmd);
226 if (!imsi)
227 return CTRL_CMD_ERROR;
228
229 rc = db_get_auth_data(hlr->dbc, imsi, &aud2g, &aud3g, NULL);
230
231 if (rc == -ENOENT) {
232 /* No auth data found, tell the print*() functions about it. */
233 aud2g.algo = OSMO_AUTH_ALG_NONE;
234 aud3g.algo = OSMO_AUTH_ALG_NONE;
235 } else if (rc) {
236 cmd->reply = "Error retrieving authentication data.";
Max9cacb6f2017-02-20 17:22:56 +0100237 return CTRL_CMD_ERROR;
238 }
239
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200240 print_subscr_info_aud2g(cmd, &aud2g);
241 print_subscr_info_aud3g(cmd, &aud3g);
242
243 return CTRL_CMD_REPLY;
244}
245
246CTRL_CMD_DEFINE_RO(subscr_info_all, "info-all");
247static int get_subscr_info_all(struct ctrl_cmd *cmd, void *data)
248{
249 struct hlr_subscriber subscr;
250 struct osmo_sub_auth_data aud2g;
251 struct osmo_sub_auth_data aud3g;
252 struct hlr *hlr = data;
253 const char *by_selector = cmd->node;
254 int rc;
255
256 if (!get_subscriber(hlr->dbc, by_selector, &subscr, cmd))
257 return CTRL_CMD_ERROR;
258
259 rc = db_get_auth_data(hlr->dbc, subscr.imsi, &aud2g, &aud3g, NULL);
260
261 if (rc == -ENOENT) {
262 /* No auth data found, tell the print*() functions about it. */
263 aud2g.algo = OSMO_AUTH_ALG_NONE;
264 aud3g.algo = OSMO_AUTH_ALG_NONE;
265 } else if (rc) {
266 cmd->reply = "Error retrieving authentication data.";
Max9cacb6f2017-02-20 17:22:56 +0100267 return CTRL_CMD_ERROR;
268 }
269
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200270 print_subscr_info(cmd, &subscr);
271 print_subscr_info_aud2g(cmd, &aud2g);
272 print_subscr_info_aud3g(cmd, &aud3g);
273
274 return CTRL_CMD_REPLY;
275}
276
277static int verify_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
278{
279 if (!value || !*value
280 || (strcmp(value, "0") && strcmp(value, "1")))
281 return 1;
282 return 0;
283}
284
285static int get_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, void *data,
286 bool is_ps)
287{
288 struct hlr_subscriber subscr;
289 struct hlr *hlr = data;
290 const char *by_selector = cmd->node;
291
292 if (!get_subscriber(hlr->dbc, by_selector, &subscr, cmd))
293 return CTRL_CMD_ERROR;
294
295 cmd->reply = (is_ps ? subscr.nam_ps : subscr.nam_cs)
296 ? "1" : "0";
297 return CTRL_CMD_REPLY;
298}
299
300static int set_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, void *data,
301 bool is_ps)
302{
303 const char *imsi;
304 struct hlr *hlr = data;
305 const char *by_selector = cmd->node;
306
307 imsi = get_subscriber_imsi(hlr->dbc, by_selector, cmd);
308 if (!imsi)
309 return CTRL_CMD_ERROR;
310 if (db_subscr_nam(hlr->dbc, imsi, strcmp(cmd->value, "1") == 0, is_ps))
311 return CTRL_CMD_ERROR;
Max9cacb6f2017-02-20 17:22:56 +0100312 cmd->reply = "OK";
Max9cacb6f2017-02-20 17:22:56 +0100313 return CTRL_CMD_REPLY;
314}
315
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200316CTRL_CMD_DEFINE(subscr_ps_enabled, "ps-enabled");
317static int verify_subscr_ps_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
Max9cacb6f2017-02-20 17:22:56 +0100318{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200319 return verify_subscr_cs_ps_enabled(cmd, value, data);
320}
321static int get_subscr_ps_enabled(struct ctrl_cmd *cmd, void *data)
322{
323 return get_subscr_cs_ps_enabled(cmd, data, true);
324}
325static int set_subscr_ps_enabled(struct ctrl_cmd *cmd, void *data)
326{
327 return set_subscr_cs_ps_enabled(cmd, data, true);
Max9cacb6f2017-02-20 17:22:56 +0100328}
329
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200330CTRL_CMD_DEFINE(subscr_cs_enabled, "cs-enabled");
331static int verify_subscr_cs_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
Max9cacb6f2017-02-20 17:22:56 +0100332{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200333 return verify_subscr_cs_ps_enabled(cmd, value, data);
Max9cacb6f2017-02-20 17:22:56 +0100334}
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200335static int get_subscr_cs_enabled(struct ctrl_cmd *cmd, void *data)
Max372868b2017-03-02 12:12:00 +0100336{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200337 return get_subscr_cs_ps_enabled(cmd, data, false);
338}
339static int set_subscr_cs_enabled(struct ctrl_cmd *cmd, void *data)
340{
341 return set_subscr_cs_ps_enabled(cmd, data, false);
Max372868b2017-03-02 12:12:00 +0100342}
343
344int hlr_ctrl_cmds_install()
345{
346 int rc = 0;
347
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200348 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info);
349 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_aud);
350 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_all);
351 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_ps_enabled);
352 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_cs_enabled);
Max372868b2017-03-02 12:12:00 +0100353
354 return rc;
355}
356
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200357static int hlr_ctrl_node_lookup(void *data, vector vline, int *node_type,
358 void **node_data, int *i)
359{
360 const char *token = vector_slot(vline, *i);
361
362 switch (*node_type) {
363 case CTRL_NODE_ROOT:
364 if (strcmp(token, "subscriber") != 0)
365 return 0;
366 *node_data = NULL;
367 *node_type = CTRL_NODE_SUBSCR;
368 break;
369 case CTRL_NODE_SUBSCR:
370 if (!startswith(token, "by-"))
371 return 0;
372 *node_data = (void*)token;
373 *node_type = CTRL_NODE_SUBSCR_BY;
374 break;
375 default:
376 return 0;
377 }
378
379 return 1;
380}
381
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200382struct ctrl_handle *hlr_controlif_setup(struct hlr *hlr)
Max372868b2017-03-02 12:12:00 +0100383{
384 int rc;
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200385 struct ctrl_handle *hdl = ctrl_interface_setup_dynip2(hlr,
386 hlr->ctrl_bind_addr,
387 OSMO_CTRL_PORT_HLR,
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200388 hlr_ctrl_node_lookup,
389 _LAST_CTRL_NODE_HLR);
Max372868b2017-03-02 12:12:00 +0100390 if (!hdl)
391 return NULL;
392
393 rc = hlr_ctrl_cmds_install();
394 if (rc) /* FIXME: close control interface? */
395 return NULL;
396
397 return hdl;
398}