2 * $Id: config.c,v 1.10 2001-12-31 20:01:17 srittau 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>
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"
64 #ifndef IFF_SLAVE /* a little backward compatibility */
66 #endif /* IFF_SLAVE */
68 int router(), dontroute(), seed(), phase(), net(), addr(), zone();
75 { "dontroute", dontroute },
83 #define ARGV_CHUNK_SIZE 128
84 char **parseline(const char *line)
88 char *buffer, *tmpbuf;
91 /* Ignore empty lines and lines with leading hash marks. */
93 while ( isspace( *p ) ) {
96 if ( *p == '#' || *p == '\0' ) {
100 buffer = (char *) malloc( strlen( p ) + 1 );
102 /* FIXME: error handling */
108 argv = (char **) malloc( ARGV_CHUNK_SIZE * sizeof( char * ) );
110 /* FIXME: error handling */
116 * This parser should be made more powerful -- it should
117 * handle various escapes, e.g. \" and \031.
120 if ( *tmpbuf == '"' ) {
121 argv[ argc++ ] = ++tmpbuf;
122 while ( *tmpbuf != '\0' && *tmpbuf != '"' ) {
125 if ( *tmpbuf == '"' ) {
126 /* FIXME: error handling */
129 argv[ argc++ ] = tmpbuf;
130 while ( *tmpbuf != '\0' && !isspace( *tmpbuf )) {
136 /* Make room for a NULL pointer and our special pointer (s.b.) */
137 if ( (argc + 1) % ARGV_CHUNK_SIZE == 0 ) {
139 tmp = (char **) realloc( argv, argc + 1 + ARGV_CHUNK_SIZE );
141 /* FIXME: error handling */
149 /* Skip white spaces. */
150 while ( isspace( *tmpbuf ) ) {
153 } while ( *tmpbuf != '\0' );
155 argv[ argc++ ] = NULL;
156 /* We store our buffer pointer in argv, too, so we can free it later.
157 * (But don't tell anyone.)
159 argv[ argc ] = buffer;
164 void freeline( char **argv )
181 char *path, *p, newpath[ MAXPATHLEN ], line[ 1024 ];
183 FILE *conf, *newconf;
184 struct interface *iface;
189 path = _PATH_ATALKDCONF;
194 /* check if old conf is writable */
195 if ( stat( path, &st ) == 0 ) {
196 if (( st.st_mode & S_IWUSR ) == 0 ) {
197 syslog( LOG_INFO, "%s not writable, won't rewrite", path );
203 if (( p = strrchr( path, '/' )) == NULL ) {
204 strcpy( newpath, _PATH_ATALKDTMP );
206 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
208 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
209 syslog( LOG_ERR, "%s: %s", newpath, strerror(errno) );
212 if (( newconf = fdopen( fd, "w" )) == NULL ) {
213 syslog( LOG_ERR, "fdreopen %s: %s", newpath, strerror(errno) );
217 if (( conf = fopen( path, "r" )) == NULL && cf ) {
218 syslog( LOG_ERR, "%s: %s", path, strerror(errno) );
222 iface = interfaces->i_next;
224 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
225 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
226 if ( fputs( line, newconf ) == EOF ) {
227 syslog( LOG_ERR, "fputs: %s", strerror(errno) );
234 /* write real lines */
236 fprintf( newconf, "%s", iface->i_name );
237 if ( iface->i_flags & IFACE_RSEED ) {
238 fprintf( newconf, " -router" );
239 } else if ( iface->i_flags & IFACE_SEED ) {
240 fprintf( newconf, " -seed" );
242 if ( iface->i_flags & IFACE_DONTROUTE) {
243 fprintf( newconf, " -dontroute");
246 fprintf( newconf, " -phase %d",
247 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
248 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
249 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
250 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
252 fprintf( newconf, " -addr %u.%u",
253 ntohs( iface->i_addr.sat_addr.s_net ),
254 iface->i_addr.sat_addr.s_node );
255 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
256 fprintf( newconf, " -zone \"%.*s\"",
257 ((struct ziptab *)l->l_data)->zt_len,
258 ((struct ziptab *)l->l_data)->zt_name );
260 fprintf( newconf, "\n" );
262 iface = iface->i_next;
263 if ( conf == NULL && iface == NULL ) {
268 if ( conf != NULL ) {
273 if ( rename( newpath, path ) < 0 ) {
274 syslog( LOG_ERR, "rename %s to %s: %s", newpath, path, strerror(errno) );
281 * Read our config file. If it's not there, return -1. If it is there,
282 * but has syntax errors, exit. Format of the file is as follows:
284 * interface [ -seed ] [ -phase number ] [ -net net-range ]
285 * [ -addr net.node ] [ -zone zonename ]...
287 * le0 -phase 1 -net 7938 -zone Argus
289 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
290 * le0 -phase 1 -net 7938 -zone Argus
292 * Pretty much everything is optional. Anything that is unspecified is
293 * searched for on the network. If -seed is not specified, the
294 * configuration is assumed to be soft, i.e. it can be overridden by
295 * another router. If -seed is specified, atalkd will exit if another
296 * router disagrees. If the phase is unspecified, it defaults to phase
297 * 2 (the default can be overridden on the command line). -addr can
298 * replace -net, if the network in question isn't a range. The default
299 * zone for an interface is the first zone encountered for that
306 struct interface *iface, *niface;
307 char line[ 1024 ], **argv, *p;
312 p = _PATH_ATALKDCONF;
316 if (( conf = fopen( p, "r" )) == NULL ) {
321 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
326 #endif /* __svr4__ */
328 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
329 if (( argv = parseline( line )) == NULL ) {
335 * Check that av[ 0 ] is a valid interface.
336 * Not possible under sysV.
338 strncpy( ifr.ifr_name, argv[ 0 ], sizeof(ifr.ifr_name) );
339 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
341 /* for devices that don't support appletalk */
342 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
347 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
352 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
353 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
358 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
359 fprintf(stderr, "%s: multicast may not work properly.\n",
361 #endif /* IFF_MULTICAST */
363 /* configure hw multicast for this interface. */
364 if (addmulti(ifr.ifr_name, NULL) < 0) {
365 perror(ifr.ifr_name);
366 fprintf(stderr, "Can't configure multicast.\n");
369 #endif /* __svr4__ */
371 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
372 perror( "newiface" );
376 for ( i = 1; argv[ i ]; i += cc ) {
377 if ( argv[ i ][ 0 ] == '-' ) {
380 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
381 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
382 if ( params[ j ].p_func != NULL ) {
383 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
390 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
391 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
396 for ( iface = interfaces; iface; iface = iface->i_next ) {
397 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
398 ((( niface->i_flags & iface->i_flags &
399 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
400 niface->i_flags == 0 || iface->i_flags == 0 )) {
404 if ( iface ) { /* Already have this interface and phase */
405 fprintf( stderr, "%s already configured!\n", niface->i_name );
409 if ( interfaces == NULL ) {
412 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
414 iface->i_next = niface;
416 niface->i_next = NULL;
421 #endif /* __svr4__ */
426 * Note: we've added lo0 to the interface list previously, so we must
427 * have configured more than one interface...
429 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
431 if ( cc >= IFBASE ) {
440 #endif /* __svr4__ */
446 int router( iface, av )
447 struct interface *iface;
450 /* make sure "-router" and "-dontroute" aren't both on the same line. */
451 if (iface->i_flags & IFACE_DONTROUTE) {
452 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
457 * Check to be sure "-router" is before "-zone".
459 if ( iface->i_czt ) {
460 fprintf( stderr, "Must specify -router before -zone.\n");
464 /* -router also implies -seed */
465 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
470 int dontroute( iface, av )
471 struct interface *iface;
474 /* make sure "-router" and "-dontroute" aren't both on the same line. */
475 if (iface->i_flags & IFACE_RSEED) {
476 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
480 iface->i_flags |= IFACE_DONTROUTE;
485 int seed( iface, av )
486 struct interface *iface;
490 * Check to be sure "-seed" is before "-zone". we keep the old
491 * semantics of just ignoring this in a routerless world.
493 if ( iface->i_czt ) {
494 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
495 iface->i_czt->zt_name);
499 iface->i_flags |= IFACE_SEED;
503 int phase( iface, av )
504 struct interface *iface;
510 if (( pnum = av[ 0 ] ) == NULL ) {
511 fprintf( stderr, "No phase.\n" );
515 switch ( n = atoi( pnum )) {
517 iface->i_flags |= IFACE_PHASE1;
521 iface->i_flags |= IFACE_PHASE2;
525 fprintf( stderr, "No phase %d.\n", n );
532 struct interface *iface;
539 if (( nrange = av[ 0 ] ) == NULL ) {
540 fprintf( stderr, "No network.\n" );
544 if (( stop = strchr( nrange, '-' )) != 0 ) {
547 net = atoi( nrange );
548 if ( net < 0 || net >= 0xffff ) {
549 fprintf( stderr, "Bad network: %d\n", net );
553 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
558 if ( iface->i_flags & IFACE_PHASE1 ) {
560 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
563 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
564 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
565 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
566 net, ntohs( iface->i_caddr.sat_addr.s_net ));
569 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
570 } else if ( iface->i_flags & IFACE_PHASE2 ) {
571 iface->i_rt->rt_firstnet = htons( net );
574 if ( net < 0 || net >= 0xffff ) {
575 fprintf( stderr, "Bad network: %d\n", net );
579 iface->i_rt->rt_lastnet = htons( net );
580 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
581 ( ntohs( iface->i_rt->rt_firstnet ) >
582 ntohs( iface->i_caddr.sat_addr.s_net ) ||
583 ntohs( iface->i_rt->rt_lastnet ) <
584 ntohs( iface->i_caddr.sat_addr.s_net ))) {
585 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
586 ntohs( iface->i_rt->rt_firstnet ),
587 ntohs( iface->i_rt->rt_lastnet ),
588 ntohs( iface->i_caddr.sat_addr.s_net ));
591 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
592 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
595 fprintf( stderr, "Must specify phase before networks.\n" );
601 int addr( iface, av )
602 struct interface *iface;
605 if ( av[ 0 ] == NULL ) {
606 fprintf( stderr, "No address.\n" );
609 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
610 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
615 if ( ntohs( iface->i_rt->rt_firstnet ) >
616 ntohs( iface->i_caddr.sat_addr.s_net ) ||
617 ntohs( iface->i_rt->rt_lastnet ) <
618 ntohs( iface->i_caddr.sat_addr.s_net )) {
619 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
620 ntohs( iface->i_caddr.sat_addr.s_net ),
621 ntohs( iface->i_rt->rt_firstnet ),
622 ntohs( iface->i_rt->rt_lastnet ));
626 if (( iface->i_rt = newrt(iface)) == NULL ) {
630 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
631 iface->i_caddr.sat_addr.s_net;
637 int zone( iface, av )
638 struct interface *iface;
644 if (( zname = av[ 0 ] ) == NULL ) {
645 fprintf( stderr, "No zone.\n" );
650 * Only process "-zone" if this interface has "-seed". We keep our
651 * list of configured zones in the interface structure. Then we can
652 * check that the network has given us good zones.
654 if ( iface->i_flags & IFACE_SEED ) {
655 if ( iface->i_rt == NULL ) {
656 fprintf( stderr, "Must specify net-range before zones.\n" );
659 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
663 if ( iface->i_czt == NULL ) {
666 zt->zt_next = iface->i_czt->zt_next;
667 iface->i_czt->zt_next = zt;
674 * Get the configuration from the kernel. Only called if there's no
679 struct interface *iface, *niface;
681 char **start, **list;
684 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
689 start = list = getifacelist();
690 while (list && *list) {
691 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
694 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
697 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
700 if ((ifr.ifr_flags & IFF_UP) == 0)
703 /* for devices that don't support appletalk */
704 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
707 for ( iface = interfaces; iface; iface = iface->i_next ) {
708 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
712 if ( iface ) { /* Already have this interface name */
718 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
719 fprintf(stderr, "%s: multicast may not work correctly.\n",
721 #endif /* IFF_MULTICAST */
723 if (addmulti(ifr.ifr_name, NULL) < 0) {
724 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
728 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
729 perror( "newiface" );
731 freeifacelist(start);
735 * Could try to get the address from the kernel...
738 if ( interfaces == NULL ) {
741 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
743 iface->i_next = niface;
745 niface->i_next = NULL;
747 freeifacelist(start);
753 * Allocate a new interface structure. Centralized here so we can change
754 * the interface structure and have it updated nicely.
757 struct interface *newiface( name )
760 struct interface *niface;
762 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
766 strncpy( niface->i_name, name, sizeof(niface->i_name));
768 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
770 niface->i_addr.sat_family = AF_APPLETALK;
772 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
774 niface->i_caddr.sat_family = AF_APPLETALK;
781 struct interface *iface;
782 char device[ MAXPATHLEN + 1], *p;
785 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
786 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
790 strcpy( device, "/dev/" );
791 strcat( device, iface->i_name );
792 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
793 syslog( LOG_ERR, "plumb: invalid device: %s", device );
799 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
800 syslog( LOG_ERR, "%s: %m", device );
803 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
804 syslog( LOG_ERR, "I_PUSH: %m" );
808 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
809 syslog( LOG_ERR, "IF_UNITSEL: %m" );
814 /* configure multicast. */
815 if (addmulti(iface->i_name, NULL) < 0) {
816 perror(iface->i_name);
817 fprintf(stderr,"Can't configure multicast.\n");
822 syslog( LOG_INFO, "plumbed %s%d", device, ppa );
827 #endif /* __svr4__ */