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