2 * $Id: config.c,v 1.14 2005-04-28 20:49:46 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 <atalk/unicode.h>
59 #include "interface.h"
60 #include "multicast.h"
66 #ifndef IFF_SLAVE /* a little backward compatibility */
68 #endif /* IFF_SLAVE */
70 int router(), dontroute(), seed(), phase(), net(), addr(), zone(), noallmulti();
72 static const struct param {
77 { "dontroute", dontroute },
83 { "noallmulti", noallmulti }
86 #define ARGV_CHUNK_SIZE 128
87 #define MAXLINELEN 2048
88 char **parseline(const char *line)
92 char *buffer, *tmpbuf;
95 /* Ignore empty lines and lines with leading hash marks. */
97 while ( isspace( *p ) ) {
100 if ( *p == '#' || *p == '\0' ) {
104 buffer = (char *) malloc( strlen( p ) + 1 );
106 /* FIXME: error handling */
112 argv = (char **) malloc( ARGV_CHUNK_SIZE * sizeof( char * ) );
114 /* FIXME: error handling */
120 * This parser should be made more powerful -- it should
121 * handle various escapes, e.g. \" and \031.
124 if ( *tmpbuf == '"' ) {
125 argv[ argc++ ] = ++tmpbuf;
126 while ( *tmpbuf != '\0' && *tmpbuf != '"' ) {
129 if ( *tmpbuf == '"' ) {
130 /* FIXME: error handling */
133 argv[ argc++ ] = tmpbuf;
134 while ( *tmpbuf != '\0' && !isspace( *tmpbuf )) {
140 /* Make room for a NULL pointer and our special pointer (s.b.) */
141 if ( (argc + 1) % ARGV_CHUNK_SIZE == 0 ) {
143 tmp = (char **) realloc( argv, argc + 1 + ARGV_CHUNK_SIZE * sizeof( char * ) );
145 /* FIXME: error handling */
153 /* Skip white spaces. */
154 while ( isspace( *tmpbuf ) ) {
157 } while ( *tmpbuf != '\0' );
159 argv[ argc++ ] = NULL;
160 /* We store our buffer pointer in argv, too, so we can free it later.
161 * (But don't tell anyone.)
163 argv[ argc ] = buffer;
168 void freeline( char **argv )
185 char *path, *p, newpath[ MAXPATHLEN ], line[ MAXLINELEN ];
187 FILE *conf, *newconf;
188 struct interface *iface;
195 path = _PATH_ATALKDCONF;
200 /* check if old conf is writable */
201 if ( stat( path, &st ) == 0 ) {
202 if (( st.st_mode & S_IWUSR ) == 0 ) {
203 LOG(log_info, logtype_atalkd, "%s not writable, won't rewrite", path );
209 if (( p = strrchr( path, '/' )) == NULL ) {
210 strcpy( newpath, _PATH_ATALKDTMP );
212 sprintf( newpath, "%.*s/%s", (int)(p - path), path, _PATH_ATALKDTMP );
214 if (( fd = open( newpath, O_WRONLY|O_CREAT|O_TRUNC, mode )) < 0 ) {
215 LOG(log_error, logtype_atalkd, "%s: %s", newpath, strerror(errno) );
218 if (( newconf = fdopen( fd, "w" )) == NULL ) {
219 LOG(log_error, logtype_atalkd, "fdreopen %s: %s", newpath, strerror(errno) );
223 if (( conf = fopen( path, "r" )) == NULL && cf ) {
224 LOG(log_error, logtype_atalkd, "%s: %s", path, strerror(errno) );
228 iface = interfaces->i_next;
230 while ( conf == NULL || fgets( line, sizeof( line ), conf ) != NULL ) {
231 if ( conf != NULL && ( argv = parseline( line )) == NULL ) {
232 if ( fputs( line, newconf ) == EOF ) {
233 LOG(log_error, logtype_atalkd, "fputs: %s", strerror(errno) );
240 /* write real lines */
242 fprintf( newconf, "%s", iface->i_name );
243 if ( iface->i_flags & IFACE_RSEED ) {
244 fprintf( newconf, " -router" );
245 } else if ( iface->i_flags & IFACE_SEED ) {
246 fprintf( newconf, " -seed" );
248 if ( iface->i_flags & IFACE_DONTROUTE) {
249 fprintf( newconf, " -dontroute");
252 if ( !(iface->i_flags & IFACE_ALLMULTI)) {
253 fprintf( newconf, " -noallmulti");
257 fprintf( newconf, " -phase %d",
258 ( iface->i_flags & IFACE_PHASE1 ) ? 1 : 2 );
259 fprintf( newconf, " -net %d", ntohs( iface->i_rt->rt_firstnet ));
260 if ( iface->i_rt->rt_lastnet != iface->i_rt->rt_firstnet ) {
261 fprintf( newconf, "-%d", ntohs( iface->i_rt->rt_lastnet ));
263 fprintf( newconf, " -addr %u.%u",
264 ntohs( iface->i_addr.sat_addr.s_net ),
265 iface->i_addr.sat_addr.s_node );
266 for ( l = iface->i_rt->rt_zt; l; l = l->l_next ) {
267 /* codepage conversion */
268 if ((size_t)(-1) == (len = convert_string_allocate(CH_MAC, CH_UNIX,
269 ((struct ziptab *)l->l_data)->zt_name,
270 ((struct ziptab *)l->l_data)->zt_len,
273 (zonename = strdup(((struct ziptab *)l->l_data)->zt_name))) {
274 LOG(log_error, logtype_atalkd, "malloc: %m" );
277 len = ((struct ziptab *)l->l_data)->zt_len;
279 fprintf( newconf, " -zone \"%.*s\"", (int)len, zonename);
282 fprintf( newconf, "\n" );
284 iface = iface->i_next;
285 if ( conf == NULL && iface == NULL ) {
290 if ( conf != NULL ) {
295 if ( rename( newpath, path ) < 0 ) {
296 LOG(log_error, logtype_atalkd, "rename %s to %s: %s", newpath, path, strerror(errno) );
303 * Read our config file. If it's not there, return -1. If it is there,
304 * but has syntax errors, exit. Format of the file is as follows:
306 * interface [ -seed ] [ -phase number ] [ -net net-range ]
307 * [ -addr net.node ] [ -zone zonename ]...
309 * le0 -phase 1 -net 7938 -zone Argus
311 * le0 -phase 2 -net 8043-8044 -zone Argus -zone "Research Systems"
312 * le0 -phase 1 -net 7938 -zone Argus
314 * Pretty much everything is optional. Anything that is unspecified is
315 * searched for on the network. If -seed is not specified, the
316 * configuration is assumed to be soft, i.e. it can be overridden by
317 * another router. If -seed is specified, atalkd will exit if another
318 * router disagrees. If the phase is unspecified, it defaults to phase
319 * 2 (the default can be overridden on the command line). -addr can
320 * replace -net, if the network in question isn't a range. The default
321 * zone for an interface is the first zone encountered for that
328 struct interface *iface, *niface;
329 char line[ MAXLINELEN ], **argv, *p;
335 p = _PATH_ATALKDCONF;
339 if (( conf = fopen( p, "r" )) == NULL ) {
344 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
349 #endif /* __svr4__ */
351 while ( fgets( line, sizeof( line ), conf ) != NULL ) {
352 if (( argv = parseline( line )) == NULL ) {
358 * Check that av[ 0 ] is a valid interface.
359 * Not possible under sysV.
361 strlcpy( ifr.ifr_name, argv[ 0 ], sizeof(ifr.ifr_name) );
363 /* for devices that don't support appletalk */
364 if ((ioctl(s, SIOCGIFADDR, &ifr) < 0) && (errno == ENODEV)) {
369 if ( ioctl( s, SIOCGIFFLAGS, &ifr ) < 0 ) {
374 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT |IFF_SLAVE)) {
375 fprintf( stderr, "%s: can't configure.\n", ifr.ifr_name );
380 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
381 fprintf(stderr, "%s: multicast may not work properly.\n",
383 #endif /* IFF_MULTICAST */
385 /* configure hw multicast for this interface. */
386 if (addmulti(ifr.ifr_name, NULL) < 0) {
387 perror(ifr.ifr_name);
388 fprintf(stderr, "Can't configure multicast.\n");
392 #endif /* __svr4__ */
394 if (( niface = newiface( argv[ 0 ] )) == NULL ) {
395 perror( "newiface" );
399 for ( i = 1; argv[ i ]; i += cc ) {
400 if ( argv[ i ][ 0 ] == '-' ) {
403 for ( j = 0; j < sizeof( params ) / sizeof( params[ 0 ] ); j++ ) {
404 if ( strcmp( argv[ i ], params[ j ].p_name ) == 0 ) {
405 if ( params[ j ].p_func != NULL ) {
406 cc = (*params[ j ].p_func)( niface, &argv[ i + 1 ] );
413 if ( j >= sizeof( params ) / sizeof( params[ 0 ] )) {
414 fprintf( stderr, "%s: attribute not found.\n", argv[ i ] );
419 for ( iface = interfaces; iface; iface = iface->i_next ) {
420 if ( strcmp( niface->i_name, iface->i_name ) == 0 &&
421 ((( niface->i_flags & iface->i_flags &
422 ( IFACE_PHASE1|IFACE_PHASE2 )) != 0 ) ||
423 niface->i_flags == 0 || iface->i_flags == 0 )) {
427 if ( iface ) { /* Already have this interface and phase */
428 fprintf( stderr, "%s already configured!\n", niface->i_name );
433 /* Don't set interface to allmulti if it already is, or -noallmulti was given */
434 if ((ifr.ifr_flags & IFF_ALLMULTI))
435 niface->i_flags |= IFACE_WASALLMULTI;
437 if ((niface->i_flags & IFACE_ALLMULTI) && !(niface->i_flags & IFACE_WASALLMULTI))
438 ifsetallmulti(ifr.ifr_name, 1);
441 if ( interfaces == NULL ) {
444 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
446 iface->i_next = niface;
448 niface->i_next = NULL;
453 #endif /* __svr4__ */
458 * Note: we've added lo0 to the interface list previously, so we must
459 * have configured more than one interface...
461 for ( iface = interfaces, cc = 0; iface; iface = iface->i_next, cc++ )
463 if ( cc >= IFBASE ) {
472 #endif /* __svr4__ */
477 int noallmulti( iface, av )
478 struct interface *iface;
481 /* Linux specific, no effect on other platforms */
482 iface->i_flags &= !IFACE_ALLMULTI;
488 int router( iface, av )
489 struct interface *iface;
492 /* make sure "-router" and "-dontroute" aren't both on the same line. */
493 if (iface->i_flags & IFACE_DONTROUTE) {
494 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
499 * Check to be sure "-router" is before "-zone".
501 if ( iface->i_czt ) {
502 fprintf( stderr, "Must specify -router before -zone.\n");
506 /* -router also implies -seed */
507 iface->i_flags |= IFACE_RSEED | IFACE_SEED | IFACE_ISROUTER;
512 int dontroute( iface, av )
513 struct interface *iface;
516 /* make sure "-router" and "-dontroute" aren't both on the same line. */
517 if (iface->i_flags & IFACE_RSEED) {
518 fprintf( stderr, "Can't specify both -router and -dontroute.\n");
522 iface->i_flags |= IFACE_DONTROUTE;
527 int seed( iface, av )
528 struct interface *iface;
532 * Check to be sure "-seed" is before "-zone". we keep the old
533 * semantics of just ignoring this in a routerless world.
535 if ( iface->i_czt ) {
536 fprintf( stderr, "Must specify -seed before -zone(%s).\n",
537 iface->i_czt->zt_name);
541 iface->i_flags |= IFACE_SEED;
545 int phase( iface, av )
546 struct interface *iface;
552 if (( pnum = av[ 0 ] ) == NULL ) {
553 fprintf( stderr, "No phase.\n" );
557 switch ( n = atoi( pnum )) {
559 iface->i_flags |= IFACE_PHASE1;
563 iface->i_flags |= IFACE_PHASE2;
567 fprintf( stderr, "No phase %d.\n", n );
574 struct interface *iface;
581 if (( nrange = av[ 0 ] ) == NULL ) {
582 fprintf( stderr, "No network.\n" );
586 if (( stop = strchr( nrange, '-' )) != 0 ) {
589 net = atoi( nrange );
590 if ( net < 0 || net >= 0xffff ) {
591 fprintf( stderr, "Bad network: %d\n", net );
595 if ( iface->i_rt == NULL && ( iface->i_rt = newrt(iface)) == NULL ) {
600 if ( iface->i_flags & IFACE_PHASE1 ) {
602 fprintf( stderr, "Phase 1 doesn't use an address range.\n" );
605 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
606 ntohs( iface->i_caddr.sat_addr.s_net ) != net ) {
607 fprintf( stderr, "Net-range (%u) doesn't match net %u.\n",
608 net, ntohs( iface->i_caddr.sat_addr.s_net ));
611 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = htons( net );
612 } else if ( iface->i_flags & IFACE_PHASE2 ) {
613 iface->i_rt->rt_firstnet = htons( net );
616 if ( net < 0 || net >= 0xffff ) {
617 fprintf( stderr, "Bad network: %d\n", net );
621 iface->i_rt->rt_lastnet = htons( net );
622 if ( iface->i_caddr.sat_addr.s_net != ATADDR_ANYNET &&
623 ( ntohs( iface->i_rt->rt_firstnet ) >
624 ntohs( iface->i_caddr.sat_addr.s_net ) ||
625 ntohs( iface->i_rt->rt_lastnet ) <
626 ntohs( iface->i_caddr.sat_addr.s_net ))) {
627 fprintf( stderr, "Net-range (%u-%u) doesn't contain net (%u).\n",
628 ntohs( iface->i_rt->rt_firstnet ),
629 ntohs( iface->i_rt->rt_lastnet ),
630 ntohs( iface->i_caddr.sat_addr.s_net ));
633 if ( iface->i_rt->rt_firstnet != iface->i_rt->rt_lastnet ) {
634 iface->i_rt->rt_flags |= RTMPTAB_EXTENDED;
637 fprintf( stderr, "Must specify phase before networks.\n" );
643 int addr( iface, av )
644 struct interface *iface;
647 if ( av[ 0 ] == NULL ) {
648 fprintf( stderr, "No address.\n" );
651 if ( atalk_aton( av[ 0 ], &iface->i_caddr.sat_addr ) == 0 ) {
652 fprintf( stderr, "Bad address, %s\n", av[ 0 ] );
657 if ( ntohs( iface->i_rt->rt_firstnet ) >
658 ntohs( iface->i_caddr.sat_addr.s_net ) ||
659 ntohs( iface->i_rt->rt_lastnet ) <
660 ntohs( iface->i_caddr.sat_addr.s_net )) {
661 fprintf( stderr, "Net (%u) not in net-range (%u-%u).\n",
662 ntohs( iface->i_caddr.sat_addr.s_net ),
663 ntohs( iface->i_rt->rt_firstnet ),
664 ntohs( iface->i_rt->rt_lastnet ));
668 if (( iface->i_rt = newrt(iface)) == NULL ) {
672 iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet =
673 iface->i_caddr.sat_addr.s_net;
679 int zone( iface, av )
680 struct interface *iface;
686 if ( av[ 0 ] == NULL ) {
687 fprintf( stderr, "No zone.\n" );
691 /* codepage conversion */
692 if ((size_t)(-1) == convert_string_allocate(CH_UNIX, CH_MAC, av[0], strlen(av[0]), &zname)) {
693 zname = strdup(av[0]);
697 * Only process "-zone" if this interface has "-seed". We keep our
698 * list of configured zones in the interface structure. Then we can
699 * check that the network has given us good zones.
701 if ( iface->i_flags & IFACE_SEED ) {
702 if ( iface->i_rt == NULL ) {
703 fprintf( stderr, "Must specify net-range before zones.\n" );
707 if (( zt = newzt( strlen( zname ), zname )) == NULL ) {
711 if ( iface->i_czt == NULL ) {
714 zt->zt_next = iface->i_czt->zt_next;
715 iface->i_czt->zt_next = zt;
724 * Get the configuration from the kernel. Only called if there's no
729 struct interface *iface, *niface;
731 char **start, **list;
734 if (( s = socket( AF_APPLETALK, SOCK_DGRAM, 0 )) < 0 ) {
739 start = list = getifacelist();
740 while (list && *list) {
741 strlcpy(ifr.ifr_name, *list, sizeof(ifr.ifr_name));
744 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
747 if (ifr.ifr_flags & (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_SLAVE))
750 if ((ifr.ifr_flags & IFF_UP) == 0)
753 /* for devices that don't support appletalk */
754 if (ioctl(s, SIOCGIFADDR, &ifr) < 0 && (errno == ENODEV))
757 for ( iface = interfaces; iface; iface = iface->i_next ) {
758 if ( strcmp( iface->i_name, ifr.ifr_name ) == 0 ) {
762 if ( iface ) { /* Already have this interface name */
768 if ((ifr.ifr_flags & IFF_MULTICAST) == 0)
769 fprintf(stderr, "%s: multicast may not work correctly.\n",
771 #endif /* IFF_MULTICAST */
773 if (addmulti(ifr.ifr_name, NULL) < 0) {
774 fprintf(stderr, "%s: disabled.\n", ifr.ifr_name);
778 if (( niface = newiface( ifr.ifr_name )) == NULL ) {
779 perror( "newiface" );
781 freeifacelist(start);
785 * Could try to get the address from the kernel...
788 if ( interfaces == NULL ) {
791 for ( iface = interfaces; iface->i_next; iface = iface->i_next )
793 iface->i_next = niface;
795 niface->i_next = NULL;
797 freeifacelist(start);
803 * Allocate a new interface structure. Centralized here so we can change
804 * the interface structure and have it updated nicely.
807 struct interface *newiface( name )
810 struct interface *niface;
812 if (( niface = (struct interface *)calloc(1, sizeof( struct interface )))
816 strlcpy( niface->i_name, name, sizeof(niface->i_name));
818 niface->i_addr.sat_len = sizeof( struct sockaddr_at );
820 niface->i_addr.sat_family = AF_APPLETALK;
822 niface->i_caddr.sat_len = sizeof( struct sockaddr_at );
824 niface->i_caddr.sat_family = AF_APPLETALK;
826 niface->i_flags = IFACE_ALLMULTI;
834 struct interface *iface;
835 char device[ MAXPATHLEN + 1], *p;
838 for ( iface = interfaces; iface != NULL; iface = iface->i_next ) {
839 if ( strcmp( iface->i_name, LOOPIFACE ) == 0 ) {
843 strcpy( device, "/dev/" );
844 strcat( device, iface->i_name );
845 if (( p = strpbrk( device, "0123456789" )) == NULL ) {
846 LOG(log_error, logtype_atalkd, "plumb: invalid device: %s", device );
852 if (( fd = open( device, O_RDWR, 0 )) < 0 ) {
853 LOG(log_error, logtype_atalkd, "%s: %m", device );
856 if ( ioctl( fd, I_PUSH, "ddp" ) < 0 ) {
857 LOG(log_error, logtype_atalkd, "I_PUSH: %m" );
861 if ( ioctl( fd, IF_UNITSEL, ppa ) < 0 ) {
862 LOG(log_error, logtype_atalkd, "IF_UNITSEL: %m" );
867 /* configure multicast. */
868 if (addmulti(iface->i_name, NULL) < 0) {
869 perror(iface->i_name);
870 fprintf(stderr,"Can't configure multicast.\n");
875 LOG(log_info, logtype_atalkd, "plumbed %s%d", device, ppa );
880 #endif /* __svr4__ */