]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
don't create an adouble file in ad_metadata for noadouble volume
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.30 2009-11-23 19:04:14 franklahm 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 <sys/socket.h>
18 #include <atalk/logger.h>
19
20 #ifdef BSD4_4
21 #include <sys/param.h>
22 #ifndef HAVE_GETHOSTID
23 #include <sys/sysctl.h>
24 #endif /* HAVE_GETHOSTID */
25 #endif /* BSD4_4 */
26
27 #include <netatalk/at.h>
28 #include <netatalk/endian.h>
29 #include <atalk/dsi.h>
30 #include <atalk/atp.h>
31 #include <atalk/asp.h>
32 #include <atalk/nbp.h>
33 #include <atalk/unicode.h>
34
35 #include "globals.h"  /* includes <netdb.h> */
36 #include "status.h"
37 #include "afp_config.h"
38 #include "icon.h"
39
40 static   size_t maxstatuslen = 0;
41
42 static void status_flags(char *data, const int notif, const int ipok,
43                          const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
44 {
45     u_int16_t           status;
46
47     status = AFPSRVRINFO_COPY;
48     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
49         status |= AFPSRVRINFO_PASSWD;
50     if (passwdbits & PASSWD_NOSAVE)
51         status |= AFPSRVRINFO_NOSAVEPASSWD;
52     status |= AFPSRVRINFO_SRVSIGNATURE;
53     /* only advertise tcp/ip if we have a valid address */
54     if (ipok)
55         status |= AFPSRVRINFO_TCPIP;
56     status |= AFPSRVRINFO_SRVMSGS;
57     /* Allow the user to decide if we should support server notifications.
58      * With this turned off, the clients will poll for directory changes every
59      * 10 seconds.  This might be too costly to network resources, so make
60      * this an optional thing.  Default will be to _not_ support server
61      * notifications. */
62     if (notif) {
63         status |= AFPSRVRINFO_SRVNOTIFY;
64     }
65     status |= AFPSRVRINFO_FASTBOZO;
66     status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
67     /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
68
69     if (flags & OPTION_UUID)    /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
70         status |= AFPSRVRINFO_UUID;
71
72     status = htons(status);
73     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
74 }
75
76 static int status_server(char *data, const char *server, const struct afp_options *options)
77 {
78     char                *start = data;
79     char                *Obj, *Type, *Zone;
80     char                buf[32];
81     u_int16_t           status;
82     size_t              len;
83
84     /* make room for all offsets before server name */
85     data += AFPSTATUS_PRELEN;
86
87     /* extract the obj part of the server */
88     Obj = (char *) server;
89     nbp_name(server, &Obj, &Type, &Zone);
90     if ((size_t)-1 == (len = convert_string( 
91                         options->unixcharset, options->maccharset, 
92                         Obj, -1, buf, sizeof(buf))) ) {
93         len = MIN(strlen(Obj), 31);
94         *data++ = len;
95         memcpy( data, Obj, len );
96         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
97     } else {
98         *data++ = len;
99         memcpy( data, buf, len );
100     }
101     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
102         len++;
103     data += len;
104
105     /* make room for signature and net address offset. save location of
106      * signature offset. we're also making room for directory names offset
107      * and the utf-8 server name offset.
108      *
109      * NOTE: technically, we don't need to reserve space for the
110      * signature and net address offsets if they're not going to be
111      * used. as there are no offsets after them, it doesn't hurt to
112      * have them specified though. so, we just do that to simplify
113      * things.  
114      *
115      * NOTE2: AFP3.1 Documentation states that the directory names offset
116      * is a required feature, even though it can be set to zero.
117      */
118     len = data - start;
119     status = htons(len + AFPSTATUS_POSTLEN);
120     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
121     return len; /* return the offset to beginning of signature offset */
122 }
123
124 static void status_machine(char *data)
125 {
126     char                *start = data;
127     u_int16_t           status;
128     int                 len;
129 #ifdef AFS
130     const char          *machine = "afs";
131 #else /* !AFS */
132     const char          *machine = "Netatalk";
133 #endif /* AFS */
134
135     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
136     data += ntohs( status );
137     len = strlen( machine );
138     *data++ = len;
139     memcpy( data, machine, len );
140     data += len;
141     status = htons(data - start);
142     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
143 }
144
145 /* -------------------------------- 
146  * it turns out that a server signature screws up separate
147  * servers running on the same machine. to work around that, 
148  * i add in an increment.
149  * Not great, server signature are config dependent but well.
150  */
151  
152 static int           Id = 0;
153
154 /* server signature is a 16-byte quantity */
155 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
156                                   const struct afp_options *options)
157 {
158     char                 *status;
159     char                 *usersign;
160     int                  i;
161     u_int16_t            offset, sigoff;
162     long                 hostid;
163 #ifdef BSD4_4
164     int                  mib[2];
165     size_t               len;
166 #endif /* BSD4_4 */
167
168     status = data;
169
170     /* get server signature offset */
171     memcpy(&offset, data + *servoffset, sizeof(offset));
172     sigoff = offset = ntohs(offset);
173
174     /* jump to server signature offset */
175     data += offset;
176
177     /* Signature type is user string */
178     if (strncmp(options->signature, "user", 4) == 0) {
179         if (strlen(options->signature) <= 5) {
180             LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
181                             options->signature);
182             goto server_signature_hostid;
183     }
184     usersign = options->signature + 5;
185         if (strlen(usersign) < 3) 
186             LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
187                             options->signature);
188     
189         memset(data, 0, 16);
190         strncpy(data, usersign, 16);
191         data += 16;
192         goto server_signature_done;
193     } /* signature = user */
194
195     /* If signature type is a standard hostid... */
196 server_signature_hostid:
197     /* 16-byte signature consists of copies of the hostid */
198 #if defined(BSD4_4) && !defined(HAVE_GETHOSTID)
199     mib[0] = CTL_KERN;
200     mib[1] = KERN_HOSTID;
201     len = sizeof(hostid);
202     sysctl(mib, 2, &hostid, &len, NULL, 0);
203 #else /* BSD4_4 && !HAVE_GETHOSTID */
204     hostid = gethostid();
205 #endif /* BSD4_4 && !HAVE_GETHOSTID */
206     if (!hostid) {
207         if (dsi) {
208             if (dsi->server.ss_family == AF_INET) { /* IPv4 */
209                 hostid = ((struct sockaddr_in *)(&dsi->server))->sin_addr.s_addr;
210             } else {                                /* IPv6 */
211                 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
212                 /* Use the last "sizeof(long) bytes of the IPv6 addr */
213                 memcpy(&hostid, sa6->sin6_addr.s6_addr + (16 - sizeof(long)), sizeof(long));
214             }
215         } else {
216             struct hostent *host;
217
218             if ((host = gethostbyname(options->hostname)))
219                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
220         }
221     }
222
223     /* it turns out that a server signature screws up separate
224      * servers running on the same machine. to work around that, 
225      * i add in an increment */
226     hostid += Id;
227     Id++;
228     for (i = 0; i < 16; i += sizeof(hostid)) {
229         memcpy(data, &hostid, sizeof(hostid));
230         data += sizeof(hostid);
231      }
232
233 server_signature_done:
234     /* calculate net address offset */
235     *servoffset += sizeof(offset);
236     offset = htons(data - status);
237     memcpy(status + *servoffset, &offset, sizeof(offset));
238     return sigoff;
239 }
240
241 static size_t status_netaddress(char *data, int *servoffset,
242                              const ASP asp, const DSI *dsi,
243                              const struct afp_options *options)
244 {
245     char               *begin;
246     u_int16_t          offset;
247     size_t             addresses_len = 0;
248
249     begin = data;
250
251     /* get net address offset */
252     memcpy(&offset, data + *servoffset, sizeof(offset));
253     data += ntohs(offset);
254
255     /* format:
256        Address count (byte) 
257        len (byte = sizeof(length + address type + address) 
258        address type (byte, ip address = 0x01, ip + port = 0x02,
259                            ddp address = 0x03, fqdn = 0x04) 
260        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
261        */
262
263     /* number of addresses. this currently screws up if we have a dsi
264        connection, but we don't have the ip address. to get around this,
265        we turn off the status flag for tcp/ip. */
266     *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
267               (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
268
269     /* ip address */
270     if (dsi) {
271         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
272             const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
273             if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
274                 *data++ = 6; /* length */
275                 *data++ = 0x01; /* basic ip address */
276                 memcpy(data, &inaddr->sin_addr.s_addr,
277                        sizeof(inaddr->sin_addr.s_addr));
278                 data += sizeof(inaddr->sin_addr.s_addr);
279                 addresses_len += 7;
280             } else {
281                 /* ip address + port */
282                 *data++ = 8;
283                 *data++ = 0x02; /* ip address with port */
284                 memcpy(data, &inaddr->sin_addr.s_addr,
285                        sizeof(inaddr->sin_addr.s_addr));
286                 data += sizeof(inaddr->sin_addr.s_addr);
287                 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
288                 data += sizeof(inaddr->sin_port);
289                 addresses_len += 9;
290             }
291         } else { /* IPv6 */
292             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
293             if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
294                 *data++ = 18; /* length */
295                 *data++ = 6; /* type */
296                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
297                 data += sizeof(sa6->sin6_addr.s6_addr);
298                 addresses_len += 19;
299             } else {
300                 /* ip address + port */
301                 *data++ = 20; /* length */
302                 *data++ = 7; /* type*/
303                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
304                 data += sizeof(sa6->sin6_addr.s6_addr);
305                 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
306                 data += sizeof(sa6->sin6_port);
307                 addresses_len += 21;
308             }
309
310         }
311     }
312
313     /* handle DNS names */
314     if (options->fqdn && dsi) {
315         size_t len = strlen(options->fqdn);
316         if ( len + 2 + addresses_len < maxstatuslen - offset) {
317             *data++ = len +2;
318             *data++ = 0x04;
319             memcpy(data, options->fqdn, len);
320             data += len;
321             addresses_len += len+2;
322         }
323
324         /* Annouce support for SSH tunneled AFP session, 
325          * this feature is available since 10.3.2.
326          * According to the specs (AFP 3.1 p.225) this should
327          * be an IP+Port style value, but it only works with 
328          * a FQDN. OSX Server uses FQDN as well.
329          */
330         if ( len + 2 + addresses_len < maxstatuslen - offset) {
331             if (options->flags & OPTION_ANNOUNCESSH) {
332                 *data++ = len +2;
333                 *data++ = 0x05;
334                 memcpy(data, options->fqdn, len);
335                 data += len;
336             }
337         }
338     }
339
340 #ifndef NO_DDP
341     if (asp) {
342         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
343
344         /* ddp address */
345         *data++ = 6;
346         *data++ = 0x03; /* ddp address */
347         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
348         data += sizeof(ddpaddr->sat_addr.s_net);
349         memcpy(data, &ddpaddr->sat_addr.s_node,
350                sizeof(ddpaddr->sat_addr.s_node));
351         data += sizeof(ddpaddr->sat_addr.s_node);
352         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
353         data += sizeof(ddpaddr->sat_port);
354     }
355 #endif /* ! NO_DDP */
356
357     /* calculate/store Directory Services Names offset */
358     offset = htons(data - begin); 
359     *servoffset += sizeof(offset);
360     memcpy(begin + *servoffset, &offset, sizeof(offset));
361
362     /* return length of buffer */
363     return (data - begin);
364 }
365
366 static size_t status_directorynames(char *data, int *diroffset, 
367                                  const DSI *dsi _U_, 
368                                  const struct afp_options *options)
369 {
370     char *begin = data;
371     u_int16_t offset;
372     memcpy(&offset, data + *diroffset, sizeof(offset));
373     offset = ntohs(offset);
374     data += offset;
375
376     /* I can not find documentation of any other purpose for the
377      * DirectoryNames field.
378      */
379     /*
380      * Try to synthesize a principal:
381      * service '/' fqdn '@' realm
382      */
383     if (options->k5service && options->k5realm && options->fqdn) {
384         /* should k5princ be utf8 encoded? */
385         size_t len;
386         char *p = strchr( options->fqdn, ':' );
387         if (p) 
388             *p = '\0';
389         len = strlen( options->k5service ) 
390                         + strlen( options->fqdn )
391                         + strlen( options->k5realm );
392         len+=2; /* '/' and '@' */
393         if ( len > 255 || len+2 > maxstatuslen - offset) {
394             *data++ = 0;
395             LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
396         }        
397         else {
398             *data++ = 1; /* DirectoryNamesCount */
399             *data++ = len;
400             snprintf( data, len + 1, "%s/%s@%s", options->k5service,
401                                 options->fqdn, options->k5realm );
402             data += len;
403             if (p)
404                 *p = ':';
405        }
406     } else {
407         *data++ = 0;
408     }
409
410     /* Calculate and store offset for UTF8ServerName */
411     *diroffset += sizeof(u_int16_t);
412     offset = htons(data - begin);
413     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
414
415     /* return length of buffer */
416     return (data - begin);
417 }
418
419 static size_t status_utf8servername(char *data, int *nameoffset,
420                                  const DSI *dsi _U_,
421                                  const struct afp_options *options)
422 {
423     char *Obj, *Type, *Zone;
424     u_int16_t namelen;
425     size_t len;
426     char *begin = data;
427     u_int16_t offset, status;
428
429     memcpy(&offset, data + *nameoffset, sizeof(offset));
430     offset = ntohs(offset);
431     data += offset;
432
433     /* FIXME:
434      * What is the valid character range for an nbpname?
435      *
436      * Apple's server likes to use the non-qualified hostname
437      * This obviously won't work very well if multiple servers are running
438      * on the box.
439      */
440
441     /* extract the obj part of the server */
442     Obj = (char *) (options->server ? options->server : options->hostname);
443     nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
444
445     if ((size_t) -1 == (len = convert_string (
446                                         options->unixcharset, CH_UTF8_MAC, 
447                                         Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
448         LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
449
450         /* set offset to 0 */
451         memset(begin + *nameoffset, 0, sizeof(offset));
452         data = begin + offset;
453     }
454     else {
455         namelen = htons(len);
456         memcpy( data, &namelen, sizeof(namelen));
457         data += sizeof(namelen);
458         data += len;
459         offset = htons(offset);
460         memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
461         
462         /* Now set the flag ... */
463         memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
464         status = ntohs(status);
465         status |= AFPSRVRINFO_SRVUTF8;
466         status = htons(status);
467         memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
468     }
469
470     /* return length of buffer */
471     return (data - begin);
472
473 }
474
475 /* returns actual offset to signature */
476 static void status_icon(char *data, const unsigned char *icondata,
477                         const int iconlen, const int sigoffset)
478 {
479     char                *start = data;
480     char                *sigdata = data + sigoffset;
481     u_int16_t           ret, status;
482
483     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
484     if ( icondata == NULL ) {
485         ret = status;
486         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
487     } else {
488         data += ntohs( status );
489         memcpy( data, icondata, iconlen);
490         data += iconlen;
491         ret = htons(data - start);
492     }
493
494     /* put in signature offset */
495     if (sigoffset)
496         memcpy(sigdata, &ret, sizeof(ret));
497 }
498
499 /* ---------------------
500 */
501 void status_reset(void)
502 {
503     Id = 0;
504 }
505
506
507 /* ---------------------
508  */
509 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
510                  const struct afp_options *options)
511 {
512     ASP asp;
513     DSI *dsi;
514     char *status = NULL;
515     size_t statuslen;
516     int c, sigoff, ipok;
517
518     if (!(aspconfig || dsiconfig) || !options)
519         return;
520
521     if (aspconfig) {
522         status = aspconfig->status;
523         maxstatuslen=sizeof(aspconfig->status);
524         asp = aspconfig->obj.handle;
525     } else
526         asp = NULL;
527         
528     ipok = 0;
529     if (dsiconfig) {
530         status = dsiconfig->status;
531         maxstatuslen=sizeof(dsiconfig->status);
532         dsi = dsiconfig->obj.handle;
533         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
534             const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
535             ipok = sa4->sin_addr.s_addr ? 1 : 0;
536         } else { /* IPv6 */
537             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
538             for (int i=0; i<16; i++) {
539                 if (sa6->sin6_addr.s6_addr[i]) {
540                     ipok = 1;
541                     break;
542                 }
543             }
544         }
545     } else
546         dsi = NULL;
547
548     /*
549      * These routines must be called in order -- earlier calls
550      * set the offsets for later calls.
551      *
552      * using structs is a bad idea, but that's what the original code
553      * does. solaris, in fact, will segfault with them. so, now
554      * we just use the powers of #defines and memcpy.
555      *
556      * reply block layout (offsets are 16-bit quantities):
557      * machine type offset -> AFP version count offset ->
558      * UAM count offset -> vol icon/mask offset -> flags ->
559      *
560      * server name [padded to even boundary] -> signature offset ->
561      * network address offset ->
562      *
563      * at the appropriate offsets:
564      * machine type, afp versions, uams, server signature
565      * (16-bytes), network addresses, volume icon/mask 
566      */
567
568     status_flags(status,
569                  options->server_notif,
570                  (options->fqdn || ipok),
571                  options->passwdbits, 
572                  (options->k5service && options->k5realm && options->fqdn),
573                  options->flags);
574     /* returns offset to signature offset */
575     c = status_server(status, options->server ? options->server :
576                       options->hostname, options);
577     status_machine(status);
578     status_versions(status, asp, dsi);
579     status_uams(status, options->uamlist);
580     if (options->flags & OPTION_CUSTOMICON)
581         status_icon(status, icon, sizeof(icon), c);
582     else
583         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
584
585     sigoff = status_signature(status, &c, dsi, options);
586     /* c now contains the offset where the netaddress offset lives */
587
588     status_netaddress(status, &c, asp, dsi, options);
589     /* c now contains the offset where the Directory Names Count offset lives */
590
591     statuslen = status_directorynames(status, &c, dsi, options);
592     /* c now contains the offset where the UTF-8 ServerName offset lives */
593
594     if ( statuslen < maxstatuslen) 
595         statuslen = status_utf8servername(status, &c, dsi, options);
596
597 #ifndef NO_DDP
598     if (aspconfig) {
599         if (dsiconfig) /* status is dsiconfig->status */
600             memcpy(aspconfig->status, status, statuslen);
601         asp_setstatus(asp, status, statuslen);
602         aspconfig->signature = status + sigoff;
603         aspconfig->statuslen = statuslen;
604     }
605 #endif /* ! NO_DDP */
606
607     if (dsiconfig) {
608         if ((options->flags & OPTION_CUSTOMICON) == 0) {
609             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
610         }
611         dsi_setstatus(dsi, status, statuslen);
612         dsiconfig->signature = status + sigoff;
613         dsiconfig->statuslen = statuslen;
614     }
615 }
616
617 /* this is the same as asp/dsi_getstatus */
618 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
619 {
620     AFPConfig *config = obj->config;
621
622     memcpy(rbuf, config->status, config->statuslen);
623     *rbuflen = config->statuslen;
624     return AFP_OK;
625 }