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