2 * $Id: config.c,v 1.13.6.3 2004-02-14 00:30:51 didg Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
12 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <atalk/logger.h>
17 #include <sys/param.h>
20 #include <net/route.h>
23 #include <netatalk/at.h>
24 #include <netatalk/endian.h>
25 #include <atalk/paths.h>
26 #include <atalk/util.h>
37 #else /* STDC_HEADERS */
41 #endif /* HAVE_STRCHR */
42 char *strchr (), *strrchr ();
44 #define memcpy(d,s,n) bcopy ((s), (d), (n))
45 #define memmove(d,s,n) bcopy ((s), (d), (n))
46 #endif /* ! HAVE_MEMCPY */
47 #endif /* STDC_HEADERS */
51 #endif /* HAVE_FCNTL_H */
54 #include <sys/sockio.h>
55 #include <sys/stropts.h>
58 #include "interface.h"
59 #include "multicast.h"
65 #ifndef IFF_SLAVE /* a little backward compatibility */
67 #endif /* IFF_SLAVE */
69 int router(), dontroute(), seed(), phase(), net(), addr(), zone(), noallmulti();
76 { "dontroute", dontroute },
82 { "noallmulti", noallmulti }
85 #define ARGV_CHUNK_SIZE 128
86 #define MAXLINELEN 2048
87 char **parseline(const char *line)
91 char *buffer, *tmpbuf;
94 /* Ignore empty lines and lines with leading hash marks. */
96 while ( isspace( *p ) ) {
99 if ( *p == '#' || *p == '\0' ) {
103 buffer = (char *) malloc( strlen( p ) + 1 );
105 /* FIXME: error handling */
111 argv = (char **) malloc( ARGV_CHUNK_SIZE * sizeof( char * ) );
113 /* FIXME: error handling */
119 * This parser should be made more powerful -- it should
120 * handle various escapes, e.g. \" and \031.
123 if ( *tmpbuf == '"' ) {
124 argv[ argc++ ] = ++tmpbuf;
125 while ( *tmpbuf != '\0' && *tmpbuf != '"' ) {
128 if ( *tmpbuf == '"' ) {
129 /* FIXME: error handling */
132 argv[ argc++ ] = tmpbuf;
133 while ( *tmpbuf != '\0' && !isspace( *tmpbuf )) {
139 /* Make room for a NULL pointer and our special pointer (s.b.) */
140 if ( (argc + 1) % ARGV_CHUNK_SIZE == 0 ) {
142 tmp = (char **) realloc( argv, argc + 1 + ARGV_CHUNK_SIZE * sizeof( char * ) );
144 /* FIXME: error handling */
152 /* Skip white spaces. */
153 while ( isspace( *tmpbuf ) ) {
156 } while ( *tmpbuf != '\0' );
158 argv[ argc++ ] = NULL;
159 /* We store our buffer pointer in argv, too, so we can free it later.
160 * (But don't tell anyone.)
162 argv[ argc ] = buffer;
167 void freeline( char **argv )
184 char *path, *p, newpath[ MAXPATHLEN ], line[ MAXLINELEN ];
186 FILE *conf, *newconf;
187 struct interface *iface;
192 path = _PATH_ATALKDCONF;
197 /* check if old conf is writable */
198 if ( stat( path, &st ) == 0 ) {
199 if (( st.st_mode & S_IWUSR ) == 0 ) {
200 LOG(log_info, logtype_atalkd, "%s not writable, won't rewrite", path );
206 if (( p = strrchr( path, '/' )) == NULL ) {
207 strcpy( newpath, _PATH_ATALKDTMP );
209 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
211 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
212 LOG(log_error, logtype_atalkd, "%s: %s", newpath, strerror(errno) );
215 if (( newconf = fdopen( fd, "w" )) == NULL ) {
216 LOG(log_error, logtype_atalkd, "fdreopen %s: %s", newpath, strerror(errno) );
220 if (( conf = fopen( path, "r" )) == NULL && cf ) {
221 LOG(log_error, logtype_atalkd, "%s: %s", path, strerror(errno) );
225 iface = interfaces->i_next;
227 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
228 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
229 if ( fputs( line, newconf ) == EOF ) {
230 LOG(log_error, logtype_atalkd, "fputs: %s", strerror(errno) );
237 /* write real lines */
239 fprintf( newconf, "%s", iface->i_name );
240 if ( iface->i_flags & IFACE_RSEED ) {
241 fprintf( newconf, " -router" );
242 } else if ( iface->i_flags & IFACE_SEED ) {
243 fprintf( newconf, " -seed" );
245 if ( iface->i_flags & IFACE_DONTROUTE) {
246 fprintf( newconf, " -dontroute");
248 if ( !(iface->i_flags & IFACE_ALLMULTI)) {
249 fprintf( newconf, " -noallmulti");
252 fprintf( newconf, " -phase %d",
253 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
254 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
255 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
256 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
258 fprintf( newconf, " -addr %u.%u",
259 ntohs( iface->i_addr.sat_addr.s_net ),
260 iface->i_addr.sat_addr.s_node );
261 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
262 fprintf( newconf, " -zone \"%.*s\"",
263 ((struct ziptab *)l->l_data)->zt_len,
264 ((struct ziptab *)l->l_data)->zt_name );
266 fprintf( newconf, "\n" );
268 iface = iface->i_next;
269 if ( conf == NULL && iface == NULL ) {
274 if ( conf != NULL ) {
279 if ( rename( newpath, path ) < 0 ) {
280 LOG(log_error, logtype_atalkd, "rename %s to %s: %s", newpath, path, strerror(errno) );
287 * Read our config file. If it's not there, return -1. If it is there,
288 * but has syntax errors, exit. Format of the file is as follows:
290 * interface [ -seed ] [ -phase number ] [ -net net-range ]
291 * [ -addr net.node ] [ -zone zonename ]...
293 * le0 -phase 1 -net 7938 -zone Argus
295 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
296 * le0 -phase 1 -net 7938 -zone Argus
298 * Pretty much everything is optional. Anything that is unspecified is
299 * searched for on the network. If -seed is not specified, the
300 * configuration is assumed to be soft, i.e. it can be overridden by
301 * another router. If -seed is specified, atalkd will exit if another
302 * router disagrees. If the phase is unspecified, it defaults to phase
303 * 2 (the default can be overridden on the command line). -addr can
304 * replace -net, if the network in question isn't a range. The default
305 * zone for an interface is the first zone encountered for that
312 struct interface *iface, *niface;
313 char line[ MAXLINELEN ], **argv, *p;
319 p = _PATH_ATALKDCONF;
323 if (( conf = fopen( p, "r" )) == NULL ) {
328 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
333 #endif /* __svr4__ */
335 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
336 if (( argv = parseline( line )) == NULL ) {
342 * Check that av[ 0 ] is a valid interface.
343 * Not possible under sysV.
345 strlcpy( ifr.ifr_name, argv[ 0 ], sizeof(ifr.ifr_name) );
347 /* for devices that don't support appletalk */
348 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
353 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
358 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
359 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
364 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
365 fprintf(stderr, "%s: multicast may not work properly.\n",
367 #endif /* IFF_MULTICAST */
369 /* configure hw multicast for this interface. */
370 if (addmulti(ifr.ifr_name, NULL) < 0) {
371 perror(ifr.ifr_name);
372 fprintf(stderr, "Can't configure multicast.\n");
376 #endif /* __svr4__ */
378 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
379 perror( "newiface" );
383 for ( i = 1; argv[ i ]; i += cc ) {
384 if ( argv[ i ][ 0 ] == '-' ) {
387 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
388 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
389 if ( params[ j ].p_func != NULL ) {
390 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
397 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
398 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
403 for ( iface = interfaces; iface; iface = iface->i_next ) {
404 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
405 ((( niface->i_flags & iface->i_flags &
406 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
407 niface->i_flags == 0 || iface->i_flags == 0 )) {
411 if ( iface ) { /* Already have this interface and phase */
412 fprintf( stderr, "%s already configured!\n", niface->i_name );
417 /* Don't set interface to allmulti if it already is, or -noallmulti was given */
418 if ((ifr.ifr_flags & IFF_ALLMULTI))
419 niface->i_flags |= IFACE_WASALLMULTI;
421 if ((niface->i_flags & IFACE_ALLMULTI) && !(niface->i_flags & IFACE_WASALLMULTI))
422 ifsetallmulti(ifr.ifr_name, 1);
425 if ( interfaces == NULL ) {
428 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
430 iface->i_next = niface;
432 niface->i_next = NULL;
437 #endif /* __svr4__ */
442 * Note: we've added lo0 to the interface list previously, so we must
443 * have configured more than one interface...
445 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
447 if ( cc >= IFBASE ) {
456 #endif /* __svr4__ */
461 int noallmulti( iface, av )
462 struct interface *iface;
465 /* Linux specific, no effect on other platforms */
466 iface->i_flags &= !IFACE_ALLMULTI;
472 int router( iface, av )
473 struct interface *iface;
476 /* make sure "-router" and "-dontroute" aren't both on the same line. */
477 if (iface->i_flags & IFACE_DONTROUTE) {
478 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
483 * Check to be sure "-router" is before "-zone".
485 if ( iface->i_czt ) {
486 fprintf( stderr, "Must specify -router before -zone.\n");
490 /* -router also implies -seed */
491 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
496 int dontroute( iface, av )
497 struct interface *iface;
500 /* make sure "-router" and "-dontroute" aren't both on the same line. */
501 if (iface->i_flags & IFACE_RSEED) {
502 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
506 iface->i_flags |= IFACE_DONTROUTE;
511 int seed( iface, av )
512 struct interface *iface;
516 * Check to be sure "-seed" is before "-zone". we keep the old
517 * semantics of just ignoring this in a routerless world.
519 if ( iface->i_czt ) {
520 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
521 iface->i_czt->zt_name);
525 iface->i_flags |= IFACE_SEED;
529 int phase( iface, av )
530 struct interface *iface;
536 if (( pnum = av[ 0 ] ) == NULL ) {
537 fprintf( stderr, "No phase.\n" );
541 switch ( n = atoi( pnum )) {
543 iface->i_flags |= IFACE_PHASE1;
547 iface->i_flags |= IFACE_PHASE2;
551 fprintf( stderr, "No phase %d.\n", n );
558 struct interface *iface;
565 if (( nrange = av[ 0 ] ) == NULL ) {
566 fprintf( stderr, "No network.\n" );
570 if (( stop = strchr( nrange, '-' )) != 0 ) {
573 net = atoi( nrange );
574 if ( net < 0 || net >= 0xffff ) {
575 fprintf( stderr, "Bad network: %d\n", net );
579 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
584 if ( iface->i_flags & IFACE_PHASE1 ) {
586 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
589 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
590 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
591 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
592 net, ntohs( iface->i_caddr.sat_addr.s_net ));
595 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
596 } else if ( iface->i_flags & IFACE_PHASE2 ) {
597 iface->i_rt->rt_firstnet = htons( net );
600 if ( net < 0 || net >= 0xffff ) {
601 fprintf( stderr, "Bad network: %d\n", net );
605 iface->i_rt->rt_lastnet = htons( net );
606 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
607 ( ntohs( iface->i_rt->rt_firstnet ) >
608 ntohs( iface->i_caddr.sat_addr.s_net ) ||
609 ntohs( iface->i_rt->rt_lastnet ) <
610 ntohs( iface->i_caddr.sat_addr.s_net ))) {
611 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
612 ntohs( iface->i_rt->rt_firstnet ),
613 ntohs( iface->i_rt->rt_lastnet ),
614 ntohs( iface->i_caddr.sat_addr.s_net ));
617 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
618 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
621 fprintf( stderr, "Must specify phase before networks.\n" );
627 int addr( iface, av )
628 struct interface *iface;
631 if ( av[ 0 ] == NULL ) {
632 fprintf( stderr, "No address.\n" );
635 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
636 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
641 if ( ntohs( iface->i_rt->rt_firstnet ) >
642 ntohs( iface->i_caddr.sat_addr.s_net ) ||
643 ntohs( iface->i_rt->rt_lastnet ) <
644 ntohs( iface->i_caddr.sat_addr.s_net )) {
645 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
646 ntohs( iface->i_caddr.sat_addr.s_net ),
647 ntohs( iface->i_rt->rt_firstnet ),
648 ntohs( iface->i_rt->rt_lastnet ));
652 if (( iface->i_rt = newrt(iface)) == NULL ) {
656 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
657 iface->i_caddr.sat_addr.s_net;
663 int zone( iface, av )
664 struct interface *iface;
670 if (( zname = av[ 0 ] ) == NULL ) {
671 fprintf( stderr, "No zone.\n" );
676 * Only process "-zone" if this interface has "-seed". We keep our
677 * list of configured zones in the interface structure. Then we can
678 * check that the network has given us good zones.
680 if ( iface->i_flags & IFACE_SEED ) {
681 if ( iface->i_rt == NULL ) {
682 fprintf( stderr, "Must specify net-range before zones.\n" );
685 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
689 if ( iface->i_czt == NULL ) {
692 zt->zt_next = iface->i_czt->zt_next;
693 iface->i_czt->zt_next = zt;
700 * Get the configuration from the kernel. Only called if there's no
705 struct interface *iface, *niface;
707 char **start, **list;
710 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
715 start = list = getifacelist();
716 while (list && *list) {
717 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
720 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
723 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
726 if ((ifr.ifr_flags & IFF_UP) == 0)
729 /* for devices that don't support appletalk */
730 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
733 for ( iface = interfaces; iface; iface = iface->i_next ) {
734 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
738 if ( iface ) { /* Already have this interface name */
744 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
745 fprintf(stderr, "%s: multicast may not work correctly.\n",
747 #endif /* IFF_MULTICAST */
749 if (addmulti(ifr.ifr_name, NULL) < 0) {
750 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
754 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
755 perror( "newiface" );
757 freeifacelist(start);
761 * Could try to get the address from the kernel...
764 if ( interfaces == NULL ) {
767 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
769 iface->i_next = niface;
771 niface->i_next = NULL;
773 freeifacelist(start);
779 * Allocate a new interface structure. Centralized here so we can change
780 * the interface structure and have it updated nicely.
783 struct interface *newiface( name )
786 struct interface *niface;
788 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
792 strlcpy( niface->i_name, name, sizeof(niface->i_name));
794 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
796 niface->i_addr.sat_family = AF_APPLETALK;
798 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
800 niface->i_caddr.sat_family = AF_APPLETALK;
802 niface->i_flags = IFACE_ALLMULTI;
810 struct interface *iface;
811 char device[ MAXPATHLEN + 1], *p;
814 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
815 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
819 strcpy( device, "/dev/" );
820 strcat( device, iface->i_name );
821 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
822 LOG(log_error, logtype_atalkd, "plumb: invalid device: %s", device );
828 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
829 LOG(log_error, logtype_atalkd, "%s: %m", device );
832 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
833 LOG(log_error, logtype_atalkd, "I_PUSH: %m" );
837 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
838 LOG(log_error, logtype_atalkd, "IF_UNITSEL: %m" );
843 /* configure multicast. */
844 if (addmulti(iface->i_name, NULL) < 0) {
845 perror(iface->i_name);
846 fprintf(stderr,"Can't configure multicast.\n");
851 LOG(log_info, logtype_atalkd, "plumbed %s%d", device, ppa );
856 #endif /* __svr4__ */