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