2 * $Id: config.c,v 1.13.6.1 2004-01-10 08:09:12 bfernhomberg 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;
314 p = _PATH_ATALKDCONF;
318 if (( conf = fopen( p, "r" )) == NULL ) {
323 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
328 #endif /* __svr4__ */
330 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
331 if (( argv = parseline( line )) == NULL ) {
337 * Check that av[ 0 ] is a valid interface.
338 * Not possible under sysV.
340 strncpy( ifr.ifr_name, argv[ 0 ], sizeof(ifr.ifr_name) );
341 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
343 /* for devices that don't support appletalk */
344 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
349 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
354 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
355 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
360 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
361 fprintf(stderr, "%s: multicast may not work properly.\n",
363 #endif /* IFF_MULTICAST */
365 /* configure hw multicast for this interface. */
366 if (addmulti(ifr.ifr_name, NULL) < 0) {
367 perror(ifr.ifr_name);
368 fprintf(stderr, "Can't configure multicast.\n");
371 #endif /* __svr4__ */
373 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
374 perror( "newiface" );
378 for ( i = 1; argv[ i ]; i += cc ) {
379 if ( argv[ i ][ 0 ] == '-' ) {
382 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
383 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
384 if ( params[ j ].p_func != NULL ) {
385 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
392 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
393 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
398 for ( iface = interfaces; iface; iface = iface->i_next ) {
399 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
400 ((( niface->i_flags & iface->i_flags &
401 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
402 niface->i_flags == 0 || iface->i_flags == 0 )) {
406 if ( iface ) { /* Already have this interface and phase */
407 fprintf( stderr, "%s already configured!\n", niface->i_name );
411 if ( interfaces == NULL ) {
414 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
416 iface->i_next = niface;
418 niface->i_next = NULL;
423 #endif /* __svr4__ */
428 * Note: we've added lo0 to the interface list previously, so we must
429 * have configured more than one interface...
431 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
433 if ( cc >= IFBASE ) {
442 #endif /* __svr4__ */
448 int router( iface, av )
449 struct interface *iface;
452 /* make sure "-router" and "-dontroute" aren't both on the same line. */
453 if (iface->i_flags & IFACE_DONTROUTE) {
454 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
459 * Check to be sure "-router" is before "-zone".
461 if ( iface->i_czt ) {
462 fprintf( stderr, "Must specify -router before -zone.\n");
466 /* -router also implies -seed */
467 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
472 int dontroute( 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_RSEED) {
478 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
482 iface->i_flags |= IFACE_DONTROUTE;
487 int seed( iface, av )
488 struct interface *iface;
492 * Check to be sure "-seed" is before "-zone". we keep the old
493 * semantics of just ignoring this in a routerless world.
495 if ( iface->i_czt ) {
496 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
497 iface->i_czt->zt_name);
501 iface->i_flags |= IFACE_SEED;
505 int phase( iface, av )
506 struct interface *iface;
512 if (( pnum = av[ 0 ] ) == NULL ) {
513 fprintf( stderr, "No phase.\n" );
517 switch ( n = atoi( pnum )) {
519 iface->i_flags |= IFACE_PHASE1;
523 iface->i_flags |= IFACE_PHASE2;
527 fprintf( stderr, "No phase %d.\n", n );
534 struct interface *iface;
541 if (( nrange = av[ 0 ] ) == NULL ) {
542 fprintf( stderr, "No network.\n" );
546 if (( stop = strchr( nrange, '-' )) != 0 ) {
549 net = atoi( nrange );
550 if ( net < 0 || net >= 0xffff ) {
551 fprintf( stderr, "Bad network: %d\n", net );
555 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
560 if ( iface->i_flags & IFACE_PHASE1 ) {
562 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
565 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
566 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
567 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
568 net, ntohs( iface->i_caddr.sat_addr.s_net ));
571 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
572 } else if ( iface->i_flags & IFACE_PHASE2 ) {
573 iface->i_rt->rt_firstnet = htons( net );
576 if ( net < 0 || net >= 0xffff ) {
577 fprintf( stderr, "Bad network: %d\n", net );
581 iface->i_rt->rt_lastnet = htons( net );
582 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
583 ( ntohs( iface->i_rt->rt_firstnet ) >
584 ntohs( iface->i_caddr.sat_addr.s_net ) ||
585 ntohs( iface->i_rt->rt_lastnet ) <
586 ntohs( iface->i_caddr.sat_addr.s_net ))) {
587 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
588 ntohs( iface->i_rt->rt_firstnet ),
589 ntohs( iface->i_rt->rt_lastnet ),
590 ntohs( iface->i_caddr.sat_addr.s_net ));
593 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
594 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
597 fprintf( stderr, "Must specify phase before networks.\n" );
603 int addr( iface, av )
604 struct interface *iface;
607 if ( av[ 0 ] == NULL ) {
608 fprintf( stderr, "No address.\n" );
611 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
612 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
617 if ( ntohs( iface->i_rt->rt_firstnet ) >
618 ntohs( iface->i_caddr.sat_addr.s_net ) ||
619 ntohs( iface->i_rt->rt_lastnet ) <
620 ntohs( iface->i_caddr.sat_addr.s_net )) {
621 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
622 ntohs( iface->i_caddr.sat_addr.s_net ),
623 ntohs( iface->i_rt->rt_firstnet ),
624 ntohs( iface->i_rt->rt_lastnet ));
628 if (( iface->i_rt = newrt(iface)) == NULL ) {
632 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
633 iface->i_caddr.sat_addr.s_net;
639 int zone( iface, av )
640 struct interface *iface;
646 if (( zname = av[ 0 ] ) == NULL ) {
647 fprintf( stderr, "No zone.\n" );
652 * Only process "-zone" if this interface has "-seed". We keep our
653 * list of configured zones in the interface structure. Then we can
654 * check that the network has given us good zones.
656 if ( iface->i_flags & IFACE_SEED ) {
657 if ( iface->i_rt == NULL ) {
658 fprintf( stderr, "Must specify net-range before zones.\n" );
661 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
665 if ( iface->i_czt == NULL ) {
668 zt->zt_next = iface->i_czt->zt_next;
669 iface->i_czt->zt_next = zt;
676 * Get the configuration from the kernel. Only called if there's no
681 struct interface *iface, *niface;
683 char **start, **list;
686 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
691 start = list = getifacelist();
692 while (list && *list) {
693 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
696 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
699 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
702 if ((ifr.ifr_flags & IFF_UP) == 0)
705 /* for devices that don't support appletalk */
706 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
709 for ( iface = interfaces; iface; iface = iface->i_next ) {
710 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
714 if ( iface ) { /* Already have this interface name */
720 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
721 fprintf(stderr, "%s: multicast may not work correctly.\n",
723 #endif /* IFF_MULTICAST */
725 if (addmulti(ifr.ifr_name, NULL) < 0) {
726 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
730 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
731 perror( "newiface" );
733 freeifacelist(start);
737 * Could try to get the address from the kernel...
740 if ( interfaces == NULL ) {
743 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
745 iface->i_next = niface;
747 niface->i_next = NULL;
749 freeifacelist(start);
755 * Allocate a new interface structure. Centralized here so we can change
756 * the interface structure and have it updated nicely.
759 struct interface *newiface( name )
762 struct interface *niface;
764 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
768 strncpy( niface->i_name, name, sizeof(niface->i_name));
770 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
772 niface->i_addr.sat_family = AF_APPLETALK;
774 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
776 niface->i_caddr.sat_family = AF_APPLETALK;
783 struct interface *iface;
784 char device[ MAXPATHLEN + 1], *p;
787 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
788 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
792 strcpy( device, "/dev/" );
793 strcat( device, iface->i_name );
794 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
795 LOG(log_error, logtype_atalkd, "plumb: invalid device: %s", device );
801 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
802 LOG(log_error, logtype_atalkd, "%s: %m", device );
805 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
806 LOG(log_error, logtype_atalkd, "I_PUSH: %m" );
810 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
811 LOG(log_error, logtype_atalkd, "IF_UNITSEL: %m" );
816 /* configure multicast. */
817 if (addmulti(iface->i_name, NULL) < 0) {
818 perror(iface->i_name);
819 fprintf(stderr,"Can't configure multicast.\n");
824 LOG(log_info, logtype_atalkd, "plumbed %s%d", device, ppa );
829 #endif /* __svr4__ */