]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
added log statements to follow setup_logtype option. Also fixed a bug, that bombed...
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.23 2002-04-02 02:45:28 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
173     if (strstr(buf, " -nouservolfirst"))
174         options->flags &= ~OPTION_USERVOLFIRST;
175     if (strstr(buf, " -uservolfirst"))
176         options->flags |= OPTION_USERVOLFIRST;
177     if (strstr(buf, " -nouservol"))
178         options->flags |= OPTION_NOUSERVOL;
179     if (strstr(buf, " -uservol"))
180         options->flags &= ~OPTION_NOUSERVOL;
181     if (strstr(buf, " -proxy"))
182         options->flags |= OPTION_PROXY;
183     if (strstr(buf, " -noicon"))
184         options->flags &= ~OPTION_CUSTOMICON;
185     if (strstr(buf, " -icon"))
186         options->flags |= OPTION_CUSTOMICON;
187
188     /* passwd bits */
189     if (strstr(buf, " -nosavepassword"))
190         options->passwdbits |= PASSWD_NOSAVE;
191     if (strstr(buf, " -savepassword"))
192         options->passwdbits &= ~PASSWD_NOSAVE;
193     if (strstr(buf, " -nosetpassword"))
194         options->passwdbits &= ~PASSWD_SET;
195     if (strstr(buf, " -setpassword"))
196         options->passwdbits |= PASSWD_SET;
197
198     /* transports */
199     if (strstr(buf, " -transall"))
200         options->transports = AFPTRANS_ALL;
201     if (strstr(buf, " -notransall"))
202         options->transports = AFPTRANS_NONE;
203     if (strstr(buf, " -tcp"))
204         options->transports |= AFPTRANS_TCP;
205     if (strstr(buf, " -notcp"))
206         options->transports &= ~AFPTRANS_TCP;
207     if (strstr(buf, " -ddp"))
208         options->transports |= AFPTRANS_DDP;
209     if (strstr(buf, " -noddp"))
210         options->transports &= ~AFPTRANS_DDP;
211     if (strstr(buf, "-client_polling"))
212         options->server_notif = 0;
213
214     /* figure out options w/ values. currently, this will ignore the setting
215      * if memory is lacking. */
216     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
217         options->defaultvol = opt;
218     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
219         options->systemvol = opt;
220     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
221         options->loginmesg = opt;
222     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
223         options->guest = opt;
224     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
225         options->passwdfile = opt;
226     if ((c = getoption(buf, "-passwdminlen")))
227         options->passwdminlen = MIN(1, atoi(c));
228     if ((c = getoption(buf, "-loginmaxfail")))
229         options->loginmaxfail = atoi(c);
230     if ((c = getoption(buf, "-tickleval"))) {
231         options->tickleval = atoi(c);
232         if (options->tickleval <= 0) {
233             options->tickleval = 30;
234         }
235     }
236     if ((c = getoption(buf, "-timeout"))) {
237         options->timeout = atoi(c);
238         if (options->timeout <= 0) {
239             options->timeout = 4;
240         }
241     }
242
243     if ((c = getoption(buf, "-server_quantum")))
244         options->server_quantum = strtoul(c, NULL, 0);
245
246     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
247     if ((c = getoption(buf, "-setuplogtype")))
248     {
249       char *ptr, *logsource, *logtype, *loglevel, *filename;
250
251       LOG(log_extradebug, logtype_afpd, "setting up logtype, c is %s", c);
252       logsource = ptr = c;
253       if (ptr)
254       {
255         ptr = strpbrk(ptr, " \t");
256         if (ptr) 
257         {
258           *ptr++ = 0;
259           while (*ptr && isspace(*ptr))
260             ptr++;
261         }
262       }
263
264       logtype = ptr;
265       if (ptr)
266       {
267         ptr = strpbrk(ptr, " \t");
268         if (ptr) 
269         {
270           *ptr++ = 0;
271           while (*ptr && isspace(*ptr))
272             ptr++;
273         }
274       }
275
276       loglevel = ptr;
277       if (ptr)
278       {
279         ptr = strpbrk(ptr, " \t");
280         if (ptr) 
281         {
282           *ptr++ = 0;
283           while (*ptr && isspace(*ptr))
284             ptr++;
285         }
286       }
287
288       filename = ptr;
289       if (ptr)
290       {
291         ptr = strpbrk(ptr, " \t");
292         if (ptr) 
293         {
294           *ptr++ = 0;
295           while (*ptr && isspace(*ptr))
296             ptr++;
297         }
298       }
299
300       LOG(log_extradebug, logtype_afpd, "Doing setuplog %s %s %s %s", 
301           logsource, logtype, loglevel, filename);
302
303       setuplog(logsource, logtype, loglevel, filename);
304     }
305
306 #ifdef ADMIN_GRP
307     if ((c = getoption(buf, "-admingroup"))) {
308         struct group *gr = getgrnam(c);
309         if (gr != NULL) {
310             options->admingid = gr->gr_gid;
311         }
312     }
313 #endif /* ADMIN_GRP */
314
315     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
316         options->authprintdir = opt;
317     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
318         options->uampath = opt;
319     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
320         options->uamlist = opt;
321     if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
322         options->nlspath = opt;
323
324     if ((c = getoption(buf, "-ipaddr"))) {
325         struct in_addr inaddr;
326         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
327             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
328                 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
329             options->ipaddr = opt;
330         }
331     }
332
333     if ((c = getoption(buf, "-port")))
334         options->port = atoi(c);
335     if ((c = getoption(buf, "-ddpaddr")))
336         atalk_aton(c, &options->ddpaddr);
337
338     /* do a little checking for the domain name. */
339     if ((c = getoption(buf, "-fqdn"))) {
340         char *p = strchr(c, ':');
341         if (p)
342             *p = '\0';
343         if (gethostbyname(c)) {
344             if (p)
345                 *p = ':';
346             if ((opt = strdup(c)))
347                 options->fqdn = opt;
348         }
349     }
350
351     return 1;
352 }
353
354 int afp_options_parse(int ac, char **av, struct afp_options *options)
355 {
356     extern char *optarg;
357     extern int optind;
358
359     char *p;
360     char *tmp;  /* Used for error checking the result of strtol */
361     int c, err = 0;
362
363     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
364         perror( "gethostname" );
365         return 0;
366     }
367     if (( p = strchr(options->hostname, '.' )) != 0 ) {
368         *p = '\0';
369     }
370
371     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
372         p = av[ 0 ];
373     } else {
374         p++;
375     }
376
377     while (( c = getopt( ac, av, OPTIONS )) != EOF ) {
378         switch ( c ) {
379         case 'd' :
380             options->flags |= OPTION_DEBUG;
381             break;
382         case 'n' :
383             options->server = optarg;
384             break;
385         case 'f' :
386             options->defaultvol = optarg;
387             break;
388         case 's' :
389             options->systemvol = optarg;
390             break;
391         case 'u' :
392             options->flags |= OPTION_USERVOLFIRST;
393             break;
394         case 'c' :
395             options->connections = atoi( optarg );
396             break;
397         case 'g' :
398             options->guest = optarg;
399             break;
400
401         case 'P' :
402             options->pidfile = optarg;
403             break;
404
405         case 'p':
406             options->passwdbits |= PASSWD_NOSAVE;
407             break;
408         case 't':
409             options->passwdbits |= PASSWD_SET;
410             break;
411
412         case 'D':
413             options->transports &= ~AFPTRANS_DDP;
414             break;
415         case 'S':
416             options->port = atoi(optarg);
417             break;
418         case 'T':
419             options->transports &= ~AFPTRANS_TCP;
420             break;
421         case 'L':
422             options->loginmesg = optarg;
423             break;
424         case 'F':
425             options->configfile = optarg;
426             break;
427         case 'U':
428             options->uamlist = optarg;
429             break;
430         case 'v':       /* version */
431             printf( "afpd (version %s)\n", VERSION );
432             exit ( 1 );
433             break;
434         case 'I':
435             options->flags |= OPTION_CUSTOMICON;
436             break;
437         case 'm':
438             options->umask = strtoul(optarg, &tmp, 8);
439             if ((options->umask > 0777)) {
440                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
441                 err++;
442             }
443             if (tmp[0] != '\0') {
444                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
445                 err++;
446             }
447             break;
448         default :
449             err++;
450         }
451     }
452     if ( err || optind != ac ) {
453         fprintf( stderr,
454                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
455                  [ -P pidfile ] [ -s sysvols ] \n", p );
456         fprintf( stderr,
457                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
458                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
459         return 0;
460     }
461
462 #ifdef ultrix
463     openlog( p, LOG_PID ); /* ultrix only */
464 #else /* ultrix */
465     set_processname(p);
466     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
467 #endif /* ultrix */
468
469     return 1;
470 }