]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
The following were necessary in order to implement a Kerbers 5 UAM compatible
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.30 2003-04-16 22:45:08 samnoble 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 && (opt->defaultvol != save->defaultvol))
104         free(opt->defaultvol);
105     if (opt->systemvol && (opt->systemvol != save->systemvol))
106         free(opt->systemvol);
107     if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
108         free(opt->loginmesg);
109     if (opt->guest && (opt->guest != save->guest))
110         free(opt->guest);
111     if (opt->server && (opt->server != save->server))
112         free(opt->server);
113     if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
114         free(opt->ipaddr);
115     if (opt->fqdn && (opt->fqdn != save->fqdn))
116         free(opt->fqdn);
117     if (opt->uampath && (opt->uampath != save->uampath))
118         free(opt->uampath);
119     if (opt->uamlist && (opt->uamlist != save->uamlist))
120         free(opt->uamlist);
121     if (opt->nlspath && (opt->nlspath != save->nlspath))
122         free(opt->nlspath);
123     if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
124         free(opt->passwdfile);
125     if (opt->signature && (opt->signature != save->signature))
126         free(opt->signature);
127     if (opt->k5service && (opt->k5service != save->k5service))
128         free(opt->k5service);
129     if (opt->k5realm && (opt->k5realm != save->k5realm))
130         free(opt->k5realm);
131 }
132
133 /* initialize options */
134 void afp_options_init(struct afp_options *options)
135 {
136     memset(options, 0, sizeof(struct afp_options));
137     options->connections = 20;
138     options->pidfile = _PATH_AFPDLOCK;
139     options->defaultvol = _PATH_AFPDDEFVOL;
140     options->systemvol = _PATH_AFPDSYSVOL;
141     options->configfile = _PATH_AFPDCONF;
142     options->nlspath = _PATH_AFPDNLSPATH;
143     options->uampath = _PATH_AFPDUAMPATH;
144     options->uamlist = "uams_clrtxt.so,uams_dhx.so";
145     options->guest = "nobody";
146     options->loginmesg = "";
147     options->transports = AFPTRANS_ALL;
148     options->passwdfile = _PATH_AFPDPWFILE;
149     options->tickleval = 30;
150     options->timeout = 4;
151     options->server_notif = 1;
152     options->authprintdir = NULL;
153     options->signature = "host";
154     options->umask = 0;
155 #ifdef ADMIN_GRP
156     options->admingid = 0;
157 #endif /* ADMIN_GRP */
158     options->k5service = NULL;
159     options->k5realm = NULL;
160 }
161
162 /* parse an afpd.conf line. i'm doing it this way because it's
163  * easy. it is, however, massively hokey. sample afpd.conf:
164  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
165  * "private machine"@zone2 -noguest -port 11012
166  * server2 -nocleartxt -nodsi
167  *
168  * NOTE: this ignores unknown options 
169  */
170 int afp_options_parseline(char *buf, struct afp_options *options)
171 {
172     char *c, *opt;
173
174     /* handle server */
175     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
176         options->server = opt;
177
178     /* parse toggles */
179     if (strstr(buf, " -nodebug"))
180         options->flags &= ~OPTION_DEBUG;
181 #ifdef USE_SRVLOC
182     if (strstr(buf, " -noslp"))
183         options->flags |= OPTION_NOSLP;
184 #endif /* USE_SRVLOC */
185
186     if (strstr(buf, " -nouservolfirst"))
187         options->flags &= ~OPTION_USERVOLFIRST;
188     if (strstr(buf, " -uservolfirst"))
189         options->flags |= OPTION_USERVOLFIRST;
190     if (strstr(buf, " -nouservol"))
191         options->flags |= OPTION_NOUSERVOL;
192     if (strstr(buf, " -uservol"))
193         options->flags &= ~OPTION_NOUSERVOL;
194     if (strstr(buf, " -proxy"))
195         options->flags |= OPTION_PROXY;
196     if (strstr(buf, " -noicon"))
197         options->flags &= ~OPTION_CUSTOMICON;
198     if (strstr(buf, " -icon"))
199         options->flags |= OPTION_CUSTOMICON;
200
201     /* passwd bits */
202     if (strstr(buf, " -nosavepassword"))
203         options->passwdbits |= PASSWD_NOSAVE;
204     if (strstr(buf, " -savepassword"))
205         options->passwdbits &= ~PASSWD_NOSAVE;
206     if (strstr(buf, " -nosetpassword"))
207         options->passwdbits &= ~PASSWD_SET;
208     if (strstr(buf, " -setpassword"))
209         options->passwdbits |= PASSWD_SET;
210
211     /* transports */
212     if (strstr(buf, " -transall"))
213         options->transports = AFPTRANS_ALL;
214     if (strstr(buf, " -notransall"))
215         options->transports = AFPTRANS_NONE;
216     if (strstr(buf, " -tcp"))
217         options->transports |= AFPTRANS_TCP;
218     if (strstr(buf, " -notcp"))
219         options->transports &= ~AFPTRANS_TCP;
220     if (strstr(buf, " -ddp"))
221         options->transports |= AFPTRANS_DDP;
222     if (strstr(buf, " -noddp"))
223         options->transports &= ~AFPTRANS_DDP;
224     if (strstr(buf, "-client_polling"))
225         options->server_notif = 0;
226
227     /* figure out options w/ values. currently, this will ignore the setting
228      * if memory is lacking. */
229     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
230         options->defaultvol = opt;
231     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
232         options->systemvol = opt;
233     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
234         options->loginmesg = opt;
235     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
236         options->guest = opt;
237     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
238         options->passwdfile = opt;
239     if ((c = getoption(buf, "-passwdminlen")))
240         options->passwdminlen = MIN(1, atoi(c));
241     if ((c = getoption(buf, "-loginmaxfail")))
242         options->loginmaxfail = atoi(c);
243     if ((c = getoption(buf, "-tickleval"))) {
244         options->tickleval = atoi(c);
245         if (options->tickleval <= 0) {
246             options->tickleval = 30;
247         }
248     }
249     if ((c = getoption(buf, "-timeout"))) {
250         options->timeout = atoi(c);
251         if (options->timeout <= 0) {
252             options->timeout = 4;
253         }
254     }
255
256     if ((c = getoption(buf, "-server_quantum")))
257         options->server_quantum = strtoul(c, NULL, 0);
258
259 #ifndef DISABLE_LOGGER
260     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
261     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
262     if ((c = getoption(buf, "-setuplog")))
263     {
264       char *ptr, *logsource, *logtype, *loglevel, *filename;
265
266       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
267       ptr = c;
268       
269       /* 
270       logsource = ptr = c;
271       if (ptr)
272       {
273         ptr = strpbrk(ptr, " \t");
274         if (ptr) 
275         {
276           *ptr++ = 0;
277           while (*ptr && isspace(*ptr))
278             ptr++;
279         }
280       }
281       */
282
283       logtype = ptr; 
284       if (ptr)
285       {
286         ptr = strpbrk(ptr, " \t");
287         if (ptr) 
288         {
289           *ptr++ = 0;
290           while (*ptr && isspace(*ptr))
291             ptr++;
292         }
293       }
294
295       loglevel = ptr;
296       if (ptr)
297       {
298         ptr = strpbrk(ptr, " \t");
299         if (ptr) 
300         {
301           *ptr++ = 0;
302           while (*ptr && isspace(*ptr))
303             ptr++;
304         }
305       }
306
307       filename = ptr;
308       if (ptr)
309       {
310         ptr = strpbrk(ptr, " \t");
311         if (ptr) 
312         {
313           *ptr++ = 0;
314           while (*ptr && isspace(*ptr))
315             ptr++;
316         }
317       }
318
319       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
320           logtype, loglevel, filename);
321
322       setuplog(logtype, loglevel, filename);
323     }
324
325     if ((c = getoption(buf, "-unsetuplog")))
326     {
327       char *ptr, *logtype, *loglevel, *filename;
328
329       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
330
331       ptr = c;
332       logtype = ptr;
333       if (ptr)
334       {
335         ptr = strpbrk(ptr, " \t");
336         if (ptr)
337         {
338           *ptr++ = 0;
339           while (*ptr && isspace(*ptr))
340             ptr++;
341         }
342       }
343
344       loglevel = ptr;
345       if (ptr)
346       {
347         ptr = strpbrk(ptr, " \t");
348         if (ptr)
349         {
350           *ptr++ = 0;
351            while (*ptr && isspace(*ptr))
352              ptr++;
353         }
354       }
355
356       filename = ptr;
357       if (ptr)
358       {
359         ptr = strpbrk(ptr, " \t");
360         if (ptr)
361         {
362           *ptr++ = 0;
363           while (*ptr && isspace(*ptr))
364             ptr++;
365         }
366       }
367       
368       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
369               logtype, NULL, filename);
370
371       setuplog(logtype, NULL, filename);
372     }
373 #endif /* DISABLE_LOGGER */
374 #ifdef ADMIN_GRP
375     if ((c = getoption(buf, "-admingroup"))) {
376         struct group *gr = getgrnam(c);
377         if (gr != NULL) {
378             options->admingid = gr->gr_gid;
379         }
380     }
381 #endif /* ADMIN_GRP */
382
383     if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
384         options->k5service = opt;
385     if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
386         options->k5realm = opt;
387     if ((c = getoption(buf, "-k5keytab")))
388         setenv( "KRB5_KTNAME", c, 1 );
389     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
390         options->authprintdir = opt;
391     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
392         options->uampath = opt;
393     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
394         options->uamlist = opt;
395     if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
396         options->nlspath = opt;
397
398     if ((c = getoption(buf, "-ipaddr"))) {
399         struct in_addr inaddr;
400         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
401             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
402                 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
403             options->ipaddr = opt;
404         }
405     }
406
407     if ((c = getoption(buf, "-port")))
408         options->port = atoi(c);
409     if ((c = getoption(buf, "-ddpaddr")))
410         atalk_aton(c, &options->ddpaddr);
411     if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
412         options->signature = opt;
413
414     /* do a little checking for the domain name. */
415     if ((c = getoption(buf, "-fqdn"))) {
416         char *p = strchr(c, ':');
417         if (p)
418             *p = '\0';
419         if (gethostbyname(c)) {
420             if (p)
421                 *p = ':';
422             if ((opt = strdup(c)))
423                 options->fqdn = opt;
424         }
425     }
426
427     return 1;
428 }
429
430 int afp_options_parse(int ac, char **av, struct afp_options *options)
431 {
432     extern char *optarg;
433     extern int optind;
434
435     char *p;
436     char *tmp;  /* Used for error checking the result of strtol */
437     int c, err = 0;
438
439     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
440         perror( "gethostname" );
441         return 0;
442     }
443     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
444         *p = '\0';
445     }
446
447     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
448         p = av[ 0 ];
449     } else {
450         p++;
451     }
452
453     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
454         switch ( c ) {
455         case 'd' :
456             options->flags |= OPTION_DEBUG;
457             break;
458         case 'n' :
459             options->server = optarg;
460             break;
461         case 'f' :
462             options->defaultvol = optarg;
463             break;
464         case 's' :
465             options->systemvol = optarg;
466             break;
467         case 'u' :
468             options->flags |= OPTION_USERVOLFIRST;
469             break;
470         case 'c' :
471             options->connections = atoi( optarg );
472             break;
473         case 'g' :
474             options->guest = optarg;
475             break;
476
477         case 'P' :
478             options->pidfile = optarg;
479             break;
480
481         case 'p':
482             options->passwdbits |= PASSWD_NOSAVE;
483             break;
484         case 't':
485             options->passwdbits |= PASSWD_SET;
486             break;
487
488         case 'D':
489             options->transports &= ~AFPTRANS_DDP;
490             break;
491         case 'S':
492             options->port = atoi(optarg);
493             break;
494         case 'T':
495             options->transports &= ~AFPTRANS_TCP;
496             break;
497         case 'L':
498             options->loginmesg = optarg;
499             break;
500         case 'F':
501             options->configfile = optarg;
502             break;
503         case 'U':
504             options->uamlist = optarg;
505             break;
506         case 'v':       /* version */
507             printf( "afpd (version %s)\n", VERSION );
508             exit ( 1 );
509             break;
510         case 'I':
511             options->flags |= OPTION_CUSTOMICON;
512             break;
513         case 'm':
514             options->umask = strtoul(optarg, &tmp, 8);
515             if ((options->umask > 0777)) {
516                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
517                 err++;
518             }
519             if (tmp[0] != '\0') {
520                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
521                 err++;
522             }
523             break;
524         default :
525             err++;
526         }
527     }
528     if ( err || optind != ac ) {
529         fprintf( stderr,
530                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
531                  [ -P pidfile ] [ -s sysvols ] \n", p );
532         fprintf( stderr,
533                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
534                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
535         return 0;
536     }
537
538 #ifdef ultrix
539     openlog( p, LOG_PID ); /* ultrix only */
540 #else /* ultrix */
541     set_processname(p);
542     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
543 #endif /* ultrix */
544
545     return 1;
546 }