2 * $Id: config.c,v 1.13 2003-03-18 23:32: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>
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"
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 #define MAXLINELEN 2048
85 char **parseline(const char *line)
89 char *buffer, *tmpbuf;
92 /* Ignore empty lines and lines with leading hash marks. */
94 while ( isspace( *p ) ) {
97 if ( *p == '#' || *p == '\0' ) {
101 buffer = (char *) malloc( strlen( p ) + 1 );
103 /* FIXME: error handling */
109 argv = (char **) malloc( ARGV_CHUNK_SIZE * sizeof( char * ) );
111 /* FIXME: error handling */
117 * This parser should be made more powerful -- it should
118 * handle various escapes, e.g. \" and \031.
121 if ( *tmpbuf == '"' ) {
122 argv[ argc++ ] = ++tmpbuf;
123 while ( *tmpbuf != '\0' && *tmpbuf != '"' ) {
126 if ( *tmpbuf == '"' ) {
127 /* FIXME: error handling */
130 argv[ argc++ ] = tmpbuf;
131 while ( *tmpbuf != '\0' && !isspace( *tmpbuf )) {
137 /* Make room for a NULL pointer and our special pointer (s.b.) */
138 if ( (argc + 1) % ARGV_CHUNK_SIZE == 0 ) {
140 tmp = (char **) realloc( argv, argc + 1 + ARGV_CHUNK_SIZE * sizeof( char * ) );
142 /* FIXME: error handling */
150 /* Skip white spaces. */
151 while ( isspace( *tmpbuf ) ) {
154 } while ( *tmpbuf != '\0' );
156 argv[ argc++ ] = NULL;
157 /* We store our buffer pointer in argv, too, so we can free it later.
158 * (But don't tell anyone.)
160 argv[ argc ] = buffer;
165 void freeline( char **argv )
182 char *path, *p, newpath[ MAXPATHLEN ], line[ MAXLINELEN ];
184 FILE *conf, *newconf;
185 struct interface *iface;
190 path = _PATH_ATALKDCONF;
195 /* check if old conf is writable */
196 if ( stat( path, &st ) == 0 ) {
197 if (( st.st_mode & S_IWUSR ) == 0 ) {
198 LOG(log_info, logtype_atalkd, "%s not writable, won't rewrite", path );
204 if (( p = strrchr( path, '/' )) == NULL ) {
205 strcpy( newpath, _PATH_ATALKDTMP );
207 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
209 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
210 LOG(log_error, logtype_atalkd, "%s: %s", newpath, strerror(errno) );
213 if (( newconf = fdopen( fd, "w" )) == NULL ) {
214 LOG(log_error, logtype_atalkd, "fdreopen %s: %s", newpath, strerror(errno) );
218 if (( conf = fopen( path, "r" )) == NULL && cf ) {
219 LOG(log_error, logtype_atalkd, "%s: %s", path, strerror(errno) );
223 iface = interfaces->i_next;
225 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
226 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
227 if ( fputs( line, newconf ) == EOF ) {
228 LOG(log_error, logtype_atalkd, "fputs: %s", strerror(errno) );
235 /* write real lines */
237 fprintf( newconf, "%s", iface->i_name );
238 if ( iface->i_flags & IFACE_RSEED ) {
239 fprintf( newconf, " -router" );
240 } else if ( iface->i_flags & IFACE_SEED ) {
241 fprintf( newconf, " -seed" );
243 if ( iface->i_flags & IFACE_DONTROUTE) {
244 fprintf( newconf, " -dontroute");
247 fprintf( newconf, " -phase %d",
248 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
249 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
250 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
251 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
253 fprintf( newconf, " -addr %u.%u",
254 ntohs( iface->i_addr.sat_addr.s_net ),
255 iface->i_addr.sat_addr.s_node );
256 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
257 fprintf( newconf, " -zone \"%.*s\"",
258 ((struct ziptab *)l->l_data)->zt_len,
259 ((struct ziptab *)l->l_data)->zt_name );
261 fprintf( newconf, "\n" );
263 iface = iface->i_next;
264 if ( conf == NULL && iface == NULL ) {
269 if ( conf != NULL ) {
274 if ( rename( newpath, path ) < 0 ) {
275 LOG(log_error, logtype_atalkd, "rename %s to %s: %s", newpath, path, strerror(errno) );
282 * Read our config file. If it's not there, return -1. If it is there,
283 * but has syntax errors, exit. Format of the file is as follows:
285 * interface [ -seed ] [ -phase number ] [ -net net-range ]
286 * [ -addr net.node ] [ -zone zonename ]...
288 * le0 -phase 1 -net 7938 -zone Argus
290 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
291 * le0 -phase 1 -net 7938 -zone Argus
293 * Pretty much everything is optional. Anything that is unspecified is
294 * searched for on the network. If -seed is not specified, the
295 * configuration is assumed to be soft, i.e. it can be overridden by
296 * another router. If -seed is specified, atalkd will exit if another
297 * router disagrees. If the phase is unspecified, it defaults to phase
298 * 2 (the default can be overridden on the command line). -addr can
299 * replace -net, if the network in question isn't a range. The default
300 * zone for an interface is the first zone encountered for that
307 struct interface *iface, *niface;
308 char line[ MAXLINELEN ], **argv, *p;
313 p = _PATH_ATALKDCONF;
317 if (( conf = fopen( p, "r" )) == NULL ) {
322 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
327 #endif /* __svr4__ */
329 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
330 if (( argv = parseline( line )) == NULL ) {
336 * Check that av[ 0 ] is a valid interface.
337 * Not possible under sysV.
339 strncpy( ifr.ifr_name, argv[ 0 ], sizeof(ifr.ifr_name) );
340 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
342 /* for devices that don't support appletalk */
343 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
348 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
353 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
354 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
359 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
360 fprintf(stderr, "%s: multicast may not work properly.\n",
362 #endif /* IFF_MULTICAST */
364 /* configure hw multicast for this interface. */
365 if (addmulti(ifr.ifr_name, NULL) < 0) {
366 perror(ifr.ifr_name);
367 fprintf(stderr, "Can't configure multicast.\n");
370 #endif /* __svr4__ */
372 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
373 perror( "newiface" );
377 for ( i = 1; argv[ i ]; i += cc ) {
378 if ( argv[ i ][ 0 ] == '-' ) {
381 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
382 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
383 if ( params[ j ].p_func != NULL ) {
384 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
391 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
392 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
397 for ( iface = interfaces; iface; iface = iface->i_next ) {
398 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
399 ((( niface->i_flags & iface->i_flags &
400 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
401 niface->i_flags == 0 || iface->i_flags == 0 )) {
405 if ( iface ) { /* Already have this interface and phase */
406 fprintf( stderr, "%s already configured!\n", niface->i_name );
410 if ( interfaces == NULL ) {
413 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
415 iface->i_next = niface;
417 niface->i_next = NULL;
422 #endif /* __svr4__ */
427 * Note: we've added lo0 to the interface list previously, so we must
428 * have configured more than one interface...
430 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
432 if ( cc >= IFBASE ) {
441 #endif /* __svr4__ */
447 int router( iface, av )
448 struct interface *iface;
451 /* make sure "-router" and "-dontroute" aren't both on the same line. */
452 if (iface->i_flags & IFACE_DONTROUTE) {
453 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
458 * Check to be sure "-router" is before "-zone".
460 if ( iface->i_czt ) {
461 fprintf( stderr, "Must specify -router before -zone.\n");
465 /* -router also implies -seed */
466 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
471 int dontroute( iface, av )
472 struct interface *iface;
475 /* make sure "-router" and "-dontroute" aren't both on the same line. */
476 if (iface->i_flags & IFACE_RSEED) {
477 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
481 iface->i_flags |= IFACE_DONTROUTE;
486 int seed( iface, av )
487 struct interface *iface;
491 * Check to be sure "-seed" is before "-zone". we keep the old
492 * semantics of just ignoring this in a routerless world.
494 if ( iface->i_czt ) {
495 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
496 iface->i_czt->zt_name);
500 iface->i_flags |= IFACE_SEED;
504 int phase( iface, av )
505 struct interface *iface;
511 if (( pnum = av[ 0 ] ) == NULL ) {
512 fprintf( stderr, "No phase.\n" );
516 switch ( n = atoi( pnum )) {
518 iface->i_flags |= IFACE_PHASE1;
522 iface->i_flags |= IFACE_PHASE2;
526 fprintf( stderr, "No phase %d.\n", n );
533 struct interface *iface;
540 if (( nrange = av[ 0 ] ) == NULL ) {
541 fprintf( stderr, "No network.\n" );
545 if (( stop = strchr( nrange, '-' )) != 0 ) {
548 net = atoi( nrange );
549 if ( net < 0 || net >= 0xffff ) {
550 fprintf( stderr, "Bad network: %d\n", net );
554 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
559 if ( iface->i_flags & IFACE_PHASE1 ) {
561 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
564 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
565 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
566 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
567 net, ntohs( iface->i_caddr.sat_addr.s_net ));
570 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
571 } else if ( iface->i_flags & IFACE_PHASE2 ) {
572 iface->i_rt->rt_firstnet = htons( net );
575 if ( net < 0 || net >= 0xffff ) {
576 fprintf( stderr, "Bad network: %d\n", net );
580 iface->i_rt->rt_lastnet = htons( net );
581 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
582 ( ntohs( iface->i_rt->rt_firstnet ) >
583 ntohs( iface->i_caddr.sat_addr.s_net ) ||
584 ntohs( iface->i_rt->rt_lastnet ) <
585 ntohs( iface->i_caddr.sat_addr.s_net ))) {
586 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
587 ntohs( iface->i_rt->rt_firstnet ),
588 ntohs( iface->i_rt->rt_lastnet ),
589 ntohs( iface->i_caddr.sat_addr.s_net ));
592 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
593 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
596 fprintf( stderr, "Must specify phase before networks.\n" );
602 int addr( iface, av )
603 struct interface *iface;
606 if ( av[ 0 ] == NULL ) {
607 fprintf( stderr, "No address.\n" );
610 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
611 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
616 if ( ntohs( iface->i_rt->rt_firstnet ) >
617 ntohs( iface->i_caddr.sat_addr.s_net ) ||
618 ntohs( iface->i_rt->rt_lastnet ) <
619 ntohs( iface->i_caddr.sat_addr.s_net )) {
620 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
621 ntohs( iface->i_caddr.sat_addr.s_net ),
622 ntohs( iface->i_rt->rt_firstnet ),
623 ntohs( iface->i_rt->rt_lastnet ));
627 if (( iface->i_rt = newrt(iface)) == NULL ) {
631 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
632 iface->i_caddr.sat_addr.s_net;
638 int zone( iface, av )
639 struct interface *iface;
645 if (( zname = av[ 0 ] ) == NULL ) {
646 fprintf( stderr, "No zone.\n" );
651 * Only process "-zone" if this interface has "-seed". We keep our
652 * list of configured zones in the interface structure. Then we can
653 * check that the network has given us good zones.
655 if ( iface->i_flags & IFACE_SEED ) {
656 if ( iface->i_rt == NULL ) {
657 fprintf( stderr, "Must specify net-range before zones.\n" );
660 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
664 if ( iface->i_czt == NULL ) {
667 zt->zt_next = iface->i_czt->zt_next;
668 iface->i_czt->zt_next = zt;
675 * Get the configuration from the kernel. Only called if there's no
680 struct interface *iface, *niface;
682 char **start, **list;
685 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
690 start = list = getifacelist();
691 while (list && *list) {
692 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
695 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
698 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
701 if ((ifr.ifr_flags & IFF_UP) == 0)
704 /* for devices that don't support appletalk */
705 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
708 for ( iface = interfaces; iface; iface = iface->i_next ) {
709 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
713 if ( iface ) { /* Already have this interface name */
719 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
720 fprintf(stderr, "%s: multicast may not work correctly.\n",
722 #endif /* IFF_MULTICAST */
724 if (addmulti(ifr.ifr_name, NULL) < 0) {
725 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
729 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
730 perror( "newiface" );
732 freeifacelist(start);
736 * Could try to get the address from the kernel...
739 if ( interfaces == NULL ) {
742 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
744 iface->i_next = niface;
746 niface->i_next = NULL;
748 freeifacelist(start);
754 * Allocate a new interface structure. Centralized here so we can change
755 * the interface structure and have it updated nicely.
758 struct interface *newiface( name )
761 struct interface *niface;
763 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
767 strncpy( niface->i_name, name, sizeof(niface->i_name));
769 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
771 niface->i_addr.sat_family = AF_APPLETALK;
773 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
775 niface->i_caddr.sat_family = AF_APPLETALK;
782 struct interface *iface;
783 char device[ MAXPATHLEN + 1], *p;
786 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
787 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
791 strcpy( device, "/dev/" );
792 strcat( device, iface->i_name );
793 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
794 LOG(log_error, logtype_atalkd, "plumb: invalid device: %s", device );
800 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
801 LOG(log_error, logtype_atalkd, "%s: %m", device );
804 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
805 LOG(log_error, logtype_atalkd, "I_PUSH: %m" );
809 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
810 LOG(log_error, logtype_atalkd, "IF_UNITSEL: %m" );
815 /* configure multicast. */
816 if (addmulti(iface->i_name, NULL) < 0) {
817 perror(iface->i_name);
818 fprintf(stderr,"Can't configure multicast.\n");
823 LOG(log_info, logtype_atalkd, "plumbed %s%d", device, ppa );
828 #endif /* __svr4__ */