2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
17 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <atalk/logger.h>
25 #include <sys/param.h>
26 #ifndef HAVE_GETHOSTID
27 #include <sys/sysctl.h>
28 #endif /* HAVE_GETHOSTID */
32 #include <arpa/inet.h>
35 #ifdef HAVE_KRB5_KRB5_H
36 #include <krb5/krb5.h>
39 #endif /* HAVE_KRB5_KRB5_H */
40 #endif /* HAVE_KERBEROS */
42 #include <atalk/dsi.h>
43 #include <atalk/unicode.h>
44 #include <atalk/util.h>
45 #include <atalk/globals.h>
48 #include "afp_config.h"
52 static size_t maxstatuslen = 0;
54 static int uam_gss_enabled()
56 /* XXX: must be a better way to find out if uam_gss is active */
57 return auth_uamfind(UAM_SERVER_LOGIN_EXT,
59 sizeof("Client Krb v2")) != NULL;
62 static void status_flags(char *data,
65 const unsigned char passwdbits,
71 status = AFPSRVRINFO_COPY
72 | AFPSRVRINFO_SRVSIGNATURE
74 | AFPSRVRINFO_FASTBOZO
76 | AFPSRVRINFO_EXTSLEEP;
78 if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
79 status |= AFPSRVRINFO_PASSWD;
80 if (passwdbits & PASSWD_NOSAVE)
81 status |= AFPSRVRINFO_NOSAVEPASSWD;
82 if (ipok) /* only advertise tcp/ip if we have a valid address */
83 status |= AFPSRVRINFO_TCPIP;
84 if (notif) /* Default is yes */
85 status |= AFPSRVRINFO_SRVNOTIFY;
87 status |= AFPSRVRINFO_SRVRDIR;
88 if (flags & OPTION_UUID)
89 status |= AFPSRVRINFO_UUID;
91 status = htons(status);
92 memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
95 static int status_server(char *data, const char *server, const struct afp_options *options)
98 char *Obj, *Type, *Zone;
103 /* make room for all offsets before server name */
104 data += AFPSTATUS_PRELEN;
106 /* extract the obj part of the server */
107 Obj = (char *) server;
108 if ((size_t)-1 == (len = convert_string(
109 options->unixcharset, options->maccharset,
110 Obj, -1, buf, sizeof(buf))) ) {
111 len = MIN(strlen(Obj), 31);
113 memcpy( data, Obj, len );
114 LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
117 memcpy( data, buf, len );
119 if ((len + 1) & 1) /* pad server name and length byte to even boundary */
123 /* make room for signature and net address offset. save location of
124 * signature offset. we're also making room for directory names offset
125 * and the utf-8 server name offset.
127 * NOTE: technically, we don't need to reserve space for the
128 * signature and net address offsets if they're not going to be
129 * used. as there are no offsets after them, it doesn't hurt to
130 * have them specified though. so, we just do that to simplify
133 * NOTE2: AFP3.1 Documentation states that the directory names offset
134 * is a required feature, even though it can be set to zero.
137 status = htons(len + AFPSTATUS_POSTLEN);
138 memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
139 return len; /* return the offset to beginning of signature offset */
142 static void status_machine(char *data)
148 const char *machine = "afs";
150 const char *machine = "Netatalk%s";
152 char buf[AFPSTATUS_MACHLEN+1];
154 memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
155 data += ntohs( status );
157 if ((strlen(machine) + strlen(VERSION)) <= AFPSTATUS_MACHLEN) {
158 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, VERSION);
160 if (strlen(VERSION) > AFPSTATUS_MACHLEN) {
161 len = snprintf(buf, AFPSTATUS_MACHLEN + 1, VERSION);
163 (void)snprintf(buf, AFPSTATUS_MACHLEN + 1, machine, "");
164 (void)snprintf(buf + AFPSTATUS_MACHLEN - strlen(VERSION),
167 len = AFPSTATUS_MACHLEN;
172 memcpy( data, buf, len );
175 status = htons(data - start);
176 memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
179 /* server signature is a 16-byte quantity */
180 static uint16_t status_signature(char *data, int *servoffset,
181 const struct afp_options *options)
184 uint16_t offset, sigoff;
188 /* get server signature offset */
189 memcpy(&offset, data + *servoffset, sizeof(offset));
190 sigoff = offset = ntohs(offset);
192 /* jump to server signature offset */
196 memcpy(data, options->signature, 16);
199 /* calculate net address offset */
200 *servoffset += sizeof(offset);
201 offset = htons(data - status);
202 memcpy(status + *servoffset, &offset, sizeof(offset));
206 static size_t status_netaddress(char *data, int *servoffset,
208 const struct afp_options *options)
212 size_t addresses_len = 0;
216 /* get net address offset */
217 memcpy(&offset, data + *servoffset, sizeof(offset));
218 data += ntohs(offset);
222 len (byte = sizeof(length + address type + address)
223 address type (byte, ip address = 0x01, ip + port = 0x02,
224 ddp address = 0x03, fqdn = 0x04)
225 address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
228 /* number of addresses. this currently screws up if we have a dsi
229 connection, but we don't have the ip address. to get around this,
230 we turn off the status flag for tcp/ip. */
231 *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) +
232 (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
236 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
237 const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
238 if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
239 *data++ = 6; /* length */
240 *data++ = 0x01; /* basic ip address */
241 memcpy(data, &inaddr->sin_addr.s_addr,
242 sizeof(inaddr->sin_addr.s_addr));
243 data += sizeof(inaddr->sin_addr.s_addr);
246 /* ip address + port */
248 *data++ = 0x02; /* ip address with port */
249 memcpy(data, &inaddr->sin_addr.s_addr,
250 sizeof(inaddr->sin_addr.s_addr));
251 data += sizeof(inaddr->sin_addr.s_addr);
252 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
253 data += sizeof(inaddr->sin_port);
257 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
258 if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
259 *data++ = 18; /* length */
260 *data++ = 6; /* type */
261 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
262 data += sizeof(sa6->sin6_addr.s6_addr);
265 /* ip address + port */
266 *data++ = 20; /* length */
267 *data++ = 7; /* type*/
268 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
269 data += sizeof(sa6->sin6_addr.s6_addr);
270 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
271 data += sizeof(sa6->sin6_port);
278 /* handle DNS names */
279 if (options->fqdn && dsi) {
280 size_t len = strlen(options->fqdn);
281 if ( len + 2 + addresses_len < maxstatuslen - offset) {
284 memcpy(data, options->fqdn, len);
286 addresses_len += len+2;
289 /* Annouce support for SSH tunneled AFP session,
290 * this feature is available since 10.3.2.
291 * According to the specs (AFP 3.1 p.225) this should
292 * be an IP+Port style value, but it only works with
293 * a FQDN. OSX Server uses FQDN as well.
295 if ( len + 2 + addresses_len < maxstatuslen - offset) {
296 if (options->flags & OPTION_ANNOUNCESSH) {
299 memcpy(data, options->fqdn, len);
305 /* calculate/store Directory Services Names offset */
306 offset = htons(data - begin);
307 *servoffset += sizeof(offset);
308 memcpy(begin + *servoffset, &offset, sizeof(offset));
310 /* return length of buffer */
311 return (data - begin);
314 static bool append_directoryname(char **pdata,
317 char* DirectoryNamesCount,
321 if (sizeof(uint8_t) + len > maxstatuslen - offset - *size) {
322 LOG(log_error, logtype_afpd,
323 "status:DirectoryNames: out of space for principal '%s' (len=%d)",
326 } else if (len > 255) {
327 LOG(log_error, logtype_afpd,
328 "status:DirectoryNames: principal '%s' (len=%d) too long (max=255)",
333 LOG(log_info, logtype_afpd,
334 "DirectoryNames[%d]=%s",
335 *DirectoryNamesCount, principal);
337 *DirectoryNamesCount += 1;
340 strncpy(data, principal, len);
343 *size += sizeof(uint8_t) + len;
349 * DirectoryNamesCount offset: uint16_t
351 * DirectoryNamesCount: uint8_t
352 * DirectoryNames: list of UTF-8 Pascal strings (uint8_t + char[1,255])
354 static size_t status_directorynames(char *data,
357 const struct afp_options *options _U_)
362 memcpy(&offset, begin + *diroffset, sizeof(offset));
363 offset = ntohs(offset);
366 char *DirectoryNamesCount = data++;
367 char *DirectoryNames = data;
368 size_t size = sizeof(uint8_t);
369 *DirectoryNamesCount = 0;
371 if (!uam_gss_enabled())
375 krb5_context context;
377 const char *error_msg;
379 if (krb5_init_context(&context)) {
380 LOG(log_error, logtype_afpd,
381 "status:DirectoryNames failed to intialize a krb5_context");
386 if ((ret = krb5_kt_default(context, &keytab)))
389 // figure out which service principal to use
390 krb5_keytab_entry entry;
392 if (options->k5service && options->fqdn && options->k5realm) {
393 LOG(log_debug, logtype_afpd,
394 "status:DirectoryNames: using service principal specified in options");
396 krb5_principal service_principal;
397 if ((ret = krb5_build_principal(context,
399 strlen(options->k5realm),
406 // try to get the given service principal from keytab
407 ret = krb5_kt_get_entry(context,
410 0, // kvno - wildcard
411 0, // enctype - wildcard
413 if (ret == KRB5_KT_NOTFOUND) {
414 krb5_unparse_name(context, service_principal, &principal);
415 LOG(log_error, logtype_afpd,
416 "status:DirectoryNames: specified service principal '%s' not found in keytab",
418 // XXX: should this be krb5_xfree?
419 #ifdef HAVE_KRB5_FREE_UNPARSED_NAME
420 krb5_free_unparsed_name(context, principal);
422 krb5_xfree(principal);
426 krb5_free_principal(context, service_principal);
430 LOG(log_debug, logtype_afpd,
431 "status:DirectoryNames: using first entry from keytab as service principal");
433 krb5_kt_cursor cursor;
434 if ((ret = krb5_kt_start_seq_get(context, keytab, &cursor)))
437 ret = krb5_kt_next_entry(context, keytab, &entry, &cursor);
438 krb5_kt_end_seq_get(context, keytab, &cursor);
443 krb5_unparse_name(context, entry.principal, &principal);
444 krb5_kt_free_entry(context, &entry);
446 append_directoryname(&data,
458 error_msg = krb5_get_error_message(context, ret);
459 LOG(log_note, logtype_afpd, "Can't get principal from default keytab: %s",
461 #ifdef HAVE_KRB5_FREE_ERROR_MESSAGE
462 krb5_free_error_message(context, error_msg);
464 krb5_xfree(error_msg);
469 krb5_kt_close(context, keytab);
470 krb5_free_context(context);
472 if (!options->k5service || !options->fqdn || !options->k5realm)
476 size_t len = snprintf(principal, sizeof(principal), "%s/%s@%s",
477 options->k5service, options->fqdn, options->k5realm);
479 append_directoryname(&data,
485 #endif // HAVE_KERBEROS
488 /* Calculate and store offset for UTF8ServerName */
489 *diroffset += sizeof(uint16_t);
490 offset = htons(data - begin);
491 memcpy(begin + *diroffset, &offset, sizeof(uint16_t));
493 /* return length of buffer */
494 return (data - begin);
497 static size_t status_utf8servername(char *data, int *nameoffset,
499 const struct afp_options *options)
504 uint16_t offset, status;
506 memcpy(&offset, data + *nameoffset, sizeof(offset));
507 offset = ntohs(offset);
510 LOG(log_info, logtype_afpd, "servername: %s", options->hostname);
512 if ((len = convert_string(options->unixcharset,
516 data + sizeof(namelen),
517 maxstatuslen-offset)) == (size_t)-1) {
518 LOG(log_error, logtype_afpd, "Could not set utf8 servername");
520 /* set offset to 0 */
521 memset(begin + *nameoffset, 0, sizeof(offset));
522 data = begin + offset;
524 namelen = htons(len);
525 memcpy( data, &namelen, sizeof(namelen));
526 data += sizeof(namelen);
528 offset = htons(offset);
529 memcpy(begin + *nameoffset, &offset, sizeof(uint16_t));
532 /* return length of buffer */
533 return (data - begin);
537 /* returns actual offset to signature */
538 static void status_icon(char *data, const unsigned char *icondata,
539 const int iconlen, const int sigoffset)
542 char *sigdata = data + sigoffset;
543 uint16_t ret, status;
545 memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
546 if ( icondata == NULL ) {
548 memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
550 data += ntohs( status );
551 memcpy( data, icondata, iconlen);
553 ret = htons(data - start);
556 /* put in signature offset */
558 memcpy(sigdata, &ret, sizeof(ret));
561 /* ---------------------
563 void status_init(AFPObj *obj, DSI *dsi)
565 char *status = dsi->status;
567 int c, sigoff, ipok = 0;
568 const struct afp_options *options = &obj->options;
570 maxstatuslen = sizeof(dsi->status);
572 if (dsi->server.ss_family == AF_INET) { /* IPv4 */
573 const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
574 ipok = sa4->sin_addr.s_addr ? 1 : 0;
576 const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
577 for (int i=0; i<16; i++) {
578 if (sa6->sin6_addr.s6_addr[i]) {
586 * These routines must be called in order -- earlier calls
587 * set the offsets for later calls.
589 * using structs is a bad idea, but that's what the original code
590 * does. solaris, in fact, will segfault with them. so, now
591 * we just use the powers of #defines and memcpy.
593 * reply block layout (offsets are 16-bit quantities):
594 * machine type offset -> AFP version count offset ->
595 * UAM count offset -> vol icon/mask offset -> flags ->
597 * server name [padded to even boundary] -> signature offset ->
598 * network address offset ->
600 * at the appropriate offsets:
601 * machine type, afp versions, uams, server signature
602 * (16-bytes), network addresses, volume icon/mask
606 options->flags & OPTION_SERVERNOTIF,
607 (options->fqdn || ipok),
611 /* returns offset to signature offset */
612 c = status_server(status, options->hostname, options);
613 status_machine(status);
614 status_versions(status, dsi);
615 status_uams(status, options->uamlist);
616 status_icon(status, icon, sizeof(icon), c);
618 sigoff = status_signature(status, &c, options);
619 /* c now contains the offset where the netaddress offset lives */
621 status_netaddress(status, &c, dsi, options);
622 /* c now contains the offset where the Directory Names Count offset lives */
624 statuslen = status_directorynames(status, &c, dsi, options);
625 /* c now contains the offset where the UTF-8 ServerName offset lives */
627 if ( statuslen < maxstatuslen)
628 statuslen = status_utf8servername(status, &c, dsi, options);
630 dsi->signature = status + sigoff;
631 dsi->statuslen = statuslen;
634 /* set_signature() */
636 /* If found in conf file, use it. */
637 /* If not found in conf file, genarate and append in conf file. */
638 /* If conf file don't exist, create and genarate. */
639 /* If cannot open conf file, use one-time signature. */
640 /* If signature = xxxxx, use it. */
642 void set_signature(struct afp_options *options) {
645 char *servername_conf;
648 FILE *fp = NULL, *randomp;
652 server_tmp = options->hostname;
653 len = strlen(options->signatureopt);
655 goto server_signature_auto; /* default */
656 } else if (len < 3) {
657 LOG(log_warning, logtype_afpd, "WARNING: signature string %s is very short !", options->signatureopt);
658 goto server_signature_user;
659 } else if (len > 16) {
660 LOG(log_warning, logtype_afpd, "WARNING: signature string %s is very long !", options->signatureopt);
662 goto server_signature_user;
664 LOG(log_info, logtype_afpd, "signature string is %s.", options->signatureopt);
665 goto server_signature_user;
668 server_signature_user:
670 /* Signature is defined in afp.conf */
671 memset(options->signature, 0, 16);
672 memcpy(options->signature, options->signatureopt, len);
673 goto server_signature_done;
675 server_signature_auto:
677 /* Signature type is auto, using afp_signature.conf */
678 if (!stat(options->sigconffile, &tmpstat)) { /* conf file exists? */
679 if ((fp = fopen(options->sigconffile, "r")) != NULL) { /* read open? */
680 /* scan in the conf file */
681 while (fgets(buf, sizeof(buf), fp) != NULL) {
683 while (p && isblank(*p))
685 if (!p || (*p == '#') || (*p == '\n'))
686 continue; /* invalid line */
689 if ((servername_conf = strtok( p, "\"" )) == NULL)
690 continue; /* syntax error: invalid quoted servername */
692 if ((servername_conf = strtok( p, " \t" )) == NULL)
693 continue; /* syntax error: invalid servername */
698 continue; /* syntax error: missing signature */
700 if (strcmp(server_tmp, servername_conf))
701 continue; /* another servername */
703 while (p && isblank(*p))
705 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
706 &options->signature[ 0], &options->signature[ 1],
707 &options->signature[ 2], &options->signature[ 3],
708 &options->signature[ 4], &options->signature[ 5],
709 &options->signature[ 6], &options->signature[ 7],
710 &options->signature[ 8], &options->signature[ 9],
711 &options->signature[10], &options->signature[11],
712 &options->signature[12], &options->signature[13],
713 &options->signature[14], &options->signature[15]
716 goto server_signature_done; /* found in conf file */
719 if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
720 fseek(fp, 0L, SEEK_END);
721 if(ftell(fp) == 0) { /* size = 0 */
723 goto server_signature_random;
725 fseek(fp, -1L, SEEK_END);
726 if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
727 goto server_signature_random;
730 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
731 options->sigconffile, strerror(errno));
732 goto server_signature_random;
735 LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
736 options->sigconffile, strerror(errno));
737 goto server_signature_random;
739 } else { /* conf file don't exist */
740 if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
741 LOG(log_error, logtype_afpd, "ERROR: Cannot create %s (%s). Using one-time signature.",
742 options->sigconffile, strerror(errno));
743 goto server_signature_random;
745 if (( fp = fdopen( fd, "w" )) == NULL ) {
746 LOG(log_error, logtype_afpd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
747 options->sigconffile, strerror(errno));
749 goto server_signature_random;
752 goto server_signature_random;
755 server_signature_random:
757 /* generate signature from random number */
758 randombytes(options->signature, 16);
760 if (fp && header) { /* conf file is created or size=0 */
761 fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
762 fprintf(fp, "# This file is auto-generated by afpd.\n");
764 fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
765 fprintf(fp, "# the same server twice.\n");
767 fprintf(fp, "# If setting \"signature = xxxxx\" in afp.conf, this file is not used.\n\n");
771 fprintf(fp, "\"%s\"\t", server_tmp);
772 for (i=0 ; i<16 ; i++) {
773 fprintf(fp, "%02X", (options->signature)[i]);
775 fprintf(fp, "%s", "\n");
779 server_signature_done:
782 LOG(log_info, logtype_afpd,
783 "signature is %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
784 (options->signature)[ 0], (options->signature)[ 1],
785 (options->signature)[ 2], (options->signature)[ 3],
786 (options->signature)[ 4], (options->signature)[ 5],
787 (options->signature)[ 6], (options->signature)[ 7],
788 (options->signature)[ 8], (options->signature)[ 9],
789 (options->signature)[10], (options->signature)[11],
790 (options->signature)[12], (options->signature)[13],
791 (options->signature)[14], (options->signature)[15]);
796 /* this is the same as asp/dsi_getstatus */
797 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
799 memcpy(rbuf, obj->dsi->status, obj->dsi->statuslen);
800 *rbuflen = obj->dsi->statuslen;