]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
Warning fixes.
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.15 2003-06-09 14:42:40 srittau Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <atalk/logger.h>
18
19 #ifdef BSD4_4
20 #include <sys/param.h>
21 #ifndef USE_GETHOSTID
22 #include <sys/sysctl.h>
23 #endif /* USE_GETHOSTID */
24 #endif /* BSD4_4 */
25
26 #include <netatalk/at.h>
27 #include <netatalk/endian.h>
28 #include <atalk/dsi.h>
29 #include <atalk/atp.h>
30 #include <atalk/asp.h>
31 #include <atalk/nbp.h>
32
33 #include "globals.h"  /* includes <netdb.h> */
34 #include "status.h"
35 #include "afp_config.h"
36 #include "icon.h"
37
38 static void status_flags(char *data, const int notif, const int ipok,
39                          const unsigned char passwdbits, const int dirsrvcs)
40 {
41     u_int16_t           status;
42
43     status = AFPSRVRINFO_COPY;
44     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
45         status |= AFPSRVRINFO_PASSWD;
46     if (passwdbits & PASSWD_NOSAVE)
47         status |= AFPSRVRINFO_NOSAVEPASSWD;
48     status |= AFPSRVRINFO_SRVSIGNATURE;
49     /* only advertise tcp/ip if we have a valid address */
50     if (ipok)
51         status |= AFPSRVRINFO_TCPIP;
52     status |= AFPSRVRINFO_SRVMSGS;
53     /* Allow the user to decide if we should support server notifications.
54      * With this turned off, the clients will poll for directory changes every
55      * 10 seconds.  This might be too costly to network resources, so make
56      * this an optional thing.  Default will be to _not_ support server
57      * notifications. */
58     if (notif) {
59         status |= AFPSRVRINFO_SRVNOTIFY;
60     }
61     status |= AFPSRVRINFO_FASTBOZO;
62     if (dirsrvcs)
63         status |= AFPSRVRINFO_SRVRDIR;
64     status = htons(status);
65     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
66 }
67
68 static int status_server(char *data, const char *server)
69 {
70     char                *start = data;
71     char                *Obj, *Type, *Zone;
72     u_int16_t           status;
73     int                 len;
74
75     /* make room for all offsets before server name */
76     data += AFPSTATUS_PRELEN;
77
78     /* extract the obj part of the server */
79     Obj = (char *) server;
80     nbp_name(server, &Obj, &Type, &Zone);
81     len = strlen(Obj);
82     *data++ = len;
83     memcpy( data, Obj, len );
84     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
85         len++;
86     data += len;
87
88     /* Make room for signature and net address offset. Save location of
89      * signature offset. We're also making room for directory names offset
90      * and the utf-8 server name offset.
91      *
92      * NOTE: Technically, we don't need to reserve space for the
93      * signature and net address offsets if they're not going to be
94      * used. As there are no offsets after them, it doesn't hurt to
95      * have them specified though. So, we just do that to simplify
96      * things.  
97      *
98      * NOTE2: AFP3.1 Documentation states that the directory names offset
99      * is a required feature, even though it can be set to zero.
100      */
101     len = data - start;
102     status = htons(len + AFPSTATUS_POSTLEN);
103     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
104     return len; /* return the offset to beginning of signature offset */
105 }
106
107 static void status_machine(char *data)
108 {
109     char                *start = data;
110     u_int16_t           status;
111     int                 len;
112 #ifdef AFS
113     const char          *machine = "afs";
114 #else /* !AFS */
115     const char          *machine = "unix";
116 #endif /* AFS */
117
118     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
119     data += ntohs( status );
120     len = strlen( machine );
121     *data++ = len;
122     memcpy( data, machine, len );
123     data += len;
124     status = htons(data - start);
125     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
126 }
127
128
129 /* server signature is a 16-byte quantity */
130 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
131                                   const struct afp_options *options)
132 {
133     char                 *status;
134     char                 *usersign;
135     int                  i;
136     u_int16_t            offset, sigoff;
137     long                 hostid;
138     static int           id = 0;
139 #ifdef BSD4_4
140     int                  mib[2];
141     size_t               len;
142 #endif /* BSD4_4 */
143
144     status = data;
145
146     /* get server signature offset */
147     memcpy(&offset, data + *servoffset, sizeof(offset));
148     sigoff = offset = ntohs(offset);
149
150     /* jump to server signature offset */
151     data += offset;
152
153     /* Signature type is user string */
154     if (strncmp(options->signature, "user", 4) == 0) {
155         if (strlen(options->signature) <= 5) {
156             LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
157                             options->signature);
158             goto server_signature_hostid;
159     }
160     usersign = options->signature + 5;
161         if (strlen(usersign) < 3) 
162             LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
163                             options->signature);
164     
165         memset(data, 0, 16);
166         strncpy(data, usersign, 16);
167         data += 16;
168         goto server_signature_done;
169     } /* signature = user */
170
171     /* If signature type is a standard hostid... */
172 server_signature_hostid:
173     /* 16-byte signature consists of copies of the hostid */
174 #if defined(BSD4_4) && defined(USE_GETHOSTID)
175     mib[0] = CTL_KERN;
176     mib[1] = KERN_HOSTID;
177     len = sizeof(hostid);
178     sysctl(mib, 2, &hostid, &len, NULL, 0);
179 #else /* BSD4_4 && USE_GETHOSTID */
180     hostid = gethostid();
181 #endif /* BSD4_4 && USE_GETHOSTID */
182     if (!hostid) {
183         if (dsi)
184             hostid = dsi->server.sin_addr.s_addr;
185         else {
186             struct hostent *host;
187
188             if ((host = gethostbyname(options->hostname)))
189                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
190         }
191     }
192
193     /* it turns out that a server signature screws up separate
194      * servers running on the same machine. to work around that, 
195      * i add in an increment */
196     hostid += id;
197     id++;
198     for (i = 0; i < 16; i += sizeof(hostid)) {
199         memcpy(data, &hostid, sizeof(hostid));
200         data += sizeof(hostid);
201      }
202
203 server_signature_done:
204     /* calculate net address offset */
205     *servoffset += sizeof(offset);
206     offset = htons(data - status);
207     memcpy(status + *servoffset, &offset, sizeof(offset));
208     return sigoff;
209 }
210
211 static int status_netaddress(char *data, int *servoffset,
212                              const ASP asp, const DSI *dsi,
213                              const char *fqdn)
214 {
215     char               *begin;
216     u_int16_t          offset;
217
218     begin = data;
219
220     /* get net address offset */
221     memcpy(&offset, data + *servoffset, sizeof(offset));
222     data += ntohs(offset);
223
224     /* format:
225        Address count (byte) 
226        len (byte = sizeof(length + address type + address) 
227        address type (byte, ip address = 0x01, ip + port = 0x02,
228                            ddp address = 0x03, fqdn = 0x04) 
229        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
230        */
231
232     /* number of addresses. this currently screws up if we have a dsi
233        connection, but we don't have the ip address. to get around this,
234        we turn off the status flag for tcp/ip. */
235     *data++ = ((fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
236
237     /* handle DNS names */
238     if (fqdn && dsi) {
239         int len = strlen(fqdn);
240         *data++ = len +2;
241         *data++ = 0x04;
242         memcpy(data, fqdn, len);
243         data += len;
244     }
245
246     /* ip address */
247     if (dsi) {
248         const struct sockaddr_in *inaddr = &dsi->server;
249
250         if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
251             *data++ = 6; /* length */
252             *data++ = 0x01; /* basic ip address */
253             memcpy(data, &inaddr->sin_addr.s_addr,
254                    sizeof(inaddr->sin_addr.s_addr));
255             data += sizeof(inaddr->sin_addr.s_addr);
256         } else {
257             /* ip address + port */
258             *data++ = 8;
259             *data++ = 0x02; /* ip address with port */
260             memcpy(data, &inaddr->sin_addr.s_addr,
261                    sizeof(inaddr->sin_addr.s_addr));
262             data += sizeof(inaddr->sin_addr.s_addr);
263             memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
264             data += sizeof(inaddr->sin_port);
265         }
266     }
267
268 #ifndef NO_DDP
269     if (asp) {
270         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
271
272         /* ddp address */
273         *data++ = 6;
274         *data++ = 0x03; /* ddp address */
275         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
276         data += sizeof(ddpaddr->sat_addr.s_net);
277         memcpy(data, &ddpaddr->sat_addr.s_node,
278                sizeof(ddpaddr->sat_addr.s_node));
279         data += sizeof(ddpaddr->sat_addr.s_node);
280         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
281         data += sizeof(ddpaddr->sat_port);
282     }
283 #endif /* ! NO_DDP */
284
285     /* calculate/store Directory Services Names offset */
286     offset = htons(data - begin); 
287     *servoffset += sizeof(offset);
288     memcpy(begin + *servoffset, &offset, sizeof(offset));
289
290     /* return length of buffer */
291     return (data - begin);
292 }
293
294 static int status_directorynames(char *data, int *diroffset, 
295                                  const DSI *dsi, 
296                                  const struct afp_options *options)
297 {
298     char *begin = data;
299     u_int16_t offset;
300     memcpy(&offset, data + *diroffset, sizeof(offset));
301     offset = ntohs(offset);
302     data += offset;
303
304     /* I can not find documentation of any other purpose for the
305      * DirectoryNames field.
306      */
307     /*
308      * Try to synthesize a principal:
309      * service '/' fqdn '@' realm
310      */
311     if (options->k5service && options->k5realm && options->fqdn) {
312         /* should k5princ be utf8 encoded? */
313         u_int8_t len;
314         char *p = strchr( options->fqdn, ':' );
315         if (p) 
316             *p = '\0';
317         len = strlen( options->k5service ) 
318                         + strlen( options->fqdn )
319                         + strlen( options->k5realm );
320         len+=2; /* '/' and '@' */
321         *data++ = 1; /* DirectoryNamesCount */
322         *data++ = len;
323         snprintf( data, len + 1, "%s/%s@%s", options->k5service,
324                                 options->fqdn, options->k5realm );
325         data += len;
326         if (p)
327             *p = ':';
328     } else {
329         memset(begin + *diroffset, 0, sizeof(offset));
330     }
331
332     /* Calculate and store offset for UTF8ServerName */
333     *diroffset += sizeof(u_int16_t);
334     offset = htons(data - begin);
335     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
336
337     /* return length of buffer */
338     return (data - begin);
339 }
340
341 static int status_utf8servername(char *data, int *nameoffset,
342                                  const DSI *dsi,
343                                  const struct afp_options *options)
344 {
345     char *begin = data;
346     u_int16_t offset;
347
348     memcpy(&offset, data + *nameoffset, sizeof(offset));
349     offset = ntohs(offset);
350     data += offset;
351
352     /* FIXME: For now we set the UTF-8 ServerName offset to 0
353      * It's irrelevent anyway until we set the appropriate Flags value.
354      * Later this can be set to something meaningful.
355      *
356      * What is the valid character range for an nbpname?
357      *
358      * Apple's server likes to use the non-qualified hostname
359      * This obviously won't work very well if multiple servers are running
360      * on the box.
361      */
362     memset(begin + *nameoffset, 0, sizeof(offset));
363
364     /* return length of buffer */
365     return (data - begin);
366
367 }
368
369 /* returns actual offset to signature */
370 static void status_icon(char *data, const unsigned char *icondata,
371                         const int iconlen, const int sigoffset)
372 {
373     char                *start = data;
374     char                *sigdata = data + sigoffset;
375     u_int16_t           ret, status;
376
377     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
378     if ( icondata == NULL ) {
379         ret = status;
380         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
381     } else {
382         data += ntohs( status );
383         memcpy( data, icondata, iconlen);
384         data += iconlen;
385         ret = htons(data - start);
386     }
387
388     /* put in signature offset */
389     if (sigoffset)
390         memcpy(sigdata, &ret, sizeof(ret));
391 }
392
393 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
394                  const struct afp_options *options)
395 {
396     ASP asp;
397     DSI *dsi;
398     char *status;
399     int statuslen, c, sigoff;
400
401     if (!(aspconfig || dsiconfig) || !options)
402         return;
403
404     if (aspconfig) {
405         asp = aspconfig->obj.handle;
406         status = aspconfig->status;
407     } else
408         asp = NULL;
409
410     if (dsiconfig) {
411         dsi = dsiconfig->obj.handle;
412         if (!aspconfig)
413             status = dsiconfig->status;
414     } else
415         dsi = NULL;
416
417     /*
418      * These routines must be called in order -- earlier calls
419      * set the offsets for later calls.
420      *
421      * using structs is a bad idea, but that's what the original code
422      * does. solaris, in fact, will segfault with them. so, now
423      * we just use the powers of #defines and memcpy.
424      *
425      * reply block layout (offsets are 16-bit quantities):
426      * machine type offset -> AFP version count offset ->
427      * UAM count offset -> vol icon/mask offset -> flags ->
428      *
429      * server name [padded to even boundary] -> signature offset ->
430      * network address offset ->
431      *
432      * at the appropriate offsets:
433      * machine type, afp versions, uams, server signature
434      * (16-bytes), network addresses, volume icon/mask 
435      */
436
437     status_flags(status, options->server_notif, options->fqdn ||
438                  (dsiconfig && dsi->server.sin_addr.s_addr),
439                  options->passwdbits, 
440                  (options->k5service && options->k5realm && options->fqdn));
441     /* returns offset to signature offset */
442     c = status_server(status, options->server ? options->server :
443                       options->hostname);
444     status_machine(status);
445     status_versions(status);
446     status_uams(status, options->uamlist);
447     if (options->flags & OPTION_CUSTOMICON)
448         status_icon(status, icon, sizeof(icon), c);
449     else
450         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
451
452     sigoff = status_signature(status, &c, dsi, options);
453     /* c now contains the offset where the netaddress offset lives */
454
455     status_netaddress(status, &c, asp, dsi, options->fqdn);
456     /* c now contains the offset where the Directory Names Count offset lives */
457
458     status_directorynames(status, &c, dsi, options);
459     /* c now contains the offset where the UTF-8 ServerName offset lives */
460
461     statuslen = status_utf8servername(status, &c, dsi, options);
462
463
464 #ifndef NO_DDP
465     if (aspconfig) {
466         asp_setstatus(asp, status, statuslen);
467         aspconfig->signature = status + sigoff;
468         aspconfig->statuslen = statuslen;
469     }
470 #endif /* ! NO_DDP */
471
472     if (dsiconfig) {
473         if (aspconfig) { /* copy to dsiconfig */
474             memcpy(dsiconfig->status, status, ATP_MAXDATA);
475             status = dsiconfig->status;
476         }
477
478         if ((options->flags & OPTION_CUSTOMICON) == 0) {
479             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
480         }
481         dsi_setstatus(dsi, status, statuslen);
482         dsiconfig->signature = status + sigoff;
483         dsiconfig->statuslen = statuslen;
484     }
485 }
486
487 /* this is the same as asp/dsi_getstatus */
488 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
489 AFPObj      *obj;
490 char    *ibuf, *rbuf;
491 int             ibuflen, *rbuflen;
492 {
493     AFPConfig *config = obj->config;
494
495     memcpy(rbuf, config->status, config->statuslen);
496     *rbuflen = config->statuslen;
497     return AFP_OK;
498 }