]> arthur.barton.de Git - netatalk.git/commitdiff
New AFP signature algorithm, from HAT
authorfranklahm <franklahm>
Mon, 29 Mar 2010 15:22:57 +0000 (15:22 +0000)
committerfranklahm <franklahm>
Mon, 29 Mar 2010 15:22:57 +0000 (15:22 +0000)
config/afpd.conf.tmpl
configure.in
etc/afpd/Makefile.am
etc/afpd/afp_config.c
etc/afpd/afp_options.c
etc/afpd/globals.h
etc/afpd/status.c
etc/afpd/status.h

index c2830c1450f2a96762881fb56b8b75c346fbca40..91ac7c6f938f0aeb23777fc6a54e55eceea97e83 100644 (file)
 #                         Debugging config
 #                         -setuplog "default log_maxdebug /var/log/afpd.log"
 #
-#     -signature { user:<text> | host }
+#     -signature { user:<text> | auto }
 #                         Specify a server signature. This option is useful while
 #                         running multiple independent instances of afpd on one 
 #                         machine (eg. in clustered environments, to provide fault
-#                         isolation etc.). "host" signature type allows afpd generating
-#                         signature automatically (based on machine primary IP address).
+#                         isolation etc.).
+#                         Default is "auto".
+#                         "auto" signature type allows afpd generating signature and
+#                         saving it to afp_signature.conf automatically (based on random number).
+#                         "host" signature type is obsoleted. (switch back to auto)
 #                         "user" signature type allows administrator to set up a signature
 #                         string manually. Examples: three servers running on one machine:
 #           first       -signature user:USERS
index 7dfab7a106aebee15badef62efcc892735c3cb75..db8b60153175929e7cc9ba23a13458fe24814210 100644 (file)
@@ -1,4 +1,4 @@
-dnl $Id: configure.in,v 1.239 2010-03-12 15:16:48 franklahm Exp $
+dnl $Id: configure.in,v 1.240 2010-03-29 15:22:57 franklahm Exp $
 dnl configure.in for netatalk
 
 AC_INIT(etc/afpd/main.c)
@@ -137,7 +137,7 @@ AC_FUNC_WAIT3
 AC_CHECK_FUNCS(getcwd gethostname gettimeofday getusershell mkdir rmdir select socket strdup strcasestr strstr strtoul strchr memcpy)
 AC_CHECK_FUNCS(backtrace_symbols setlocale nl_langinfo)
 AC_CHECK_FUNCS(waitpid getcwd strdup strndup strnlen strtoul strerror chown fchown chmod fchmod chroot link mknod mknod64)
