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 <arpa/inet.h>
33 #include <atalk/dsi.h>
34 #include <atalk/unicode.h>
35 #include <atalk/util.h>
37 #include "globals.h" /* includes <netdb.h> */
39 #include "afp_config.h"
42 static size_t maxstatuslen = 0;
44 static void status_flags(char *data, const int notif, const int ipok,
45 const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
49 status = AFPSRVRINFO_COPY;
50 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
51 status |= AFPSRVRINFO_PASSWD;
52 if (passwdbits & PASSWD_NOSAVE)
53 status |= AFPSRVRINFO_NOSAVEPASSWD;
54 status |= AFPSRVRINFO_SRVSIGNATURE;
55 /* only advertise tcp/ip if we have a valid address */
57 status |= AFPSRVRINFO_TCPIP;
58 status |= AFPSRVRINFO_SRVMSGS;
59 /* Allow the user to decide if we should support server notifications.
60 * With this turned off, the clients will poll for directory changes every
61 * 10 seconds. This might be too costly to network resources, so make
62 * this an optional thing. Default will be to _not_ support server
65 status |= AFPSRVRINFO_SRVNOTIFY;
67 status |= AFPSRVRINFO_FASTBOZO;
68 status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
69 /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
71 if (flags & OPTION_UUID) /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
72 status |= AFPSRVRINFO_UUID;
74 status = htons(status);
75 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
78 static int status_server(char *data, const char *server, const struct afp_options *options)
81 char *Obj, *Type, *Zone;
86 /* make room for all offsets before server name */
87 data += AFPSTATUS_PRELEN;
89 /* extract the obj part of the server */
90 Obj = (char *) server;
91 if ((size_t)-1 == (len = convert_string(
92 options->unixcharset, options->maccharset,
93 Obj, -1, buf, sizeof(buf))) ) {
94 len = MIN(strlen(Obj), 31);
96 memcpy( data, Obj, len );
97 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
100 memcpy( data, buf, len );
102 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
106 /* make room for signature and net address offset. save location of
107 * signature offset. we're also making room for directory names offset
108 * and the utf-8 server name offset.
110 * NOTE: technically, we don't need to reserve space for the
111 * signature and net address offsets if they're not going to be
112 * used. as there are no offsets after them, it doesn't hurt to
113 * have them specified though. so, we just do that to simplify
116 * NOTE2: AFP3.1 Documentation states that the directory names offset
117 * is a required feature, even though it can be set to zero.
120 status = htons(len + AFPSTATUS_POSTLEN);
121 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
122 return len; /* return the offset to beginning of signature offset */
125 static void status_machine(char *data)
131 const char *machine = "afs";
133 const char *machine = "Netatalk %s";
137 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
138 data += ntohs( status );
140 // len = strlen( machine );
141 len = snprintf(buf, 64, machine, VERSION);
143 memcpy( data, buf, len );
146 status = htons(data - start);
147 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
150 /* server signature is a 16-byte quantity */
151 static u_int16_t status_signature(char *data, int *servoffset,
152 const struct afp_options *options)
155 u_int16_t offset, sigoff;
159 /* get server signature offset */
160 memcpy(&offset, data + *servoffset, sizeof(offset));
161 sigoff = offset = ntohs(offset);
163 /* jump to server signature offset */
167 memcpy(data, options->signature, 16);
170 /* calculate net address offset */
171 *servoffset += sizeof(offset);
172 offset = htons(data - status);
173 memcpy(status + *servoffset, &offset, sizeof(offset));
177 static size_t status_netaddress(char *data, int *servoffset,
179 const struct afp_options *options)
183 size_t addresses_len = 0;
187 /* get net address offset */
188 memcpy(&offset, data + *servoffset, sizeof(offset));
189 data += ntohs(offset);
193 len (byte = sizeof(length + address type + address)
194 address type (byte, ip address = 0x01, ip + port = 0x02,
195 ddp address = 0x03, fqdn = 0x04)
196 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
199 /* number of addresses. this currently screws up if we have a dsi
200 connection, but we don't have the ip address. to get around this,
201 we turn off the status flag for tcp/ip. */
202 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) +
203 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
207 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
208 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
209 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
210 *data++ = 6; /* length */
211 *data++ = 0x01; /* basic ip address */
212 memcpy(data, &inaddr->sin_addr.s_addr,
213 sizeof(inaddr->sin_addr.s_addr));
214 data += sizeof(inaddr->sin_addr.s_addr);
217 /* ip address + port */
219 *data++ = 0x02; /* ip address with port */
220 memcpy(data, &inaddr->sin_addr.s_addr,
221 sizeof(inaddr->sin_addr.s_addr));
222 data += sizeof(inaddr->sin_addr.s_addr);
223 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
224 data += sizeof(inaddr->sin_port);
228 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
229 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
230 *data++ = 18; /* length */
231 *data++ = 6; /* type */
232 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
233 data += sizeof(sa6->sin6_addr.s6_addr);
236 /* ip address + port */
237 *data++ = 20; /* length */
238 *data++ = 7; /* type*/
239 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
240 data += sizeof(sa6->sin6_addr.s6_addr);
241 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
242 data += sizeof(sa6->sin6_port);
249 /* handle DNS names */
250 if (options->fqdn && dsi) {
251 size_t len = strlen(options->fqdn);
252 if ( len + 2 + addresses_len < maxstatuslen - offset) {
255 memcpy(data, options->fqdn, len);
257 addresses_len += len+2;
260 /* Annouce support for SSH tunneled AFP session,
261 * this feature is available since 10.3.2.
262 * According to the specs (AFP 3.1 p.225) this should
263 * be an IP+Port style value, but it only works with
264 * a FQDN. OSX Server uses FQDN as well.
266 if ( len + 2 + addresses_len < maxstatuslen - offset) {
267 if (options->flags & OPTION_ANNOUNCESSH) {
270 memcpy(data, options->fqdn, len);
276 /* calculate/store Directory Services Names offset */
277 offset = htons(data - begin);
278 *servoffset += sizeof(offset);
279 memcpy(begin + *servoffset, &offset, sizeof(offset));
281 /* return length of buffer */
282 return (data - begin);
285 static size_t status_directorynames(char *data, int *diroffset,
287 const struct afp_options *options)
291 memcpy(&offset, data + *diroffset, sizeof(offset));
292 offset = ntohs(offset);
295 /* I can not find documentation of any other purpose for the
296 * DirectoryNames field.
299 * Try to synthesize a principal:
300 * service '/' fqdn '@' realm
302 if (options->k5service && options->k5realm && options->fqdn) {
303 /* should k5princ be utf8 encoded? */
305 char *p = strchr( options->fqdn, ':' );
308 len = strlen( options->k5service )
309 + strlen( options->fqdn )
310 + strlen( options->k5realm );
311 len+=2; /* '/' and '@' */
312 if ( len > 255 || len+2 > maxstatuslen - offset) {
314 LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
317 *data++ = 1; /* DirectoryNamesCount */
319 snprintf( data, len + 1, "%s/%s@%s", options->k5service,
320 options->fqdn, options->k5realm );
329 /* Calculate and store offset for UTF8ServerName */
330 *diroffset += sizeof(u_int16_t);
331 offset = htons(data - begin);
332 memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
334 /* return length of buffer */
335 return (data - begin);
338 static size_t status_utf8servername(char *data, int *nameoffset,
340 const struct afp_options *options)
342 char *Obj, *Type, *Zone;
346 u_int16_t offset, status;
348 memcpy(&offset, data + *nameoffset, sizeof(offset));
349 offset = ntohs(offset);
353 * What is the valid character range for an nbpname?
355 * Apple's server likes to use the non-qualified hostname
356 * This obviously won't work very well if multiple servers are running
360 /* extract the obj part of the server */
361 Obj = (char *) (options->server ? options->server : options->hostname);
362 if ((size_t) -1 == (len = convert_string (
363 options->unixcharset, CH_UTF8_MAC,
364 Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
365 LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
367 /* set offset to 0 */
368 memset(begin + *nameoffset, 0, sizeof(offset));
369 data = begin + offset;
371 namelen = htons(len);
372 memcpy( data, &namelen, sizeof(namelen));
373 data += sizeof(namelen);
375 offset = htons(offset);
376 memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
378 /* Now set the flag ... */
379 memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
380 status = ntohs(status);
381 status |= AFPSRVRINFO_SRVUTF8;
382 status = htons(status);
383 memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
386 /* return length of buffer */
387 return (data - begin);
391 /* returns actual offset to signature */
392 static void status_icon(char *data, const unsigned char *icondata,
393 const int iconlen, const int sigoffset)
396 char *sigdata = data + sigoffset;
397 u_int16_t ret, status;
399 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
400 if ( icondata == NULL ) {
402 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
404 data += ntohs( status );
405 memcpy( data, icondata, iconlen);
407 ret = htons(data - start);
410 /* put in signature offset */
412 memcpy(sigdata, &ret, sizeof(ret));
415 /* ---------------------
417 void status_init(AFPConfig *dsiconfig,
418 const struct afp_options *options)
425 if (!dsiconfig || !options)
430 status = dsiconfig->status;
431 maxstatuslen=sizeof(dsiconfig->status);
432 dsi = dsiconfig->obj.handle;
433 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
434 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
435 ipok = sa4->sin_addr.s_addr ? 1 : 0;
437 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
438 for (int i=0; i<16; i++) {
439 if (sa6->sin6_addr.s6_addr[i]) {
449 * These routines must be called in order -- earlier calls
450 * set the offsets for later calls.
452 * using structs is a bad idea, but that's what the original code
453 * does. solaris, in fact, will segfault with them. so, now
454 * we just use the powers of #defines and memcpy.
456 * reply block layout (offsets are 16-bit quantities):
457 * machine type offset -> AFP version count offset ->
458 * UAM count offset -> vol icon/mask offset -> flags ->
460 * server name [padded to even boundary] -> signature offset ->
461 * network address offset ->
463 * at the appropriate offsets:
464 * machine type, afp versions, uams, server signature
465 * (16-bytes), network addresses, volume icon/mask
469 options->server_notif,
470 (options->fqdn || ipok),
472 (options->k5service && options->k5realm && options->fqdn),
474 /* returns offset to signature offset */
475 c = status_server(status, options->server ? options->server :
476 options->hostname, options);
477 status_machine(status);
478 status_versions(status, dsi);
479 status_uams(status, options->uamlist);
480 if (options->flags & OPTION_CUSTOMICON)
481 status_icon(status, icon, sizeof(icon), c);
483 status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
485 sigoff = status_signature(status, &c, options);
486 /* c now contains the offset where the netaddress offset lives */
488 status_netaddress(status, &c, dsi, options);
489 /* c now contains the offset where the Directory Names Count offset lives */
491 statuslen = status_directorynames(status, &c, dsi, options);
492 /* c now contains the offset where the UTF-8 ServerName offset lives */
494 if ( statuslen < maxstatuslen)
495 statuslen = status_utf8servername(status, &c, dsi, options);
498 if ((options->flags & OPTION_CUSTOMICON) == 0) {
499 status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
501 dsi_setstatus(dsi, status, statuslen);
502 dsiconfig->signature = status + sigoff;
503 dsiconfig->statuslen = statuslen;
507 /* set_signature() */
509 /* If found in conf file, use it. */
510 /* If not found in conf file, genarate and append in conf file. */
511 /* If conf file don't exist, create and genarate. */
512 /* If cannot open conf file, use one-time signature. */
513 /* If -signature user:xxxxx, use it. */
515 void set_signature(struct afp_options *options) {
519 char *servername_conf;
522 FILE *fp = NULL, *randomp;
526 server_tmp = (options->server ? options->server : options->hostname);
527 if (strcmp(options->signatureopt, "auto") == 0) {
528 goto server_signature_auto; /* default */
529 } else if (strcmp(options->signatureopt, "host") == 0) {
530 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
531 goto server_signature_auto; /* same as auto */
532 } else if (strncmp(options->signatureopt, "user", 4) == 0) {
533 goto server_signature_user; /* user string */
535 LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
536 goto server_signature_auto; /* switch back to auto*/
539 server_signature_user:
541 /* Signature type is user string */
542 len = strlen(options->signatureopt);
544 LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
545 goto server_signature_auto;
547 usersign = options->signatureopt + 5;
550 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !", usersign);
552 } else if (len >= 3) {
553 LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
555 LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !", usersign);
557 memset(options->signature, 0, 16);
558 memcpy(options->signature, usersign, len);
559 goto server_signature_done;
561 server_signature_auto:
563 /* Signature type is auto, using afp_signature.conf */
564 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
565 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
566 /* scan in the conf file */
567 while (fgets(buf, sizeof(buf), fp) != NULL) {
569 while (p && isblank(*p))
571 if (!p || (*p == '#') || (*p == '\n'))
572 continue; /* invalid line */
575 if ((servername_conf = strtok( p, "\"" )) == NULL)
576 continue; /* syntax error: invalid quoted servername */
578 if ((servername_conf = strtok( p, " \t" )) == NULL)
579 continue; /* syntax error: invalid servername */
584 continue; /* syntax error: missing signature */
586 if (strcmp(server_tmp, servername_conf))
587 continue; /* another servername */
589 while (p && isblank(*p))
591 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
592 &options->signature[ 0], &options->signature[ 1],
593 &options->signature[ 2], &options->signature[ 3],
594 &options->signature[ 4], &options->signature[ 5],
595 &options->signature[ 6], &options->signature[ 7],
596 &options->signature[ 8], &options->signature[ 9],
597 &options->signature[10], &options->signature[11],
598 &options->signature[12], &options->signature[13],
599 &options->signature[14], &options->signature[15]
602 goto server_signature_done; /* found in conf file */
605 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
606 fseek(fp, 0L, SEEK_END);
607 if(ftell(fp) == 0) { /* size = 0 */
609 goto server_signature_random;
611 fseek(fp, -1L, SEEK_END);
612 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
613 goto server_signature_random;
616 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
617 options->sigconffile, strerror(errno));
618 goto server_signature_random;
621 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
622 options->sigconffile, strerror(errno));
623 goto server_signature_random;
625 } else { /* conf file don't exist */
626 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
627 LOG(log_error, logtype_atalkd, "Cannot create %s (%s). Using one-time signature.",
628 options->sigconffile, strerror(errno));
629 goto server_signature_random;
631 if (( fp = fdopen( fd, "w" )) == NULL ) {
632 LOG(log_error, logtype_atalkd, "Cannot fdopen %s (%s). Using one-time signature.",
633 options->sigconffile, strerror(errno));
635 goto server_signature_random;
638 goto server_signature_random;
641 server_signature_random:
643 /* generate signature from random number */
644 randombytes(options->signature, 16);
646 if (fp && header) { /* conf file is created or size=0 */
647 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
648 fprintf(fp, "# This file is auto-generated by afpd.\n");
650 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
651 fprintf(fp, "# the same server twice.\n");
653 fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
657 fprintf(fp, "\"%s\"\t", server_tmp);
658 for (i=0 ; i<16 ; i++) {
659 fprintf(fp, "%02X", (options->signature)[i]);
661 fprintf(fp, "%s", "\n");
665 server_signature_done:
668 LOG(log_info, logtype_afpd,
669 " \"%s\"'s signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
671 (options->signature)[ 0], (options->signature)[ 1],
672 (options->signature)[ 2], (options->signature)[ 3],
673 (options->signature)[ 4], (options->signature)[ 5],
674 (options->signature)[ 6], (options->signature)[ 7],
675 (options->signature)[ 8], (options->signature)[ 9],
676 (options->signature)[10], (options->signature)[11],
677 (options->signature)[12], (options->signature)[13],
678 (options->signature)[14], (options->signature)[15]);
683 /* this is the same as asp/dsi_getstatus */
684 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
686 AFPConfig *config = obj->config;
688 memcpy(rbuf, config->status, config->statuslen);
689 *rbuflen = config->statuslen;