2 * $Id: config.c,v 1.5 2001-08-15 01:39:39 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>
33 #endif /* HAVE_FCNTL_H */
37 #include <sys/sockio.h>
38 #include <sys/stropts.h>
41 #include "interface.h"
42 #include "multicast.h"
47 #ifndef IFF_SLAVE /* a little backward compatibility */
49 #endif /* IFF_SLAVE */
51 int router(), dontroute(), seed(), phase(), net(), addr(), zone();
58 { "dontroute", dontroute },
72 static char *argv[ 128 ];
73 static char buf[ 1024 ];
80 memset(argv, 0, sizeof(argv));
85 * This parser should be made more powerful -- it should
86 * handle various escapes, e.g. \" and \031.
88 while ( *p != '\0' ) {
89 while ( *p != '\0' && *p != '"' && isspace( *p )) {
98 while ( *p != '\0' && *p != '"' ) {
103 while ( *p != '\0' && !isspace( *p )) {
110 if ( argv[ 0 ] == '\0' || *argv[ 0 ] == '#' ) {
120 char *path, *p, newpath[ MAXPATHLEN ], line[ 1024 ];
122 FILE *conf, *newconf;
123 struct interface *iface;
128 path = _PATH_ATALKDCONF;
133 /* check if old conf is writable */
134 if ( stat( path, &st ) == 0 ) {
135 if (( st.st_mode & S_IWUSR ) == 0 ) {
136 syslog( LOG_INFO, "%s not writable, won't rewrite", path );
142 if (( p = strrchr( path, '/' )) == NULL ) {
143 strcpy( newpath, _PATH_ATALKDTMP );
145 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
147 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
148 syslog( LOG_ERR, "%s: %m", newpath );
151 if (( newconf = fdopen( fd, "w" )) == NULL ) {
152 syslog( LOG_ERR, "fdreopen %s: %m", newpath );
156 if (( conf = fopen( path, "r" )) == NULL && cf ) {
157 syslog( LOG_ERR, "%s: %m", path );
161 iface = interfaces->i_next;
163 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
164 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
165 if ( fputs( line, newconf ) == EOF ) {
166 syslog( LOG_ERR, "fputs: %m" );
172 /* write real lines */
174 fprintf( newconf, "%s", iface->i_name );
175 if ( iface->i_flags & IFACE_RSEED ) {
176 fprintf( newconf, " -router" );
177 } else if ( iface->i_flags & IFACE_SEED ) {
178 fprintf( newconf, " -seed" );
180 if ( iface->i_flags & IFACE_DONTROUTE) {
181 fprintf( newconf, " -dontroute");
184 fprintf( newconf, " -phase %d",
185 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
186 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
187 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
188 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
190 fprintf( newconf, " -addr %u.%u",
191 ntohs( iface->i_addr.sat_addr.s_net ),
192 iface->i_addr.sat_addr.s_node );
193 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
194 fprintf( newconf, " -zone \"%.*s\"",
195 ((struct ziptab *)l->l_data)->zt_len,
196 ((struct ziptab *)l->l_data)->zt_name );
198 fprintf( newconf, "\n" );
200 iface = iface->i_next;
201 if ( conf == NULL && iface == NULL ) {
206 if ( conf != NULL ) {
211 if ( rename( newpath, path ) < 0 ) {
212 syslog( LOG_ERR, "rename %s to %s: %m", newpath, path );
219 * Read our config file. If it's not there, return -1. If it is there,
220 * but has syntax errors, exit. Format of the file is as follows:
222 * interface [ -seed ] [ -phase number ] [ -net net-range ]
223 * [ -addr net.node ] [ -zone zonename ]...
225 * le0 -phase 1 -net 7938 -zone Argus
227 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
228 * le0 -phase 1 -net 7938 -zone Argus
230 * Pretty much everything is optional. Anything that is unspecified is
231 * searched for on the network. If -seed is not specified, the
232 * configuration is assumed to be soft, i.e. it can be overridden by
233 * another router. If -seed is specified, atalkd will exit if another
234 * router disagrees. If the phase is unspecified, it defaults to phase
235 * 2 (the default can be overridden on the command line). -addr can
236 * replace -net, if the network in question isn't a range. The default
237 * zone for an interface is the first zone encountered for that
244 struct interface *iface, *niface;
245 char line[ 1024 ], **argv, *p;
250 p = _PATH_ATALKDCONF;
254 if (( conf = fopen( p, "r" )) == NULL ) {
259 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
264 #endif /* __svr4__ */
266 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
267 if (( argv = parseline( line )) == NULL ) {
273 * Check that av[ 0 ] is a valid interface.
274 * Not possible under sysV.
276 strcpy( ifr.ifr_name, argv[ 0 ] );
278 /* for devices that don't support appletalk */
279 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
284 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
289 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
290 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
295 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
296 fprintf(stderr, "%s: multicast may not work properly.\n",
298 #endif /* IFF_MULTICAST */
300 /* configure hw multicast for this interface. */
301 if (addmulti(ifr.ifr_name, NULL) < 0) {
302 perror(ifr.ifr_name);
303 fprintf(stderr, "Can't configure multicast.\n");
306 #endif /* __svr4__ */
308 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
309 perror( "newiface" );
313 for ( i = 1; argv[ i ]; i += cc ) {
314 if ( argv[ i ][ 0 ] == '-' ) {
317 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
318 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
319 if ( params[ j ].p_func != NULL ) {
320 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
327 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
328 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
333 for ( iface = interfaces; iface; iface = iface->i_next ) {
334 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
335 ((( niface->i_flags & iface->i_flags &
336 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
337 niface->i_flags == 0 || iface->i_flags == 0 )) {
341 if ( iface ) { /* Already have this interface and phase */
342 fprintf( stderr, "%s already configured!\n", niface->i_name );
346 if ( interfaces == NULL ) {
349 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
351 iface->i_next = niface;
353 niface->i_next = NULL;
358 #endif /* __svr4__ */
363 * Note: we've added lo0 to the interface list previously, so we must
364 * have configured more than one interface...
366 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
368 if ( cc >= IFBASE ) {
377 #endif /* __svr4__ */
383 int router( iface, av )
384 struct interface *iface;
387 /* make sure "-router" and "-dontroute" aren't both on the same line. */
388 if (iface->i_flags & IFACE_DONTROUTE) {
389 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
394 * Check to be sure "-router" is before "-zone".
396 if ( iface->i_czt ) {
397 fprintf( stderr, "Must specify -router before -zone.\n");
401 /* -router also implies -seed */
402 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
407 int dontroute( iface, av )
408 struct interface *iface;
411 /* make sure "-router" and "-dontroute" aren't both on the same line. */
412 if (iface->i_flags & IFACE_RSEED) {
413 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
417 iface->i_flags |= IFACE_DONTROUTE;
422 int seed( iface, av )
423 struct interface *iface;
427 * Check to be sure "-seed" is before "-zone". we keep the old
428 * semantics of just ignoring this in a routerless world.
430 if ( iface->i_czt ) {
431 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
432 iface->i_czt->zt_name);
436 iface->i_flags |= IFACE_SEED;
440 int phase( iface, av )
441 struct interface *iface;
447 if (( pnum = av[ 0 ] ) == NULL ) {
448 fprintf( stderr, "No phase.\n" );
452 switch ( n = atoi( pnum )) {
454 iface->i_flags |= IFACE_PHASE1;
458 iface->i_flags |= IFACE_PHASE2;
462 fprintf( stderr, "No phase %d.\n", n );
469 struct interface *iface;
476 if (( nrange = av[ 0 ] ) == NULL ) {
477 fprintf( stderr, "No network.\n" );
481 if (( stop = strchr( nrange, '-' )) != 0 ) {
484 net = atoi( nrange );
485 if ( net < 0 || net >= 0xffff ) {
486 fprintf( stderr, "Bad network: %d\n", net );
490 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
495 if ( iface->i_flags & IFACE_PHASE1 ) {
497 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
500 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
501 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
502 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
503 net, ntohs( iface->i_caddr.sat_addr.s_net ));
506 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
507 } else if ( iface->i_flags & IFACE_PHASE2 ) {
508 iface->i_rt->rt_firstnet = htons( net );
511 if ( net < 0 || net >= 0xffff ) {
512 fprintf( stderr, "Bad network: %d\n", net );
516 iface->i_rt->rt_lastnet = htons( net );
517 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
518 ( ntohs( iface->i_rt->rt_firstnet ) >
519 ntohs( iface->i_caddr.sat_addr.s_net ) ||
520 ntohs( iface->i_rt->rt_lastnet ) <
521 ntohs( iface->i_caddr.sat_addr.s_net ))) {
522 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
523 ntohs( iface->i_rt->rt_firstnet ),
524 ntohs( iface->i_rt->rt_lastnet ),
525 ntohs( iface->i_caddr.sat_addr.s_net ));
528 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
529 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
532 fprintf( stderr, "Must specify phase before networks.\n" );
538 int addr( iface, av )
539 struct interface *iface;
542 if ( av[ 0 ] == NULL ) {
543 fprintf( stderr, "No address.\n" );
546 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
547 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
552 if ( ntohs( iface->i_rt->rt_firstnet ) >
553 ntohs( iface->i_caddr.sat_addr.s_net ) ||
554 ntohs( iface->i_rt->rt_lastnet ) <
555 ntohs( iface->i_caddr.sat_addr.s_net )) {
556 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
557 ntohs( iface->i_caddr.sat_addr.s_net ),
558 ntohs( iface->i_rt->rt_firstnet ),
559 ntohs( iface->i_rt->rt_lastnet ));
563 if (( iface->i_rt = newrt(iface)) == NULL ) {
567 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
568 iface->i_caddr.sat_addr.s_net;
574 int zone( iface, av )
575 struct interface *iface;
581 if (( zname = av[ 0 ] ) == NULL ) {
582 fprintf( stderr, "No zone.\n" );
587 * Only process "-zone" if this interface has "-seed". We keep our
588 * list of configured zones in the interface structure. Then we can
589 * check that the network has given us good zones.
591 if ( iface->i_flags & IFACE_SEED ) {
592 if ( iface->i_rt == NULL ) {
593 fprintf( stderr, "Must specify net-range before zones.\n" );
596 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
600 if ( iface->i_czt == NULL ) {
603 zt->zt_next = iface->i_czt->zt_next;
604 iface->i_czt->zt_next = zt;
611 * Get the configuration from the kernel. Only called if there's no
616 struct interface *iface, *niface;
618 char **start, **list;
621 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
626 start = list = getifacelist();
627 while (list && *list) {
628 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
631 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
634 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
637 if ((ifr.ifr_flags & IFF_UP) == 0)
640 /* for devices that don't support appletalk */
641 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
644 for ( iface = interfaces; iface; iface = iface->i_next ) {
645 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
649 if ( iface ) { /* Already have this interface name */
655 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
656 fprintf(stderr, "%s: multicast may not work correctly.\n",
658 #endif /* IFF_MULTICAST */
660 if (addmulti(ifr.ifr_name, NULL) < 0) {
661 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
665 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
666 perror( "newiface" );
668 freeifacelist(start);
672 * Could try to get the address from the kernel...
675 if ( interfaces == NULL ) {
678 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
680 iface->i_next = niface;
682 niface->i_next = NULL;
684 freeifacelist(start);
690 * Allocate a new interface structure. Centralized here so we can change
691 * the interface structure and have it updated nicely.
694 struct interface *newiface( name )
697 struct interface *niface;
699 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
703 strncpy( niface->i_name, name, sizeof(niface->i_name));
705 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
707 niface->i_addr.sat_family = AF_APPLETALK;
709 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
711 niface->i_caddr.sat_family = AF_APPLETALK;
718 struct interface *iface;
719 char device[ MAXPATHLEN + 1], *p;
722 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
723 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
727 strcpy( device, "/dev/" );
728 strcat( device, iface->i_name );
729 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
730 syslog( LOG_ERR, "plumb: invalid device: %s", device );
736 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
737 syslog( LOG_ERR, "%s: %m", device );
740 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
741 syslog( LOG_ERR, "I_PUSH: %m" );
745 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
746 syslog( LOG_ERR, "IF_UNITSEL: %m" );
751 /* configure multicast. */
752 if (addmulti(iface->i_name, NULL) < 0) {
753 perror(iface->i_name);
754 fprintf(stderr,"Can't configure multicast.\n");
759 syslog( LOG_INFO, "plumbed %s%d", device, ppa );
764 #endif /* __svr4__ */