-AC_CHECK_FUNCS(strlcpy strlcat setlinebuf gethostid dirfd)
+AC_CHECK_FUNCS(strlcpy strlcat setlinebuf dirfd)
 AC_CHECK_FUNC(renameat, AC_DEFINE([_ATFILE_SOURCE], 1, AT file source)) 
 AC_CHECK_MEMBERS(struct tm.tm_gmtoff,,, [#include <time.h>])
 AC_CACHE_SAVE
index 9cab23c467601f0edc0e29f1b7fc7719f73e9911..54ae84fee31e9661e5a88c92f5ec8f8e5d151488 100644 (file)
@@ -23,6 +23,7 @@ afpd_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/sys \
         -D_PATH_AFPDSYSVOL=\"$(pkgconfdir)/AppleVolumes.system\" \
         -D_PATH_AFPDPWFILE=\"$(pkgconfdir)/afppasswd\" \
         -D_PATH_AFPDCONF=\"$(pkgconfdir)/afpd.conf\" \
+        -D_PATH_AFPDSIGCONF=\"$(pkgconfdir)/afp_signature.conf\" \
         -D_PATH_AFPDUAMPATH=\"$(UAMS_PATH)/\" \
         -D_PATH_ACL_LDAPCONF=\"$(pkgconfdir)/afp_ldap.conf\" \
         -DAPPLCNAME \
index c52430d5da5bc09234b033d1efa4ac5729537393..0a46ee2d63acf117d495260f5b81d52a69840715 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: afp_config.c,v 1.31 2009-11-20 17:45:47 franklahm Exp $
+ * $Id: afp_config.c,v 1.32 2010-03-29 15:22:57 franklahm Exp $
  *
  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
  * All Rights Reserved.  See COPYRIGHT.
@@ -548,7 +548,6 @@ AFPConfig *configinit(struct afp_options *cmdline)
     LOG(log_debug, logtype_afpd, "Finished parsing afp_ldap.conf");
 #endif
 
-    status_reset();
     /* if config file doesn't exist, load defaults */
     if ((fp = fopen(cmdline->configfile, "r")) == NULL)
     {
index 3c57fd22fbbcc05ea3a7be1016717eaba0f72f73..59c1cdfa022a5dd0768b97a480d4d0c55174c2e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: afp_options.c,v 1.52 2010-02-03 11:35:58 franklahm Exp $
+ * $Id: afp_options.c,v 1.53 2010-03-29 15:22:57 franklahm Exp $
  *
  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
@@ -165,6 +165,7 @@ void afp_options_init(struct afp_options *options)
     options->defaultvol.name = _PATH_AFPDDEFVOL;
     options->systemvol.name = _PATH_AFPDSYSVOL;
     options->configfile = _PATH_AFPDCONF;
+    options->sigconffile = _PATH_AFPDSIGCONF;
     options->uampath = _PATH_AFPDUAMPATH;
     options->uamlist = "uams_dhx.so,uams_dhx2.so";
     options->guest = "nobody";
@@ -176,7 +177,6 @@ void afp_options_init(struct afp_options *options)
     options->sleep = 10* 120; /* 10 h in 30 seconds tick */
     options->server_notif = 1;
     options->authprintdir = NULL;
-    options->signature = "host";
     options->umask = 0;
 #ifdef ADMIN_GRP
     options->admingid = 0;
@@ -398,8 +398,6 @@ int afp_options_parseline(char *buf, struct afp_options *options)
         options->port = strdup(c);
     if ((c = getoption(buf, "-ddpaddr")))
         atalk_aton(c, &options->ddpaddr);
-    if ((c = getoption(buf, "-signature")) && (opt = strdup(c)))
-        options->signature = opt;
 
     /* do a little checking for the domain name. */
     if ((c = getoption(buf, "-fqdn"))) {
@@ -449,6 +447,13 @@ int afp_options_parseline(char *buf, struct afp_options *options)
     if ((c = getoption(buf, "-ntseparator")) && (opt = strdup(c)))
        options->ntseparator = opt;
      
+    if ((c = getoption(buf, "-signature")) && (opt = strdup(c))) {
+        set_signature(opt, options);
+    }
+    else {
+        set_signature("auto", options);
+    }
+
     return 1;
 }
 
@@ -581,6 +586,7 @@ static void show_version_extended(void )
 static void show_paths( void )
 {
        printf( "             afpd.conf:\t%s\n", _PATH_AFPDCONF );
+       printf( "    afp_signature.conf:\t%s\n", _PATH_AFPDSIGCONF );
        printf( "   AppleVolumes.system:\t%s\n", _PATH_AFPDSYSVOL );
        printf( "  AppleVolumes.default:\t%s\n", _PATH_AFPDDEFVOL );
        printf( "       UAM search path:\t%s\n", _PATH_AFPDUAMPATH );
index f5be0bc358004c1369f4dc728238c4439071ce4e..e096044a2603d63bfcce5860aafd19606b66e306 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: globals.h,v 1.32 2010-03-08 19:49:59 franklahm Exp $
+ * $Id: globals.h,v 1.33 2010-03-29 15:22:57 franklahm Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -63,13 +63,14 @@ struct afp_options {
     struct at_addr ddpaddr;
     char *uampath, *fqdn;
     char *pidfile;
+    char *sigconffile;
     struct afp_volume_name defaultvol, systemvol, uservol;
     int  closevol;
 
     char *guest, *loginmesg, *keyfile, *passwdfile;
     char *uamlist;
     char *authprintdir;
-    char *signature;
+    unsigned char signature[16];
     char *k5service, *k5realm, *k5keytab;
     char *unixcodepage,*maccodepage;
     charset_t maccharset, unixcharset; 
index 96fb97d1f1e86fd876b8fe56d1920a9eb895ebfd..bc6d76ec2b9e88ea38cd1e9bb19fe60738ac9cb2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: status.c,v 1.30 2009-11-23 19:04:14 franklahm Exp $
+ * $Id: status.c,v 1.31 2010-03-29 15:22:57 franklahm Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <ctype.h>
 #include <string.h>
+#include <time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <sys/socket.h>
 #include <atalk/logger.h>
 
+#if 0
 #ifdef BSD4_4
 #include <sys/param.h>
 #ifndef HAVE_GETHOSTID
 #include <sys/sysctl.h>
 #endif /* HAVE_GETHOSTID */
 #endif /* BSD4_4 */
+#endif
 
 #include <netatalk/at.h>
 #include <netatalk/endian.h>
@@ -142,28 +148,12 @@ static void status_machine(char *data)
     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
 }
 
-/* -------------------------------- 
- * it turns out that a server signature screws up separate
- * servers running on the same machine. to work around that, 
- * i add in an increment.
- * Not great, server signature are config dependent but well.
- */
-static int           Id = 0;
-
 /* server signature is a 16-byte quantity */
-static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
+static u_int16_t status_signature(char *data, int *servoffset,
                                   const struct afp_options *options)
 {
     char                 *status;
-    char                *usersign;
-    int                  i;
     u_int16_t            offset, sigoff;
-    long                 hostid;
-#ifdef BSD4_4
-    int                  mib[2];
-    size_t               len;
-#endif /* BSD4_4 */
 
     status = data;
 
@@ -174,63 +164,10 @@ static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
     /* jump to server signature offset */
     data += offset;
 
-    /* Signature type is user string */
-    if (strncmp(options->signature, "user", 4) == 0) {
-        if (strlen(options->signature) <= 5) {
-           LOG(log_warning, logtype_afpd, "Signature %s id not valid. Switching back to hostid.",
-                           options->signature);
-           goto server_signature_hostid;
-    }
-    usersign = options->signature + 5;
-        if (strlen(usersign) < 3) 
-           LOG(log_warning, logtype_afpd, "Signature %s is very short !", 
-                           options->signature);
-    
-        memset(data, 0, 16);
-        strncpy(data, usersign, 16);
-       data += 16;
-        goto server_signature_done;
-    } /* signature = user */
-
-    /* If signature type is a standard hostid... */
-server_signature_hostid:
-    /* 16-byte signature consists of copies of the hostid */
-#if defined(BSD4_4) && !defined(HAVE_GETHOSTID)
-    mib[0] = CTL_KERN;
-    mib[1] = KERN_HOSTID;
-    len = sizeof(hostid);
-    sysctl(mib, 2, &hostid, &len, NULL, 0);
-#else /* BSD4_4 && !HAVE_GETHOSTID */
-    hostid = gethostid();
-#endif /* BSD4_4 && !HAVE_GETHOSTID */
-    if (!hostid) {
-        if (dsi) {
-            if (dsi->server.ss_family == AF_INET) { /* IPv4 */
-                hostid = ((struct sockaddr_in *)(&dsi->server))->sin_addr.s_addr;
-            } else {                                /* IPv6 */
-                struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
-                /* Use the last "sizeof(long) bytes of the IPv6 addr */
-                memcpy(&hostid, sa6->sin6_addr.s6_addr + (16 - sizeof(long)), sizeof(long));
-            }
-        } else {
-            struct hostent *host;
-
-            if ((host = gethostbyname(options->hostname)))
-                hostid = ((struct in_addr *) host->h_addr)->s_addr;
-        }
-    }
+    memset(data, 0, 16);
+    memcpy(data, options->signature, 16);
+    data += 16;
 
-    /* it turns out that a server signature screws up separate
-     * servers running on the same machine. to work around that, 
-     * i add in an increment */
-    hostid += Id;
-    Id++;
-    for (i = 0; i < 16; i += sizeof(hostid)) {
-        memcpy(data, &hostid, sizeof(hostid));
-        data += sizeof(hostid);
-     }
-
-server_signature_done:
     /* calculate net address offset */
     *servoffset += sizeof(offset);
     offset = htons(data - status);
@@ -496,14 +433,6 @@ static void status_icon(char *data, const unsigned char *icondata,
         memcpy(sigdata, &ret, sizeof(ret));
 }
 
-/* ---------------------
-*/
-void status_reset(void)
-{
-    Id = 0;
-}
-
-
 /* ---------------------
  */
 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
@@ -582,7 +511,7 @@ void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
     else
         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
 
-    sigoff = status_signature(status, &c, dsi, options);
+    sigoff = status_signature(status, &c, options);
     /* c now contains the offset where the netaddress offset lives */
 
     status_netaddress(status, &c, asp, dsi, options);
@@ -614,6 +543,212 @@ void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
     }
 }
 
+/* set_signature()                                                    */
+/*                                                                    */
+/* If found in conf file, use it.                                     */
+/* If not found in conf file, genarate and append in conf file.       */
+/* If conf file don't exist, create and genarate.                     */
+/* If cannot open conf file, use one-time signature.                  */
+/* If -signature user:xxxxx, use it.                                  */
+
+void set_signature(char *opt, struct afp_options *options) {
+    char *usersign;
+    int fd, i;
+    struct stat tmpstat;
+    char *servername_conf;
+    int header = 0;
+    char buf[1024], *p;
+    FILE *fp, *randomp;
+    size_t len;
+    
+    if (strcmp(opt, "auto") == 0) {
+        goto server_signature_auto;   /* default */
+    } else if (strcmp(opt, "host") == 0) {
+        LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", opt);
+        goto server_signature_auto;   /* same as auto */
+    } else if (strncmp(opt, "user", 4) == 0) {
+        goto server_signature_user;   /*  user string */
+    } else {
+        LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", opt);
+        goto server_signature_auto;   /* switch back to auto*/
+    }
+    
+server_signature_user:
+    
+    /* Signature type is user string */
+    len = strlen(opt);
+    if (len <= 5) {
+        LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", opt);
+        goto server_signature_auto;
+    }
+    usersign = opt + 5;
+    len = len - 5;
+    if (len > 16) {
+        LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !",  usersign);
+        len = 16;
+    } else if (len >= 3) {
+        LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
+    } else {
+        LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !",  usersign);
+    }
+    memset(options->signature, 0, 16);
+    memcpy(options->signature, usersign, len);
+    goto server_signature_done;
+    
+server_signature_auto:
+    
+    /* Signature type is auto, using afp_signature.conf */
+    if (!stat(options->sigconffile, &tmpstat)) {                /* conf file exists? */
+        if ((fp = fopen(options->sigconffile, "r")) != NULL) {  /* read open? */
+            /* scan in the conf file */
+            while (fgets(buf, sizeof(buf), fp) != NULL) { 
+                p = buf;
+                while (p && isblank(*p))
+                    p++;
+                if (!p || (*p == '#') || (*p == '\n'))
+                    continue;                             /* invalid line */
+                if (*p == '"') {
+                    p++;
+                    if ((servername_conf = strtok( p, "\"" )) == NULL)
+                        continue;                         /* syntax error: invalid quoted servername */
+                } else {
+                    if ((servername_conf = strtok( p, " \t" )) == NULL)
+                        continue;                         /* syntax error: invalid servername */
+                }
+                p = index(p, '\0');
+                p++;
+                if (*p == '\0')
+                    continue;                             /* syntax error: missing signature */
+                
+                if (strcmp(options->server, servername_conf))
+                    continue;                             /* another servername */
+                
+                while (p && isblank(*p))
+                    p++;
+                if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
+                                  &options->signature[ 0], &options->signature[ 1],
+                                  &options->signature[ 2], &options->signature[ 3],
+                                  &options->signature[ 4], &options->signature[ 5],
+                                  &options->signature[ 6], &options->signature[ 7],
+                                  &options->signature[ 8], &options->signature[ 9],
+                                  &options->signature[10], &options->signature[11],
+                                  &options->signature[12], &options->signature[13],
+                                  &options->signature[14], &options->signature[15]
+                         )) {
+                    fclose(fp);
+                    goto server_signature_done;                 /* found in conf file */
+                }
+            }
+            if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
+                fseek(fp, 0L, SEEK_END);
+                if(ftell(fp) == 0) {                     /* size = 0 */
+                    header = 1;
+                    goto server_signature_random;
+                } else {
+                    fseek(fp, -1L, SEEK_END);
+                    if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
+                    goto server_signature_random;
+                }                    
+            } else {
+                LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
+                    options->sigconffile, strerror(errno));
+                goto server_signature_random;
+            }
+        } else {
+            LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
+                options->sigconffile, strerror(errno));
+            goto server_signature_random;
+        }
+    } else {                                                          /* conf file don't exist */
+        if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
+            LOG(log_error, logtype_atalkd, "ERROR: Cannot create %s (%s). Using one-time signature.",
+                options->sigconffile, strerror(errno));
+            goto server_signature_random;
+        }
+        if (( fp = fdopen( fd, "w" )) == NULL ) {
+            LOG(log_error, logtype_atalkd, "ERROR: Cannot fdopen %s (%s). Using one-time signature.",
+                options->sigconffile, strerror(errno));
+            close(fd);
+            goto server_signature_random;
+        }
+        header = 1;
+        goto server_signature_random;
+    }
+    
+server_signature_random:
+    
+    /* generate signature from random number */
+    if ((randomp = fopen("/dev/urandom", "r")) != NULL) {   /* generate from /dev/urandom */
+        for (i=0 ; i<16 ; i++) {
+            (options->signature)[i] = fgetc(randomp);
+        }
+        LOG(log_note, logtype_afpd,
+            "generate %s's signature %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X from /dev/urandom",
+            options->server,
+            (options->signature)[ 0], (options->signature)[ 1],
+            (options->signature)[ 2], (options->signature)[ 3],
+            (options->signature)[ 4], (options->signature)[ 5],
+            (options->signature)[ 6], (options->signature)[ 7],
+            (options->signature)[ 8], (options->signature)[ 9],
+            (options->signature)[10], (options->signature)[11],
+            (options->signature)[12], (options->signature)[13],
+            (options->signature)[14], (options->signature)[15]);
+        
+    } else {                                   /* genarate from random() because cannot open /dev/urandom */
+        srandom((unsigned int)time(NULL) + (unsigned int)options + (unsigned int)options->server);
+        for (i=0 ; i<16 ; i++) {
+            (options->signature)[i] = random() & 0xFF;
+        }
+        LOG(log_note, logtype_afpd,
+            "generate %s's signature %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X from random()",
+            options->server,
+            (options->signature)[ 0], (options->signature)[ 1],
+            (options->signature)[ 2], (options->signature)[ 3],
+            (options->signature)[ 4], (options->signature)[ 5],
+            (options->signature)[ 6], (options->signature)[ 7],
+            (options->signature)[ 8], (options->signature)[ 9],
+            (options->signature)[10], (options->signature)[11],
+            (options->signature)[12], (options->signature)[13],
+            (options->signature)[14], (options->signature)[15]);
+    }
+
+    if (fp && header) {                                     /* conf file is created or size=0 */
+        fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
+        fprintf(fp, "# This file is auto-generated by afpd.\n");
+        fprintf(fp, "# \n");
+        fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
+        fprintf(fp, "# the same server twice.\n");
+        fprintf(fp, "# \n");
+        fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
+    }
+    
+    if (fp) {
+        fprintf(fp, "\"%s\"\t", options->server);
+        for (i=0 ; i<16 ; i++) {
+            fprintf(fp, "%02X", (options->signature)[i]);
+        }
+        fprintf(fp, "%s", "\n");
+        fclose(fp);
+    }
+    
+server_signature_done:
+    
+    /* retrun */
+    LOG(log_info, logtype_afpd,
+        " \"%s\"'s signature is  %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+        options->server,
+        (options->signature)[ 0], (options->signature)[ 1],
+        (options->signature)[ 2], (options->signature)[ 3],
+        (options->signature)[ 4], (options->signature)[ 5],
+        (options->signature)[ 6], (options->signature)[ 7],
+        (options->signature)[ 8], (options->signature)[ 9],
+        (options->signature)[10], (options->signature)[11],
+        (options->signature)[12], (options->signature)[13],
+        (options->signature)[14], (options->signature)[15]);
+    
+    return;
+}
+
 /* this is the same as asp/dsi_getstatus */
 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
index 759c2b26f6987c92e6b95916d2ad945ca78a9a50..c4a696bd590b9698903711e6c6d700eabf9e0168 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: status.h,v 1.10 2009-11-22 11:52:38 franklahm Exp $
+ * $Id: status.h,v 1.11 2010-03-29 15:22:57 franklahm Exp $
  */
 
 #ifndef AFPD_STATUS_H
 
 extern void status_versions (char * /*status*/, const ASP, const DSI *);
 extern void status_uams (char * /*status*/, const char * /*authlist*/);
-extern void status_reset (void );
 extern void status_init (AFPConfig *, AFPConfig *,
                                  const struct afp_options *);
+extern void set_signature(char *, struct afp_options *);
+
 /* FP functions */
 int afp_getsrvrinfo (AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf,  size_t *rbuflen);