Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 1 | /* SMPP vty interface */ |
| 2 | |
| 3 | /* (C) 2012 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 21 | #include <ctype.h> |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 22 | #include <string.h> |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 23 | #include <errno.h> |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 24 | #include <netdb.h> |
| 25 | #include <sys/socket.h> |
| 26 | |
| 27 | #include <osmocom/vty/command.h> |
| 28 | #include <osmocom/vty/buffer.h> |
| 29 | #include <osmocom/vty/vty.h> |
| 30 | |
| 31 | #include <osmocom/core/linuxlist.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | |
| 35 | #include <openbsc/vty.h> |
| 36 | |
| 37 | #include "smpp_smsc.h" |
| 38 | |
| 39 | struct smsc *smsc_from_vty(struct vty *v); |
| 40 | |
| 41 | static struct cmd_node smpp_node = { |
| 42 | SMPP_NODE, |
| 43 | "%s(config-smpp)# ", |
| 44 | 1, |
| 45 | }; |
| 46 | |
| 47 | static struct cmd_node esme_node = { |
| 48 | SMPP_ESME_NODE, |
| 49 | "%s(config-smpp-esme)# ", |
| 50 | 1, |
| 51 | }; |
| 52 | |
| 53 | DEFUN(cfg_smpp, cfg_smpp_cmd, |
| 54 | "smpp", "Configure SMPP SMS Interface") |
| 55 | { |
| 56 | vty->node = SMPP_NODE; |
| 57 | |
| 58 | return CMD_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | DEFUN(cfg_smpp_port, cfg_smpp_port_cmd, |
| 62 | "local-tcp-port <1-65535>", |
| 63 | "Set the local TCP port on which we listen for SMPP\n" |
| 64 | "TCP port number") |
| 65 | { |
| 66 | struct smsc *smsc = smsc_from_vty(vty); |
| 67 | uint16_t port = atoi(argv[0]); |
| 68 | int rc; |
| 69 | |
| 70 | rc = smpp_smsc_init(smsc, port); |
| 71 | if (rc < 0) { |
| 72 | vty_out(vty, "%% Cannot bind to new port %u nor to " |
| 73 | "old port %u%s", port, smsc->listen_port, VTY_NEWLINE); |
| 74 | return CMD_WARNING; |
| 75 | } |
| 76 | |
| 77 | if (port != smsc->listen_port) { |
| 78 | vty_out(vty, "%% Cannot bind to new port %u, staying on old" |
| 79 | "port %u%s", port, smsc->listen_port, VTY_NEWLINE); |
| 80 | return CMD_WARNING; |
| 81 | } |
| 82 | |
| 83 | return CMD_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | DEFUN(cfg_smpp_sys_id, cfg_smpp_sys_id_cmd, |
| 87 | "system-id ID", "Set the System ID of this SMSC") |
| 88 | { |
| 89 | struct smsc *smsc = smsc_from_vty(vty); |
| 90 | |
| 91 | if (strlen(argv[0])+1 > sizeof(smsc->system_id)) |
| 92 | return CMD_WARNING; |
| 93 | |
| 94 | strcpy(smsc->system_id, argv[0]); |
| 95 | |
| 96 | return CMD_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | DEFUN(cfg_smpp_policy, cfg_smpp_policy_cmd, |
| 100 | "policy (accept-all|closed)", |
| 101 | "Set the authentication policy of this SMSC\n" |
| 102 | "Accept all SMPP connections independeint of system ID / passwd\n" |
| 103 | "Accept only SMPP connections from ESMEs explicitly configured") |
| 104 | { |
| 105 | struct smsc *smsc = smsc_from_vty(vty); |
| 106 | |
| 107 | if (!strcmp(argv[0], "accept-all")) |
| 108 | smsc->accept_all = 1; |
| 109 | else |
| 110 | smsc->accept_all = 0; |
| 111 | |
| 112 | return CMD_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static int config_write_smpp(struct vty *vty) |
| 117 | { |
| 118 | struct smsc *smsc = smsc_from_vty(vty); |
| 119 | |
| 120 | vty_out(vty, "smpp%s", VTY_NEWLINE); |
| 121 | vty_out(vty, " local-tcp-port %u%s", smsc->listen_port, VTY_NEWLINE); |
| 122 | vty_out(vty, " system-id %s%s", smsc->system_id, VTY_NEWLINE); |
| 123 | vty_out(vty, " policy %s%s", |
| 124 | smsc->accept_all ? "accept-all" : "closed", VTY_NEWLINE); |
| 125 | |
| 126 | return CMD_SUCCESS; |
| 127 | } |
| 128 | |
| 129 | DEFUN(cfg_esme, cfg_esme_cmd, |
| 130 | "esme NAME", "Configure a particular ESME") |
| 131 | { |
| 132 | struct smsc *smsc = smsc_from_vty(vty); |
| 133 | struct osmo_smpp_acl *acl; |
| 134 | const char *id = argv[0]; |
| 135 | |
| 136 | if (strlen(id) > 16) { |
| 137 | vty_out(vty, "%% System ID cannot be more than 16 " |
| 138 | "characters long%s", VTY_NEWLINE); |
| 139 | return CMD_WARNING; |
| 140 | } |
| 141 | acl = smpp_acl_by_system_id(smsc, id); |
| 142 | if (!acl) { |
| 143 | acl = smpp_acl_alloc(smsc, id); |
| 144 | if (!acl) |
| 145 | return CMD_WARNING; |
| 146 | } |
| 147 | |
| 148 | vty->index = acl; |
| 149 | vty->index_sub = &acl->description; |
| 150 | vty->node = SMPP_ESME_NODE; |
| 151 | |
| 152 | return CMD_SUCCESS; |
| 153 | } |
| 154 | |
| 155 | DEFUN(cfg_no_esme, cfg_no_esme_cmd, |
| 156 | "no esme NAME", NO_STR "Remove ESME configuration") |
| 157 | { |
| 158 | struct smsc *smsc = smsc_from_vty(vty); |
| 159 | struct osmo_smpp_acl *acl; |
| 160 | const char *id = argv[0]; |
| 161 | |
| 162 | acl = smpp_acl_by_system_id(smsc, id); |
| 163 | if (!acl) { |
| 164 | vty_out(vty, "%% ESME with system id '%s' unknown%s", |
| 165 | id, VTY_NEWLINE); |
| 166 | return CMD_WARNING; |
| 167 | } |
| 168 | |
| 169 | /* FIXME: close the connection, free data structure, etc. */ |
| 170 | |
| 171 | smpp_acl_delete(acl); |
| 172 | |
| 173 | return CMD_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | DEFUN(cfg_esme_passwd, cfg_esme_passwd_cmd, |
| 178 | "password PASSWORD", "Set the password for this ESME") |
| 179 | { |
| 180 | struct osmo_smpp_acl *acl = vty->index; |
| 181 | |
| 182 | if (strlen(argv[0])+1 > sizeof(acl->passwd)) |
| 183 | return CMD_WARNING; |
| 184 | |
| 185 | strcpy(acl->passwd, argv[0]); |
| 186 | |
| 187 | return CMD_SUCCESS; |
| 188 | } |
| 189 | |
| 190 | DEFUN(cfg_esme_no_passwd, cfg_esme_no_passwd_cmd, |
| 191 | "no password", NO_STR "Set the password for this ESME") |
| 192 | { |
| 193 | struct osmo_smpp_acl *acl = vty->index; |
| 194 | |
| 195 | memset(acl->passwd, 0, sizeof(acl->passwd)); |
| 196 | |
| 197 | return CMD_SUCCESS; |
| 198 | } |
| 199 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 200 | static int osmo_is_digits(const char *str) |
| 201 | { |
| 202 | int i; |
| 203 | for (i = 0; i < strlen(str); i++) { |
| 204 | if (!isdigit(str[i])) |
| 205 | return 0; |
| 206 | } |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | static const struct value_string route_errstr[] = { |
| 211 | { -EEXIST, "Route already exists" }, |
| 212 | { -ENODEV, "Route does not exist" }, |
| 213 | { -ENOMEM, "No memory" }, |
| 214 | { -EINVAL, "Invalid" }, |
| 215 | { 0, NULL } |
| 216 | }; |
| 217 | |
| 218 | static const struct value_string smpp_ton_str_short[] = { |
| 219 | { TON_Unknown, "unknown" }, |
| 220 | { TON_International, "international" }, |
| 221 | { TON_National, "national" }, |
| 222 | { TON_Network_Specific, "network" }, |
| 223 | { TON_Subscriber_Number,"subscriber" }, |
| 224 | { TON_Alphanumeric, "alpha" }, |
| 225 | { TON_Abbreviated, "abbrev" }, |
| 226 | { 0, NULL } |
| 227 | }; |
| 228 | |
| 229 | static const struct value_string smpp_npi_str_short[] = { |
| 230 | { NPI_Unknown, "unknown" }, |
| 231 | { NPI_ISDN_E163_E164, "isdn" }, |
| 232 | { NPI_Data_X121, "x121" }, |
| 233 | { NPI_Telex_F69, "f69" }, |
| 234 | { NPI_Land_Mobile_E212, "e212" }, |
| 235 | { NPI_National, "national" }, |
| 236 | { NPI_Private, "private" }, |
| 237 | { NPI_ERMES, "ermes" }, |
| 238 | { NPI_Internet_IP, "ip" }, |
| 239 | { NPI_WAP_Client_Id, "wap" }, |
| 240 | { 0, NULL } |
| 241 | }; |
| 242 | |
| 243 | |
| 244 | #define SMPP_ROUTE_STR "Configure a route for MO-SMS to be sent to this ESME\n" |
| 245 | #define SMPP_ROUTE_P_STR "Prefix-match route\n" |
| 246 | #define SMPP_PREFIX_STR "Destination number prefix\n" |
| 247 | |
| 248 | #define TON_CMD "(unknown|international|national|network|subscriber|alpha|abbrev)" |
| 249 | #define NPI_CMD "(unknown|isdn|x121|f69|e212|national|private|ermes|ip|wap)" |
| 250 | #define TON_STR "FIXME" |
| 251 | #define NPI_STR "FIXME" |
| 252 | |
| 253 | DEFUN(cfg_esme_route_pfx, cfg_esme_route_pfx_cmd, |
| 254 | "route prefix " TON_CMD " " NPI_CMD " PREFIX", |
| 255 | SMPP_ROUTE_P_STR TON_STR NPI_STR SMPP_PREFIX_STR) |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 256 | { |
| 257 | struct osmo_smpp_acl *acl = vty->index; |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 258 | struct osmo_smpp_addr pfx; |
| 259 | int rc; |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 260 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 261 | /* check if DESTINATION is all-digits */ |
| 262 | if (!osmo_is_digits(argv[2])) { |
| 263 | vty_out(vty, "%% PREFIX has to be numeric%s", VTY_NEWLINE); |
| 264 | return CMD_WARNING; |
| 265 | } |
| 266 | |
| 267 | pfx.ton = get_string_value(smpp_ton_str_short, argv[0]); |
| 268 | pfx.npi = get_string_value(smpp_npi_str_short, argv[1]); |
| 269 | snprintf(pfx.addr, sizeof(pfx.addr), "%s", argv[2]); |
| 270 | |
| 271 | rc = smpp_route_pfx_add(acl, &pfx); |
| 272 | if (rc < 0) { |
| 273 | vty_out(vty, "%% error adding prefix route: %s%s", |
| 274 | get_value_string(route_errstr, rc), VTY_NEWLINE); |
| 275 | return CMD_WARNING; |
| 276 | } |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 277 | |
| 278 | return CMD_SUCCESS; |
| 279 | } |
| 280 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 281 | DEFUN(cfg_esme_no_route_pfx, cfg_esme_no_route_pfx_cmd, |
| 282 | "no route prefix " TON_CMD " " NPI_CMD " PREFIX", |
| 283 | NO_STR SMPP_ROUTE_P_STR TON_STR NPI_STR SMPP_PREFIX_STR) |
| 284 | { |
| 285 | struct osmo_smpp_acl *acl = vty->index; |
| 286 | struct osmo_smpp_addr pfx; |
| 287 | int rc; |
| 288 | |
| 289 | /* check if DESTINATION is all-digits */ |
| 290 | if (!osmo_is_digits(argv[2])) { |
| 291 | vty_out(vty, "%% PREFIX has to be numeric%s", VTY_NEWLINE); |
| 292 | return CMD_WARNING; |
| 293 | } |
| 294 | |
| 295 | pfx.ton = get_string_value(smpp_ton_str_short, argv[0]); |
| 296 | pfx.npi = get_string_value(smpp_npi_str_short, argv[1]); |
| 297 | snprintf(pfx.addr, sizeof(pfx.addr), "%s", argv[2]); |
| 298 | |
| 299 | rc = smpp_route_pfx_del(acl, &pfx); |
| 300 | if (rc < 0) { |
| 301 | vty_out(vty, "%% error removing prefix route: %s%s", |
| 302 | get_value_string(route_errstr, rc), VTY_NEWLINE); |
| 303 | return CMD_WARNING; |
| 304 | } |
| 305 | |
| 306 | return CMD_SUCCESS; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 311 | DEFUN(cfg_esme_defaultroute, cfg_esme_defaultroute_cmd, |
| 312 | "default-route", |
| 313 | "Set this ESME as default-route for all SMS to unknown destinations") |
| 314 | { |
| 315 | struct osmo_smpp_acl *acl = vty->index; |
| 316 | |
| 317 | acl->default_route = 1; |
| 318 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 319 | if (!acl->smsc->def_route) |
| 320 | acl->smsc->def_route = acl; |
| 321 | |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 322 | return CMD_SUCCESS; |
| 323 | } |
| 324 | |
| 325 | DEFUN(cfg_no_esme_defaultroute, cfg_esme_no_defaultroute_cmd, |
| 326 | "no default-route", NO_STR |
| 327 | "Set this ESME as default-route for all SMS to unknown destinations") |
| 328 | { |
| 329 | struct osmo_smpp_acl *acl = vty->index; |
| 330 | |
| 331 | acl->default_route = 0; |
| 332 | |
| 333 | /* remove currently active default route, if it was created by |
| 334 | * this ACL */ |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 335 | if (acl->smsc->def_route && acl->smsc->def_route == acl) |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 336 | acl->smsc->def_route = NULL; |
| 337 | |
| 338 | return CMD_SUCCESS; |
| 339 | } |
| 340 | |
| 341 | static void dump_one_esme(struct vty *vty, struct osmo_esme *esme) |
| 342 | { |
| 343 | char host[128], serv[128]; |
| 344 | |
| 345 | host[0] = 0; |
| 346 | serv[0] = 0; |
| 347 | getnameinfo((const struct sockaddr *) &esme->sa, esme->sa_len, |
| 348 | host, sizeof(host), serv, sizeof(serv), NI_NUMERICSERV); |
| 349 | |
| 350 | vty_out(vty, "ESME System ID: %s, Password: %s, SMPP Version %02x%s", |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 351 | esme->system_id, esme->acl ? esme->acl->passwd : "", |
| 352 | esme->smpp_version, VTY_NEWLINE); |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 353 | vty_out(vty, " Connected from: %s:%s%s", host, serv, VTY_NEWLINE); |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 354 | if (esme->smsc->def_route == esme->acl) |
| 355 | vty_out(vty, " Is current default route%s", VTY_NEWLINE); |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | DEFUN(show_esme, show_esme_cmd, |
| 359 | "show smpp esme", |
| 360 | SHOW_STR "SMPP Interface\n" "SMPP Extrenal SMS Entity\n") |
| 361 | { |
| 362 | struct smsc *smsc = smsc_from_vty(vty); |
| 363 | struct osmo_esme *esme; |
| 364 | |
| 365 | llist_for_each_entry(esme, &smsc->esme_list, list) |
| 366 | dump_one_esme(vty, esme); |
| 367 | |
| 368 | return CMD_SUCCESS; |
| 369 | } |
| 370 | |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 371 | static void write_esme_route_single(struct vty *vty, struct osmo_smpp_route *r) |
| 372 | { |
| 373 | switch (r->type) { |
| 374 | case SMPP_ROUTE_PREFIX: |
| 375 | vty_out(vty, " route prefix %s ", |
| 376 | get_value_string(smpp_ton_str_short, r->u.prefix.ton)); |
| 377 | vty_out(vty, "%s %s%s", |
| 378 | get_value_string(smpp_npi_str_short, r->u.prefix.npi), |
| 379 | r->u.prefix.addr, VTY_NEWLINE); |
| 380 | break; |
| 381 | case SMPP_ROUTE_NONE: |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 386 | static void config_write_esme_single(struct vty *vty, struct osmo_smpp_acl *acl) |
| 387 | { |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 388 | struct osmo_smpp_route *r; |
| 389 | |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 390 | vty_out(vty, " esme %s%s", acl->system_id, VTY_NEWLINE); |
| 391 | if (strlen(acl->passwd)) |
| 392 | vty_out(vty, " password %s%s", acl->passwd, VTY_NEWLINE); |
| 393 | if (acl->default_route) |
| 394 | vty_out(vty, " default-route%s", VTY_NEWLINE); |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 395 | if (acl->deliver_src_imsi) |
| 396 | vty_out(vty, " deliver-src-imsi%s", VTY_NEWLINE); |
| 397 | |
| 398 | llist_for_each_entry(r, &acl->route_list, list) |
| 399 | write_esme_route_single(vty, r); |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int config_write_esme(struct vty *v) |
| 403 | { |
| 404 | struct smsc *smsc = smsc_from_vty(v); |
| 405 | struct osmo_smpp_acl *acl; |
| 406 | |
| 407 | llist_for_each_entry(acl, &smsc->acl_list, list) |
| 408 | config_write_esme_single(v, acl); |
| 409 | |
| 410 | return CMD_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | int smpp_vty_init(void) |
| 414 | { |
| 415 | install_node(&smpp_node, config_write_smpp); |
| 416 | install_default(SMPP_NODE); |
| 417 | install_element(CONFIG_NODE, &cfg_smpp_cmd); |
| 418 | |
| 419 | install_element(SMPP_NODE, &cfg_smpp_port_cmd); |
| 420 | install_element(SMPP_NODE, &cfg_smpp_sys_id_cmd); |
| 421 | install_element(SMPP_NODE, &cfg_smpp_policy_cmd); |
| 422 | install_element(SMPP_NODE, &cfg_esme_cmd); |
| 423 | install_element(SMPP_NODE, &cfg_no_esme_cmd); |
| 424 | |
| 425 | install_node(&esme_node, config_write_esme); |
| 426 | install_default(SMPP_ESME_NODE); |
| 427 | install_element(SMPP_ESME_NODE, &cfg_esme_passwd_cmd); |
| 428 | install_element(SMPP_ESME_NODE, &cfg_esme_no_passwd_cmd); |
Harald Welte | 691ce33 | 2012-11-23 19:02:37 +0100 | [diff] [blame] | 429 | install_element(SMPP_ESME_NODE, &cfg_esme_route_pfx_cmd); |
| 430 | install_element(SMPP_ESME_NODE, &cfg_esme_no_route_pfx_cmd); |
Harald Welte | b7dd661 | 2012-11-20 22:22:04 +0100 | [diff] [blame] | 431 | install_element(SMPP_ESME_NODE, &cfg_esme_defaultroute_cmd); |
| 432 | install_element(SMPP_ESME_NODE, &cfg_esme_no_defaultroute_cmd); |
| 433 | install_element(SMPP_ESME_NODE, &ournode_exit_cmd); |
| 434 | |
| 435 | install_element_ve(&show_esme_cmd); |
| 436 | |
| 437 | return 0; |
| 438 | } |