2 * $Id: config.c,v 1.4 2001-06-25 20:13:45 rufustfirefly 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>
19 #include <netatalk/at.h>
20 #include <netatalk/endian.h>
21 #include <atalk/paths.h>
22 #include <atalk/util.h>
29 #endif /* HAVE_FCNTL_H */
33 #include <sys/sockio.h>
34 #include <sys/stropts.h>
37 #include "interface.h"
38 #include "multicast.h"
43 #ifndef IFF_SLAVE /* a little backward compatibility */
45 #endif /* IFF_SLAVE */
47 int router(), dontroute(), seed(), phase(), net(), addr(), zone();
54 { "dontroute", dontroute },
68 static char *argv[ 128 ];
69 static char buf[ 1024 ];
76 memset(argv, 0, sizeof(argv));
81 * This parser should be made more powerful -- it should
82 * handle various escapes, e.g. \" and \031.
84 while ( *p != '\0' ) {
85 while ( *p != '\0' && *p != '"' && isspace( *p )) {
94 while ( *p != '\0' && *p != '"' ) {
99 while ( *p != '\0' && !isspace( *p )) {
106 if ( argv[ 0 ] == '\0' || *argv[ 0 ] == '#' ) {
116 char *path, *p, newpath[ MAXPATHLEN ], line[ 1024 ];
118 FILE *conf, *newconf;
119 struct interface *iface;
124 path = _PATH_ATALKDCONF;
129 /* check if old conf is writable */
130 if ( stat( path, &st ) == 0 ) {
131 if (( st.st_mode & S_IWUSR ) == 0 ) {
132 syslog( LOG_INFO, "%s not writable, won't rewrite", path );
138 if (( p = strrchr( path, '/' )) == NULL ) {
139 strcpy( newpath, _PATH_ATALKDTMP );
141 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
143 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
144 syslog( LOG_ERR, "%s: %m", newpath );
147 if (( newconf = fdopen( fd, "w" )) == NULL ) {
148 syslog( LOG_ERR, "fdreopen %s: %m", newpath );
152 if (( conf = fopen( path, "r" )) == NULL && cf ) {
153 syslog( LOG_ERR, "%s: %m", path );
157 iface = interfaces->i_next;
159 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
160 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
161 if ( fputs( line, newconf ) == EOF ) {
162 syslog( LOG_ERR, "fputs: %m" );
168 /* write real lines */
170 fprintf( newconf, "%s", iface->i_name );
171 if ( iface->i_flags & IFACE_RSEED ) {
172 fprintf( newconf, " -router" );
173 } else if ( iface->i_flags & IFACE_SEED ) {
174 fprintf( newconf, " -seed" );
176 if ( iface->i_flags & IFACE_DONTROUTE) {
177 fprintf( newconf, " -dontroute");
180 fprintf( newconf, " -phase %d",
181 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
182 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
183 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
184 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
186 fprintf( newconf, " -addr %u.%u",
187 ntohs( iface->i_addr.sat_addr.s_net ),
188 iface->i_addr.sat_addr.s_node );
189 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
190 fprintf( newconf, " -zone \"%.*s\"",
191 ((struct ziptab *)l->l_data)->zt_len,
192 ((struct ziptab *)l->l_data)->zt_name );
194 fprintf( newconf, "\n" );
196 iface = iface->i_next;
197 if ( conf == NULL && iface == NULL ) {
202 if ( conf != NULL ) {
207 if ( rename( newpath, path ) < 0 ) {
208 syslog( LOG_ERR, "rename %s to %s: %m", newpath, path );
215 * Read our config file. If it's not there, return -1. If it is there,
216 * but has syntax errors, exit. Format of the file is as follows:
218 * interface [ -seed ] [ -phase number ] [ -net net-range ]
219 * [ -addr net.node ] [ -zone zonename ]...
221 * le0 -phase 1 -net 7938 -zone Argus
223 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
224 * le0 -phase 1 -net 7938 -zone Argus
226 * Pretty much everything is optional. Anything that is unspecified is
227 * searched for on the network. If -seed is not specified, the
228 * configuration is assumed to be soft, i.e. it can be overridden by
229 * another router. If -seed is specified, atalkd will exit if another
230 * router disagrees. If the phase is unspecified, it defaults to phase
231 * 2 (the default can be overridden on the command line). -addr can
232 * replace -net, if the network in question isn't a range. The default
233 * zone for an interface is the first zone encountered for that
240 struct interface *iface, *niface;
241 char line[ 1024 ], **argv, *p;
246 p = _PATH_ATALKDCONF;
250 if (( conf = fopen( p, "r" )) == NULL ) {
255 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
260 #endif /* __svr4__ */
262 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
263 if (( argv = parseline( line )) == NULL ) {
269 * Check that av[ 0 ] is a valid interface.
270 * Not possible under sysV.
272 strcpy( ifr.ifr_name, argv[ 0 ] );
274 /* for devices that don't support appletalk */
275 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
280 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
285 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
286 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
291 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
292 fprintf(stderr, "%s: multicast may not work properly.\n",
294 #endif /* IFF_MULTICAST */
296 /* configure hw multicast for this interface. */
297 if (addmulti(ifr.ifr_name, NULL) < 0) {
298 perror(ifr.ifr_name);
299 fprintf(stderr, "Can't configure multicast.\n");
302 #endif /* __svr4__ */
304 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
305 perror( "newiface" );
309 for ( i = 1; argv[ i ]; i += cc ) {
310 if ( argv[ i ][ 0 ] == '-' ) {
313 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
314 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
315 if ( params[ j ].p_func != NULL ) {
316 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
323 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
324 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
329 for ( iface = interfaces; iface; iface = iface->i_next ) {
330 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
331 ((( niface->i_flags & iface->i_flags &
332 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
333 niface->i_flags == 0 || iface->i_flags == 0 )) {
337 if ( iface ) { /* Already have this interface and phase */
338 fprintf( stderr, "%s already configured!\n", niface->i_name );
342 if ( interfaces == NULL ) {
345 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
347 iface->i_next = niface;
349 niface->i_next = NULL;
354 #endif /* __svr4__ */
359 * Note: we've added lo0 to the interface list previously, so we must
360 * have configured more than one interface...
362 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
364 if ( cc >= IFBASE ) {
373 #endif /* __svr4__ */
379 int router( iface, av )
380 struct interface *iface;
383 /* make sure "-router" and "-dontroute" aren't both on the same line. */
384 if (iface->i_flags & IFACE_DONTROUTE) {
385 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
390 * Check to be sure "-router" is before "-zone".
392 if ( iface->i_czt ) {
393 fprintf( stderr, "Must specify -router before -zone.\n");
397 /* -router also implies -seed */
398 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
403 int dontroute( iface, av )
404 struct interface *iface;
407 /* make sure "-router" and "-dontroute" aren't both on the same line. */
408 if (iface->i_flags & IFACE_RSEED) {
409 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
413 iface->i_flags |= IFACE_DONTROUTE;
418 int seed( iface, av )
419 struct interface *iface;
423 * Check to be sure "-seed" is before "-zone". we keep the old
424 * semantics of just ignoring this in a routerless world.
426 if ( iface->i_czt ) {
427 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
428 iface->i_czt->zt_name);
432 iface->i_flags |= IFACE_SEED;
436 int phase( iface, av )
437 struct interface *iface;
443 if (( pnum = av[ 0 ] ) == NULL ) {
444 fprintf( stderr, "No phase.\n" );
448 switch ( n = atoi( pnum )) {
450 iface->i_flags |= IFACE_PHASE1;
454 iface->i_flags |= IFACE_PHASE2;
458 fprintf( stderr, "No phase %d.\n", n );
465 struct interface *iface;
472 if (( nrange = av[ 0 ] ) == NULL ) {
473 fprintf( stderr, "No network.\n" );
477 if (( stop = strchr( nrange, '-' )) != 0 ) {
480 net = atoi( nrange );
481 if ( net < 0 || net >= 0xffff ) {
482 fprintf( stderr, "Bad network: %d\n", net );
486 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
491 if ( iface->i_flags & IFACE_PHASE1 ) {
493 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
496 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
497 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
498 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
499 net, ntohs( iface->i_caddr.sat_addr.s_net ));
502 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
503 } else if ( iface->i_flags & IFACE_PHASE2 ) {
504 iface->i_rt->rt_firstnet = htons( net );
507 if ( net < 0 || net >= 0xffff ) {
508 fprintf( stderr, "Bad network: %d\n", net );
512 iface->i_rt->rt_lastnet = htons( net );
513 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
514 ( ntohs( iface->i_rt->rt_firstnet ) >
515 ntohs( iface->i_caddr.sat_addr.s_net ) ||
516 ntohs( iface->i_rt->rt_lastnet ) <
517 ntohs( iface->i_caddr.sat_addr.s_net ))) {
518 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
519 ntohs( iface->i_rt->rt_firstnet ),
520 ntohs( iface->i_rt->rt_lastnet ),
521 ntohs( iface->i_caddr.sat_addr.s_net ));
524 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
525 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
528 fprintf( stderr, "Must specify phase before networks.\n" );
534 int addr( iface, av )
535 struct interface *iface;
538 if ( av[ 0 ] == NULL ) {
539 fprintf( stderr, "No address.\n" );
542 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
543 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
548 if ( ntohs( iface->i_rt->rt_firstnet ) >
549 ntohs( iface->i_caddr.sat_addr.s_net ) ||
550 ntohs( iface->i_rt->rt_lastnet ) <
551 ntohs( iface->i_caddr.sat_addr.s_net )) {
552 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
553 ntohs( iface->i_caddr.sat_addr.s_net ),
554 ntohs( iface->i_rt->rt_firstnet ),
555 ntohs( iface->i_rt->rt_lastnet ));
559 if (( iface->i_rt = newrt(iface)) == NULL ) {
563 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
564 iface->i_caddr.sat_addr.s_net;
570 int zone( iface, av )
571 struct interface *iface;
577 if (( zname = av[ 0 ] ) == NULL ) {
578 fprintf( stderr, "No zone.\n" );
583 * Only process "-zone" if this interface has "-seed". We keep our
584 * list of configured zones in the interface structure. Then we can
585 * check that the network has given us good zones.
587 if ( iface->i_flags & IFACE_SEED ) {
588 if ( iface->i_rt == NULL ) {
589 fprintf( stderr, "Must specify net-range before zones.\n" );
592 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
596 if ( iface->i_czt == NULL ) {
599 zt->zt_next = iface->i_czt->zt_next;
600 iface->i_czt->zt_next = zt;
607 * Get the configuration from the kernel. Only called if there's no
612 struct interface *iface, *niface;
614 char **start, **list;
617 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
622 start = list = getifacelist();
623 while (list && *list) {
624 strncpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
627 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
630 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
633 if ((ifr.ifr_flags & IFF_UP) == 0)
636 /* for devices that don't support appletalk */
637 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
640 for ( iface = interfaces; iface; iface = iface->i_next ) {
641 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
645 if ( iface ) { /* Already have this interface name */
651 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
652 fprintf(stderr, "%s: multicast may not work correctly.\n",
654 #endif /* IFF_MULTICAST */
656 if (addmulti(ifr.ifr_name, NULL) < 0) {
657 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
661 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
662 perror( "newiface" );
664 freeifacelist(start);
668 * Could try to get the address from the kernel...
671 if ( interfaces == NULL ) {
674 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
676 iface->i_next = niface;
678 niface->i_next = NULL;
680 freeifacelist(start);
686 * Allocate a new interface structure. Centralized here so we can change
687 * the interface structure and have it updated nicely.
690 struct interface *newiface( name )
693 struct interface *niface;
695 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
699 strncpy( niface->i_name, name, sizeof(niface->i_name));
701 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
703 niface->i_addr.sat_family = AF_APPLETALK;
705 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
707 niface->i_caddr.sat_family = AF_APPLETALK;
714 struct interface *iface;
715 char device[ MAXPATHLEN + 1], *p;
718 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
719 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
723 strcpy( device, "/dev/" );
724 strcat( device, iface->i_name );
725 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
726 syslog( LOG_ERR, "plumb: invalid device: %s", device );
732 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
733 syslog( LOG_ERR, "%s: %m", device );
736 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
737 syslog( LOG_ERR, "I_PUSH: %m" );
741 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
742 syslog( LOG_ERR, "IF_UNITSEL: %m" );
747 /* configure multicast. */
748 if (addmulti(iface->i_name, NULL) < 0) {
749 perror(iface->i_name);
750 fprintf(stderr,"Can't configure multicast.\n");
755 syslog( LOG_INFO, "plumbed %s%d", device, ppa );
760 #endif /* __svr4__ */