Home | History | Annotate | Line # | Download | only in ldpd
ldp_command.c revision 1.1
      1 /* $NetBSD: ldp_command.c,v 1.1 2010/12/08 07:20:14 kefren Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <arpa/inet.h>
     33 
     34 #include <netinet/in.h>
     35 
     36 #include <sys/socket.h>
     37 #include <sys/queue.h>
     38 
     39 #include <errno.h>
     40 #include <pwd.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <unistd.h>
     45 
     46 #include "label.h"
     47 #include "ldp.h"
     48 #include "ldp_command.h"
     49 #include "ldp_errors.h"
     50 #include "ldp_peer.h"
     51 #include "socketops.h"
     52 
     53 struct com_sock csockets[MAX_COMMAND_SOCKETS];
     54 extern int ldp_hello_time, debug_f, warn_f;
     55 
     56 #define	writestr(soc, str) write(soc, str, strlen(str))
     57 
     58 #define	MAXSEND 1024
     59 char sendspace[MAXSEND];
     60 
     61 static int	verify_root_pwd(char *);
     62 static void	echo_on(int s);
     63 static void	echo_off(int s);
     64 
     65 struct com_func main_commands[] = {
     66 	{ "show", show_func },
     67 	{ "set", set_func },
     68 	{ "quit", exit_func },
     69 	{ "exit", exit_func },
     70 	{ "", NULL }
     71 };
     72 
     73 struct com_func show_commands[] = {
     74 	{ "neighbours", show_neighbours },
     75 	{ "bindings", show_bindings },
     76 	{ "debug", show_debug },
     77 	{ "hellos", show_hellos },
     78 	{ "parameters", show_parameters },
     79 	{ "version", show_version },
     80 	{ "warning", show_warning },
     81 	{ "", NULL }
     82 };
     83 
     84 struct com_func set_commands[] = {
     85 	{ "debug", set_debug },
     86 	{ "hello-time", set_hello_time },
     87 	{ "warning", set_warning },
     88 	{ "", NULL }
     89 };
     90 
     91 int
     92 verify_root_pwd(char *pw)
     93 {
     94 	struct passwd *p;
     95 
     96 	if ((p = getpwuid(0)) == NULL)
     97 		return 0;
     98 
     99 	if (strcmp(crypt(pw, p->pw_passwd), p->pw_passwd))
    100 		return 0;
    101 
    102 	return 1;
    103 }
    104 
    105 
    106 void
    107 init_command_sockets()
    108 {
    109 	int i;
    110 
    111 	for (i = 0; i<MAX_COMMAND_SOCKETS; i++) {
    112 		csockets[i].socket = -1;
    113 		csockets[i].auth = 0;
    114 	}
    115 }
    116 
    117 int
    118 create_command_socket(int port)
    119 {
    120 	struct sockaddr_in sin;
    121 	int s;
    122 
    123 	sin.sin_len = sizeof(sin);
    124 	sin.sin_family = AF_INET;
    125 	sin.sin_port = htons(port);
    126 	sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
    127 
    128 	s = socket(PF_INET, SOCK_STREAM, 6);
    129 	if (s < 0)
    130 		return s;
    131 
    132 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin))) {
    133 		fatalp("bind: %s", strerror(errno));
    134 		close(s);
    135 		return -1;
    136 	}
    137 
    138 	if (listen(s, 5) == -1) {
    139 		fatalp("listen: %s", strerror(errno));
    140 		close(s);
    141 		return -1;
    142 	}
    143 	return s;
    144 }
    145 
    146 void
    147 command_accept(int s)
    148 {
    149 	int as = accept(s, NULL, 0);
    150 
    151 	if (as < 0) {
    152 		fatalp("Cannot accept new command socket %s",
    153 		    strerror(errno));
    154 		return;
    155 	}
    156 
    157 	if (add_command_socket(as) != 0) {
    158 		fatalp("Cannot accept command. Too many connections\n");
    159 		close(as);
    160 		return;
    161 	}
    162 
    163 	/* auth */
    164 	send_pwd_prompt(as);
    165 }
    166 
    167 struct com_sock *
    168 is_command_socket(int s)
    169 {
    170 	int i;
    171 
    172 	if (s == -1)
    173 		return NULL;
    174 	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
    175 		if (s == csockets[i].socket)
    176 			return &csockets[i];
    177 	return NULL;
    178 }
    179 
    180 int
    181 add_command_socket(int s)
    182 {
    183 	int i;
    184 
    185 	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
    186 		if (csockets[i].socket == -1) {
    187 			csockets[i].socket = s;
    188 			csockets[i].auth = 0;
    189 			return 0;
    190 		}
    191 	return -1;
    192 }
    193 
    194 void
    195 command_dispatch(struct com_sock *cs)
    196 {
    197 	char recvspace[MAX_COMMAND_SIZE + 1];
    198 	char *nextc = recvspace;
    199 	int r = recv(cs->socket, recvspace, MAX_COMMAND_SIZE, MSG_PEEK);
    200 
    201 	if (r < 0) {
    202 		command_close(cs->socket);
    203 		return;
    204 	}
    205 
    206 	recv(cs->socket, recvspace, r, MSG_WAITALL);
    207 
    208 	if (r < 3) { /*at least \r\n */
    209 	    if (cs->auth) {
    210 		/*writestr(cs->socket, "Unknown command. Use ? for help\n");*/
    211 		send_prompt(cs->socket);
    212 	    } else {
    213 		writestr(cs->socket, "Bad password\n");
    214 		command_close(cs->socket);
    215 	    }
    216 		return;
    217 	}
    218 
    219 	recvspace[r - 2] = '\0';
    220 
    221 	if (!cs->auth) {
    222 		if (verify_root_pwd(recvspace)) {
    223 			echo_on(cs->socket);
    224 			cs->auth = 1;
    225 			writestr(cs->socket, "\n");
    226 			send_prompt(cs->socket);
    227 		} else {
    228 			echo_on(cs->socket);
    229 			writestr(cs->socket, "Bad password\n");
    230 			command_close(cs->socket);
    231 		}
    232 		return;
    233 	}
    234 
    235 	strsep(&nextc, " ");
    236 
    237 	command_match(main_commands, cs->socket, recvspace, nextc);
    238 
    239 }
    240 
    241 void
    242 command_close(int s)
    243 {
    244 	int i;
    245 
    246 	for (i=0; i<MAX_COMMAND_SOCKETS; i++)
    247 		if (s == csockets[i].socket) {
    248 			close(s);
    249 			csockets[i].socket = -1;
    250 			csockets[i].auth = 0;
    251 			break;
    252 		}
    253 }
    254 
    255 void
    256 send_prompt(int s) {
    257 	writestr(s, "LDP> ");
    258 }
    259 
    260 void
    261 send_pwd_prompt(int s) {
    262 	echo_off(s);
    263 	writestr(s, "Password: ");
    264 }
    265 
    266 static void echo_off(int s)
    267 {
    268 	char iac_will_echo[3] = { 0xff, 0xfb, 0x01 }, bf[32];
    269 	write(s, iac_will_echo, sizeof(iac_will_echo));
    270 	read(s, bf, sizeof(bf));
    271 }
    272 
    273 static void echo_on(int s)
    274 {
    275 	char iac_wont_echo[3] = { 0xff, 0xfc, 0x01 }, bf[32];
    276 	write(s, iac_wont_echo, sizeof(iac_wont_echo));
    277 	read(s, bf, sizeof(bf));
    278 }
    279 
    280 /*
    281  * Matching function
    282  * Returns 1 if matched anything
    283  */
    284 int
    285 command_match(struct com_func *cf, int s, char *orig, char *next)
    286 {
    287 	int i, matched = 0, last_match;
    288 
    289 	if (orig == NULL || orig[0] == '\0') {
    290 		send_prompt(s);
    291 		return 0;
    292 	}
    293 
    294 	if (!strcmp(orig, "?")) {
    295 		for (i=0; cf[i].func != NULL; i++) {
    296 			snprintf(sendspace, MAXSEND, "\t%s\n", cf[i].com);
    297 			writestr(s, sendspace);
    298 		}
    299 		send_prompt(s);
    300 		return 0;
    301 	}
    302 
    303 	for (i=0; cf[i].func != NULL; i++)
    304 		if(strncasecmp(orig, cf[i].com, strlen(orig)) == 0) {
    305 			matched++;
    306 			last_match = i;
    307 		}
    308 	if (!matched) {
    309 		writestr(s, "Unknown command. Use ? for help\n");
    310 		send_prompt(s);
    311 		return 0;
    312 	}
    313 
    314 	if (matched > 1) {
    315 		writestr(s, "Ambiguous command. Use ? for help\n");
    316 		send_prompt(s);
    317 		return 0;
    318 	}
    319 
    320 	if(cf[last_match].func(s, next) != 0)
    321 		send_prompt(s);
    322 	return 1;
    323 
    324 }
    325 
    326 /*
    327  * Main CLI functions
    328  */
    329 int
    330 set_func(int s, char *recvspace)
    331 {
    332 	char *nextc = recvspace;
    333 
    334 	if (recvspace == NULL || recvspace[0] == '\0') {
    335 		writestr(s, "Unknown set command. Use set ? for help\n");
    336 		return 1;
    337 	}
    338 
    339 	strsep(&nextc, " ");
    340 
    341 	command_match(set_commands, s, recvspace, nextc);
    342 	return 0;
    343 }
    344 
    345 int
    346 show_func(int s, char *recvspace)
    347 {
    348 	char *nextc = recvspace;
    349 
    350 	if (recvspace == NULL || recvspace[0] == '\0') {
    351 		writestr(s, "Unknown show command. Use show ? for help\n");
    352 		return 1;
    353 	}
    354 
    355 	strsep(&nextc, " ");
    356 
    357 	command_match(show_commands, s, recvspace, nextc);
    358 	return 0;
    359 }
    360 
    361 int
    362 exit_func(int s, char *recvspace)
    363 {
    364 	command_close(s);
    365 	return 0;
    366 }
    367 
    368 /*
    369  * Show functions
    370  */
    371 int
    372 show_neighbours(int s, char *recvspace)
    373 {
    374 	struct ldp_peer *p;
    375 	struct ldp_peer_address *wp;
    376 	struct sockaddr_in ssin;
    377 	socklen_t sin_len = sizeof(struct sockaddr_in);
    378 
    379 	SLIST_FOREACH(p, &ldp_peer_head, peers) {
    380 		snprintf(sendspace, MAXSEND, "LDP peer: %s\n",
    381 		    inet_ntoa(p->ldp_id));
    382 		writestr(s, sendspace);
    383 		snprintf(sendspace, MAXSEND, "Transport address: %s\n",
    384 		    inet_ntoa(p->transport_address));
    385 		writestr(s, sendspace);
    386 		snprintf(sendspace, MAXSEND, "Next-hop address: %s\n",
    387 		    inet_ntoa(p->address));
    388 		writestr(s, sendspace);
    389 		snprintf(sendspace, MAXSEND, "State: %s\n",
    390 		    ldp_state_to_name(p->state));
    391 		writestr(s, sendspace);
    392 		if (p->state == LDP_PEER_ESTABLISHED) {
    393 			snprintf(sendspace, MAXSEND, "Since: %s",
    394 			    ctime(&p->established_t));
    395 			writestr(s, sendspace);
    396 		}
    397 		snprintf(sendspace, MAXSEND, "Holdtime: %d\nTimeout: %d\n",
    398 			p->holdtime, p->timeout);
    399 		writestr(s, sendspace);
    400 
    401 		switch(p->state) {
    402 		    case LDP_PEER_CONNECTING:
    403 		    case LDP_PEER_CONNECTED:
    404 		    case LDP_PEER_ESTABLISHED:
    405 			if (getsockname(p->socket,(struct sockaddr *) &ssin,
    406 			    &sin_len))
    407 				break;
    408 			snprintf(sendspace, MAXSEND,"Socket: %d\nLocal %s:%d\n",
    409 			    p->socket, inet_ntoa(ssin.sin_addr),
    410 			    ntohs(ssin.sin_port));
    411 			writestr(s, sendspace);
    412 
    413 			if (getpeername(p->socket,(struct sockaddr *) &ssin,
    414 			    &sin_len))
    415 				break;
    416 			snprintf(sendspace, MAXSEND, "Remote %s:%d\n",
    417 				inet_ntoa(ssin.sin_addr), ntohs(ssin.sin_port));
    418 			writestr(s, sendspace);
    419 		}
    420 
    421 		snprintf(sendspace, MAXSEND,"Addresses bounded to this peer: ");
    422 		writestr(s, sendspace);
    423 		SLIST_FOREACH(wp, &p->ldp_peer_address_head, addresses) {
    424 			snprintf(sendspace, MAXSEND, "%s ",
    425 			    inet_ntoa(wp->address));
    426 			writestr(s, sendspace);
    427 		}
    428 		sendspace[0] = sendspace[1] = '\n';
    429 		write(s, sendspace, 2);
    430 	}
    431 	return 1;
    432 }
    433 
    434 int
    435 show_bindings(int s, char *recvspace)
    436 {
    437 	struct label *l;
    438 
    439 	snprintf(sendspace, MAXSEND, "Local label\tNetwork\t\t\t\tNexthop\n");
    440 	writestr(s, sendspace);
    441 	SLIST_FOREACH (l, &label_head, labels) {
    442 		snprintf(sendspace, MAXSEND, "%d\t\t%s/", l->binding,
    443 		    union_ntoa(&l->so_dest));
    444 		writestr(s, sendspace);
    445 		snprintf(sendspace, MAXSEND, "%s", union_ntoa(&l->so_pref));
    446 		writestr(s, sendspace);
    447 		if (l->p)
    448 			snprintf(sendspace, MAXSEND, "\t%s:%d\n",
    449 			    inet_ntoa(l->p->address), l->label);
    450 		else
    451 			snprintf(sendspace, MAXSEND, "\n");
    452 		writestr(s, sendspace);
    453 	}
    454 	return 1;
    455 }
    456 
    457 int
    458 show_debug(int s, char *recvspace)
    459 {
    460 	if (recvspace) {
    461 		writestr(s, "Invalid command\n");
    462 		return 1;
    463 	}
    464 
    465 	snprintf(sendspace, MAXSEND, "Debug: %s\n",
    466 		debug_f ? "YES" : "NO");
    467 	writestr(s, sendspace);
    468 	return 1;
    469 }
    470 
    471 int
    472 show_hellos(int s, char *recvspace)
    473 {
    474 	struct hello_info *hi;
    475 
    476 	SLIST_FOREACH(hi, &hello_info_head, infos) {
    477 		snprintf(sendspace, MAXSEND, "%s: %ds\n", inet_ntoa(hi->ldp_id),
    478 		    hi->keepalive);
    479 		writestr(s, sendspace);
    480 	}
    481 	return 1;
    482 }
    483 
    484 int
    485 show_parameters(int s, char *recvspace)
    486 {
    487 	snprintf(sendspace, MAXSEND, "LDP ID: %s\nProtocol version: %d\n"
    488 		 "Hello time: %d\nKeepalive time: %d\nHoldtime: %d\n"
    489 		 "Minimum label: %d\nMaximum label: %d\n",
    490 		my_ldp_id,
    491 		LDP_VERSION,
    492 		ldp_hello_time,
    493 		LDP_KEEPALIVE_TIME,
    494 		LDP_HOLDTIME,
    495 		MIN_LABEL,
    496 		MAX_LABEL);
    497 	writestr(s, sendspace);
    498 	return 1;
    499 }
    500 
    501 int
    502 show_version(int s, char *recvspace)
    503 {
    504 	if (recvspace) {	/* Nothing more after this */
    505 		writestr(s, "Invalid command\n");
    506 		return 1;
    507 	}
    508 
    509 	snprintf(sendspace, MAXSEND, "NetBSD LDP daemon version: %s\n",
    510 	    LDPD_VER);
    511 	writestr(s, sendspace);
    512 	return 1;
    513 }
    514 
    515 int
    516 show_warning(int s, char *recvspace)
    517 {
    518 	if (recvspace) {
    519 		writestr(s, "Invalid command\n");
    520 		return 1;
    521 	}
    522 
    523 	snprintf(sendspace, MAXSEND, "Warnings: %s\n",
    524 		warn_f ? "YES" : "NO");
    525 	writestr(s, sendspace);
    526 	return 1;
    527 }
    528 
    529 /* Set commands */
    530 int
    531 set_hello_time(int s, char *recvspace)
    532 {
    533 	if (!recvspace || atoi(recvspace) < 1) {
    534 		writestr(s, "Invalid timeout\n");
    535 		return 1;
    536 	}
    537 
    538 	ldp_hello_time = atoi(recvspace);
    539 	return 1;
    540 }
    541 
    542 int
    543 set_debug(int s, char *recvspace)
    544 {
    545 	if (!recvspace || atoi(recvspace) < 0) {
    546 		writestr(s, "Invalid command\n");
    547 		return 1;
    548 	}
    549 
    550 	debug_f = atoi(recvspace);
    551 	return 1;
    552 }
    553 
    554 int
    555 set_warning(int s, char *recvspace)
    556 {
    557 	if (!recvspace || atoi(recvspace) < 0) {
    558 		writestr(s, "Invalid command\n");
    559 		return 1;
    560 	}
    561 
    562 	warn_f = atoi(recvspace);
    563 	return 1;
    564 }
    565