blob: b78be121e44e6ad1cae66d30e8d85cf98b6662a4 [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
Neels Hofmeyr2f758032019-11-20 00:37:07 +010031#include <osmocom/hlr/hlr.h>
32#include <osmocom/hlr/ctrl.h>
33#include <osmocom/hlr/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:
Thorsten Alteholzb07f33d2019-07-16 21:21:36 +020098 cmd->reply = "An unknown error has occurred during get_subscriber().";
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +020099 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,
Stefan Sperling705b61b2018-12-07 12:44:50 +0100135 "\nid\t%" PRIu64
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200136 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"
Stefan Sperling705b61b2018-12-07 12:44:50 +0100192 "\naud3g.sqn\t%" PRIu64
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200193 ,
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
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100231 switch (rc) {
232 case 0:
233 break;
234 case -ENOENT:
235 case -ENOKEY:
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200236 /* No auth data found, tell the print*() functions about it. */
237 aud2g.algo = OSMO_AUTH_ALG_NONE;
238 aud3g.algo = OSMO_AUTH_ALG_NONE;
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100239 break;
240 default:
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200241 cmd->reply = "Error retrieving authentication data.";
Max9cacb6f2017-02-20 17:22:56 +0100242 return CTRL_CMD_ERROR;
243 }
244
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200245 print_subscr_info_aud2g(cmd, &aud2g);
246 print_subscr_info_aud3g(cmd, &aud3g);
247
248 return CTRL_CMD_REPLY;
249}
250
251CTRL_CMD_DEFINE_RO(subscr_info_all, "info-all");
252static int get_subscr_info_all(struct ctrl_cmd *cmd, void *data)
253{
254 struct hlr_subscriber subscr;
255 struct osmo_sub_auth_data aud2g;
256 struct osmo_sub_auth_data aud3g;
257 struct hlr *hlr = data;
258 const char *by_selector = cmd->node;
259 int rc;
260
261 if (!get_subscriber(hlr->dbc, by_selector, &subscr, cmd))
262 return CTRL_CMD_ERROR;
263
264 rc = db_get_auth_data(hlr->dbc, subscr.imsi, &aud2g, &aud3g, NULL);
265
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100266 switch (rc) {
267 case 0:
268 break;
269 case -ENOENT:
270 case -ENOKEY:
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200271 /* No auth data found, tell the print*() functions about it. */
272 aud2g.algo = OSMO_AUTH_ALG_NONE;
273 aud3g.algo = OSMO_AUTH_ALG_NONE;
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100274 break;
275 default:
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200276 cmd->reply = "Error retrieving authentication data.";
Max9cacb6f2017-02-20 17:22:56 +0100277 return CTRL_CMD_ERROR;
278 }
279
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200280 print_subscr_info(cmd, &subscr);
281 print_subscr_info_aud2g(cmd, &aud2g);
282 print_subscr_info_aud3g(cmd, &aud3g);
283
284 return CTRL_CMD_REPLY;
285}
286
287static int verify_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
288{
289 if (!value || !*value
290 || (strcmp(value, "0") && strcmp(value, "1")))
291 return 1;
292 return 0;
293}
294
295static int get_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, void *data,
296 bool is_ps)
297{
298 struct hlr_subscriber subscr;
299 struct hlr *hlr = data;
300 const char *by_selector = cmd->node;
301
302 if (!get_subscriber(hlr->dbc, by_selector, &subscr, cmd))
303 return CTRL_CMD_ERROR;
304
305 cmd->reply = (is_ps ? subscr.nam_ps : subscr.nam_cs)
306 ? "1" : "0";
307 return CTRL_CMD_REPLY;
308}
309
310static int set_subscr_cs_ps_enabled(struct ctrl_cmd *cmd, void *data,
311 bool is_ps)
312{
313 const char *imsi;
314 struct hlr *hlr = data;
315 const char *by_selector = cmd->node;
316
317 imsi = get_subscriber_imsi(hlr->dbc, by_selector, cmd);
318 if (!imsi)
319 return CTRL_CMD_ERROR;
320 if (db_subscr_nam(hlr->dbc, imsi, strcmp(cmd->value, "1") == 0, is_ps))
321 return CTRL_CMD_ERROR;
Max9cacb6f2017-02-20 17:22:56 +0100322 cmd->reply = "OK";
Max9cacb6f2017-02-20 17:22:56 +0100323 return CTRL_CMD_REPLY;
324}
325
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200326CTRL_CMD_DEFINE(subscr_ps_enabled, "ps-enabled");
327static int verify_subscr_ps_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
Max9cacb6f2017-02-20 17:22:56 +0100328{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200329 return verify_subscr_cs_ps_enabled(cmd, value, data);
330}
331static int get_subscr_ps_enabled(struct ctrl_cmd *cmd, void *data)
332{
333 return get_subscr_cs_ps_enabled(cmd, data, true);
334}
335static int set_subscr_ps_enabled(struct ctrl_cmd *cmd, void *data)
336{
337 return set_subscr_cs_ps_enabled(cmd, data, true);
Max9cacb6f2017-02-20 17:22:56 +0100338}
339
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200340CTRL_CMD_DEFINE(subscr_cs_enabled, "cs-enabled");
341static int verify_subscr_cs_enabled(struct ctrl_cmd *cmd, const char *value, void *data)
Max9cacb6f2017-02-20 17:22:56 +0100342{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200343 return verify_subscr_cs_ps_enabled(cmd, value, data);
Max9cacb6f2017-02-20 17:22:56 +0100344}
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200345static int get_subscr_cs_enabled(struct ctrl_cmd *cmd, void *data)
Max372868b2017-03-02 12:12:00 +0100346{
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200347 return get_subscr_cs_ps_enabled(cmd, data, false);
348}
349static int set_subscr_cs_enabled(struct ctrl_cmd *cmd, void *data)
350{
351 return set_subscr_cs_ps_enabled(cmd, data, false);
Max372868b2017-03-02 12:12:00 +0100352}
353
354int hlr_ctrl_cmds_install()
355{
356 int rc = 0;
357
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200358 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info);
359 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_aud);
360 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_info_all);
361 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_ps_enabled);
362 rc |= ctrl_cmd_install(CTRL_NODE_SUBSCR_BY, &cmd_subscr_cs_enabled);
Max372868b2017-03-02 12:12:00 +0100363
364 return rc;
365}
366
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200367static int hlr_ctrl_node_lookup(void *data, vector vline, int *node_type,
368 void **node_data, int *i)
369{
370 const char *token = vector_slot(vline, *i);
371
372 switch (*node_type) {
373 case CTRL_NODE_ROOT:
374 if (strcmp(token, "subscriber") != 0)
375 return 0;
376 *node_data = NULL;
377 *node_type = CTRL_NODE_SUBSCR;
378 break;
379 case CTRL_NODE_SUBSCR:
380 if (!startswith(token, "by-"))
381 return 0;
382 *node_data = (void*)token;
383 *node_type = CTRL_NODE_SUBSCR_BY;
384 break;
385 default:
386 return 0;
387 }
388
389 return 1;
390}
391
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200392struct ctrl_handle *hlr_controlif_setup(struct hlr *hlr)
Max372868b2017-03-02 12:12:00 +0100393{
394 int rc;
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200395 struct ctrl_handle *hdl = ctrl_interface_setup_dynip2(hlr,
396 hlr->ctrl_bind_addr,
397 OSMO_CTRL_PORT_HLR,
Neels Hofmeyr446eb0f2017-10-17 01:58:24 +0200398 hlr_ctrl_node_lookup,
399 _LAST_CTRL_NODE_HLR);
Max372868b2017-03-02 12:12:00 +0100400 if (!hdl)
401 return NULL;
402
403 rc = hlr_ctrl_cmds_install();
404 if (rc) /* FIXME: close control interface? */
405 return NULL;
406
407 return hdl;
408}