]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
change SIGHUP and SIGUSR1, SIGHUP reload config files, Volumes file are reloaded...
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.30.2.1 2003-05-26 11:17:25 didg Exp $
3  *
4  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5  * Copyright (c) 1990,1993 Regents of The University of Michigan.
6  * All Rights Reserved.  See COPYRIGHT.
7  *
8  * modified from main.c. this handles afp options.
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 /* STDC check */
19 #if STDC_HEADERS
20 #include <string.h>
21 #else /* STDC_HEADERS */
22 #ifndef HAVE_STRCHR
23 #define strchr index
24 #define strrchr index
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
27 #ifndef HAVE_MEMCPY
28 #define memcpy(d,s,n) bcopy ((s), (d), (n))
29 #define memmove(d,s,n) bcopy ((s), (d), (n))
30 #endif /* ! HAVE_MEMCPY */
31 #endif /* STDC_HEADERS */
32
33 #include <ctype.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <atalk/logger.h>
40
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #ifdef HAVE_NETDB_H
44 #include <netdb.h>
45 #endif /* HAVE_NETDB_H */
46
47 #include <atalk/paths.h>
48 #include <atalk/util.h>
49 #include "globals.h"
50 #include "status.h"
51 #include "auth.h"
52
53 #include <atalk/compat.h>
54
55 #ifdef ADMIN_GRP
56 #include <grp.h>
57 #include <sys/types.h>
58 #endif /* ADMIN_GRP */
59
60 #ifndef MIN
61 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
62 #endif /* MIN */
63
64 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
65 #define LENGTH 512
66
67 /* return an option. this uses an internal array, so it's necessary
68  * to duplicate it if you want to hold it for long. this is probably
69  * non-optimal. */
70 static char *getoption(char *buf, const char *option)
71 {
72     static char string[LENGTH + 1];
73     char *end;
74     int len;
75
76     if (option && (buf = strstr(buf, option)))
77         buf = strpbrk(buf, " \t");
78
79     while (buf && isspace(*buf))
80         buf++;
81
82     if (!buf)
83         return NULL;
84
85     /* search for any quoted stuff */
86     if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
87         buf++;
88         len = MIN(end - buf, LENGTH);
89     } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
90         len = MIN(end - buf, LENGTH);
91     else
92         len = MIN(strlen(buf), LENGTH);
93
94     strncpy(string, buf, len);
95     string[len] = '\0';
96     return string;
97 }
98
99 /* get rid of any allocated afp_option buffers. */
100 void afp_options_free(struct afp_options *opt,
101                       const struct afp_options *save)
102 {
103     if (opt->defaultvol.name && (opt->defaultvol.name != save->defaultvol.name))
104         free(opt->defaultvol.name);
105     if (opt->defaultvol.full_name && (opt->defaultvol.full_name != save->defaultvol.full_name))
106         free(opt->defaultvol.full_name);
107
108     if (opt->systemvol.name && (opt->systemvol.name != save->systemvol.name))
109         free(opt->systemvol.name);
110     if (opt->systemvol.full_name && (opt->systemvol.full_name != save->systemvol.full_name))
111         free(opt->systemvol.full_name);
112
113     if (opt->uservol.name && (opt->uservol.name != save->uservol.name))
114         free(opt->uservol.name);
115     if (opt->uservol.full_name && (opt->uservol.full_name != save->uservol.full_name))
116         free(opt->uservol.full_name);
117
118     if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
119         free(opt->loginmesg);
120     if (opt->guest && (opt->guest != save->guest))
121         free(opt->guest);
122     if (opt->server && (opt->server != save->server))
123         free(opt->server);
124     if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
125         free(opt->ipaddr);
126     if (opt->fqdn && (opt->fqdn != save->fqdn))
127         free(opt->fqdn);
128     if (opt->uampath && (opt->uampath != save->uampath))
129         free(opt->uampath);
130     if (opt->uamlist && (opt->uamlist != save->uamlist))
131         free(opt->uamlist);
132     if (opt->nlspath && (opt->nlspath != save->nlspath))
133         free(opt->nlspath);
134     if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
135         free(opt->passwdfile);
136     if (opt->signature && (opt->signature != save->signature))
137         free(opt->signature);
138     if (opt->k5service && (opt->k5service != save->k5service))
139         free(opt->k5service);
140     if (opt->k5realm && (opt->k5realm != save->k5realm))
141         free(opt->k5realm);
142 }
143
144 /* initialize options */
145 void afp_options_init(struct afp_options *options)
146 {
147     memset(options, 0, sizeof(struct afp_options));
148     options->connections = 20;
149     options->pidfile = _PATH_AFPDLOCK;
150     options->defaultvol.name = _PATH_AFPDDEFVOL;
151     options->systemvol.name = _PATH_AFPDSYSVOL;
152     options->configfile = _PATH_AFPDCONF;
153     options->nlspath = _PATH_AFPDNLSPATH;
154     options->uampath = _PATH_AFPDUAMPATH;
155     options->uamlist = "uams_clrtxt.so,uams_dhx.so";
156     options->guest = "nobody";
157     options->loginmesg = "";
158     options->transports = AFPTRANS_ALL;
159     options->passwdfile = _PATH_AFPDPWFILE;
160     options->tickleval = 30;
161     options->timeout = 4;
162     options->server_notif = 1;
163     options->authprintdir = NULL;
164     options->signature = "host";
165     options->umask = 0;
166 #ifdef ADMIN_GRP
167     options->admingid = 0;
168 #endif /* ADMIN_GRP */
169     options->k5service = NULL;
170     options->k5realm = NULL;
171 }
172
173 /* parse an afpd.conf line. i'm doing it this way because it's
174  * easy. it is, however, massively hokey. sample afpd.conf:
175  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
176  * "private machine"@zone2 -noguest -port 11012
177  * server2 -nocleartxt -nodsi
178  *
179  * NOTE: this ignores unknown options 
180  */
181 int afp_options_parseline(char *buf, struct afp_options *options)
182 {
183     char *c, *opt;
184
185     /* handle server */
186     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
187         options->server = opt;
188
189     /* parse toggles */
190     if (strstr(buf, " -nodebug"))
191         options->flags &= ~OPTION_DEBUG;
192 #ifdef USE_SRVLOC
193     if (strstr(buf, " -noslp"))
194         options->flags |= OPTION_NOSLP;
195 #endif /* USE_SRVLOC */
196
197     if (strstr(buf, " -nouservolfirst"))
198         options->flags &= ~OPTION_USERVOLFIRST;
199     if (strstr(buf, " -uservolfirst"))
200         options->flags |= OPTION_USERVOLFIRST;
201     if (strstr(buf, " -nouservol"))
202         options->flags |= OPTION_NOUSERVOL;
203     if (strstr(buf, " -uservol"))
204         options->flags &= ~OPTION_NOUSERVOL;
205     if (strstr(buf, " -proxy"))
206         options->flags |= OPTION_PROXY;
207     if (strstr(buf, " -noicon"))
208         options->flags &= ~OPTION_CUSTOMICON;
209     if (strstr(buf, " -icon"))
210         options->flags |= OPTION_CUSTOMICON;
211
212     /* passwd bits */
213     if (strstr(buf, " -nosavepassword"))
214         options->passwdbits |= PASSWD_NOSAVE;
215     if (strstr(buf, " -savepassword"))
216         options->passwdbits &= ~PASSWD_NOSAVE;
217     if (strstr(buf, " -nosetpassword"))
218         options->passwdbits &= ~PASSWD_SET;
219     if (strstr(buf, " -setpassword"))
220         options->passwdbits |= PASSWD_SET;
221
222     /* transports */
223     if (strstr(buf, " -transall"))
224         options->transports = AFPTRANS_ALL;
225     if (strstr(buf, " -notransall"))
226         options->transports = AFPTRANS_NONE;
227     if (strstr(buf, " -tcp"))
228         options->transports |= AFPTRANS_TCP;
229     if (strstr(buf, " -notcp"))
230         options->transports &= ~AFPTRANS_TCP;
231     if (strstr(buf, " -ddp"))
232         options->transports |= AFPTRANS_DDP;
233     if (strstr(buf, " -noddp"))
234         options->transports &= ~AFPTRANS_DDP;
235     if (strstr(buf, "-client_polling"))
236         options->server_notif = 0;
237
238     /* figure out options w/ values. currently, this will ignore the setting
239      * if memory is lacking. */
240     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
241         options->defaultvol.name = opt;
242     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
243         options->systemvol.name = opt;
244     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
245         options->loginmesg = opt;
246     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
247         options->guest = opt;
248     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
249         options->passwdfile = opt;
250     if ((c = getoption(buf, "-passwdminlen")))
251         options->passwdminlen = MIN(1, atoi(c));
252     if ((c = getoption(buf, "-loginmaxfail")))
253         options->loginmaxfail = atoi(c);
254     if ((c = getoption(buf, "-tickleval"))) {
255         options->tickleval = atoi(c);
256         if (options->tickleval <= 0) {
257             options->tickleval = 30;
258         }
259     }
260     if ((c = getoption(buf, "-timeout"))) {
261         options->timeout = atoi(c);
262         if (options->timeout <= 0) {
263             options->timeout = 4;
264         }
265     }
266
267     if ((c = getoption(buf, "-server_quantum")))
268         options->server_quantum = strtoul(c, NULL, 0);
269
270 #ifndef DISABLE_LOGGER
271     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
272     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
273     if ((c = getoption(buf, "-setuplog")))
274     {
275       char *ptr, *logsource, *logtype, *loglevel, *filename;
276
277       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
278       ptr = c;
279       
280       /* 
281       logsource = ptr = c;
282       if (ptr)
283       {
284         ptr = strpbrk(ptr, " \t");
285         if (ptr) 
286         {
287           *ptr++ = 0;
288           while (*ptr && isspace(*ptr))
289             ptr++;
290         }
291       }
292       */
293
294       logtype = ptr; 
295       if (ptr)
296       {
297         ptr = strpbrk(ptr, " \t");
298         if (ptr) 
299         {
300           *ptr++ = 0;
301           while (*ptr && isspace(*ptr))
302             ptr++;
303         }
304       }
305
306       loglevel = ptr;
307       if (ptr)
308       {
309         ptr = strpbrk(ptr, " \t");
310         if (ptr) 
311         {
312           *ptr++ = 0;
313           while (*ptr && isspace(*ptr))
314             ptr++;
315         }
316       }
317
318       filename = ptr;
319       if (ptr)
320       {
321         ptr = strpbrk(ptr, " \t");
322         if (ptr) 
323         {
324           *ptr++ = 0;
325           while (*ptr && isspace(*ptr))
326             ptr++;
327         }
328       }
329
330       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
331           logtype, loglevel, filename);
332
333       setuplog(logtype, loglevel, filename);
334     }
335
336     if ((c = getoption(buf, "-unsetuplog")))
337     {
338       char *ptr, *logtype, *loglevel, *filename;
339
340       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
341
342       ptr = c;
343       logtype = ptr;
344       if (ptr)
345       {
346         ptr = strpbrk(ptr, " \t");
347         if (ptr)
348         {
349           *ptr++ = 0;
350           while (*ptr && isspace(*ptr))
351             ptr++;
352         }
353       }
354
355       loglevel = ptr;
356       if (ptr)
357       {
358         ptr = strpbrk(ptr, " \t");
359         if (ptr)
360         {
361           *ptr++ = 0;
362            while (*ptr && isspace(*ptr))
363              ptr++;
364         }
365       }
366
367       filename = ptr;
368       if (ptr)
369       {
370         ptr = strpbrk(ptr, " \t");
371         if (ptr)
372         {
373           *ptr++ = 0;
374           while (*ptr && isspace(*ptr))
375             ptr++;
376         }
377       }
378       
379       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
380               logtype, NULL, filename);
381
382       setuplog(logtype, NULL, filename);
383     }
384 #endif /* DISABLE_LOGGER */
385 #ifdef ADMIN_GRP
386     if ((c = getoption(buf, "-admingroup"))) {
387         struct group *gr = getgrnam(c);
388         if (gr != NULL) {
389             options->admingid = gr->gr_gid;
390         }
391     }
392 #endif /* ADMIN_GRP */
393
394     if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
395         options->k5service = opt;
396     if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
397         options->k5realm = opt;
398     if ((c = getoption(buf, "-k5keytab")))
399         setenv( "KRB5_KTNAME", c, 1 );
400     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
401         options->authprintdir = opt;
402     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
403         options->uampath = opt;
404     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
405         options->uamlist = opt;
406     if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
407         options->nlspath = opt;
408
409     if ((c = getoption(buf, "-ipaddr"))) {
410         struct in_addr inaddr;
411         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
412             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
413                 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
414             options->ipaddr = opt;
415         }
416     }
417
418     if ((c = getoption(buf, "-port")))
419         options->port = atoi(c);
420     if ((c = getoption(buf, "-ddpaddr")))
421         atalk_aton(c, &options->ddpaddr);
422     if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
423         options->signature = opt;
424
425     /* do a little checking for the domain name. */
426     if ((c = getoption(buf, "-fqdn"))) {
427         char *p = strchr(c, ':');
428         if (p)
429             *p = '\0';
430         if (gethostbyname(c)) {
431             if (p)
432                 *p = ':';
433             if ((opt = strdup(c)))
434                 options->fqdn = opt;
435         }
436     }
437
438     return 1;
439 }
440
441 int afp_options_parse(int ac, char **av, struct afp_options *options)
442 {
443     extern char *optarg;
444     extern int optind;
445
446     char *p;
447     char *tmp;  /* Used for error checking the result of strtol */
448     int c, err = 0;
449
450     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
451         perror( "gethostname" );
452         return 0;
453     }
454     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
455         *p = '\0';
456     }
457
458     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
459         p = av[ 0 ];
460     } else {
461         p++;
462     }
463
464     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
465         switch ( c ) {
466         case 'd' :
467             options->flags |= OPTION_DEBUG;
468             break;
469         case 'n' :
470             options->server = optarg;
471             break;
472         case 'f' :
473             options->defaultvol.name = optarg;
474             break;
475         case 's' :
476             options->systemvol.name = optarg;
477             break;
478         case 'u' :
479             options->flags |= OPTION_USERVOLFIRST;
480             break;
481         case 'c' :
482             options->connections = atoi( optarg );
483             break;
484         case 'g' :
485             options->guest = optarg;
486             break;
487
488         case 'P' :
489             options->pidfile = optarg;
490             break;
491
492         case 'p':
493             options->passwdbits |= PASSWD_NOSAVE;
494             break;
495         case 't':
496             options->passwdbits |= PASSWD_SET;
497             break;
498
499         case 'D':
500             options->transports &= ~AFPTRANS_DDP;
501             break;
502         case 'S':
503             options->port = atoi(optarg);
504             break;
505         case 'T':
506             options->transports &= ~AFPTRANS_TCP;
507             break;
508         case 'L':
509             options->loginmesg = optarg;
510             break;
511         case 'F':
512             options->configfile = optarg;
513             break;
514         case 'U':
515             options->uamlist = optarg;
516             break;
517         case 'v':       /* version */
518             printf( "afpd (version %s)\n", VERSION );
519             exit ( 1 );
520             break;
521         case 'I':
522             options->flags |= OPTION_CUSTOMICON;
523             break;
524         case 'm':
525             options->umask = strtoul(optarg, &tmp, 8);
526             if ((options->umask > 0777)) {
527                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
528                 err++;
529             }
530             if (tmp[0] != '\0') {
531                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
532                 err++;
533             }
534             break;
535         default :
536             err++;
537         }
538     }
539     if ( err || optind != ac ) {
540         fprintf( stderr,
541                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
542                  [ -P pidfile ] [ -s sysvols ] \n", p );
543         fprintf( stderr,
544                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
545                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
546         return 0;
547     }
548
549 #ifdef ultrix
550     openlog( p, LOG_PID ); /* ultrix only */
551 #else /* ultrix */
552     set_processname(p);
553     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
554 #endif /* ultrix */
555
556     return 1;
557 }