2 * $Id: status.c,v 1.30 2009-11-23 19:04:14 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <atalk/logger.h>
21 #include <sys/param.h>
22 #ifndef HAVE_GETHOSTID
23 #include <sys/sysctl.h>
24 #endif /* HAVE_GETHOSTID */
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>
35 #include "globals.h" /* includes <netdb.h> */
37 #include "afp_config.h"
40 static size_t maxstatuslen = 0;
42 static void status_flags(char *data, const int notif, const int ipok,
43 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
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 */
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
63 status |= AFPSRVRINFO_SRVNOTIFY;
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 ... */
69 if (flags & OPTION_UUID) /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
70 status |= AFPSRVRINFO_UUID;
72 status = htons(status);
73 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
76 static int status_server(char *data, const char *server, const struct afp_options *options)
79 char *Obj, *Type, *Zone;
84 /* make room for all offsets before server name */
85 data += AFPSTATUS_PRELEN;
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);
95 memcpy( data, Obj, len );
96 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
99 memcpy( data, buf, len );
101 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
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.
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
115 * NOTE2: AFP3.1 Documentation states that the directory names offset
116 * is a required feature, even though it can be set to zero.
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 */
124 static void status_machine(char *data)
130 const char *machine = "afs";
132 const char *machine = "Netatalk";
135 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
136 data += ntohs( status );
137 len = strlen( machine );
139 memcpy( data, machine, len );
141 status = htons(data - start);
142 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
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.
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)
161 u_int16_t offset, sigoff;
170 /* get server signature offset */
171 memcpy(&offset, data + *servoffset, sizeof(offset));
172 sigoff = offset = ntohs(offset);
174 /* jump to server signature offset */
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.",
182 goto server_signature_hostid;
184 usersign = options->signature + 5;
185 if (strlen(usersign) < 3)
186 LOG(log_warning, logtype_afpd, "Signature %s is very short !",
190 strncpy(data, usersign, 16);
192 goto server_signature_done;
193 } /* signature = user */
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)
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 */
208 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
209 hostid = ((struct sockaddr_in *)(&dsi->server))->sin_addr.s_addr;
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));
216 struct hostent *host;
218 if ((host = gethostbyname(options->hostname)))
219 hostid = ((struct in_addr *) host->h_addr)->s_addr;
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 */
228 for (i = 0; i < 16; i += sizeof(hostid)) {
229 memcpy(data, &hostid, sizeof(hostid));
230 data += sizeof(hostid);
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));
241 static size_t status_netaddress(char *data, int *servoffset,
242 const ASP asp, const DSI *dsi,
243 const struct afp_options *options)
247 size_t addresses_len = 0;
251 /* get net address offset */
252 memcpy(&offset, data + *servoffset, sizeof(offset));
253 data += ntohs(offset);
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)
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);
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);
281 /* ip address + port */
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);
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);
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);
313 /* handle DNS names */
314 if (options->fqdn && dsi) {
315 size_t len = strlen(options->fqdn);
316 if ( len + 2 + addresses_len < maxstatuslen - offset) {
319 memcpy(data, options->fqdn, len);
321 addresses_len += len+2;
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.
330 if ( len + 2 + addresses_len < maxstatuslen - offset) {
331 if (options->flags & OPTION_ANNOUNCESSH) {
334 memcpy(data, options->fqdn, len);
342 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
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);
355 #endif /* ! NO_DDP */
357 /* calculate/store Directory Services Names offset */
358 offset = htons(data - begin);
359 *servoffset += sizeof(offset);
360 memcpy(begin + *servoffset, &offset, sizeof(offset));
362 /* return length of buffer */
363 return (data - begin);
366 static size_t status_directorynames(char *data, int *diroffset,
368 const struct afp_options *options)
372 memcpy(&offset, data + *diroffset, sizeof(offset));
373 offset = ntohs(offset);
376 /* I can not find documentation of any other purpose for the
377 * DirectoryNames field.
380 * Try to synthesize a principal:
381 * service '/' fqdn '@' realm
383 if (options->k5service && options->k5realm && options->fqdn) {
384 /* should k5princ be utf8 encoded? */
386 char *p = strchr( options->fqdn, ':' );
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) {
395 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
398 *data++ = 1; /* DirectoryNamesCount */
400 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
401 options->fqdn, options->k5realm );
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));
415 /* return length of buffer */
416 return (data - begin);
419 static size_t status_utf8servername(char *data, int *nameoffset,
421 const struct afp_options *options)
423 char *Obj, *Type, *Zone;
427 u_int16_t offset, status;
429 memcpy(&offset, data + *nameoffset, sizeof(offset));
430 offset = ntohs(offset);
434 * What is the valid character range for an nbpname?
436 * Apple's server likes to use the non-qualified hostname
437 * This obviously won't work very well if multiple servers are running
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);
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");
450 /* set offset to 0 */
451 memset(begin + *nameoffset, 0, sizeof(offset));
452 data = begin + offset;
455 namelen = htons(len);
456 memcpy( data, &namelen, sizeof(namelen));
457 data += sizeof(namelen);
459 offset = htons(offset);
460 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
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));
470 /* return length of buffer */
471 return (data - begin);
475 /* returns actual offset to signature */
476 static void status_icon(char *data, const unsigned char *icondata,
477 const int iconlen, const int sigoffset)
480 char *sigdata = data + sigoffset;
481 u_int16_t ret, status;
483 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
484 if ( icondata == NULL ) {
486 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
488 data += ntohs( status );
489 memcpy( data, icondata, iconlen);
491 ret = htons(data - start);
494 /* put in signature offset */
496 memcpy(sigdata, &ret, sizeof(ret));
499 /* ---------------------
501 void status_reset(void)
507 /* ---------------------
509 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
510 const struct afp_options *options)
518 if (!(aspconfig || dsiconfig) || !options)
522 status = aspconfig->status;
523 maxstatuslen=sizeof(aspconfig->status);
524 asp = aspconfig->obj.handle;
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;
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]) {
549 * These routines must be called in order -- earlier calls
550 * set the offsets for later calls.
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.
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 ->
560 * server name [padded to even boundary] -> signature offset ->
561 * network address offset ->
563 * at the appropriate offsets:
564 * machine type, afp versions, uams, server signature
565 * (16-bytes), network addresses, volume icon/mask
569 options->server_notif,
570 (options->fqdn || ipok),
572 (options->k5service && options->k5realm && options->fqdn),
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);
583 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
585 sigoff = status_signature(status, &c, dsi, options);
586 /* c now contains the offset where the netaddress offset lives */
588 status_netaddress(status, &c, asp, dsi, options);
589 /* c now contains the offset where the Directory Names Count offset lives */
591 statuslen = status_directorynames(status, &c, dsi, options);
592 /* c now contains the offset where the UTF-8 ServerName offset lives */
594 if ( statuslen < maxstatuslen)
595 statuslen = status_utf8servername(status, &c, dsi, options);
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;
605 #endif /* ! NO_DDP */
608 if ((options->flags & OPTION_CUSTOMICON) == 0) {
609 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
611 dsi_setstatus(dsi, status, statuslen);
612 dsiconfig->signature = status + sigoff;
613 dsiconfig->statuslen = statuslen;
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)
620 AFPConfig *config = obj->config;
622 memcpy(rbuf, config->status, config->statuslen);
623 *rbuflen = config->statuslen;