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