]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/afp_options.c
Warning fixes.
[netatalk.git] / etc / afpd / afp_options.c
1 /* 
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * Copyright (c) 1990,1993 Regents of The University of Michigan.
4  * All Rights Reserved.  See COPYRIGHT.
5  *
6  * modified from main.c. this handles afp options.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <syslog.h>
21
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25
26 #include <atalk/paths.h>
27 #include <atalk/util.h>
28 #include "globals.h"
29 #include "status.h"
30 #include "auth.h"
31
32 #include <atalk/compat.h>
33
34 #ifdef ADMIN_GRP
35 #include <grp.h>
36 #include <sys/types.h>
37 #endif
38
39 #ifndef MIN
40 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
41 #endif
42
43 #define OPTIONS "dn:f:s:uc:g:P:ptDS:TL:F:U:I"
44 #define LENGTH 512
45
46 /* return an option. this uses an internal array, so it's necessary
47  * to duplicate it if you want to hold it for long. this is probably
48  * non-optimal. */
49 static char *getoption(char *buf, const char *option)
50 {
51   static char string[LENGTH + 1];
52   char *end;
53   int len;
54
55   if (option && (buf = strstr(buf, option)))
56     buf = strpbrk(buf, " \t");
57
58   while (buf && isspace(*buf))
59     buf++;
60       
61   if (!buf)
62     return NULL;
63
64   /* search for any quoted stuff */
65   if (*buf == '"' && (end = strchr(buf + 1, '"'))) { 
66     buf++;
67     len = MIN(end - buf, LENGTH);
68   } else if ((end = strpbrk(buf, " \t\n"))) /* option or eoln */
69     len = MIN(end - buf, LENGTH);
70   else
71     len = MIN(strlen(buf), LENGTH);
72   
73   strncpy(string, buf, len);
74   string[len] = '\0';
75   return string;
76 }
77
78 /* get rid of any allocated afp_option buffers. */
79 void afp_options_free(struct afp_options *opt, 
80                       const struct afp_options *save)
81 {
82   if (opt->defaultvol && (opt->defaultvol != save->defaultvol))
83     free(opt->defaultvol);
84   if (opt->systemvol && (opt->systemvol != save->systemvol))
85     free(opt->systemvol);
86   if (opt->loginmesg && (opt->loginmesg != save->loginmesg))
87     free(opt->loginmesg);
88   if (opt->guest && (opt->guest != save->guest)) 
89     free(opt->guest);
90   if (opt->server && (opt->server != save->server))
91     free(opt->server);
92   if (opt->ipaddr && (opt->ipaddr != save->ipaddr))
93     free(opt->ipaddr);
94   if (opt->fqdn && (opt->fqdn != save->fqdn))
95     free(opt->fqdn);
96   if (opt->uampath && (opt->uampath != save->uampath))
97     free(opt->uampath);
98   if (opt->uamlist && (opt->uamlist != save->uamlist))
99     free(opt->uamlist);
100   if (opt->nlspath && (opt->nlspath != save->nlspath))
101     free(opt->nlspath);
102   if (opt->passwdfile && (opt->passwdfile != save->passwdfile))
103     free(opt->passwdfile);
104 }
105
106 /* initialize options */
107 void afp_options_init(struct afp_options *options)
108 {
109   memset(options, 0, sizeof(struct afp_options));
110   options->connections = 20;
111   options->pidfile = _PATH_AFPDLOCK;
112   options->defaultvol = _PATH_AFPDDEFVOL;
113   options->systemvol = _PATH_AFPDSYSVOL;
114   options->configfile = _PATH_AFPDCONF;
115   options->nlspath = _PATH_AFPDNLSPATH;
116   options->uampath = _PATH_AFPDUAMPATH;
117   options->uamlist = "uams_guest.so,uams_clrtxt.so,uams_dhx.so";
118   options->guest = "nobody";
119   options->loginmesg = "";
120   options->transports = AFPTRANS_ALL;
121   options->passwdfile = _PATH_AFPDPWFILE;
122   options->tickleval = 30;
123   options->authprintdir = NULL;
124 #ifdef ADMIN_GRP
125   options->admingid = 0;
126 #endif ADMIN_GRP
127 }
128
129 /* parse an afpd.conf line. i'm doing it this way because it's
130  * easy. it is, however, massively hokey. sample afpd.conf:
131  * server:AFPServer@zone -loginmesg "blah blah blah" -nodsi 
132  * "private machine"@zone2 -noguest -port 11012
133  * server2 -nocleartxt -nodsi
134  *
135  * NOTE: this ignores unknown options 
136  */
137 int afp_options_parseline(char *buf, struct afp_options *options)
138 {
139   char *c, *opt;
140   
141   /* handle server */
142   if (*buf != '-' && (c = getoption(buf, NULL)) && (opt = strdup(c)))
143     options->server = opt;
144
145   /* parse toggles */
146   if (strstr(buf, " -nodebug"))
147     options->flags &= ~OPTION_DEBUG;
148
149   if (strstr(buf, " -nouservolfirst"))
150     options->flags &= ~OPTION_USERVOLFIRST;
151   if (strstr(buf, " -uservolfirst"))
152     options->flags |= OPTION_USERVOLFIRST;
153   if (strstr(buf, " -nouservol"))
154     options->flags |= OPTION_NOUSERVOL;
155   if (strstr(buf, " -uservol"))
156     options->flags &= ~OPTION_NOUSERVOL;
157   if (strstr(buf, " -proxy"))
158     options->flags |= OPTION_PROXY;
159   if (strstr(buf, " -noicon"))
160     options->flags &= ~OPTION_CUSTOMICON;
161   if (strstr(buf, " -icon"))
162     options->flags |= OPTION_CUSTOMICON;
163
164   /* passwd bits */
165   if (strstr(buf, " -nosavepassword"))
166     options->passwdbits |= PASSWD_NOSAVE;
167   if (strstr(buf, " -savepassword"))
168     options->passwdbits &= ~PASSWD_NOSAVE;
169   if (strstr(buf, " -nosetpassword"))
170     options->passwdbits &= ~PASSWD_SET;
171   if (strstr(buf, " -setpassword"))
172     options->passwdbits |= PASSWD_SET;
173   
174   /* transports */
175   if (strstr(buf, " -transall"))
176     options->transports = AFPTRANS_ALL;
177   if (strstr(buf, " -notransall"))
178     options->transports = AFPTRANS_NONE;
179   if (strstr(buf, " -tcp"))
180     options->transports |= AFPTRANS_TCP;
181   if (strstr(buf, " -notcp"))
182     options->transports &= ~AFPTRANS_TCP;
183   if (strstr(buf, " -ddp"))
184     options->transports |= AFPTRANS_DDP;
185   if (strstr(buf, " -noddp"))
186     options->transports &= ~AFPTRANS_DDP;
187
188   /* figure out options w/ values. currently, this will ignore the setting
189    * if memory is lacking. */
190   if ((c = getoption(buf, "-defaultvol")) && (opt = strdup(c)))
191     options->defaultvol = opt;
192   if ((c = getoption(buf, "-systemvol")) && (opt = strdup(c)))
193     options->systemvol = opt;
194   if ((c = getoption(buf, "-loginmesg")) && (opt = strdup(c)))
195     options->loginmesg = opt;
196   if ((c = getoption(buf, "-guestname")) && (opt = strdup(c)))
197     options->guest = opt;
198   if ((c = getoption(buf, "-passwdfile")) && (opt = strdup(c)))
199     options->passwdfile = opt;
200   if ((c = getoption(buf, "-passwdminlen")))
201     options->passwdminlen = MIN(1, atoi(c));
202   if ((c = getoption(buf, "-loginmaxfail")))
203     options->loginmaxfail = atoi(c);
204   if ((c = getoption(buf, "-tickleval")))
205     options->tickleval = atoi(c);
206
207   if ((c = getoption(buf, "-server_quantum")))
208     options->server_quantum = strtoul(c, NULL, 0);
209
210
211 #ifdef ADMIN_GRP
212   if ((c = getoption(buf, "-admingroup"))) {
213     struct group *gr = getgrnam(c);
214     if (gr != NULL) { 
215       options->admingid = gr->gr_gid;
216     }
217   } 
218 #endif
219
220   if ((c = getoption(buf, "-authprintdir")) && (opt = strdup(c)))
221     options->authprintdir = opt;
222   if ((c = getoption(buf, "-uampath")) && (opt = strdup(c)))
223     options->uampath = opt;
224   if ((c = getoption(buf, "-uamlist")) && (opt = strdup(c)))
225     options->uamlist = opt;
226   if ((c = getoption(buf, "-nlspath")) && (opt = strdup(c)))
227     options->nlspath = opt;
228
229   if ((c = getoption(buf, "-ipaddr"))) {
230     struct in_addr inaddr;
231     if (inet_aton(c, &inaddr) && (opt = strdup(c))) { 
232       if (!gethostbyaddr((const char *) &inaddr, sizeof(inaddr), AF_INET)) 
233         syslog(LOG_INFO, "WARNING: can't find %s\n", opt);
234       options->ipaddr = opt;
235     }
236   }
237
238   if ((c = getoption(buf, "-port")))
239     options->port = atoi(c);
240   if ((c = getoption(buf, "-ddpaddr")))
241     atalk_aton(c, &options->ddpaddr);
242
243   /* do a little checking for the domain name. */
244   if ((c = getoption(buf, "-fqdn"))) {
245     char *p = strchr(c, ':');
246     if (p)
247       *p = '\0';
248     if (gethostbyname(c)) {
249       if (p)
250         *p = ':';
251       if ((opt = strdup(c)))
252         options->fqdn = opt;
253     }
254   }
255
256   return 1;
257 }
258
259 int afp_options_parse(int ac, char **av, struct afp_options *options)
260 {
261   extern char *optarg;
262   extern int optind;
263   
264   char *p;
265   int c, err = 0;
266
267   if (gethostname(options->hostname, sizeof(options->hostname )) < 0 ) {
268     perror( "gethostname" );
269     return 0;
270   }
271     if (( p = strchr(options->hostname, '.' )) != 0 ) {
272         *p = '\0';
273     }
274
275     if (( p = strrchr( av[ 0 ], '/' )) == NULL ) {
276         p = av[ 0 ];
277     } else {
278         p++;
279     }
280
281     while (( c = getopt( ac, av, OPTIONS )) != EOF ) {
282         switch ( c ) {
283         case 'd' :
284             options->flags |= OPTION_DEBUG;
285             break;
286         case 'n' :
287             options->server = optarg;
288             break;
289         case 'f' :
290             options->defaultvol = optarg;
291             break;
292         case 's' :
293             options->systemvol = optarg;
294             break;
295         case 'u' :
296             options->flags |= OPTION_USERVOLFIRST;
297             break;
298         case 'c' :
299             options->connections = atoi( optarg );
300             break;
301         case 'g' :
302             options->guest = optarg;
303             break;
304
305         case 'P' :
306             options->pidfile = optarg;
307             break;
308
309         case 'p':
310             options->passwdbits |= PASSWD_NOSAVE;
311             break;
312         case 't':
313             options->passwdbits |= PASSWD_SET;
314             break;
315
316         case 'D':
317             options->transports &= ~AFPTRANS_DDP;
318             break;
319         case 'S':
320             options->port = atoi(optarg);
321             break;
322         case 'T':
323             options->transports &= ~AFPTRANS_TCP;
324             break;
325         case 'L':
326             options->loginmesg = optarg;
327             break;
328         case 'F':
329             options->configfile = optarg;
330             break;
331         case 'U':
332             options->uamlist = optarg;
333             break;
334         case 'I':
335             options->flags |= OPTION_CUSTOMICON;
336         default :
337             err++;
338         }
339     }
340     if ( err || optind != ac ) {
341         fprintf( stderr,
342                 "Usage:\t%s [ -dpDTIt ] [ -n nbpname ] [ -f defvols ] \
343 [ -P pidfile ] [ -s sysvols ] \n", p );
344         fprintf( stderr,
345                 "\t[ -u ] [ -c maxconn ] [ -g guest ] \
346 [ -S port ] [ -L loginmesg ] [ -F configfile ] [ -U uamlist ]\n" );
347         return 0;
348     }
349
350 #ifdef ultrix
351     openlog( p, LOG_PID );
352 #else ultrix
353     openlog( p, LOG_NDELAY|LOG_PID, LOG_DAEMON);
354 #endif ultrix
355
356     return 1;
357 }