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