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>
39 #include <atalk/globals.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 | AFPSRVRINFO_SRVSIGNATURE
55 | AFPSRVRINFO_FASTBOZO
58 | AFPSRVRINFO_EXTSLEEP;
60 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
61 status |= AFPSRVRINFO_PASSWD;
62 if (passwdbits & PASSWD_NOSAVE)
63 status |= AFPSRVRINFO_NOSAVEPASSWD;
64 if (ipok) /* only advertise tcp/ip if we have a valid address */
65 status |= AFPSRVRINFO_TCPIP;
66 if (notif) /* Default is yes */
67 status |= AFPSRVRINFO_SRVNOTIFY;
68 if (flags & OPTION_UUID)
69 status |= AFPSRVRINFO_UUID;
71 status = htons(status);
72 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
75 static int status_server(char *data, const char *server, const struct afp_options *options)
78 char *Obj, *Type, *Zone;
83 /* make room for all offsets before server name */
84 data += AFPSTATUS_PRELEN;
86 /* extract the obj part of the server */
87 Obj = (char *) server;
88 nbp_name(server, &Obj, &Type, &Zone);
89 if ((size_t)-1 == (len = convert_string(
90 options->unixcharset, options->maccharset,
91 Obj, -1, buf, sizeof(buf))) ) {
92 len = MIN(strlen(Obj), 31);
94 memcpy( data, Obj, len );
95 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
98 memcpy( data, buf, len );
100 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
104 /* make room for signature and net address offset. save location of
105 * signature offset. we're also making room for directory names offset
106 * and the utf-8 server name offset.
108 * NOTE: technically, we don't need to reserve space for the
109 * signature and net address offsets if they're not going to be
110 * used. as there are no offsets after them, it doesn't hurt to
111 * have them specified though. so, we just do that to simplify
114 * NOTE2: AFP3.1 Documentation states that the directory names offset
115 * is a required feature, even though it can be set to zero.
118 status = htons(len + AFPSTATUS_POSTLEN);
119 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
120 return len; /* return the offset to beginning of signature offset */
123 static void status_machine(char *data)
129 const char *machine = "afs";
131 const char *machine = "Netatalk%s";
133 char buf[AFPSTATUS_MACHLEN+1];
135 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
136 data += ntohs( status );
138 if ((strlen(machine) + strlen(VERSION)) <= AFPSTATUS_MACHLEN) {
139 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, VERSION);
141 if (strlen(VERSION) > AFPSTATUS_MACHLEN) {
142 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, VERSION);
144 (void)snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, "");
145 (void)snprintf(buf + AFPSTATUS_MACHLEN - strlen(VERSION),
148 len = AFPSTATUS_MACHLEN;
153 memcpy( data, buf, len );
156 status = htons(data - start);
157 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
160 /* server signature is a 16-byte quantity */
161 static u_int16_t status_signature(char *data, int *servoffset,
162 const struct afp_options *options)
165 u_int16_t offset, sigoff;
169 /* get server signature offset */
170 memcpy(&offset, data + *servoffset, sizeof(offset));
171 sigoff = offset = ntohs(offset);
173 /* jump to server signature offset */
177 memcpy(data, options->signature, 16);
180 /* calculate net address offset */
181 *servoffset += sizeof(offset);
182 offset = htons(data - status);
183 memcpy(status + *servoffset, &offset, sizeof(offset));
187 static size_t status_netaddress(char *data, int *servoffset,
188 const ASP asp, const DSI *dsi,
189 const struct afp_options *options)
193 size_t addresses_len = 0;
197 /* get net address offset */
198 memcpy(&offset, data + *servoffset, sizeof(offset));
199 data += ntohs(offset);
203 len (byte = sizeof(length + address type + address)
204 address type (byte, ip address = 0x01, ip + port = 0x02,
205 ddp address = 0x03, fqdn = 0x04)
206 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
209 /* number of addresses. this currently screws up if we have a dsi
210 connection, but we don't have the ip address. to get around this,
211 we turn off the status flag for tcp/ip. */
212 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
213 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
217 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
218 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
219 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
220 *data++ = 6; /* length */
221 *data++ = 0x01; /* basic ip address */
222 memcpy(data, &inaddr->sin_addr.s_addr,
223 sizeof(inaddr->sin_addr.s_addr));
224 data += sizeof(inaddr->sin_addr.s_addr);
227 /* ip address + port */
229 *data++ = 0x02; /* ip address with port */
230 memcpy(data, &inaddr->sin_addr.s_addr,
231 sizeof(inaddr->sin_addr.s_addr));
232 data += sizeof(inaddr->sin_addr.s_addr);
233 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
234 data += sizeof(inaddr->sin_port);
238 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
239 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
240 *data++ = 18; /* length */
241 *data++ = 6; /* type */
242 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
243 data += sizeof(sa6->sin6_addr.s6_addr);
246 /* ip address + port */
247 *data++ = 20; /* length */
248 *data++ = 7; /* type*/
249 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
250 data += sizeof(sa6->sin6_addr.s6_addr);
251 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
252 data += sizeof(sa6->sin6_port);
259 /* handle DNS names */
260 if (options->fqdn && dsi) {
261 size_t len = strlen(options->fqdn);
262 if ( len + 2 + addresses_len < maxstatuslen - offset) {
265 memcpy(data, options->fqdn, len);
267 addresses_len += len+2;
270 /* Annouce support for SSH tunneled AFP session,
271 * this feature is available since 10.3.2.
272 * According to the specs (AFP 3.1 p.225) this should
273 * be an IP+Port style value, but it only works with
274 * a FQDN. OSX Server uses FQDN as well.
276 if ( len + 2 + addresses_len < maxstatuslen - offset) {
277 if (options->flags & OPTION_ANNOUNCESSH) {
280 memcpy(data, options->fqdn, len);
288 const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
292 *data++ = 0x03; /* ddp address */
293 memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
294 data += sizeof(ddpaddr->sat_addr.s_net);
295 memcpy(data, &ddpaddr->sat_addr.s_node,
296 sizeof(ddpaddr->sat_addr.s_node));
297 data += sizeof(ddpaddr->sat_addr.s_node);
298 memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
299 data += sizeof(ddpaddr->sat_port);
301 #endif /* ! NO_DDP */
303 /* calculate/store Directory Services Names offset */
304 offset = htons(data - begin);
305 *servoffset += sizeof(offset);
306 memcpy(begin + *servoffset, &offset, sizeof(offset));
308 /* return length of buffer */
309 return (data - begin);
312 static size_t status_directorynames(char *data, int *diroffset,
314 const struct afp_options *options)
318 memcpy(&offset, data + *diroffset, sizeof(offset));
319 offset = ntohs(offset);
322 /* I can not find documentation of any other purpose for the
323 * DirectoryNames field.
326 * Try to synthesize a principal:
327 * service '/' fqdn '@' realm
329 if (options->k5service && options->k5realm && options->fqdn) {
330 /* should k5princ be utf8 encoded? */
332 char *p = strchr( options->fqdn, ':' );
335 len = strlen( options->k5service )
336 + strlen( options->fqdn )
337 + strlen( options->k5realm );
338 len+=2; /* '/' and '@' */
339 if ( len > 255 || len+2 > maxstatuslen - offset) {
341 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
344 *data++ = 1; /* DirectoryNamesCount */
346 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
347 options->fqdn, options->k5realm );
356 /* Calculate and store offset for UTF8ServerName */
357 *diroffset += sizeof(u_int16_t);
358 offset = htons(data - begin);
359 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
361 /* return length of buffer */
362 return (data - begin);
365 static size_t status_utf8servername(char *data, int *nameoffset,
367 const struct afp_options *options)
369 char *Obj, *Type, *Zone;
373 u_int16_t offset, status;
375 memcpy(&offset, data + *nameoffset, sizeof(offset));
376 offset = ntohs(offset);
380 * What is the valid character range for an nbpname?
382 * Apple's server likes to use the non-qualified hostname
383 * This obviously won't work very well if multiple servers are running
387 /* extract the obj part of the server */
388 Obj = (char *) (options->server ? options->server : options->hostname);
389 nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
391 if ((size_t) -1 == (len = convert_string (
392 options->unixcharset, CH_UTF8_MAC,
393 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
394 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
396 /* set offset to 0 */
397 memset(begin + *nameoffset, 0, sizeof(offset));
398 data = begin + offset;
401 namelen = htons(len);
402 memcpy( data, &namelen, sizeof(namelen));
403 data += sizeof(namelen);
405 offset = htons(offset);
406 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
409 /* return length of buffer */
410 return (data - begin);
414 /* returns actual offset to signature */
415 static void status_icon(char *data, const unsigned char *icondata,
416 const int iconlen, const int sigoffset)
419 char *sigdata = data + sigoffset;
420 u_int16_t ret, status;
422 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
423 if ( icondata == NULL ) {
425 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
427 data += ntohs( status );
428 memcpy( data, icondata, iconlen);
430 ret = htons(data - start);
433 /* put in signature offset */
435 memcpy(sigdata, &ret, sizeof(ret));
438 /* ---------------------
440 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
441 const struct afp_options *options)
449 if (!(aspconfig || dsiconfig) || !options)
453 status = aspconfig->status;
454 maxstatuslen=sizeof(aspconfig->status);
455 asp = aspconfig->obj.handle;
461 status = dsiconfig->status;
462 maxstatuslen=sizeof(dsiconfig->status);
463 dsi = dsiconfig->obj.handle;
464 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
465 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
466 ipok = sa4->sin_addr.s_addr ? 1 : 0;
468 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
469 for (int i=0; i<16; i++) {
470 if (sa6->sin6_addr.s6_addr[i]) {
480 * These routines must be called in order -- earlier calls
481 * set the offsets for later calls.
483 * using structs is a bad idea, but that's what the original code
484 * does. solaris, in fact, will segfault with them. so, now
485 * we just use the powers of #defines and memcpy.
487 * reply block layout (offsets are 16-bit quantities):
488 * machine type offset -> AFP version count offset ->
489 * UAM count offset -> vol icon/mask offset -> flags ->
491 * server name [padded to even boundary] -> signature offset ->
492 * network address offset ->
494 * at the appropriate offsets:
495 * machine type, afp versions, uams, server signature
496 * (16-bytes), network addresses, volume icon/mask
500 options->server_notif,
501 (options->fqdn || ipok),
503 (options->k5service && options->k5realm && options->fqdn),
505 /* returns offset to signature offset */
506 c = status_server(status, options->server ? options->server :
507 options->hostname, options);
508 status_machine(status);
509 status_versions(status, asp, dsi);
510 status_uams(status, options->uamlist);
511 if (options->flags & OPTION_CUSTOMICON)
512 status_icon(status, icon, sizeof(icon), c);
514 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
516 sigoff = status_signature(status, &c, options);
517 /* c now contains the offset where the netaddress offset lives */
519 status_netaddress(status, &c, asp, dsi, options);
520 /* c now contains the offset where the Directory Names Count offset lives */
522 statuslen = status_directorynames(status, &c, dsi, options);
523 /* c now contains the offset where the UTF-8 ServerName offset lives */
525 if ( statuslen < maxstatuslen)
526 statuslen = status_utf8servername(status, &c, dsi, options);
530 if (dsiconfig) /* status is dsiconfig->status */
531 memcpy(aspconfig->status, status, statuslen);
532 asp_setstatus(asp, status, statuslen);
533 aspconfig->signature = status + sigoff;
534 aspconfig->statuslen = statuslen;
536 #endif /* ! NO_DDP */
539 if ((options->flags & OPTION_CUSTOMICON) == 0) {
540 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
542 dsi_setstatus(dsi, status, statuslen);
543 dsiconfig->signature = status + sigoff;
544 dsiconfig->statuslen = statuslen;
548 /* set_signature() */
550 /* If found in conf file, use it. */
551 /* If not found in conf file, genarate and append in conf file. */
552 /* If conf file don't exist, create and genarate. */
553 /* If cannot open conf file, use one-time signature. */
554 /* If -signature user:xxxxx, use it. */
556 void set_signature(struct afp_options *options) {
560 char *servername_conf;
563 FILE *fp = NULL, *randomp;
567 server_tmp = (options->server ? options->server : options->hostname);
568 if (strcmp(options->signatureopt, "auto") == 0) {
569 goto server_signature_auto; /* default */
570 } else if (strcmp(options->signatureopt, "host") == 0) {
571 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
572 goto server_signature_auto; /* same as auto */
573 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
574 goto server_signature_user; /* user string */
576 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
577 goto server_signature_auto; /* switch back to auto*/
580 server_signature_user:
582 /* Signature type is user string */
583 len = strlen(options->signatureopt);
585 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
586 goto server_signature_auto;
588 usersign = options->signatureopt + 5;
591 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
593 } else if (len >= 3) {
594 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
596 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
598 memset(options->signature, 0, 16);
599 memcpy(options->signature, usersign, len);
600 goto server_signature_done;
602 server_signature_auto:
604 /* Signature type is auto, using afp_signature.conf */
605 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
606 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
607 /* scan in the conf file */
608 while (fgets(buf, sizeof(buf), fp) != NULL) {
610 while (p && isblank(*p))
612 if (!p || (*p == '#') || (*p == '\n'))
613 continue; /* invalid line */
616 if ((servername_conf = strtok( p, "\"" )) == NULL)
617 continue; /* syntax error: invalid quoted servername */
619 if ((servername_conf = strtok( p, " \t" )) == NULL)
620 continue; /* syntax error: invalid servername */
625 continue; /* syntax error: missing signature */
627 if (strcmp(server_tmp, servername_conf))
628 continue; /* another servername */
630 while (p && isblank(*p))
632 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
633 &options->signature[ 0], &options->signature[ 1],
634 &options->signature[ 2], &options->signature[ 3],
635 &options->signature[ 4], &options->signature[ 5],
636 &options->signature[ 6], &options->signature[ 7],
637 &options->signature[ 8], &options->signature[ 9],
638 &options->signature[10], &options->signature[11],
639 &options->signature[12], &options->signature[13],
640 &options->signature[14], &options->signature[15]
643 goto server_signature_done; /* found in conf file */
646 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
647 fseek(fp, 0L, SEEK_END);
648 if(ftell(fp) == 0) { /* size = 0 */
650 goto server_signature_random;
652 fseek(fp, -1L, SEEK_END);
653 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
654 goto server_signature_random;
657 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
658 options->sigconffile, strerror(errno));
659 goto server_signature_random;
662 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
663 options->sigconffile, strerror(errno));
664 goto server_signature_random;
666 } else { /* conf file don't exist */
667 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
668 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s). Using one-time signature.",
669 options->sigconffile, strerror(errno));
670 goto server_signature_random;
672 if (( fp = fdopen( fd, "w" )) == NULL ) {
673 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
674 options->sigconffile, strerror(errno));
676 goto server_signature_random;
679 goto server_signature_random;
682 server_signature_random:
684 /* generate signature from random number */
685 randombytes(options->signature, 16);
687 if (fp && header) { /* conf file is created or size=0 */
688 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
689 fprintf(fp, "# This file is auto-generated by afpd.\n");
691 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
692 fprintf(fp, "# the same server twice.\n");
694 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
698 fprintf(fp, "\"%s\"\t", server_tmp);
699 for (i=0 ; i<16 ; i++) {
700 fprintf(fp, "%02X", (options->signature)[i]);
702 fprintf(fp, "%s", "\n");
706 server_signature_done:
709 LOG(log_info, logtype_afpd,
710 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
712 (options->signature)[ 0], (options->signature)[ 1],
713 (options->signature)[ 2], (options->signature)[ 3],
714 (options->signature)[ 4], (options->signature)[ 5],
715 (options->signature)[ 6], (options->signature)[ 7],
716 (options->signature)[ 8], (options->signature)[ 9],
717 (options->signature)[10], (options->signature)[11],
718 (options->signature)[12], (options->signature)[13],
719 (options->signature)[14], (options->signature)[15]);
724 /* this is the same as asp/dsi_getstatus */
725 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
727 AFPConfig *config = obj->config;
729 memcpy(rbuf, config->status, config->statuslen);
730 *rbuflen = config->statuslen;