]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/status.c
First working IPC reconnect
[netatalk.git] / etc / afpd / status.c
index 745d349c5765768ce49960dfbd121312d55f97a0..f89615b1091dc3c2d88feeff8b5c5858378fe94f 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: status.c,v 1.27 2009-11-05 14:38:07 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>
@@ -30,8 +35,9 @@
 #include <atalk/asp.h>
 #include <atalk/nbp.h>
 #include <atalk/unicode.h>
+#include <atalk/util.h>
+#include <atalk/globals.h>
 
-#include "globals.h"  /* includes <netdb.h> */
 #include "status.h"
 #include "afp_config.h"
 #include "icon.h"
@@ -41,32 +47,26 @@ static   size_t maxstatuslen = 0;
 static void status_flags(char *data, const int notif, const int ipok,
                          const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
 {
-    u_int16_t           status;
+    uint16_t           status;
+
+    status = AFPSRVRINFO_COPY
+           | AFPSRVRINFO_SRVSIGNATURE
+           | AFPSRVRINFO_SRVMSGS
+           | AFPSRVRINFO_FASTBOZO
+           | AFPSRVRINFO_SRVRDIR
+           | AFPSRVRINFO_SRVUTF8
+           | AFPSRVRINFO_EXTSLEEP;
 
-    status = AFPSRVRINFO_COPY;
     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
         status |= AFPSRVRINFO_PASSWD;
     if (passwdbits & PASSWD_NOSAVE)
         status |= AFPSRVRINFO_NOSAVEPASSWD;
-    status |= AFPSRVRINFO_SRVSIGNATURE;
-    /* only advertise tcp/ip if we have a valid address */
-    if (ipok)
+    if (ipok) /* only advertise tcp/ip if we have a valid address */        
         status |= AFPSRVRINFO_TCPIP;
-    status |= AFPSRVRINFO_SRVMSGS;
-    /* Allow the user to decide if we should support server notifications.
-     * With this turned off, the clients will poll for directory changes every
-     * 10 seconds.  This might be too costly to network resources, so make
-     * this an optional thing.  Default will be to _not_ support server
-     * notifications. */
-    if (notif) {
+    if (notif) /* Default is yes */        
         status |= AFPSRVRINFO_SRVNOTIFY;
-    }
-    status |= AFPSRVRINFO_FASTBOZO;
-    status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
-    /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
-
-    if (flags & OPTION_UUID)   /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
-       status |= AFPSRVRINFO_UUID;
+    if (flags & OPTION_UUID)
+        status |= AFPSRVRINFO_UUID;
 
     status = htons(status);
     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
@@ -128,41 +128,29 @@ static void status_machine(char *data)
 #ifdef AFS
     const char         *machine = "afs";
 #else /* !AFS */
-    const char         *machine = "Netatalk";
+    const char         *machine = "Netatalk %s";
 #endif /* AFS */
+    char buf[64];
 
     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
     data += ntohs( status );
-    len = strlen( machine );
+
+    //    len = strlen( machine );
+    len = snprintf(buf, 64, machine, VERSION);
     *data++ = len;
-    memcpy( data, machine, len );
+    memcpy( data, buf, len );
     data += len;
+
     status = htons(data - start);
     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;
 
@@ -173,63 +161,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;
-        }
-    }
-
-    /* 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);
-     }
+    memset(data, 0, 16);
+    memcpy(data, options->signature, 16);
+    data += 16;
 
-server_signature_done:
     /* calculate net address offset */
     *servoffset += sizeof(offset);
     offset = htons(data - status);
@@ -457,13 +392,6 @@ static size_t status_utf8servername(char *data, int *nameoffset,
        data += len;
        offset = htons(offset);
        memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
-        
-        /* Now set the flag ... */
-       memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
-       status = ntohs(status);
-       status |= AFPSRVRINFO_SRVUTF8;
-       status = htons(status);
-       memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
     }
 
     /* return length of buffer */
@@ -495,14 +423,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,
@@ -524,6 +444,7 @@ void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
     } else
         asp = NULL;
        
+    ipok = 0;
     if (dsiconfig) {
         status = dsiconfig->status;
         maxstatuslen=sizeof(dsiconfig->status);
@@ -533,7 +454,6 @@ void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
             ipok = sa4->sin_addr.s_addr ? 1 : 0;
         } else { /* IPv6 */
             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
-            ipok = 0;
             for (int i=0; i<16; i++) {
                 if (sa6->sin6_addr.s6_addr[i]) {
                     ipok = 1;
@@ -574,14 +494,14 @@ void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
     c = status_server(status, options->server ? options->server :
                       options->hostname, options);
     status_machine(status);
-    status_versions(status);
+    status_versions(status, asp, dsi);
     status_uams(status, options->uamlist);
     if (options->flags & OPTION_CUSTOMICON)
         status_icon(status, icon, sizeof(icon), c);
     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);
@@ -613,6 +533,182 @@ 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(struct afp_options *options) {
+    char *usersign;
+    int fd, i;
+    struct stat tmpstat;
+    char *servername_conf;
+    int header = 0;
+    char buf[1024], *p;
+    FILE *fp = NULL, *randomp;
+    size_t len;
+    char *server_tmp;
+    
+    server_tmp = (options->server ? options->server : options->hostname);
+    if (strcmp(options->signatureopt, "auto") == 0) {
+        goto server_signature_auto;   /* default */
+    } else if (strcmp(options->signatureopt, "host") == 0) {
+        LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
+        goto server_signature_auto;   /* same as auto */
+    } else if (strncmp(options->signatureopt, "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.", options->signatureopt);
+        goto server_signature_auto;   /* switch back to auto*/
+    }
+    
+server_signature_user:
+    
+    /* Signature type is user string */
+    len = strlen(options->signatureopt);
+    if (len <= 5) {
+        LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
+        goto server_signature_auto;
+    }
+    usersign = options->signatureopt + 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 = strchr(p, '\0');
+                p++;
+                if (*p == '\0')
+                    continue;                             /* syntax error: missing signature */
+                
+                if (strcmp(server_tmp, 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_afpd, "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_afpd, "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 */
+    randombytes(options->signature, 16);
+
+    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", server_tmp);
+        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",
+        server_tmp,
+        (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)
 {