]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
add -advertise_ssh flag to afpd.conf, if set announce SSH tunneling capabilities.
[netatalk.git] / etc / afpd / afp_options.c
1 /*
2  * $Id: afp_options.c,v 1.30.2.2.2.8 2004-06-09 02:07:15 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     if (strstr(buf, " -advertise_ssh"))
225         options->flags |= OPTION_ANNOUNCESSH;
226
227     /* passwd bits */
228     if (strstr(buf, " -nosavepassword"))
229         options->passwdbits |= PASSWD_NOSAVE;
230     if (strstr(buf, " -savepassword"))
231         options->passwdbits &= ~PASSWD_NOSAVE;
232     if (strstr(buf, " -nosetpassword"))
233         options->passwdbits &= ~PASSWD_SET;
234     if (strstr(buf, " -setpassword"))
235         options->passwdbits |= PASSWD_SET;
236
237     /* transports */
238     if (strstr(buf, " -transall"))
239         options->transports = AFPTRANS_ALL;
240     if (strstr(buf, " -notransall"))
241         options->transports = AFPTRANS_NONE;
242     if (strstr(buf, " -tcp"))
243         options->transports |= AFPTRANS_TCP;
244     if (strstr(buf, " -notcp"))
245         options->transports &= ~AFPTRANS_TCP;
246     if (strstr(buf, " -ddp"))
247         options->transports |= AFPTRANS_DDP;
248     if (strstr(buf, " -noddp"))
249         options->transports &= ~AFPTRANS_DDP;
250     if (strstr(buf, "-client_polling"))
251         options->server_notif = 0;
252
253     /* figure out options w/ values. currently, this will ignore the setting
254      * if memory is lacking. */
255     if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
256         options->defaultvol.name = opt;
257     if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
258         options->systemvol.name = opt;
259     if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
260         options->loginmesg = opt;
261     if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
262         options->guest = opt;
263     if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
264         options->passwdfile = opt;
265     if ((c = getoption(buf, "-passwdminlen")))
266         options->passwdminlen = MIN(1, atoi(c));
267     if ((c = getoption(buf, "-loginmaxfail")))
268         options->loginmaxfail = atoi(c);
269     if ((c = getoption(buf, "-tickleval"))) {
270         options->tickleval = atoi(c);
271         if (options->tickleval <= 0) {
272             options->tickleval = 30;
273         }
274     }
275     if ((c = getoption(buf, "-timeout"))) {
276         options->timeout = atoi(c);
277         if (options->timeout <= 0) {
278             options->timeout = 4;
279         }
280     }
281
282     if ((c = getoption(buf, "-sleep"))) {
283         options->sleep = atoi(c) *120;
284         if (options->sleep <= 4) {
285             options->sleep = 4;
286         }
287     }
288
289     if ((c = getoption(buf, "-server_quantum")))
290         options->server_quantum = strtoul(c, NULL, 0);
291
292 #ifndef DISABLE_LOGGER
293     /* -setuplogtype <syslog|filelog> <logtype> <loglevel> <filename>*/
294     /* -[no]setuplog <logtype> <loglevel> [<filename>]*/
295     if ((c = getoption(buf, "-setuplog")))
296     {
297       char *ptr, *logsource, *logtype, *loglevel, *filename;
298
299       LOG(log_debug6, logtype_afpd, "setting up logtype, c is %s", c);
300       ptr = c;
301       
302       /* 
303       logsource = ptr = c;
304       if (ptr)
305       {
306         ptr = strpbrk(ptr, " \t");
307         if (ptr) 
308         {
309           *ptr++ = 0;
310           while (*ptr && isspace(*ptr))
311             ptr++;
312         }
313       }
314       */
315
316       logtype = ptr; 
317       if (ptr)
318       {
319         ptr = strpbrk(ptr, " \t");
320         if (ptr) 
321         {
322           *ptr++ = 0;
323           while (*ptr && isspace(*ptr))
324             ptr++;
325         }
326       }
327
328       loglevel = ptr;
329       if (ptr)
330       {
331         ptr = strpbrk(ptr, " \t");
332         if (ptr) 
333         {
334           *ptr++ = 0;
335           while (*ptr && isspace(*ptr))
336             ptr++;
337         }
338       }
339
340       filename = ptr;
341       if (ptr)
342       {
343         ptr = strpbrk(ptr, " \t");
344         if (ptr) 
345         {
346           *ptr++ = 0;
347           while (*ptr && isspace(*ptr))
348             ptr++;
349         }
350       }
351
352       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s", 
353           logtype, loglevel, filename);
354
355       setuplog(logtype, loglevel, filename);
356     }
357
358     if ((c = getoption(buf, "-unsetuplog")))
359     {
360       char *ptr, *logtype, *loglevel, *filename;
361
362       LOG(log_debug6, logtype_afpd, "unsetting up logtype, c is %s", c);
363
364       ptr = c;
365       logtype = ptr;
366       if (ptr)
367       {
368         ptr = strpbrk(ptr, " \t");
369         if (ptr)
370         {
371           *ptr++ = 0;
372           while (*ptr && isspace(*ptr))
373             ptr++;
374         }
375       }
376
377       loglevel = ptr;
378       if (ptr)
379       {
380         ptr = strpbrk(ptr, " \t");
381         if (ptr)
382         {
383           *ptr++ = 0;
384            while (*ptr && isspace(*ptr))
385              ptr++;
386         }
387       }
388
389       filename = ptr;
390       if (ptr)
391       {
392         ptr = strpbrk(ptr, " \t");
393         if (ptr)
394         {
395           *ptr++ = 0;
396           while (*ptr && isspace(*ptr))
397             ptr++;
398         }
399       }
400       
401       LOG(log_debug7, logtype_afpd, "calling setuplog %s %s %s",
402               logtype, NULL, filename);
403
404       setuplog(logtype, NULL, filename);
405     }
406 #endif /* DISABLE_LOGGER */
407 #ifdef ADMIN_GRP
408     if ((c = getoption(buf, "-admingroup"))) {
409         struct group *gr = getgrnam(c);
410         if (gr != NULL) {
411             options->admingid = gr->gr_gid;
412         }
413     }
414 #endif /* ADMIN_GRP */
415
416     if ((c = getoption(buf, "-k5service")) && (opt = strdup(c)))
417         options->k5service = opt;
418     if ((c = getoption(buf, "-k5realm")) && (opt = strdup(c)))
419         options->k5realm = opt;
420     if ((c = getoption(buf, "-k5keytab"))) {
421         if ( NULL == (options->k5keytab = (char *) malloc(sizeof(char)*(strlen(c)+14)) )) {
422                 LOG(log_error, logtype_afpd, "malloc failed");
423                 exit(-1);
424         }
425         snprintf(options->k5keytab, strlen(c)+14, "KRB5_KTNAME=%s", c);
426         putenv(options->k5keytab);
427         /* setenv( "KRB5_KTNAME", c, 1 ); */
428     }
429     if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
430         options->authprintdir = opt;
431     if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
432         options->uampath = opt;
433     if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
434         options->uamlist = opt;
435
436     if ((c = getoption(buf, "-ipaddr"))) {
437         struct in_addr inaddr;
438         if (inet_aton(c, &inaddr) && (opt = strdup(c))) {
439             if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET))
440                 LOG(log_info, logtype_afpd, "WARNING: can't find %s", opt);
441             options->ipaddr = opt;
442         }
443     }
444
445     /* FIXME CNID Cnid_srv is a server attribute */
446     if ((c = getoption(buf, "-cnidserver"))) {
447         char *p;
448         int len;        
449         p = strchr(c, ':');
450         if (p != NULL && (len = p - c) <= MAXHOSTNAMELEN) {
451             memcpy(Cnid_srv, c, len);
452             Cnid_srv[len] = 0;
453             Cnid_port = atoi(p +1);
454         }
455     }
456
457     if ((c = getoption(buf, "-port")))
458         options->port = atoi(c);
459     if ((c = getoption(buf, "-ddpaddr")))
460         atalk_aton(c, &options->ddpaddr);
461     if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
462         options->signature = opt;
463
464     /* do a little checking for the domain name. */
465     if ((c = getoption(buf, "-fqdn"))) {
466         char *p = strchr(c, ':');
467         if (p)
468             *p = '\0';
469         if (gethostbyname(c)) {
470             if (p)
471                 *p = ':';
472             if ((opt = strdup(c)))
473                 options->fqdn = opt;
474         }
475         else {
476             LOG(log_error, logtype_afpd, "error parsing -fqdn, gethostbyname failed for: %s", c);
477         }
478     }
479
480     if ((c = getoption(buf, "-unixcodepage"))) {
481         if ((charset_t)-1  == ( options->unixcharset = add_charset(c)) ) {
482             options->unixcharset = CH_UNIX;
483             LOG(log_warning, logtype_afpd, "setting Unix codepage to '%s' failed", c);
484         }
485         else {
486             if ((opt = strdup(c)))
487                 options->unixcodepage = opt;
488         }
489     }
490         
491     if ((c = getoption(buf, "-maccodepage"))) {
492         if ((charset_t)-1 == ( options->maccharset = add_charset(c)) ) {
493             options->maccharset = CH_MAC;
494             LOG(log_warning, logtype_afpd, "setting Mac codepage to '%s' failed", c);
495         }
496         else {
497             if ((opt = strdup(c)))
498                 options->maccodepage = opt;
499         }
500     }
501
502     return 1;
503 }
504
505 int afp_options_parse(int ac, char **av, struct afp_options *options)
506 {
507     extern char *optarg;
508     extern int optind;
509
510     char *p;
511     char *tmp;  /* Used for error checking the result of strtol */
512     int c, err = 0;
513
514     if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
515         perror( "gethostname" );
516         return 0;
517     }
518     if (NULL != ( p = strchr(options->hostname, '.' )) ) {
519         *p = '\0';
520     }
521
522     if (NULL == ( p = strrchr( av[ 0 ], '/' )) ) {
523         p = av[ 0 ];
524     } else {
525         p++;
526     }
527
528     while (EOF != ( c = getopt( ac, av, OPTIONS )) ) {
529         switch ( c ) {
530         case 'd' :
531             options->flags |= OPTION_DEBUG;
532             break;
533         case 'n' :
534             options->server = optarg;
535             break;
536         case 'f' :
537             options->defaultvol.name = optarg;
538             break;
539         case 's' :
540             options->systemvol.name = optarg;
541             break;
542         case 'u' :
543             options->flags |= OPTION_USERVOLFIRST;
544             break;
545         case 'c' :
546             options->connections = atoi( optarg );
547             break;
548         case 'g' :
549             options->guest = optarg;
550             break;
551
552         case 'P' :
553             options->pidfile = optarg;
554             break;
555
556         case 'p':
557             options->passwdbits |= PASSWD_NOSAVE;
558             break;
559         case 't':
560             options->passwdbits |= PASSWD_SET;
561             break;
562
563         case 'D':
564             options->transports &= ~AFPTRANS_DDP;
565             break;
566         case 'S':
567             options->port = atoi(optarg);
568             break;
569         case 'T':
570             options->transports &= ~AFPTRANS_TCP;
571             break;
572         case 'L':
573             options->loginmesg = optarg;
574             break;
575         case 'F':
576             options->configfile = optarg;
577             break;
578         case 'U':
579             options->uamlist = optarg;
580             break;
581         case 'v':       /* version */
582             printf( "afpd (version %s)\n", VERSION );
583             exit ( 1 );
584             break;
585         case 'I':
586             options->flags |= OPTION_CUSTOMICON;
587             break;
588         case 'm':
589             options->umask = strtoul(optarg, &tmp, 8);
590             if ((options->umask > 0777)) {
591                 fprintf(stderr, "%s: out of range umask setting provided\n", p);
592                 err++;
593             }
594             if (tmp[0] != '\0') {
595                 fprintf(stderr, "%s: invalid characters in umask setting provided\n", p);
596                 err++;
597             }
598             break;
599         default :
600             err++;
601         }
602     }
603     if ( err || optind != ac ) {
604         fprintf( stderr,
605                  "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
606                  [ -P pidfile ] [ -s sysvols ] \n", p );
607         fprintf( stderr,
608                  "\t[ -u ] [ -c maxconn ] [ -g guest ] \
609                  [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
610         return 0;
611     }
612
613 #ifdef ultrix
614     openlog( p, LOG_PID ); /* ultrix only */
615 #else /* ultrix */
616     set_processname(p);
617     syslog_setup(log_debug, logtype_default, logoption_ndelay|logoption_pid, logfacility_daemon);
618 #endif /* ultrix */
619
620     return 1;
621 }