2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <atalk/logger.h>
24 #include <sys/param.h>
25 #ifndef HAVE_GETHOSTID
26 #include <sys/sysctl.h>
27 #endif /* HAVE_GETHOSTID */
31 #include <netatalk/at.h>
32 #include <netatalk/endian.h>
33 #include <atalk/dsi.h>
34 #include <atalk/atp.h>
35 #include <atalk/asp.h>
36 #include <atalk/nbp.h>
37 #include <atalk/unicode.h>
38 #include <atalk/util.h>
40 #include "globals.h" /* includes <netdb.h> */
42 #include "afp_config.h"
45 static size_t maxstatuslen = 0;
47 static void status_flags(char *data, const int notif, const int ipok,
48 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
52 status = AFPSRVRINFO_COPY;
53 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
54 status |= AFPSRVRINFO_PASSWD;
55 if (passwdbits & PASSWD_NOSAVE)
56 status |= AFPSRVRINFO_NOSAVEPASSWD;
57 status |= AFPSRVRINFO_SRVSIGNATURE;
58 /* only advertise tcp/ip if we have a valid address */
60 status |= AFPSRVRINFO_TCPIP;
61 status |= AFPSRVRINFO_SRVMSGS;
62 /* Allow the user to decide if we should support server notifications.
63 * With this turned off, the clients will poll for directory changes every
64 * 10 seconds. This might be too costly to network resources, so make
65 * this an optional thing. Default will be to _not_ support server
68 status |= AFPSRVRINFO_SRVNOTIFY;
70 status |= AFPSRVRINFO_FASTBOZO;
71 status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
72 /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
74 if (flags & OPTION_UUID) /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
75 status |= AFPSRVRINFO_UUID;
77 status = htons(status);
78 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
81 static int status_server(char *data, const char *server, const struct afp_options *options)
84 char *Obj, *Type, *Zone;
89 /* make room for all offsets before server name */
90 data += AFPSTATUS_PRELEN;
92 /* extract the obj part of the server */
93 Obj = (char *) server;
95 nbp_name(server, &Obj, &Type, &Zone);
97 if ((size_t)-1 == (len = convert_string(
98 options->unixcharset, options->maccharset,
99 Obj, -1, buf, sizeof(buf))) ) {
100 len = MIN(strlen(Obj), 31);
102 memcpy( data, Obj, len );
103 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
106 memcpy( data, buf, len );
108 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
112 /* make room for signature and net address offset. save location of
113 * signature offset. we're also making room for directory names offset
114 * and the utf-8 server name offset.
116 * NOTE: technically, we don't need to reserve space for the
117 * signature and net address offsets if they're not going to be
118 * used. as there are no offsets after them, it doesn't hurt to
119 * have them specified though. so, we just do that to simplify
122 * NOTE2: AFP3.1 Documentation states that the directory names offset
123 * is a required feature, even though it can be set to zero.
126 status = htons(len + AFPSTATUS_POSTLEN);
127 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
128 return len; /* return the offset to beginning of signature offset */
131 static void status_machine(char *data)
137 const char *machine = "afs";
139 const char *machine = "Netatalk %s";
143 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
144 data += ntohs( status );
146 // len = strlen( machine );
147 len = snprintf(buf, 64, machine, VERSION);
149 memcpy( data, buf, len );
152 status = htons(data - start);
153 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
156 /* server signature is a 16-byte quantity */
157 static u_int16_t status_signature(char *data, int *servoffset,
158 const struct afp_options *options)
161 u_int16_t offset, sigoff;
165 /* get server signature offset */
166 memcpy(&offset, data + *servoffset, sizeof(offset));
167 sigoff = offset = ntohs(offset);
169 /* jump to server signature offset */
173 memcpy(data, options->signature, 16);
176 /* calculate net address offset */
177 *servoffset += sizeof(offset);
178 offset = htons(data - status);
179 memcpy(status + *servoffset, &offset, sizeof(offset));
183 static size_t status_netaddress(char *data, int *servoffset,
184 const ASP asp, const DSI *dsi,
185 const struct afp_options *options)
189 size_t addresses_len = 0;
193 /* get net address offset */
194 memcpy(&offset, data + *servoffset, sizeof(offset));
195 data += ntohs(offset);
199 len (byte = sizeof(length + address type + address)
200 address type (byte, ip address = 0x01, ip + port = 0x02,
201 ddp address = 0x03, fqdn = 0x04)
202 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
205 /* number of addresses. this currently screws up if we have a dsi
206 connection, but we don't have the ip address. to get around this,
207 we turn off the status flag for tcp/ip. */
208 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
209 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
213 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
214 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
215 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
216 *data++ = 6; /* length */
217 *data++ = 0x01; /* basic ip address */
218 memcpy(data, &inaddr->sin_addr.s_addr,
219 sizeof(inaddr->sin_addr.s_addr));
220 data += sizeof(inaddr->sin_addr.s_addr);
223 /* ip address + port */
225 *data++ = 0x02; /* ip address with port */
226 memcpy(data, &inaddr->sin_addr.s_addr,
227 sizeof(inaddr->sin_addr.s_addr));
228 data += sizeof(inaddr->sin_addr.s_addr);
229 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
230 data += sizeof(inaddr->sin_port);
234 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
235 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
236 *data++ = 18; /* length */
237 *data++ = 6; /* type */
238 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
239 data += sizeof(sa6->sin6_addr.s6_addr);
242 /* ip address + port */
243 *data++ = 20; /* length */
244 *data++ = 7; /* type*/
245 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
246 data += sizeof(sa6->sin6_addr.s6_addr);
247 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
248 data += sizeof(sa6->sin6_port);
255 /* handle DNS names */
256 if (options->fqdn && dsi) {
257 size_t len = strlen(options->fqdn);
258 if ( len + 2 + addresses_len < maxstatuslen - offset) {
261 memcpy(data, options->fqdn, len);
263 addresses_len += len+2;
266 /* Annouce support for SSH tunneled AFP session,
267 * this feature is available since 10.3.2.
268 * According to the specs (AFP 3.1 p.225) this should
269 * be an IP+Port style value, but it only works with
270 * a FQDN. OSX Server uses FQDN as well.
272 if ( len + 2 + addresses_len < maxstatuslen - offset) {
273 if (options->flags & OPTION_ANNOUNCESSH) {
276 memcpy(data, options->fqdn, len);
284 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
288 *data++ = 0x03; /* ddp address */
289 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
290 data += sizeof(ddpaddr->sat_addr.s_net);
291 memcpy(data, &ddpaddr->sat_addr.s_node,
292 sizeof(ddpaddr->sat_addr.s_node));
293 data += sizeof(ddpaddr->sat_addr.s_node);
294 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
295 data += sizeof(ddpaddr->sat_port);
297 #endif /* ! NO_DDP */
299 /* calculate/store Directory Services Names offset */
300 offset = htons(data - begin);
301 *servoffset += sizeof(offset);
302 memcpy(begin + *servoffset, &offset, sizeof(offset));
304 /* return length of buffer */
305 return (data - begin);
308 static size_t status_directorynames(char *data, int *diroffset,
310 const struct afp_options *options)
314 memcpy(&offset, data + *diroffset, sizeof(offset));
315 offset = ntohs(offset);
318 /* I can not find documentation of any other purpose for the
319 * DirectoryNames field.
322 * Try to synthesize a principal:
323 * service '/' fqdn '@' realm
325 if (options->k5service && options->k5realm && options->fqdn) {
326 /* should k5princ be utf8 encoded? */
328 char *p = strchr( options->fqdn, ':' );
331 len = strlen( options->k5service )
332 + strlen( options->fqdn )
333 + strlen( options->k5realm );
334 len+=2; /* '/' and '@' */
335 if ( len > 255 || len+2 > maxstatuslen - offset) {
337 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
340 *data++ = 1; /* DirectoryNamesCount */
342 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
343 options->fqdn, options->k5realm );
352 /* Calculate and store offset for UTF8ServerName */
353 *diroffset += sizeof(u_int16_t);
354 offset = htons(data - begin);
355 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
357 /* return length of buffer */
358 return (data - begin);
361 static size_t status_utf8servername(char *data, int *nameoffset,
363 const struct afp_options *options)
365 char *Obj, *Type, *Zone;
369 u_int16_t offset, status;
371 memcpy(&offset, data + *nameoffset, sizeof(offset));
372 offset = ntohs(offset);
376 * What is the valid character range for an nbpname?
378 * Apple's server likes to use the non-qualified hostname
379 * This obviously won't work very well if multiple servers are running
383 /* extract the obj part of the server */
384 Obj = (char *) (options->server ? options->server : options->hostname);
386 nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
388 if ((size_t) -1 == (len = convert_string (
389 options->unixcharset, CH_UTF8_MAC,
390 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
391 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
393 /* set offset to 0 */
394 memset(begin + *nameoffset, 0, sizeof(offset));
395 data = begin + offset;
397 namelen = htons(len);
398 memcpy( data, &namelen, sizeof(namelen));
399 data += sizeof(namelen);
401 offset = htons(offset);
402 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
404 /* Now set the flag ... */
405 memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
406 status = ntohs(status);
407 status |= AFPSRVRINFO_SRVUTF8;
408 status = htons(status);
409 memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
412 /* return length of buffer */
413 return (data - begin);
417 /* returns actual offset to signature */
418 static void status_icon(char *data, const unsigned char *icondata,
419 const int iconlen, const int sigoffset)
422 char *sigdata = data + sigoffset;
423 u_int16_t ret, status;
425 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
426 if ( icondata == NULL ) {
428 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
430 data += ntohs( status );
431 memcpy( data, icondata, iconlen);
433 ret = htons(data - start);
436 /* put in signature offset */
438 memcpy(sigdata, &ret, sizeof(ret));
441 /* ---------------------
443 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
444 const struct afp_options *options)
452 if (!(aspconfig || dsiconfig) || !options)
456 status = aspconfig->status;
457 maxstatuslen=sizeof(aspconfig->status);
458 asp = aspconfig->obj.handle;
464 status = dsiconfig->status;
465 maxstatuslen=sizeof(dsiconfig->status);
466 dsi = dsiconfig->obj.handle;
467 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
468 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
469 ipok = sa4->sin_addr.s_addr ? 1 : 0;
471 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
472 for (int i=0; i<16; i++) {
473 if (sa6->sin6_addr.s6_addr[i]) {
483 * These routines must be called in order -- earlier calls
484 * set the offsets for later calls.
486 * using structs is a bad idea, but that's what the original code
487 * does. solaris, in fact, will segfault with them. so, now
488 * we just use the powers of #defines and memcpy.
490 * reply block layout (offsets are 16-bit quantities):
491 * machine type offset -> AFP version count offset ->
492 * UAM count offset -> vol icon/mask offset -> flags ->
494 * server name [padded to even boundary] -> signature offset ->
495 * network address offset ->
497 * at the appropriate offsets:
498 * machine type, afp versions, uams, server signature
499 * (16-bytes), network addresses, volume icon/mask
503 options->server_notif,
504 (options->fqdn || ipok),
506 (options->k5service && options->k5realm && options->fqdn),
508 /* returns offset to signature offset */
509 c = status_server(status, options->server ? options->server :
510 options->hostname, options);
511 status_machine(status);
512 status_versions(status, asp, dsi);
513 status_uams(status, options->uamlist);
514 if (options->flags & OPTION_CUSTOMICON)
515 status_icon(status, icon, sizeof(icon), c);
517 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
519 sigoff = status_signature(status, &c, options);
520 /* c now contains the offset where the netaddress offset lives */
522 status_netaddress(status, &c, asp, dsi, options);
523 /* c now contains the offset where the Directory Names Count offset lives */
525 statuslen = status_directorynames(status, &c, dsi, options);
526 /* c now contains the offset where the UTF-8 ServerName offset lives */
528 if ( statuslen < maxstatuslen)
529 statuslen = status_utf8servername(status, &c, dsi, options);
533 if (dsiconfig) /* status is dsiconfig->status */
534 memcpy(aspconfig->status, status, statuslen);
535 asp_setstatus(asp, status, statuslen);
536 aspconfig->signature = status + sigoff;
537 aspconfig->statuslen = statuslen;
539 #endif /* ! NO_DDP */
542 if ((options->flags & OPTION_CUSTOMICON) == 0) {
543 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
545 dsi_setstatus(dsi, status, statuslen);
546 dsiconfig->signature = status + sigoff;
547 dsiconfig->statuslen = statuslen;
551 /* set_signature() */
553 /* If found in conf file, use it. */
554 /* If not found in conf file, genarate and append in conf file. */
555 /* If conf file don't exist, create and genarate. */
556 /* If cannot open conf file, use one-time signature. */
557 /* If -signature user:xxxxx, use it. */
559 void set_signature(struct afp_options *options) {
563 char *servername_conf;
566 FILE *fp = NULL, *randomp;
570 server_tmp = (options->server ? options->server : options->hostname);
571 if (strcmp(options->signatureopt, "auto") == 0) {
572 goto server_signature_auto; /* default */
573 } else if (strcmp(options->signatureopt, "host") == 0) {
574 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
575 goto server_signature_auto; /* same as auto */
576 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
577 goto server_signature_user; /* user string */
579 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
580 goto server_signature_auto; /* switch back to auto*/
583 server_signature_user:
585 /* Signature type is user string */
586 len = strlen(options->signatureopt);
588 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
589 goto server_signature_auto;
591 usersign = options->signatureopt + 5;
594 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
596 } else if (len >= 3) {
597 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
599 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
601 memset(options->signature, 0, 16);
602 memcpy(options->signature, usersign, len);
603 goto server_signature_done;
605 server_signature_auto:
607 /* Signature type is auto, using afp_signature.conf */
608 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
609 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
610 /* scan in the conf file */
611 while (fgets(buf, sizeof(buf), fp) != NULL) {
613 while (p && isblank(*p))
615 if (!p || (*p == '#') || (*p == '\n'))
616 continue; /* invalid line */
619 if ((servername_conf = strtok( p, "\"" )) == NULL)
620 continue; /* syntax error: invalid quoted servername */
622 if ((servername_conf = strtok( p, " \t" )) == NULL)
623 continue; /* syntax error: invalid servername */
628 continue; /* syntax error: missing signature */
630 if (strcmp(server_tmp, servername_conf))
631 continue; /* another servername */
633 while (p && isblank(*p))
635 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
636 &options->signature[ 0], &options->signature[ 1],
637 &options->signature[ 2], &options->signature[ 3],
638 &options->signature[ 4], &options->signature[ 5],
639 &options->signature[ 6], &options->signature[ 7],
640 &options->signature[ 8], &options->signature[ 9],
641 &options->signature[10], &options->signature[11],
642 &options->signature[12], &options->signature[13],
643 &options->signature[14], &options->signature[15]
646 goto server_signature_done; /* found in conf file */
649 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
650 fseek(fp, 0L, SEEK_END);
651 if(ftell(fp) == 0) { /* size = 0 */
653 goto server_signature_random;
655 fseek(fp, -1L, SEEK_END);
656 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
657 goto server_signature_random;
660 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
661 options->sigconffile, strerror(errno));
662 goto server_signature_random;
665 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
666 options->sigconffile, strerror(errno));
667 goto server_signature_random;
669 } else { /* conf file don't exist */
670 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
671 LOG(log_error, logtype_atalkd, "Cannot create %s (%s). Using one-time signature.",
672 options->sigconffile, strerror(errno));
673 goto server_signature_random;
675 if (( fp = fdopen( fd, "w" )) == NULL ) {
676 LOG(log_error, logtype_atalkd, "Cannot fdopen %s (%s). Using one-time signature.",
677 options->sigconffile, strerror(errno));
679 goto server_signature_random;
682 goto server_signature_random;
685 server_signature_random:
687 /* generate signature from random number */
688 randombytes(options->signature, 16);
690 if (fp && header) { /* conf file is created or size=0 */
691 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
692 fprintf(fp, "# This file is auto-generated by afpd.\n");
694 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
695 fprintf(fp, "# the same server twice.\n");
697 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
701 fprintf(fp, "\"%s\"\t", server_tmp);
702 for (i=0 ; i<16 ; i++) {
703 fprintf(fp, "%02X", (options->signature)[i]);
705 fprintf(fp, "%s", "\n");
709 server_signature_done:
712 LOG(log_info, logtype_afpd,
713 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
715 (options->signature)[ 0], (options->signature)[ 1],
716 (options->signature)[ 2], (options->signature)[ 3],
717 (options->signature)[ 4], (options->signature)[ 5],
718 (options->signature)[ 6], (options->signature)[ 7],
719 (options->signature)[ 8], (options->signature)[ 9],
720 (options->signature)[10], (options->signature)[11],
721 (options->signature)[12], (options->signature)[13],
722 (options->signature)[14], (options->signature)[15]);
727 /* this is the same as asp/dsi_getstatus */
728 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
730 AFPConfig *config = obj->config;
732 memcpy(rbuf, config->status, config->statuslen);
733 *rbuflen = config->statuslen;