Debian Codegraph @

ax25/ax25d.c

raw
1 /*
2 * This is my version of axl.c, written for the LBBS code to make it
3 * compatible with the kernel AX25 driver. It appears to work, with
4 * my setup, so it'll probably not work else where :-).
5 *
6 * This was inspired by the example code written by Alan Cox (GW4PTS)
7 * axl.c in AX25USER.TGZ from sunacm.swan.ac.uk.
8 *
9 *
10 * Copyright (C) 1995, 1996 by Darryl L. Miles, G7LED.
11 * Copyright (C) 1996 by Jonathan Naylor G4KLX
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
26 * 02110-1301, USA.
27 *
28 *
29 *
30 * Just a quickie Feb 1995.
31 * It *was* just a quickie (at the time) but you'd know how these things
32 * just... Apr 1995.
33 *
34 * If your AX25/NETROM system is relying on this code for
35 * securetty/firewalling then please be aware this has been coded
36 * with the intent on striving on through system/(mis)configuration
37 * errors in the hope that at worst it will run with a degraded
38 * service. Rather than leave your system providing no service at
39 * all, if opinions require the old behavior back when let me know
40 * and I'll #ifdef it in.
41 *
42 *
43 * History:
44 *
45 * 1.0 Feb 1995 Basic AX25 listening daemon, Multi-port, Call
46 * matching, etc...
47 *
48 * 1.1 Feb 1995 Moved entry scanning before fork().
49 * Added setgroups() to plug security hole.
50 * Minor fixes + Improved handling.
51 *
52 * 1.2 Apr 1995 NETROM support added from developing AX25
53 * 028b/029.
54 * Added 'defaults' port setting.
55 * Added FLAG_NODIGIS.
56 *
57 * 1.3 Jul 1995 Make it a little more intelligent about what to
58 * do with errors.
59 * Added exec and argv[0] as two different fields,
60 * much like inetd uses.
61 *
62 * 1.4 Aug 1995 Confirmed support for AX25 030 (1.3.20 + hacks),
63 * it appears to work.
64 * It will now bootup even if initial config errors
65 * occur when setting up and binding (e.g. port(s)
66 * down), it will skip the port(s) with a problem
67 * and listen out on those which are left standing.
68 *
69 * 1.5 Aug 1995 Updated old (buggy) libax25.a function copies in axl.
70 * Causing all sorts of problems.
71 *
72 * 1.6 Aug 1995 Reset the 'defaults' entry's when we start parsing
73 * a new interface.
74 *
75 * 1.7 Dec 1995 Added BROKEN_NETROM_KERNEL define for setsockopt.
76 *
77 * 1.8 Jan 1996 Added support for AX25_BIND_ANY_DEVICE, specify just
78 * [CALL-X VIA *].
79 * Better param parsing, T1 and T2 now using the real
80 * time in seconds as params, and not kernel units.
81 * Connection loggin added, either via it's own logfile
82 * or syslog.
83 * Modified 'defaults' to 'parameters'.
84 *
85 * 1.9 Jun 1996 Reworked config file parsing to use port names instead
86 * of callsigns. Reformated source code.
87 *
88 * Under alpha:
89 * BPQ like clever mode called for.... also a mode that
90 * requires a packet to kick application open.
91 * A flag/mode which will cause a call to initgroups()
92 * based on uid.
93 * Callsign validation check.
94 * Logging of errors.
95 * Handling of AX25.IP mode VC connections - dl9sau
96 * recommended settings in ax25d.conf:
97 * parameters_extAX25 VC-wait-login VC-disc-on-linkfailure-msg VC-log-connections
98 or
99 * parameters_extAX25 VC-reject-login VC-send-failure-msg VC-log-connections
100 *
101 *
102 * TODO:
103 * The timing of the 'accept()' might be changed, defered to the
104 * child, then that child fork() itself, to stop race conditions
105 * around 'accept()'.
106 * Add a config file to allow/disallow connections/services at
107 * different times of the day to restrict access say.
108 */
109
110 #include <stdio.h>
111 #include <stdlib.h>
112 #include <unistd.h>
113 #include <signal.h>
114 #include <string.h>
115 #include <ctype.h>
116 #include <time.h>
117 #include <pwd.h>
118 #include <grp.h>
119 #include <syslog.h>
120 #include <errno.h>
121
122 #include <config.h>
123
124 #include <sys/time.h>
125 #include <sys/types.h>
126 #include <sys/fcntl.h>
127 #include <sys/file.h>
128 #include <sys/stat.h>
129 #include <sys/ioctl.h>
130 #include <sys/socket.h>
131
132 #include <netax25/ax25.h>
133 #include <netrom/netrom.h>
134 #include <netrose/rose.h>
135
136 #include <netax25/axlib.h>
137 #include <netax25/axconfig.h>
138 #include <netax25/nrconfig.h>
139 #include <netax25/rsconfig.h>
140 #include <netax25/daemon.h>
141
142 #include <scm-version.h>
143
144 #include "../pathnames.h"
145
146 /* Maximum number of command line arguments for the application we run */
147 #define MAX_ARGS 32
148
149 #define FLAG_VALIDCALL 0x01 /* NOTUSED */
150 #define FLAG_NOLOGGING 0x02 /* Don't log this connection */
151 #define FLAG_CHKNRN 0x04 /* Check NetRom Neighbour - NOTUSED */
152 #define FLAG_NODIGIS 0x08 /* Disallow digipeated uplinks */
153 #define FLAG_LOCKOUT 0x10 /* Disallow connection */
154
155
156 struct axlist { /* Have used same struct for quickness */
157 struct axlist *next; /* Port list */
158
159 char *port; /* Port call, only set across the port list */
160 int fd; /* The listening socket fd */
161
162 int af_type; /* AF_AX25, AF_NETROM or AF_ROSE port */
163
164 struct axlist *ents; /* Exec line entries */
165 char *call; /* Call in listing entries */
166 char *node; /* Node call in listing entries */
167 uid_t uid; /* UID to run program as */
168 gid_t gid; /* GID to run program as */
169 char *exec; /* Real exec */
170 char *shell; /* Command line. With possible escapes. */
171
172 unsigned int window; /* Set window to... */
173 unsigned long t1; /* Set T1 to... (Retrans timer) */
174 unsigned long t2; /* Set T2 to... (Ack delay) */
175 unsigned long t3; /* Set T3 to... (Idle Poll timer) */
176 unsigned long idle; /* Set T4 to... (Link Drop timer) */
177 unsigned long n2; /* Set N2 to... (Retries) */
178 unsigned long flags; /* FLAG_ values ORed... */
179
180 int checkVC; /* check for AX25.IP mode VC users? */
181 int VCdiscOnLinkfailureMsg; /* action when mode VC user and checkVC == 1 */
182 int VCsendFailureMsg; /* action when mode VC user and on checkVC > 1 */
183 int VCloginEnable; /* trigger login when mode VC user and checkVC == 1 and one line is read */
184 int LoggingVC; /* extra log stuff */
185 };
186
187 static struct axlist *AXL;
188 static char *ConfigFile = CONF_AX25D_FILE;
189 static char User[10]; /* Room for 'GB9ZZZ-15\0' */
190 static char Node[11]; /* Room for 'GB9ZZZ-15\0' (NETROM) and 10 bytes ROSE '6505551234\0' */
191 static char myAX25Name[10]; /* Room for 'GB9ZZZ-15\0' */
192 static char *Port;
193 static int Logging = FALSE;
194
195 static int ReadConfig(void);
196
197 static fd_set fdread;
198 static int maxfd = -1;
199
200 /*--------------------------------------------------------------------------*/
201
202 static void update_maxfd(void)
203 {
204 struct axlist *paxl;
205
206 FD_ZERO(&fdread);
207
208 for (maxfd = -1, paxl = AXL; paxl != NULL && paxl->fd >= 0; paxl = paxl->next) {
209 FD_SET(paxl->fd, &fdread);
210
211 if (paxl->fd > maxfd)
212 maxfd = paxl->fd;
213 }
214 }
215
216 /*--------------------------------------------------------------------------*/
217
218 static void err_config(void)
219 {
220 if (AXL == NULL) {
221 if (Logging)
222 syslog(LOG_ERR, "config file reload error, exiting");
223 exit(1);
224 } else {
225 if (Logging)
226 syslog(LOG_INFO, "config file reload error, continuing with original");
227 }
228 }
229
230 /*--------------------------------------------------------------------------*/
231
232 static void _ReadConfig(int dummy)
233 {
234 ReadConfig();
235 }
236
237 /*--------------------------------------------------------------------------*/
238
239 static void reload_timer(int sec)
240 {
241 struct itimerval itv;
242
243 itv.it_value.tv_sec = sec;
244 itv.it_value.tv_usec = 0;
245 itv.it_interval.tv_sec = 0;
246 itv.it_interval.tv_usec = 0;
247 setitimer(ITIMER_REAL, &itv, NULL);
248 signal(SIGALRM, _ReadConfig);
249 }
250
251 /*--------------------------------------------------------------------------*/
252
253 static void SignalHUP(int code)
254 {
255 ReadConfig();
256 }
257
258 static void SignalTERM(int code)
259 {
260 if (Logging) {
261 syslog(LOG_INFO, "terminating on SIGTERM\n");
262 closelog();
263 }
264
265 exit(0);
266 }
267
268 static void WorkoutArgs(int af_type, char *shell, int *argc, char **argv)
269 {
270 char buffer[1024]; /* Maximum arg size */
271 char *sp, *cp;
272 int cnt = 0;
273 int args = 0;
274
275 for (cp = shell; *cp != '\0'; cp++) {
276 if (isspace(*cp) && cnt != 0) {
277 buffer[cnt] = '\0';
278 argv[args++] = strdup(buffer);
279 cnt = 0;
280
281 if (args == MAX_ARGS - 1) {
282 argv[args] = NULL;
283 *argc = args;
284 return;
285 }
286
287 continue;
288 } else if (isspace(*cp)) /* && !cnt */
289 continue;
290
291 if (*cp == '%') {
292 cp++;
293
294 switch (*cp) {
295 case 'd': /* portname */
296 for (sp = Port; *sp != '\0' && *sp != '-'; sp++)
297 buffer[cnt++] = *sp;
298 break;
299
300 case 'U': /* username in UPPER */
301 for (sp = User; *sp != '\0' && *sp != '-'; sp++)
302 buffer[cnt++] = toupper(*sp);
303 break;
304
305 case 'u': /* USERNAME IN lower */
306 for (sp = User; *sp != '\0' && *sp != '-'; sp++)
307 buffer[cnt++] = tolower(*sp);
308 break;
309
310 case 'S': /* username in UPPER (with SSID) */
311 for (sp = User; *sp != '\0'; sp++)
312 buffer[cnt++] = toupper(*sp);
313 break;
314
315 case 's': /* USERNAME IN lower (with SSID) */
316 for (sp = User; *sp != '\0'; sp++)
317 buffer[cnt++] = tolower(*sp);
318 break;
319
320 case 'P': /* nodename in UPPER */
321 if (af_type == AF_NETROM) {
322 for (sp = Node; *sp != '\0' && *sp != '-'; sp++)
323 buffer[cnt++] = toupper(*sp);
324 } else {
325 buffer[cnt++] = '%';
326 }
327 break;
328
329 case 'p': /* NODENAME IN lower */
330 if (af_type == AF_NETROM) {
331 for (sp = Node; *sp != '\0' && *sp != '-'; sp++)
332 buffer[cnt++] = tolower(*sp);
333 } else {
334 buffer[cnt++] = '%';
335 }
336 break;
337
338 case 'R': /* nodename in UPPER (with SSID) */
339 if (af_type == AF_NETROM) {
340 for (sp = Node; *sp != '\0'; sp++)
341 buffer[cnt++] = toupper(*sp);
342 } else {
343 buffer[cnt++] = '%';
344 }
345 break;
346
347 case 'r': /* NODENAME IN lower (with SSID) */
348 if (af_type == AF_NETROM) {
349 for (sp = Node; *sp != '\0'; sp++)
350 buffer[cnt++] = tolower(*sp);
351 } else {
352 buffer[cnt++] = '%';
353 }
354 break;
355
356 case '\0':
357 case '%':
358 default:
359 buffer[cnt++] = '%';
360 break;
361 }
362 } else {
363 buffer[cnt++] = *cp;
364 }
365 }
366
367 if (cnt != 0) {
368 buffer[cnt] = '\0';
369 argv[args++] = strdup(buffer);
370 }
371
372 argv[args] = NULL;
373 *argc = args;
374 }
375
376 static void SetupOptions(int new, struct axlist *axl)
377 {
378 switch (axl->af_type) {
379 case AF_AX25:
380 if (axl->window != 0)
381 setsockopt(new, SOL_AX25, AX25_WINDOW, &axl->window, sizeof(axl->window));
382 if (axl->t1 != 0)
383 setsockopt(new, SOL_AX25, AX25_T1, &axl->t1, sizeof(axl->t1));
384 if (axl->n2 != 0)
385 setsockopt(new, SOL_AX25, AX25_N2, &axl->n2, sizeof(axl->n2));
386 if (axl->t2 != 0)
387 setsockopt(new, SOL_AX25, AX25_T2, &axl->t2, sizeof(axl->t2));
388 if (axl->t3 != 0)
389 setsockopt(new, SOL_AX25, AX25_T3, &axl->t3, sizeof(axl->t3));
390 if (axl->idle != 0)
391 setsockopt(new, SOL_AX25, AX25_IDLE, &axl->idle, sizeof(axl->idle));
392 break;
393 case AF_NETROM:
394 if (axl->t1 != 0)
395 setsockopt(new, SOL_NETROM, NETROM_T1, &axl->t1, sizeof(axl->t1));
396 if (axl->n2 != 0)
397 setsockopt(new, SOL_NETROM, NETROM_N2, &axl->n2, sizeof(axl->n2));
398 if (axl->t2 != 0)
399 setsockopt(new, SOL_NETROM, NETROM_T2, &axl->t2, sizeof(axl->t2));
400 break;
401 case AF_ROSE:
402 if (axl->idle != 0)
403 setsockopt(new, SOL_ROSE, ROSE_IDLE, &axl->idle, sizeof(axl->idle));
404 break;
405 }
406 }
407
408 /**************************** CONFIGURATION STUFF ***************************/
409
410 static unsigned long ParseFlags(const char *kp, int line)
411 {
412 unsigned long flags = 0UL;
413
414 for (; *kp != '\0'; kp++) {
415 switch (*kp) {
416 case 'v':
417 case 'V':
418 flags |= FLAG_VALIDCALL;
419 break;
420
421 case 'q':
422 case 'Q':
423 flags |= FLAG_NOLOGGING;
424 break;
425
426 case 'n':
427 case 'N':
428 flags |= FLAG_CHKNRN;
429 break;
430
431 case 'd':
432 case 'D':
433 flags |= FLAG_NODIGIS;
434 break;
435
436 case 'l':
437 case 'L':
438 flags |= FLAG_LOCKOUT;
439 break;
440
441 case '0':
442 case '*':
443 case '-': /* Be compatible and allow markers */
444 break;
445
446 default:
447 fprintf(stderr, "ax25d: config file line %d bad flag option '%c'.\n", line, *kp);
448 break;
449 }
450 }
451
452 return flags;
453 }
454
455 static struct axlist *ClearList(struct axlist *list)
456 {
457 struct axlist *tp1, *tp2, *tmp;
458
459 for (tp1 = list; tp1 != NULL; ) {
460 for (tp2 = tp1->ents; tp2 != NULL; ) {
461 if (tp2->port != NULL)
462 free(tp2->port);
463 if (tp2->call != NULL)
464 free(tp2->call);
465 if (tp2->node != NULL)
466 free(tp2->node);
467 if (tp2->exec != NULL)
468 free(tp2->exec);
469 if (tp2->shell != NULL)
470 free(tp2->shell);
471
472 tmp = tp2->ents;
473 free(tp2);
474 tp2 = tmp;
475 }
476
477 if (tp1->port != NULL)
478 free(tp1->port);
479 if (tp1->call != NULL)
480 free(tp1->call);
481 if (tp1->node != NULL)
482 free(tp1->node);
483 if (tp1->exec != NULL)
484 free(tp1->exec);
485 if (tp1->shell != NULL)
486 free(tp1->shell);
487
488 close(tp1->fd);
489
490 tmp = tp1->next;
491 free(tp1);
492 tp1 = tmp;
493 }
494
495 return NULL;
496 }
497
498 static int ReadConfig(void)
499 {
500 struct axlist axl_defaults;
501 struct axlist *axl_build = NULL;
502 struct axlist *axl_port = NULL;
503 struct axlist *axl_ent, *axltmp;
504 union {
505 struct full_sockaddr_ax25 ax25;
506 struct sockaddr_rose rose;
507 } sockaddr;
508 struct passwd *pwd;
509 FILE *fp;
510 char buffer[2048];
511 char *s, *port, *call, *node, *addr = NULL;
512 unsigned long val;
513 socklen_t addrlen;
514 int af_type = 0; /* Keep GCC happy */
515 int line = 0;
516 int hunt = TRUE, error = FALSE;
517 int parameters = 0;
518
519 signal(SIGALRM, SIG_IGN);
520 memset(&axl_defaults, 0, sizeof(axl_defaults));
521
522 fp = fopen(ConfigFile, "r");
523 if (fp == NULL)
524 return -1;
525
526 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
527 line++;
528
529 s = strchr(buffer, '\n');
530 if (s != NULL)
531 *s = '\0';
532 s = strchr(buffer, '\r');
533 if (s != NULL)
534 *s = '\0';
535
536 if (buffer[0] == '#')
537 continue;
538
539 switch (buffer[0]) {
540 case '[': /* AX25 port call */
541 af_type = AF_AX25;
542 hunt = TRUE;
543 error = FALSE;
544 break;
545
546 case '<': /* NETROM iface call */
547 af_type = AF_NETROM;
548 hunt = TRUE;
549 error = FALSE;
550 break;
551
552 case '{': /* ROSE iface call */
553 af_type = AF_ROSE;
554 hunt = TRUE;
555 error = FALSE;
556 break;
557
558 default:
559 if (hunt && !error)
560 goto BadLine;
561 break;
562 }
563
564 if (hunt) { /* We've found a Iface entry */
565 /* Reset 'defaults' entry on the interface */
566 memset(&axl_defaults, 0, sizeof(axl_defaults));
567
568 switch (af_type) {
569 case AF_AX25:
570 s = strchr(buffer, ']');
571 if (s == NULL)
572 goto BadLine;
573 *s = '\0';
574 s = strtok(buffer + 1, " \t");
575 if (s == NULL)
576 goto BadLine;
577 port = s;
578 call = NULL;
579 s = strtok(NULL, " \t");
580 if (s != NULL) {
581 if (strcasecmp(s, "VIA") == 0 || strcasecmp(s, "V") == 0) {
582 s = strtok(NULL, " \t");
583 if (s == NULL)
584 goto BadLine;
585 }
586
587 call = port;
588 port = s;
589
590 s = strchr(call, '*');
591 if (s != NULL) {
592 *s = '\0';
593 }
594 }
595 if (strcmp(port, "*") == 0 && call == NULL) {
596 fprintf(stderr, "ax25d: invalid port name\n");
597 continue;
598 }
599 if (strcmp(port, "*") != 0) {
600 addr = ax25_config_get_addr(port);
601 if (addr == NULL) {
602 fprintf(stderr, "ax25d: invalid AX.25 port '%s'\n", port);
603 continue;
604 }
605 }
606 if (call == NULL) {
607 sockaddr.ax25.fsa_ax25.sax25_family = AF_AX25;
608 sockaddr.ax25.fsa_ax25.sax25_ndigis = 0;
609 ax25_aton_entry(addr, sockaddr.ax25.fsa_ax25.sax25_call.ax25_call);
610 } else {
611 sockaddr.ax25.fsa_ax25.sax25_family = AF_AX25;
612 sockaddr.ax25.fsa_ax25.sax25_ndigis = 1;
613 ax25_aton_entry(call, sockaddr.ax25.fsa_ax25.sax25_call.ax25_call);
614 if (strcmp(port, "*") != 0)
615 ax25_aton_entry(addr, sockaddr.ax25.fsa_digipeater[0].ax25_call);
616 else
617 sockaddr.ax25.fsa_digipeater[0] = null_ax25_address;
618 }
619 addrlen = sizeof(struct full_sockaddr_ax25);
620 break;
621
622 case AF_NETROM:
623 s = strchr(buffer, '>');
624 if (s == NULL)
625 goto BadLine;
626 *s = '\0';
627 port = buffer + 1;
628 addr = nr_config_get_addr(port);
629 if (addr == NULL) {
630 fprintf(stderr, "ax25d: invalid NET/ROM port '%s'\n", port);
631 continue;
632 }
633 sockaddr.ax25.fsa_ax25.sax25_family = AF_NETROM;
634 sockaddr.ax25.fsa_ax25.sax25_ndigis = 0;
635 ax25_aton_entry(addr, sockaddr.ax25.fsa_ax25.sax25_call.ax25_call);
636 addrlen = sizeof(struct full_sockaddr_ax25);
637 break;
638
639 case AF_ROSE:
640 s = strchr(buffer, '}');
641 if (s == NULL)
642 goto BadLine;
643 *s = '\0';
644 s = strtok(buffer + 1, " \t");
645 if (s == NULL)
646 goto BadLine;
647 call = s;
648 s = strtok(NULL, " \t");
649 if (s == NULL)
650 goto BadLine;
651 if (strcasecmp(s, "VIA") == 0 || strcasecmp(s, "V") == 0) {
652 s = strtok(NULL, " \t");
653 if (s == NULL)
654 goto BadLine;
655 }
656 port = s;
657 addr = rs_config_get_addr(port);
658 if (addr == NULL) {
659 fprintf(stderr, "ax25d: invalid Rose port '%s'\n", port);
660 continue;
661 }
662 if (strcmp(call, "*") == 0) {
663 sockaddr.rose.srose_family = AF_ROSE;
664 sockaddr.rose.srose_ndigis = 0;
665 rose_aton(addr, sockaddr.rose.srose_addr.rose_addr);
666 sockaddr.rose.srose_call = null_ax25_address;
667 } else {
668 sockaddr.rose.srose_family = AF_ROSE;
669 sockaddr.rose.srose_ndigis = 0;
670 rose_aton(addr, sockaddr.rose.srose_addr.rose_addr);
671 ax25_aton_entry(call, sockaddr.rose.srose_call.ax25_call);
672 }
673 addrlen = sizeof(struct sockaddr_rose);
674 break;
675
676 default:
677 fprintf(stderr, "ax25d: unknown af_type=%d\n", af_type);
678 exit(1);
679 }
680
681 axl_port = calloc(1, sizeof(*axl_port));
682 if (axl_port == NULL) {
683 fprintf(stderr, "ax25d: out of memory\n");
684 goto Error;
685 }
686
687 axl_port->port = strdup(port);
688 axl_port->af_type = af_type;
689
690 axl_port->fd = socket(axl_port->af_type,
691 SOCK_SEQPACKET, 0);
692 if (axl_port->fd < 0) {
693 fprintf(stderr, "ax25d: socket: %s\n", strerror(errno));
694 free(axl_port->port);
695 free(axl_port);
696 error = TRUE;
697 reload_timer(60);
698 continue;
699 }
700
701 if (bind(axl_port->fd, (struct sockaddr *)&sockaddr, addrlen) < 0) {
702 fprintf(stderr, "ax25d: bind: %s on port %s\n", strerror(errno), axl_port->port);
703 close(axl_port->fd);
704 free(axl_port->port);
705 free(axl_port);
706 error = TRUE;
707 reload_timer(60);
708 continue;
709 }
710
711 if (listen(axl_port->fd, SOMAXCONN) < 0) {
712 fprintf(stderr, "ax25d: listen: %s\n", strerror(errno));
713 close(axl_port->fd);
714 free(axl_port->port);
715 free(axl_port);
716 error = TRUE;
717 reload_timer(60);
718 continue;
719 }
720
721 /* Add it to the head of the list we are building */
722 if (axl_build == NULL) {
723 axl_build = axl_port;
724 } else {
725 for (axltmp = axl_build; axltmp->next != NULL; axltmp = axltmp->next);
726 axltmp->next = axl_port;
727 }
728
729 hunt = FALSE; /* Next lines will be entries */
730 } else { /* This is an entry */
731 axl_ent = calloc(1, sizeof(*axl_ent));
732 if (axl_ent == NULL) {
733 fprintf(stderr, "ax25d: out of memory\n");
734 goto Error;
735 }
736
737 axl_ent->af_type = axl_port->af_type; /* Inherit this */
738
739 call = strtok(buffer, " \t");
740 if (call == NULL) {
741 free(axl_ent);
742 continue;
743 }
744
745 strupr(call);
746
747 if (axl_ent->af_type == AF_NETROM) {
748 s = strchr(call, '@');
749 if (s != NULL) {
750 node = s + 1;
751 *s = '\0';
752
753 if (*node == '\0') {
754 free(axl_ent);
755 continue;
756 }
757
758 axl_ent->node = strdup(node);
759
760 if (*call == '\0')
761 call = "default"; /* @NODE means default@NODE */
762 }
763 }
764
765 if (axl_ent->af_type == AF_AX25) {
766 if ((strcasecmp("parameters_extAX25", call) == 0)) {
767 axl_defaults.checkVC = axl_defaults.VCdiscOnLinkfailureMsg = axl_defaults.VCsendFailureMsg = axl_defaults.VCloginEnable = axl_defaults.LoggingVC = 0;
768 while ((s = strtok(NULL, " \t")) != NULL) {
769 if (strncmp(s, "VC", 2))
770 goto ignore;
771 if (!strncmp(s, "VC", 2)) {
772 if (!strcasecmp(s, "VC-reject-login"))
773 axl_defaults.checkVC += 2;
774 else if (!strcasecmp(s, "VC-wait-login"))
775 axl_defaults.checkVC += 1;
776 else if (!strcasecmp(s, "VC-disc-on-linkfailure-msg"))
777 axl_defaults.VCdiscOnLinkfailureMsg = 1;
778 else if (!strcasecmp(s, "VC-send-failure-msg"))
779 axl_defaults.VCsendFailureMsg = 1;
780 else if (!strcasecmp(s, "VC-login-ok"))
781 axl_defaults.VCloginEnable = 1;
782 else if (!strcasecmp(s, "VC-log-connections"))
783 axl_defaults.LoggingVC += 1;
784 else if (!strcasecmp(s, "VC-debug"))
785 axl_defaults.LoggingVC += 2;
786 else goto ignore;
787 }
788 continue;
789 ignore:
790 fprintf(stderr, "ax25d: bad config entry on line %d: %s", line, s);
791 fprintf(stderr, " valid parametrs for parameters_extAX25 are:\n[VC-reject-login ||\nVC-wait-login [VC-login-ok] [VC-disc-on-linkfailure-msg]||\nVC-send-failure-msg || VC-log-connections]\n");
792 }
793 if (axl_defaults.checkVC) {
794 if (axl_defaults.checkVC > 2) {
795 fprintf(stderr, "warning: line %d: VC-reject-login and VC-wait-login are exclusive. using VC-reject-login", line);
796 axl_defaults.checkVC = 2;
797 }
798 }
799 continue;
800 }
801 if (axl_defaults.checkVC != 0) {
802 axl_ent->checkVC = axl_defaults.checkVC;
803 axl_ent->VCdiscOnLinkfailureMsg = axl_defaults.VCdiscOnLinkfailureMsg;
804 axl_ent->VCsendFailureMsg = axl_defaults.VCsendFailureMsg;
805 axl_ent->VCloginEnable = axl_defaults.VCloginEnable;
806 axl_ent->LoggingVC = axl_defaults.LoggingVC;
807 }
808 }
809
810 parameters = FALSE;
811
812 if (strcasecmp("parameters", call) == 0)
813 parameters = TRUE;
814 else if (strcasecmp("default", call) != 0)
815 axl_ent->call = strdup(call);
816
817 /* Window */
818 s = strtok(NULL, " \t");
819 if (s == NULL)
820 goto BadArgsFree;
821
822 if (!parameters) {
823 if (strcmp(s, "*") != 0)
824 axl_ent->window = atoi(s);
825 else
826 axl_ent->window = axl_defaults.window;
827 } else {
828 if (strcmp(s, "*") != 0)
829 axl_defaults.window = atoi(s);
830 }
831
832 /* T1 */
833 s = strtok(NULL, " \t");
834 if (s == NULL)
835 goto BadArgsFree;
836
837 if (!parameters) {
838 if (strcmp(s, "*") != 0) {
839 val = (unsigned long)(atof(s) / 0.1);
840
841 if (val == 0 || val > 65535)
842 axl_ent->t1 = axl_defaults.t1;
843 else
844 axl_ent->t1 = val;
845 } else {
846 axl_ent->t1 = axl_defaults.t1;
847 }
848 } else {
849 if (strcmp(s, "*") != 0) {
850 val = (unsigned long)(atof(s) / 0.1);
851
852 if (val > 0 && val < 65535)
853 axl_defaults.t1 = val;
854 }
855 }
856
857 /* T2 */
858 s = strtok(NULL, " \t");
859 if (s == NULL)
860 goto BadArgsFree;
861
862 if (!parameters) {
863 if (strcmp(s, "*") != 0) {
864 val = (unsigned long)(atof(s) / 0.1);
865
866 if (val == 0 || val > 65535)
867 axl_ent->t2 = axl_defaults.t2;
868 else
869 axl_ent->t2 = val;
870 } else {
871 axl_ent->t2 = axl_defaults.t2;
872 }
873 } else {
874 if (strcmp(s, "*") != 0) {
875 val = (unsigned long)(atof(s) / 0.1);
876
877 if (val > 0 && val < 65535)
878 axl_defaults.t2 = val;
879 }
880 }
881
882 /* T3 */
883 s = strtok(NULL, " \t");
884 if (s == NULL)
885 goto BadArgsFree;
886
887 if (!parameters) {
888 if (strcmp(s, "*") != 0)
889 axl_ent->t3 = atoi(s);
890 else
891 axl_ent->t3 = axl_defaults.t3;
892 } else {
893 if (strcmp(s, "*") != 0)
894 axl_defaults.t3 = atoi(s);
895 }
896
897 /* Idle */
898 s = strtok(NULL, " \t");
899 if (s == NULL)
900 goto BadArgsFree;
901
902 if (!parameters) {
903 if (strcmp(s, "*") != 0)
904 axl_ent->idle = atoi(s);
905 else
906 axl_ent->idle = axl_defaults.idle;
907 } else {
908 if (strcmp(s, "*") != 0)
909 axl_defaults.idle = atoi(s);
910 }
911
912 /* N2 */
913 s = strtok(NULL, " \t");
914 if (s == NULL)
915 goto BadArgsFree;
916
917 if (!parameters) {
918 if (strcmp(s, "*") != 0)
919 axl_ent->n2 = atoi(s);
920 else
921 axl_ent->n2 = axl_defaults.n2;
922 } else {
923 if (strcmp(s, "*") != 0)
924 axl_defaults.n2 = atoi(s);
925 }
926
927 if (!parameters) {
928 /* Flags */
929 s = strtok(NULL, " \t");
930 if (s == NULL)
931 goto BadArgsFree;
932
933 axl_ent->flags = ParseFlags(s, line);
934
935 if (!(axl_ent->flags & FLAG_LOCKOUT)) {
936 /* Get username */
937 s = strtok(NULL, " \t");
938 if (s == NULL)
939 goto BadArgsFree;
940
941 pwd = getpwnam(s);
942 if (pwd == NULL) {
943 fprintf(stderr, "ax25d: UID for user '%s' is unknown, ignoring entry\n", s);
944 goto BadUID;
945 }
946
947 axl_ent->uid = pwd->pw_uid;
948 axl_ent->gid = pwd->pw_gid;
949
950 /* Get exec file */
951 s = strtok(NULL, " \t");
952 if (s == NULL)
953 goto BadArgsFree;
954
955 axl_ent->exec = strdup(s);
956
957 /* Get command line */
958 s = strtok(NULL, "");
959 if (s == NULL)
960 goto BadArgsFree2;
961
962 axl_ent->shell = strdup(s);
963 }
964
965 axl_ent->next = NULL;
966
967 if (axl_port->ents == NULL) {
968 axl_port->ents = axl_ent;
969 } else {
970 for (axltmp = axl_port->ents; axltmp->ents != NULL; axltmp = axltmp->ents)
971 ;
972 axltmp->ents = axl_ent;
973 }
974 }
975 }
976
977 continue;
978
979 BadLine:
980 fprintf(stderr, "ax25d: bad config entry on line %d\n", line);
981 continue;
982
983 BadUID:
984 if (axl_ent->call != NULL)
985 free(axl_ent->call);
986 free(axl_ent);
987 continue;
988
989 BadArgsFree2:
990 if (axl_ent->exec != NULL)
991 free(axl_ent->exec);
992 BadArgsFree:
993 if (axl_ent->call != NULL)
994 free(axl_ent->call);
995 free(axl_ent);
996
997 /* BadArgs: */
998 fprintf(stderr, "ax25d: bad config entry on line %d, not enough fields.\n", line);
999 continue;
1000 }
1001
1002 fclose(fp);
1003
1004 AXL = ClearList(AXL);
1005 AXL = axl_build; /* Assign our built list to AXL */
1006
1007 if (Logging)
1008 syslog(LOG_INFO, "new config file loaded successfully");
1009
1010 update_maxfd();
1011 return 0;
1012
1013 Error:
1014 axl_build = ClearList(axl_build);
1015 err_config();
1016
1017 return -1;
1018 }
1019
1020 static char *stripssid(const char *call)
1021 {
1022 static char newcall[10];
1023 char *s;
1024
1025 strcpy(newcall, call);
1026
1027 s = strchr(newcall, '-');
1028 if (s != NULL)
1029 *s = '\0';
1030
1031 return newcall;
1032 }
1033
1034 int main(int argc, char *argv[])
1035 {
1036 struct axlist *axltmp, *paxl, *raxl;
1037 union {
1038 struct full_sockaddr_ax25 ax25;
1039 struct sockaddr_rose rose;
1040 } sockaddr;
1041 struct sigaction act, oact;
1042 socklen_t addrlen;
1043 int cnt;
1044 char buf[1024];
1045 char *p;
1046 char *mesg;
1047
1048 while ((cnt = getopt(argc, argv, "c:lv")) != EOF) {
1049 switch (cnt) {
1050 case 'c':
1051 ConfigFile = optarg;
1052 break;
1053
1054 case 'l':
1055 Logging = TRUE;
1056 break;
1057
1058 case 'v':
1059 printf("ax25d: %s\n", FULL_VER);
1060 return 1;
1061
1062 default:
1063 fprintf(stderr, "Usage: ax25d [-v] [-c altfile] [-l]\n");
1064 return 1;
1065 }
1066 }
1067
1068 if (ax25_config_load_ports() == 0) {
1069 fprintf(stderr, "ax25d: no AX.25 port data configured\n");
1070 return 1;
1071 }
1072
1073 nr_config_load_ports();
1074
1075 rs_config_load_ports();
1076
1077 if (!daemon_start(TRUE)) {
1078 fprintf(stderr, "ax25d: cannot become a daemon\n");
1079 return 1;
1080 }
1081
1082 if (Logging) {
1083 openlog("ax25d", LOG_PID, LOG_DAEMON);
1084 syslog(LOG_INFO, "starting");
1085 }
1086
1087 act.sa_handler = SignalHUP;
1088 sigemptyset(&act.sa_mask);
1089 act.sa_flags = 0;
1090 sigaction(SIGHUP, &act, &oact);
1091
1092 act.sa_handler = SignalTERM;
1093 sigemptyset(&act.sa_mask);
1094 act.sa_flags = 0;
1095 sigaction(SIGTERM, &act, &oact);
1096
1097 ReadConfig();
1098
1099 for (;;) {
1100
1101 update_maxfd();
1102 if (maxfd < 0) {
1103 sleep(10);
1104 continue;
1105 }
1106
1107 if (select(maxfd + 1, &fdread, NULL, NULL, NULL) <= 0)
1108 continue;
1109
1110 for (paxl = AXL; paxl != NULL; paxl = paxl->next) {
1111 if (paxl->fd > 0 && FD_ISSET(paxl->fd, &fdread)) {
1112 pid_t pid;
1113 gid_t grps[2];
1114 char *argv[MAX_ARGS];
1115 int argc;
1116 int new;
1117 int i;
1118
1119 /*
1120 * Setting up a non-blocking accept() so is does not hang up
1121 * - I am not sure at this time why I didn't/don't assign
1122 * the socket non-blocking to start with.
1123 */
1124 /*
1125 * We really need to setup the netrom window option here so
1126 * that it's negotiated correctly on accepting the connection.
1127 */
1128 /*
1129 * It would be very useful if recvmsg/sendmsg were supported
1130 * then we can move the call checking up here.
1131 */
1132 i = TRUE;
1133 ioctl(paxl->fd, FIONBIO, &i);
1134
1135 addrlen = sizeof(struct full_sockaddr_ax25);
1136 new = accept(paxl->fd, (struct sockaddr *)&sockaddr, &addrlen);
1137
1138 i = FALSE;
1139 ioctl(paxl->fd, FIONBIO, &i);
1140
1141 if (new < 0) {
1142 if (errno == EWOULDBLOCK)
1143 continue; /* It's gone ??? */
1144
1145 if (Logging)
1146 syslog(LOG_ERR, "accept error %m, closing socket on port %s", paxl->port);
1147 close(paxl->fd);
1148 paxl->fd = -1;
1149 reload_timer(10);
1150 continue;
1151 }
1152
1153 switch (paxl->af_type) {
1154 case AF_AX25:
1155 strcpy(User, ax25_ntoa(&sockaddr.ax25.fsa_ax25.sax25_call));
1156 strcpy(Node, "");
1157 break;
1158 case AF_NETROM:
1159 strcpy(User, ax25_ntoa(&sockaddr.ax25.fsa_ax25.sax25_call));
1160 strcpy(Node, ax25_ntoa(&sockaddr.ax25.fsa_digipeater[0]));
1161 break;
1162 case AF_ROSE:
1163 strcpy(User, ax25_ntoa(&sockaddr.rose.srose_call));
1164 strcpy(Node, rose_ntoa(&sockaddr.rose.srose_addr));
1165 break;
1166 }
1167
1168 for (raxl = paxl->ents; raxl != NULL; raxl = raxl->ents) {
1169 if (paxl->af_type == AF_NETROM && raxl->node != NULL && Node[0] != '\0') {
1170 if (strchr(raxl->node, '-') == NULL) {
1171 if (strcasecmp(raxl->node, stripssid(Node)) != 0)
1172 continue; /* Found no match (for any SSID) */
1173 } else {
1174 if (strcasecmp(raxl->node, Node) != 0)
1175 continue; /* Found no match */
1176 }
1177 }
1178
1179 if (raxl->call == NULL) /* default */
1180 break;
1181
1182 if (strchr(raxl->call, '-') == NULL) {
1183 if (strcasecmp(raxl->call, stripssid(User)) == 0)
1184 break; /* Found a match (for any SSID) */
1185 } else {
1186 if (strcasecmp(raxl->call, User) == 0)
1187 break; /* Found a match */
1188 }
1189 }
1190
1191 addrlen = sizeof(struct full_sockaddr_ax25);
1192 getsockname(new, (struct sockaddr *)&sockaddr, &addrlen);
1193
1194 switch (paxl->af_type) {
1195 case AF_AX25:
1196 Port = ax25_config_get_port(&sockaddr.ax25.fsa_digipeater[0]);
1197 break;
1198 case AF_NETROM:
1199 Port = nr_config_get_port(&sockaddr.ax25.fsa_ax25.sax25_call);
1200 break;
1201 case AF_ROSE:
1202 Port = rs_config_get_port(&sockaddr.rose.srose_addr);
1203 break;
1204 default:
1205 Port = "???";
1206 break;
1207 }
1208
1209 if (raxl == NULL) {
1210 /* No default */
1211 if (Logging && !(paxl->flags & FLAG_NOLOGGING)) {
1212 switch (paxl->af_type) {
1213 case AF_AX25:
1214 syslog(LOG_INFO, "AX.25 %s (%s) rejected - no default", User, Port);
1215 break;
1216 case AF_NETROM:
1217 syslog(LOG_INFO, "NET/ROM %s@%s (%s) rejected - no default", User, Node, Port);
1218 break;
1219 case AF_ROSE:
1220 syslog(LOG_INFO, "Rose %s@%s (%s) rejected - no default", User, Node, Port);
1221 break;
1222 }
1223 }
1224 close(new);
1225 continue;
1226 }
1227
1228 if (raxl->flags & FLAG_LOCKOUT) {
1229 /* Not allowed to connect */
1230 if (Logging && !(paxl->flags & FLAG_NOLOGGING)) {
1231 switch (raxl->af_type) {
1232 case AF_AX25:
1233 syslog(LOG_INFO, "AX.25 %s (%s) rejected - port locked", User, Port);
1234 break;
1235 case AF_NETROM:
1236 syslog(LOG_INFO, "NET/ROM %s@%s (%s) rejected - port locked", User, Node, Port);
1237 break;
1238 case AF_ROSE:
1239 syslog(LOG_INFO, "Rose %s@%s (%s) rejected - port locked", User, Node, Port);
1240 break;
1241 }
1242 }
1243 close(new);
1244 continue;
1245 }
1246
1247 if (raxl->af_type == AF_AX25 && (raxl->flags & FLAG_NODIGIS) && sockaddr.ax25.fsa_ax25.sax25_ndigis != 0) {
1248 /* Not allowed to uplink via digi's */
1249 if (Logging && !(paxl->flags & FLAG_NOLOGGING))
1250 syslog(LOG_INFO, "AX.25 %s (%s) rejected - digipeaters", User, Port);
1251 close(new);
1252 continue;
1253 }
1254
1255 pid = fork();
1256
1257 switch (pid) {
1258 case -1:
1259 if (Logging)
1260 syslog(LOG_ERR, "fork error %m");
1261 /*
1262 * I don't think AX25 at the moment will hold the
1263 * connection open, if the above does not make it
1264 * through first time.
1265 */
1266 close(new);
1267 break; /* Oh well... */
1268
1269 case 0:
1270
1271 if (raxl->af_type == AF_AX25 && raxl->checkVC) {
1272 /* to which of my own addresses has the user connected? */
1273 getsockname(new, (struct sockaddr *)&sockaddr, &addrlen);
1274 strcpy(myAX25Name, ax25_ntoa(&sockaddr.ax25.fsa_ax25.sax25_call));
1275
1276 sprintf(buf, "/usr/sbin/ax25rtctl -l ip | grep -i ' %s ' | /bin/grep 'v ' >/dev/null", User);
1277
1278 /* he's not a VC user? */
1279 if (system(buf) != 0)
1280 goto login;
1281
1282 if (raxl->checkVC > 1) {
1283 /* setproctitle("ax25d [%d]: rejecting, User); */
1284 } else {
1285 /* setproctitle("ax25d [%d]: %s, User, (VCloginEnable ? "waiting" : "discarding")); */
1286 }
1287
1288 /* is a VC user. checkVC > 0? then reject now, or fork and wait for pid=textdata.. */
1289 if (raxl->VCsendFailureMsg) {
1290 sprintf(buf, "*** %s: NO MODE VC with %s\r", myAX25Name, myAX25Name);
1291 write(new, buf, strlen(buf));
1292 if (raxl->checkVC > 1)
1293 sleep(1);
1294 }
1295
1296 if (raxl->checkVC > 1) {
1297 if (Logging && (!(paxl->flags & FLAG_NOLOGGING) || raxl->LoggingVC))
1298 syslog((raxl->LoggingVC ? LOG_NOTICE : LOG_INFO), "AX.25 %s (%s:%s) rejected - AX25.IP-VC entry found", User, Port, myAX25Name);
1299 goto close_link;
1300 }
1301
1302 /* checkVC == 1: wait for data */
1303 if (Logging && (!(paxl->flags & FLAG_NOLOGGING) || raxl->LoggingVC)) {
1304 syslog((raxl->LoggingVC ? LOG_NOTICE : LOG_INFO), "AX.25 %s (%s:%s) AX25.IP-VC host connected. %s",
1305 User, Port, myAX25Name,
1306 (raxl->VCloginEnable ? "waiting for first PID=text packet for triggered login" : "discarding PID=text packets"));
1307 }
1308
1309 *buf = 0;
1310 mesg = NULL;
1311 while ((i = read(new, buf, (sizeof(buf)-1))) > 0) {
1312 buf[i] = 0;
1313 if (Logging && raxl->LoggingVC > 1) {
1314 /* debug */
1315 syslog((LOG_DEBUG), "DEBUG: AX.25 %s (%s:%s) AX25.IP-VC host said: >%s<", User, Port, myAX25Name, buf);
1316 }
1317 /* skip leading mess */
1318 for (p = buf; *p && (isspace(*p & 0xff) || *p == '\r'); p++) ;
1319 if (raxl->VCdiscOnLinkfailureMsg && !strncmp(p, "***", 3)) {
1320 /* format received line for debug purposes */
1321 /* 1. skip "***" */
1322 for (p += 3; *p && isspace(*p & 0xff); p++ ) ;
1323 mesg = p;
1324 /* 2. get rid off EOL delimiters */
1325 while (*p) {
1326 if (*p == '\r' || *p == '\n') {
1327 *p = 0;
1328 break;
1329 }
1330 p++;
1331 }
1332 /* end read loop */
1333 break;
1334 }
1335 /* per default, we discard all messages,
1336 * because there's no useful combination
1337 * using VC and pidText togegther
1338 */
1339 if (raxl->VCloginEnable)
1340 goto login;
1341 }
1342
1343 if (Logging && (!(paxl->flags & FLAG_NOLOGGING) || raxl->LoggingVC)) {
1344 syslog((raxl->LoggingVC ? LOG_NOTICE : LOG_INFO),
1345 "AX.25 %s (%s:%s) AX25.IP-VC host disconnected%s%s",
1346 User,
1347 Port,
1348 myAX25Name,
1349 (mesg ? ": " : "."),
1350 (mesg ? mesg : ""));
1351 }
1352 /* point of no return */
1353 close_link:
1354 /* close link */
1355 /* setproctitle("ax25d [%s]: disconnecting", User); */
1356 close(new);
1357 exit(0);
1358 }
1359 login:
1360 /* setproctitle("ax25d [%s]: login", User); */
1361
1362 SetupOptions(new, raxl);
1363 WorkoutArgs(raxl->af_type, raxl->shell, &argc, argv);
1364
1365 if (Logging && !(paxl->flags & FLAG_NOLOGGING)) {
1366 switch (paxl->af_type) {
1367 case AF_AX25:
1368 syslog(LOG_INFO, "AX.25 %s (%s) %s", User, Port, argv[0]);
1369 break;
1370 case AF_NETROM:
1371 syslog(LOG_INFO, "NET/ROM %s@%s (%s) %s", User, Node, Port, argv[0]);
1372 break;
1373 case AF_ROSE:
1374 syslog(LOG_INFO, "Rose %s@%s (%s) %s", User, Node, Port, argv[0]);
1375 break;
1376 }
1377 }
1378
1379 dup2(new, STDIN_FILENO);
1380 dup2(new, STDOUT_FILENO);
1381 close(new);
1382
1383 /*
1384 * Might be more efficient if we just went down AXL,
1385 * we cleaned up our parents left overs on bootup.
1386 */
1387 for (axltmp = AXL; axltmp != NULL; axltmp = axltmp->next)
1388 close(axltmp->fd);
1389
1390 if (Logging)
1391 closelog();
1392
1393 /* Make root secure, before we exec() */
1394 /* Strip any supplementary gid's */
1395 if (setgroups(0, grps) == -1)
1396 exit(1);
1397 if (setgid(raxl->gid) == -1)
1398 exit(1);
1399 if (setuid(raxl->uid) == -1)
1400 exit(1);
1401 execve(raxl->exec, argv, NULL);
1402 exit(1);
1403
1404 default:
1405 close(new);
1406 break;
1407 }
1408 }
1409 }
1410 }
1411
1412 /* NOT REACHED */
1413 return 0;
1414 }