]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
f75085d110002fa79d1490d4ecf6092899235319
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.30.2.2.2.5 2004-01-24 18:05:26 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 /* FIXME CNID */
65 char             Cnid_srv[MAXHOSTNAMELEN + 1] = "localhost";
66 int              Cnid_port = 4700;
67
68 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:Ivm:"
69 #define LENGTH 512
70
71 /* return an option. this uses an internal array, so it's necessary
72  * to duplicate it if you want to hold it for long. this is probably
73  * non-optimal. */
74 static char *getoption(char *buf, const char *option)
75 {
76     static char string[LENGTH + 1];
77     char *end;
78     int len;
79
80     if (option && (buf = strstr(buf, option)))
81         buf = strpbrk(buf, " \t");
82
83     while (buf && isspace(*buf))
84         buf++;
85
86     if (!buf)
87         return NULL;
88
89     /* search for any quoted stuff */
90     if (*buf == '"' && (end = strchr(buf + 1, '"'))) {
91         buf++;
92         len = MIN(end - buf, LENGTH);
93     } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
94         len = MIN(end - buf, LENGTH);
95     else
96         len = MIN(strlen(buf), LENGTH);
97
98     strncpy(string, buf, len);
99     string[len] = '\0';
100     return string;
101 }
102
103 /* get rid of any allocated afp_option buffers. */
104 void afp_options_free(struct afp_options *opt,
105                       const struct afp_options *save)
106 {
107     if (opt->defaultvol.name && (opt->defaultvol.name != save->defaultvol.name))
108         free(opt->defaultvol.name);
109     if (opt->defaultvol.full_name && (opt->defaultvol.full_name != save->defaultvol.full_name))
110         free(opt->defaultvol.full_name);
111
112     if (opt->systemvol.name && (opt->systemvol.name != save->systemvol.name))
113         free(opt->systemvol.name);
114     if (opt->systemvol.full_name && (opt->systemvol.full_name != save->systemvol.full_name))
115         free(opt->systemvol.full_name);
116
117     if (opt->uservol.name && (opt->uservol.name != save->uservol.name))
118         free(opt->uservol.name);
119     if (opt->uservol.full_name && (opt->uservol.full_name != save->uservol.full_name))
120         free(opt->uservol.full_name);
121
122     if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
123         free(opt->loginmesg);
124     if (opt->guest && (opt->guest != save->guest))
125         free(opt->guest);
126     if (opt->server && (opt->server != save->server))
127         free(opt->server);
128     if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
129         free(opt->ipaddr);
130     if (opt->fqdn && (opt->fqdn != save->fqdn))
131         free(opt->fqdn);
132     if (opt->uampath && (opt->uampath != save->uampath))
133         free(opt->uampath);
134     if (opt->uamlist && (opt->uamlist != save->uamlist))
135         free(opt->uamlist);
136     if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
137         free(opt->passwdfile);
138     if (opt->signature && (opt->signature != save->signature))
139         free(opt->signature);
140     if (opt->k5service && (opt->k5service != save->k5service))
141         free(opt->k5service);
142     if (opt->k5realm && (opt->k5realm != save->k5realm))
143         free(opt->k5realm);
144     if (opt->k5keytab && (opt->k5keytab != save->k5keytab))
145         free(opt->k5keytab);
146     if (opt->unixcodepage && (opt->unixcodepage != save->unixcodepage))
147         free(opt->unixcodepage);
148     if (opt->maccodepage && (opt->maccodepage != save->maccodepage))
149         free(opt->maccodepage);
150 }
151
152 /* initialize options */
153 void afp_options_init(struct afp_options *options)
154 {
155     memset(options, 0, sizeof(struct afp_options));
156     options->connections = 20;
157     options->pidfile = _PATH_AFPDLOCK;
158     options->defaultvol.name = _PATH_AFPDDEFVOL;
159     options->systemvol.name = _PATH_AFPDSYSVOL;
160     options->configfile = _PATH_AFPDCONF;
161     options->uampath = _PATH_AFPDUAMPATH;
162     options->uamlist = "uams_clrtxt.so,uams_dhx.so";
163     options->guest = "nobody";
164     options->loginmesg = "";
165     options->transports = AFPTRANS_ALL;
166     options->passwdfile = _PATH_AFPDPWFILE;
167     options->tickleval = 30;
168     options->timeout = 4;
169     options->sleep = 10* 120; /* 10 h in 30 seconds tick */
170     options->server_notif = 1;
171     options->authprintdir = NULL;
172     options->signature = "host";
173     options->umask = 0;
174 #ifdef ADMIN_GRP
175     options->admingid = 0;
176 #endif /* ADMIN_GRP */
177     options->k5service = NULL;
178     options->k5realm = NULL;
179     options->k5keytab = NULL;
180     options->unixcharset = CH_UNIX;
181     options->unixcodepage = "LOCALE";
182     options->maccharset = CH_MAC;
183     options->maccodepage = "MAC_ROMAN";
184 }
185
186 /* parse an afpd.conf line. i'm doing it this way because it's
187  * easy. it is, however, massively hokey. sample afpd.conf:
188  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
189  * "private machine"@zone2 -noguest -port 11012
190  * server2 -nocleartxt -nodsi
191  *
192  * NOTE: this ignores unknown options 
193  */
194 int afp_options_parseline(char *buf, struct afp_options *options)
195 {
196     char *c, *opt;
197
198     /* handle server */
199     if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
200         options->server = opt;
201
202     /* parse toggles */
203     if (strstr(buf, " -nodebug"))
204         options->flags &= ~OPTION_DEBUG;
205 #ifdef USE_SRVLOC
206     if (strstr(buf, " -noslp"))
207         options->flags |= OPTION_NOSLP;
208 #endif /* USE_SRVLOC */
209
210     if (strstr(buf, " -nouservolfirst"))
211         options->flags &= ~OPTION_USERVOLFIRST;
212     if (strstr(buf, " -uservolfirst"))
213         options->flags |= OPTION_USERVOLFIRST;
214     if (strstr(buf, " -nouservol"))
215         options->flags |= OPTION_NOUSERVOL;
216     if (strstr(buf, " -uservol"))
217         options->flags &= ~OPTION_NOUSERVOL;
218     if (strstr(buf, " -proxy"))
219         options->flags |= OPTION_PROXY;
220     if (strstr(buf, " -noicon"))
221         options->flags &= ~OPTION_CUSTOMICON;
222     if (strstr(buf, " -icon"))
223         options->flags |= OPTION_CUSTOMICON;
224
225     /* passwd bits */
226     if (strstr(buf, " -nosavepassword"))
227         options->passwdbits |= PASSWD_NOSAVE;
228     if (strstr(buf, " -savepassword"))
229         options->passwdbits &= ~PASSWD_NOSAVE;
230     if (strstr(buf, " -nosetpassword"))
231         options->passwdbits &= ~PASSWD_SET;
232     if (strstr(buf, " -setpassword"))
233         options->passwdbits |= PASSWD_SET;
234
235     /* transports */
236     if (strstr(buf, " -transall"))
237         options->transports = AFPTRANS_ALL;
238     if (strstr(buf, " -notransall"))
239         options->transports = AFPTRANS_NONE;
240     if (strstr(buf, " -tcp"))
241         options->transports |= AFPTRANS_TCP;
242     if (strstr(buf, " -notcp"))
243         options->transports &= ~AFPTRANS_TCP;
244     if (strstr(buf, " -ddp"))
245         options->transports |= AFPTRANS_DDP;
246     if (strstr(buf, " -noddp"))
247         options->transports &= ~AFPTRANS_DDP;
248     if (strstr(buf, "-client_polling"))
249         options->server_notif = 0;
250
251     /* figure out options w/ values. currently, this will ignore the setting
252      * if memory is lacking. */
253     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
254         options->defaultvol.name = opt;
255     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
256         options->systemvol.name = opt;
257     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
258         options->loginmesg = opt;
259     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
260         options->guest = opt;
261     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
262         options->passwdfile = opt;
263     if ((c = getoption(buf, "-passwdminlen")))
264         options->passwdminlen = MIN(1, atoi(c));
265     if ((c = getoption(buf, "-loginmaxfail")))
266         options->loginmaxfail = atoi(c);
267     if ((c = getoption(buf, "-tickleval"))) {
268         options->tickleval = atoi(c);
269         if (options->tickleval <= 0) {
270             options->tickleval = 30;
271         }
272     }
273     if ((c = getoption(buf, "-timeout"))) {
274         options->timeout = atoi(c);
275         if (options->timeout <= 0) {
276             options->timeout = 4;
277         }
278     }
279
280     if ((c = getoption(buf, "-sleep"))) {
281         options->sleep = atoi(c) *120;
282         if (options->sleep <= 4) {
283             options->sleep = 4;
284         }
285     }
286
287     if ((c = getoption(buf, "-server_quantum")))
288         options->server_quantum = strtoul(c, NULL, 0);
289
290 #ifndef DISABLE_LOGGER
291     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
292     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
293     if ((c = getoption(buf, "-setuplog")))
294     {
295       char *ptr, *logsource, *logtype, *loglevel, *filename;
296
297       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
298       ptr = c;
299       
300       /* 
301       logsource = ptr = c;
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
314       logtype = ptr; 
315       if (ptr)
316       {
317         ptr = strpbrk(ptr, " \t");
318         if (ptr) 
319         {
320           *ptr++ = 0;
321           while (*ptr && isspace(*ptr))
322             ptr++;
323         }
324       }
325
326       loglevel = 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       filename = 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       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
351           logtype, loglevel, filename);
352
353       setuplog(logtype, loglevel, filename);
354     }
355
356     if ((c = getoption(buf, "-unsetuplog")))
357     {
358       char *ptr, *logtype, *loglevel, *filename;
359
360       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
361
362       ptr = c;
363       logtype = ptr;
364       if (ptr)
365       {
366         ptr = strpbrk(ptr, " \t");
367         if (ptr)
368         {
369           *ptr++ = 0;
370           while (*ptr && isspace(*ptr))
371             ptr++;
372         }
373       }
374
375       loglevel = ptr;
376       if (ptr)
377       {
378         ptr = strpbrk(ptr, " \t");
379         if (ptr)
380         {
381           *ptr++ = 0;
382            while (*ptr && isspace(*ptr))
383              ptr++;
384         }
385       }
386
387       filename = ptr;
388       if (ptr)
389       {
390         ptr = strpbrk(ptr, " \t");
391         if (ptr)
392         {
393           *ptr++ = 0;
394           while (*ptr && isspace(*ptr))
395             ptr++;
396         }
397       }
398       
399       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
400               logtype, NULL, filename);
401
402       setuplog(logtype, NULL, filename);
403     }
404 #endif /* DISABLE_LOGGER */
405 #ifdef ADMIN_GRP
406     if ((c = getoption(buf, "-admingroup"))) {
407         struct group *gr = getgrnam(c);
408         if (gr != NULL) {
409             options->admingid = gr->gr_gid;
410         }
411     }
412 #endif /* ADMIN_GRP */
413
414     if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
415         options->k5service = opt;
416     if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
417         options->k5realm = opt;
418     if ((c = getoption(buf, "-k5keytab"))) {
419         if ( NULL == (options->k5keytab = (char *) malloc(sizeof(char)*(strlen(c)+14)) )) {
420                 LOG(log_error, logtype_afpd, "malloc failed");
421                 exit(-1);
422         }
423         snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
424         putenv(options->k5keytab);
425         /* setenv( "KRB5_KTNAME", c, 1 ); */
426     }
427     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
428         options->authprintdir = opt;
429     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
430         options->uampath = opt;
431     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
432         options->uamlist = opt;
433
434     if ((c = getoption(buf, "-ipaddr"))) {
435         struct in_addr inaddr;
436         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
437             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
438                 LOG(log_info, logtype_afpd, "WARNING: can't find %s\n", opt);
439             options->ipaddr = opt;
440         }
441     }
442
443     /* FIXME CNID Cnid_srv is a server attribute */
444     if ((c = getoption(buf, "-cnidserver"))) {
445         char *p;
446         int len;        
447         p = strchr(c, ':');
448         if (p != NULL && (len = p - c) <= MAXHOSTNAMELEN) {
449             memcpy(Cnid_srv, c, len);
450             Cnid_srv[len] = 0;
451             Cnid_port = atoi(p +1);
452         }
453     }
454
455     if ((c = getoption(buf, "-port")))
456         options->port = atoi(c);
457     if ((c = getoption(buf, "-ddpaddr")))
458         atalk_aton(c, &options->ddpaddr);
459     if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
460         options->signature = opt;
461
462     /* do a little checking for the domain name. */
463     if ((c = getoption(buf, "-fqdn"))) {
464         char *p = strchr(c, ':');
465         if (p)
466             *p = '\0';
467         if (gethostbyname(c)) {
468             if (p)
469                 *p = ':';
470             if ((opt = strdup(c)))
471                 options->fqdn = opt;
472         }
473         else {
474             LOG(log_error, logtype_afpd, "error parsing -fqdn, gethostbyname failed for: %s", c);
475         }
476     }
477
478     if ((c = getoption(buf, "-unixcodepage"))) {
479         if ((charset_t)-1  == ( options->unixcharset = add_charset(c)) ) {
480             options->unixcharset = CH_UNIX;
481             LOG(log_warning, logtype_afpd, "setting Unix codepage to '%s' failed", c);
482         }
483         else {
484             if (opt = strdup(c))
485                 options->unixcodepage = strdup(c);
486         }
487     }
488         
489     if ((c = getoption(buf, "-maccodepage"))) {
490         if ((charset_t)-1 == ( options->maccharset = add_charset(c)) ) {
491             options->maccharset = CH_MAC;
492             LOG(log_warning, logtype_afpd, "setting Mac codepage to '%s' failed", c);
493         }
494         else {
495             if (opt = strdup(c))
496                 options->maccodepage = strdup(c);
497         }
498     }
499
500     return 1;
501 }
502
503 int afp_options_parse(int ac, char **av, struct afp_options *options)
504 {
505     extern char *optarg;
506     extern int optind;
507
508     char *p;
509     char *tmp;  /* Used for error checking the result of strtol */
510     int c, err = 0;
511
512     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
513         perror( "gethostname" );
514         return 0;
515     }
516     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
517         *p = '\0';
518     }
519
520     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
521         p = av[ 0 ];
522     } else {
523         p++;
524     }
525
526     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
527         switch ( c ) {
528         case 'd' :
529             options->flags |= OPTION_DEBUG;
530             break;
531         case 'n' :
532             options->server = optarg;
533             break;
534         case 'f' :
535             options->defaultvol.name = optarg;
536             break;
537         case 's' :
538             options->systemvol.name = optarg;
539             break;
540         case 'u' :
541             options->flags |= OPTION_USERVOLFIRST;
542             break;
543         case 'c' :
544             options->connections = atoi( optarg );
545             break;
546         case 'g' :
547             options->guest = optarg;
548             break;
549
550         case 'P' :
551             options->pidfile = optarg;
552             break;
553
554         case 'p':
555             options->passwdbits |= PASSWD_NOSAVE;
556             break;
557         case 't':
558             options->passwdbits |= PASSWD_SET;
559             break;
560
561         case 'D':
562             options->transports &= ~AFPTRANS_DDP;
563             break;
564         case 'S':
565             options->port = atoi(optarg);
566             break;
567         case 'T':
568             options->transports &= ~AFPTRANS_TCP;
569             break;
570         case 'L':
571             options->loginmesg = optarg;
572             break;
573         case 'F':
574             options->configfile = optarg;
575             break;
576         case 'U':
577             options->uamlist = optarg;
578             break;
579         case 'v':       /* version */
580             printf( "afpd (version %s)\n", VERSION );
581             exit ( 1 );
582             break;
583         case 'I':
584             options->flags |= OPTION_CUSTOMICON;
585             break;
586         case 'm':
587             options->umask = strtoul(optarg, &tmp, 8);
588             if ((options->umask > 0777)) {
589                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
590                 err++;
591             }
592             if (tmp[0] != '\0') {
593                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
594                 err++;
595             }
596             break;
597         default :
598             err++;
599         }
600     }
601     if ( err || optind != ac ) {
602         fprintf( stderr,
603                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
604                  [ -P pidfile ] [ -s sysvols ] \n", p );
605         fprintf( stderr,
606                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
607                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
608         return 0;
609     }
610
611 #ifdef ultrix
612     openlog( p, LOG_PID ); /* ultrix only */
613 #else /* ultrix */
614     set_processname(p);
615     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
616 #endif /* ultrix */
617
618     return 1;
619 }