]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
2203d257b65d953fe3fb65c27dca8a63de463dfc
[netatalk.git] / etc / afpd / status.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <time.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <sys/socket.h>
20 #include <atalk/logger.h>
21
22 #if 0
23 #ifdef BSD4_4
24 #include <sys/param.h>
25 #ifndef HAVE_GETHOSTID
26 #include <sys/sysctl.h>
27 #endif /* HAVE_GETHOSTID */
28 #endif /* BSD4_4 */
29 #endif
30
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
39 #include "globals.h"  /* includes <netdb.h> */
40 #include "status.h"
41 #include "afp_config.h"
42 #include "icon.h"
43
44 static   size_t maxstatuslen = 0;
45
46 static void status_flags(char *data, const int notif, const int ipok,
47                          const unsigned char passwdbits, const int dirsrvcs _U_, int flags)
48 {
49     u_int16_t           status;
50
51     status = AFPSRVRINFO_COPY;
52     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
53         status |= AFPSRVRINFO_PASSWD;
54     if (passwdbits & PASSWD_NOSAVE)
55         status |= AFPSRVRINFO_NOSAVEPASSWD;
56     status |= AFPSRVRINFO_SRVSIGNATURE;
57     /* only advertise tcp/ip if we have a valid address */
58     if (ipok)
59         status |= AFPSRVRINFO_TCPIP;
60     status |= AFPSRVRINFO_SRVMSGS;
61     /* Allow the user to decide if we should support server notifications.
62      * With this turned off, the clients will poll for directory changes every
63      * 10 seconds.  This might be too costly to network resources, so make
64      * this an optional thing.  Default will be to _not_ support server
65      * notifications. */
66     if (notif) {
67         status |= AFPSRVRINFO_SRVNOTIFY;
68     }
69     status |= AFPSRVRINFO_FASTBOZO;
70     status |= AFPSRVRINFO_SRVRDIR; /* AFP 3.1 specs says we need to specify this, but may set the count to 0 */
71     /* We don't set the UTF8 name flag here, we don't know whether we have enough space ... */
72
73     if (flags & OPTION_UUID)    /* 05122008 FIXME: can we set AFPSRVRINFO_UUID here ? see AFPSRVRINFO_SRVRDIR*/
74         status |= AFPSRVRINFO_UUID;
75
76     status = htons(status);
77     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
78 }
79
80 static int status_server(char *data, const char *server, const struct afp_options *options)
81 {
82     char                *start = data;
83     char                *Obj, *Type, *Zone;
84     char                buf[32];
85     u_int16_t           status;
86     size_t              len;
87
88     /* make room for all offsets before server name */
89     data += AFPSTATUS_PRELEN;
90
91     /* extract the obj part of the server */
92     Obj = (char *) server;
93     nbp_name(server, &Obj, &Type, &Zone);
94     if ((size_t)-1 == (len = convert_string( 
95                         options->unixcharset, options->maccharset, 
96                         Obj, -1, buf, sizeof(buf))) ) {
97         len = MIN(strlen(Obj), 31);
98         *data++ = len;
99         memcpy( data, Obj, len );
100         LOG ( log_error, logtype_afpd, "Could not set servername, using fallback");
101     } else {
102         *data++ = len;
103         memcpy( data, buf, len );
104     }
105     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
106         len++;
107     data += len;
108
109     /* make room for signature and net address offset. save location of
110      * signature offset. we're also making room for directory names offset
111      * and the utf-8 server name offset.
112      *
113      * NOTE: technically, we don't need to reserve space for the
114      * signature and net address offsets if they're not going to be
115      * used. as there are no offsets after them, it doesn't hurt to
116      * have them specified though. so, we just do that to simplify
117      * things.  
118      *
119      * NOTE2: AFP3.1 Documentation states that the directory names offset
120      * is a required feature, even though it can be set to zero.
121      */
122     len = data - start;
123     status = htons(len + AFPSTATUS_POSTLEN);
124     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
125     return len; /* return the offset to beginning of signature offset */
126 }
127
128 static void status_machine(char *data)
129 {
130     char                *start = data;
131     u_int16_t           status;
132     int                 len;
133 #ifdef AFS
134     const char          *machine = "afs";
135 #else /* !AFS */
136     const char          *machine = "Netatalk";
137 #endif /* AFS */
138
139     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
140     data += ntohs( status );
141     len = strlen( machine );
142     *data++ = len;
143     memcpy( data, machine, len );
144     data += len;
145     status = htons(data - start);
146     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
147 }
148
149 /* server signature is a 16-byte quantity */
150 static u_int16_t status_signature(char *data, int *servoffset,
151                                   const struct afp_options *options)
152 {
153     char                 *status;
154     u_int16_t            offset, sigoff;
155
156     status = data;
157
158     /* get server signature offset */
159     memcpy(&offset, data + *servoffset, sizeof(offset));
160     sigoff = offset = ntohs(offset);
161
162     /* jump to server signature offset */
163     data += offset;
164
165     memset(data, 0, 16);
166     memcpy(data, options->signature, 16);
167     data += 16;
168
169     /* calculate net address offset */
170     *servoffset += sizeof(offset);
171     offset = htons(data - status);
172     memcpy(status + *servoffset, &offset, sizeof(offset));
173     return sigoff;
174 }
175
176 static size_t status_netaddress(char *data, int *servoffset,
177                              const ASP asp, const DSI *dsi,
178                              const struct afp_options *options)
179 {
180     char               *begin;
181     u_int16_t          offset;
182     size_t             addresses_len = 0;
183
184     begin = data;
185
186     /* get net address offset */
187     memcpy(&offset, data + *servoffset, sizeof(offset));
188     data += ntohs(offset);
189
190     /* format:
191        Address count (byte) 
192        len (byte = sizeof(length + address type + address) 
193        address type (byte, ip address = 0x01, ip + port = 0x02,
194                            ddp address = 0x03, fqdn = 0x04) 
195        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
196        */
197
198     /* number of addresses. this currently screws up if we have a dsi
199        connection, but we don't have the ip address. to get around this,
200        we turn off the status flag for tcp/ip. */
201     *data++ = ((options->fqdn && dsi)? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0) +
202               (((options->flags & OPTION_ANNOUNCESSH) && options->fqdn && dsi)? 1 : 0);
203
204     /* ip address */
205     if (dsi) {
206         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
207             const struct sockaddr_in *inaddr = (struct sockaddr_in *)&dsi->server;
208             if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
209                 *data++ = 6; /* length */
210                 *data++ = 0x01; /* basic ip address */
211                 memcpy(data, &inaddr->sin_addr.s_addr,
212                        sizeof(inaddr->sin_addr.s_addr));
213                 data += sizeof(inaddr->sin_addr.s_addr);
214                 addresses_len += 7;
215             } else {
216                 /* ip address + port */
217                 *data++ = 8;
218                 *data++ = 0x02; /* ip address with port */
219                 memcpy(data, &inaddr->sin_addr.s_addr,
220                        sizeof(inaddr->sin_addr.s_addr));
221                 data += sizeof(inaddr->sin_addr.s_addr);
222                 memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
223                 data += sizeof(inaddr->sin_port);
224                 addresses_len += 9;
225             }
226         } else { /* IPv6 */
227             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
228             if (sa6->sin6_port == htons(DSI_AFPOVERTCP_PORT)) {
229                 *data++ = 18; /* length */
230                 *data++ = 6; /* type */
231                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
232                 data += sizeof(sa6->sin6_addr.s6_addr);
233                 addresses_len += 19;
234             } else {
235                 /* ip address + port */
236                 *data++ = 20; /* length */
237                 *data++ = 7; /* type*/
238                 memcpy(data, &sa6->sin6_addr.s6_addr, sizeof(sa6->sin6_addr.s6_addr));
239                 data += sizeof(sa6->sin6_addr.s6_addr);
240                 memcpy(data, &sa6->sin6_port, sizeof(sa6->sin6_port));
241                 data += sizeof(sa6->sin6_port);
242                 addresses_len += 21;
243             }
244
245         }
246     }
247
248     /* handle DNS names */
249     if (options->fqdn && dsi) {
250         size_t len = strlen(options->fqdn);
251         if ( len + 2 + addresses_len < maxstatuslen - offset) {
252             *data++ = len +2;
253             *data++ = 0x04;
254             memcpy(data, options->fqdn, len);
255             data += len;
256             addresses_len += len+2;
257         }
258
259         /* Annouce support for SSH tunneled AFP session, 
260          * this feature is available since 10.3.2.
261          * According to the specs (AFP 3.1 p.225) this should
262          * be an IP+Port style value, but it only works with 
263          * a FQDN. OSX Server uses FQDN as well.
264          */
265         if ( len + 2 + addresses_len < maxstatuslen - offset) {
266             if (options->flags & OPTION_ANNOUNCESSH) {
267                 *data++ = len +2;
268                 *data++ = 0x05;
269                 memcpy(data, options->fqdn, len);
270                 data += len;
271             }
272         }
273     }
274
275 #ifndef NO_DDP
276     if (asp) {
277         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
278
279         /* ddp address */
280         *data++ = 6;
281         *data++ = 0x03; /* ddp address */
282         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
283         data += sizeof(ddpaddr->sat_addr.s_net);
284         memcpy(data, &ddpaddr->sat_addr.s_node,
285                sizeof(ddpaddr->sat_addr.s_node));
286         data += sizeof(ddpaddr->sat_addr.s_node);
287         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
288         data += sizeof(ddpaddr->sat_port);
289     }
290 #endif /* ! NO_DDP */
291
292     /* calculate/store Directory Services Names offset */
293     offset = htons(data - begin); 
294     *servoffset += sizeof(offset);
295     memcpy(begin + *servoffset, &offset, sizeof(offset));
296
297     /* return length of buffer */
298     return (data - begin);
299 }
300
301 static size_t status_directorynames(char *data, int *diroffset, 
302                                  const DSI *dsi _U_, 
303                                  const struct afp_options *options)
304 {
305     char *begin = data;
306     u_int16_t offset;
307     memcpy(&offset, data + *diroffset, sizeof(offset));
308     offset = ntohs(offset);
309     data += offset;
310
311     /* I can not find documentation of any other purpose for the
312      * DirectoryNames field.
313      */
314     /*
315      * Try to synthesize a principal:
316      * service '/' fqdn '@' realm
317      */
318     if (options->k5service && options->k5realm && options->fqdn) {
319         /* should k5princ be utf8 encoded? */
320         size_t len;
321         char *p = strchr( options->fqdn, ':' );
322         if (p) 
323             *p = '\0';
324         len = strlen( options->k5service ) 
325                         + strlen( options->fqdn )
326                         + strlen( options->k5realm );
327         len+=2; /* '/' and '@' */
328         if ( len > 255 || len+2 > maxstatuslen - offset) {
329             *data++ = 0;
330             LOG ( log_error, logtype_afpd, "status: could not set directory service list, no more room");
331         }        
332         else {
333             *data++ = 1; /* DirectoryNamesCount */
334             *data++ = len;
335             snprintf( data, len + 1, "%s/%s@%s", options->k5service,
336                                 options->fqdn, options->k5realm );
337             data += len;
338             if (p)
339                 *p = ':';
340        }
341     } else {
342         *data++ = 0;
343     }
344
345     /* Calculate and store offset for UTF8ServerName */
346     *diroffset += sizeof(u_int16_t);
347     offset = htons(data - begin);
348     memcpy(begin + *diroffset, &offset, sizeof(u_int16_t));
349
350     /* return length of buffer */
351     return (data - begin);
352 }
353
354 static size_t status_utf8servername(char *data, int *nameoffset,
355                                  const DSI *dsi _U_,
356                                  const struct afp_options *options)
357 {
358     char *Obj, *Type, *Zone;
359     u_int16_t namelen;
360     size_t len;
361     char *begin = data;
362     u_int16_t offset, status;
363
364     memcpy(&offset, data + *nameoffset, sizeof(offset));
365     offset = ntohs(offset);
366     data += offset;
367
368     /* FIXME:
369      * What is the valid character range for an nbpname?
370      *
371      * Apple's server likes to use the non-qualified hostname
372      * This obviously won't work very well if multiple servers are running
373      * on the box.
374      */
375
376     /* extract the obj part of the server */
377     Obj = (char *) (options->server ? options->server : options->hostname);
378     nbp_name(options->server ? options->server : options->hostname, &Obj, &Type, &Zone);
379
380     if ((size_t) -1 == (len = convert_string (
381                                         options->unixcharset, CH_UTF8_MAC, 
382                                         Obj, -1, data+sizeof(namelen), maxstatuslen-offset )) ) {
383         LOG ( log_error, logtype_afpd, "Could not set utf8 servername");
384
385         /* set offset to 0 */
386         memset(begin + *nameoffset, 0, sizeof(offset));
387         data = begin + offset;
388     }
389     else {
390         namelen = htons(len);
391         memcpy( data, &namelen, sizeof(namelen));
392         data += sizeof(namelen);
393         data += len;
394         offset = htons(offset);
395         memcpy(begin + *nameoffset, &offset, sizeof(u_int16_t));
396         
397         /* Now set the flag ... */
398         memcpy(&status, begin + AFPSTATUS_FLAGOFF, sizeof(status));
399         status = ntohs(status);
400         status |= AFPSRVRINFO_SRVUTF8;
401         status = htons(status);
402         memcpy(begin + AFPSTATUS_FLAGOFF, &status, sizeof(status));
403     }
404
405     /* return length of buffer */
406     return (data - begin);
407
408 }
409
410 /* returns actual offset to signature */
411 static void status_icon(char *data, const unsigned char *icondata,
412                         const int iconlen, const int sigoffset)
413 {
414     char                *start = data;
415     char                *sigdata = data + sigoffset;
416     u_int16_t           ret, status;
417
418     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
419     if ( icondata == NULL ) {
420         ret = status;
421         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
422     } else {
423         data += ntohs( status );
424         memcpy( data, icondata, iconlen);
425         data += iconlen;
426         ret = htons(data - start);
427     }
428
429     /* put in signature offset */
430     if (sigoffset)
431         memcpy(sigdata, &ret, sizeof(ret));
432 }
433
434 /* ---------------------
435  */
436 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
437                  const struct afp_options *options)
438 {
439     ASP asp;
440     DSI *dsi;
441     char *status = NULL;
442     size_t statuslen;
443     int c, sigoff, ipok;
444
445     if (!(aspconfig || dsiconfig) || !options)
446         return;
447
448     if (aspconfig) {
449         status = aspconfig->status;
450         maxstatuslen=sizeof(aspconfig->status);
451         asp = aspconfig->obj.handle;
452     } else
453         asp = NULL;
454         
455     ipok = 0;
456     if (dsiconfig) {
457         status = dsiconfig->status;
458         maxstatuslen=sizeof(dsiconfig->status);
459         dsi = dsiconfig->obj.handle;
460         if (dsi->server.ss_family == AF_INET) { /* IPv4 */
461             const struct sockaddr_in *sa4 = (struct sockaddr_in *)&dsi->server;
462             ipok = sa4->sin_addr.s_addr ? 1 : 0;
463         } else { /* IPv6 */
464             const struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&dsi->server;
465             for (int i=0; i<16; i++) {
466                 if (sa6->sin6_addr.s6_addr[i]) {
467                     ipok = 1;
468                     break;
469                 }
470             }
471         }
472     } else
473         dsi = NULL;
474
475     /*
476      * These routines must be called in order -- earlier calls
477      * set the offsets for later calls.
478      *
479      * using structs is a bad idea, but that's what the original code
480      * does. solaris, in fact, will segfault with them. so, now
481      * we just use the powers of #defines and memcpy.
482      *
483      * reply block layout (offsets are 16-bit quantities):
484      * machine type offset -> AFP version count offset ->
485      * UAM count offset -> vol icon/mask offset -> flags ->
486      *
487      * server name [padded to even boundary] -> signature offset ->
488      * network address offset ->
489      *
490      * at the appropriate offsets:
491      * machine type, afp versions, uams, server signature
492      * (16-bytes), network addresses, volume icon/mask 
493      */
494
495     status_flags(status,
496                  options->server_notif,
497                  (options->fqdn || ipok),
498                  options->passwdbits, 
499                  (options->k5service && options->k5realm && options->fqdn),
500                  options->flags);
501     /* returns offset to signature offset */
502     c = status_server(status, options->server ? options->server :
503                       options->hostname, options);
504     status_machine(status);
505     status_versions(status, asp, dsi);
506     status_uams(status, options->uamlist);
507     if (options->flags & OPTION_CUSTOMICON)
508         status_icon(status, icon, sizeof(icon), c);
509     else
510         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
511
512     sigoff = status_signature(status, &c, options);
513     /* c now contains the offset where the netaddress offset lives */
514
515     status_netaddress(status, &c, asp, dsi, options);
516     /* c now contains the offset where the Directory Names Count offset lives */
517
518     statuslen = status_directorynames(status, &c, dsi, options);
519     /* c now contains the offset where the UTF-8 ServerName offset lives */
520
521     if ( statuslen < maxstatuslen) 
522         statuslen = status_utf8servername(status, &c, dsi, options);
523
524 #ifndef NO_DDP
525     if (aspconfig) {
526         if (dsiconfig) /* status is dsiconfig->status */
527             memcpy(aspconfig->status, status, statuslen);
528         asp_setstatus(asp, status, statuslen);
529         aspconfig->signature = status + sigoff;
530         aspconfig->statuslen = statuslen;
531     }
532 #endif /* ! NO_DDP */
533
534     if (dsiconfig) {
535         if ((options->flags & OPTION_CUSTOMICON) == 0) {
536             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
537         }
538         dsi_setstatus(dsi, status, statuslen);
539         dsiconfig->signature = status + sigoff;
540         dsiconfig->statuslen = statuslen;
541     }
542 }
543
544 /* set_signature()                                                    */
545 /*                                                                    */
546 /* If found in conf file, use it.                                     */
547 /* If not found in conf file, genarate and append in conf file.       */
548 /* If conf file don't exist, create and genarate.                     */
549 /* If cannot open conf file, use one-time signature.                  */
550 /* If -signature user:xxxxx, use it.                                  */
551
552 void set_signature(struct afp_options *options) {
553     char *usersign;
554     int fd, i;
555     struct stat tmpstat;
556     char *servername_conf;
557     int header = 0;
558     char buf[1024], *p;
559     FILE *fp = NULL, *randomp;
560     size_t len;
561     char *server_tmp;
562     
563     server_tmp = (options->server ? options->server : options->hostname);
564     if (strcmp(options->signatureopt, "auto") == 0) {
565         goto server_signature_auto;   /* default */
566     } else if (strcmp(options->signatureopt, "host") == 0) {
567         LOG(log_warning, logtype_afpd, "WARNING: option \"-signature host\" is obsoleted. Switching back to auto.", options->signatureopt);
568         goto server_signature_auto;   /* same as auto */
569     } else if (strncmp(options->signatureopt, "user", 4) == 0) {
570         goto server_signature_user;   /*  user string */
571     } else {
572         LOG(log_error, logtype_afpd, "ERROR: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
573         goto server_signature_auto;   /* switch back to auto*/
574     }
575     
576 server_signature_user:
577     
578     /* Signature type is user string */
579     len = strlen(options->signatureopt);
580     if (len <= 5) {
581         LOG(log_warning, logtype_afpd, "WARNING: option \"-signature %s\" is not valid. Switching back to auto.", options->signatureopt);
582         goto server_signature_auto;
583     }
584     usersign = options->signatureopt + 5;
585     len = len - 5;
586     if (len > 16) {
587         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very long !",  usersign);
588         len = 16;
589     } else if (len >= 3) {
590         LOG(log_info, logtype_afpd, "signature user string is %s.", usersign);
591     } else {
592         LOG(log_warning, logtype_afpd, "WARNING: signature user string %s is very short !",  usersign);
593     }
594     memset(options->signature, 0, 16);
595     memcpy(options->signature, usersign, len);
596     goto server_signature_done;
597     
598 server_signature_auto:
599     
600     /* Signature type is auto, using afp_signature.conf */
601     if (!stat(options->sigconffile, &tmpstat)) {                /* conf file exists? */
602         if ((fp = fopen(options->sigconffile, "r")) != NULL) {  /* read open? */
603             /* scan in the conf file */
604             while (fgets(buf, sizeof(buf), fp) != NULL) { 
605                 p = buf;
606                 while (p && isblank(*p))
607                     p++;
608                 if (!p || (*p == '#') || (*p == '\n'))
609                     continue;                             /* invalid line */
610                 if (*p == '"') {
611                     p++;
612                     if ((servername_conf = strtok( p, "\"" )) == NULL)
613                         continue;                         /* syntax error: invalid quoted servername */
614                 } else {
615                     if ((servername_conf = strtok( p, " \t" )) == NULL)
616                         continue;                         /* syntax error: invalid servername */
617                 }
618                 p = strchr(p, '\0');
619                 p++;
620                 if (*p == '\0')
621                     continue;                             /* syntax error: missing signature */
622                 
623                 if (strcmp(server_tmp, servername_conf))
624                     continue;                             /* another servername */
625                 
626                 while (p && isblank(*p))
627                     p++;
628                 if ( 16 == sscanf(p, "%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX%2hhX",
629                                   &options->signature[ 0], &options->signature[ 1],
630                                   &options->signature[ 2], &options->signature[ 3],
631                                   &options->signature[ 4], &options->signature[ 5],
632                                   &options->signature[ 6], &options->signature[ 7],
633                                   &options->signature[ 8], &options->signature[ 9],
634                                   &options->signature[10], &options->signature[11],
635                                   &options->signature[12], &options->signature[13],
636                                   &options->signature[14], &options->signature[15]
637                          )) {
638                     fclose(fp);
639                     goto server_signature_done;                 /* found in conf file */
640                 }
641             }
642             if ((fp = freopen(options->sigconffile, "a+", fp)) != NULL) { /* append because not found */
643                 fseek(fp, 0L, SEEK_END);
644                 if(ftell(fp) == 0) {                     /* size = 0 */
645                     header = 1;
646                     goto server_signature_random;
647                 } else {
648                     fseek(fp, -1L, SEEK_END);
649                     if(fgetc(fp) != '\n') fputc('\n', fp); /* last char is \n? */
650                     goto server_signature_random;
651                 }                    
652             } else {
653                 LOG(log_error, logtype_afpd, "ERROR: Cannot write in %s (%s). Using one-time signature.",
654                     options->sigconffile, strerror(errno));
655                 goto server_signature_random;
656             }
657         } else {
658             LOG(log_error, logtype_afpd, "ERROR: Cannot read %s (%s). Using one-time signature.",
659                 options->sigconffile, strerror(errno));
660             goto server_signature_random;
661         }
662     } else { /* conf file don't exist */
663         if (( fd = creat(options->sigconffile, 0644 )) < 0 ) {
664             LOG(log_error, logtype_atalkd, "Cannot create %s (%s). Using one-time signature.",
665                 options->sigconffile, strerror(errno));
666             goto server_signature_random;
667         }
668         if (( fp = fdopen( fd, "w" )) == NULL ) {
669             LOG(log_error, logtype_atalkd, "Cannot fdopen %s (%s). Using one-time signature.",
670                 options->sigconffile, strerror(errno));
671             close(fd);
672             goto server_signature_random;
673         }
674         header = 1;
675         goto server_signature_random;
676     }
677     
678 server_signature_random:
679     
680     /* generate signature from random number */
681     randombytes(options->signature, 16);
682
683     if (fp && header) { /* conf file is created or size=0 */
684         fprintf(fp, "# DON'T TOUCH NOR COPY THOUGHTLESSLY!\n");
685         fprintf(fp, "# This file is auto-generated by afpd.\n");
686         fprintf(fp, "# \n");
687         fprintf(fp, "# ServerSignature is unique identifier used to prevent logging on to\n");
688         fprintf(fp, "# the same server twice.\n");
689         fprintf(fp, "# \n");
690         fprintf(fp, "# If setting \"-signature user:xxxxx\" in afpd.conf, this file is not used.\n\n");
691     }
692     
693     if (fp) {
694         fprintf(fp, "\"%s\"\t", server_tmp);
695         for (i=0 ; i<16 ; i++) {
696             fprintf(fp, "%02X", (options->signature)[i]);
697         }
698         fprintf(fp, "%s", "\n");
699         fclose(fp);
700     }
701     
702 server_signature_done:
703     
704     /* retrun */
705     LOG(log_info, logtype_afpd,
706         " \"%s\"'s signature is  %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
707         server_tmp,
708         (options->signature)[ 0], (options->signature)[ 1],
709         (options->signature)[ 2], (options->signature)[ 3],
710         (options->signature)[ 4], (options->signature)[ 5],
711         (options->signature)[ 6], (options->signature)[ 7],
712         (options->signature)[ 8], (options->signature)[ 9],
713         (options->signature)[10], (options->signature)[11],
714         (options->signature)[12], (options->signature)[13],
715         (options->signature)[14], (options->signature)[15]);
716     
717     return;
718 }
719
720 /* this is the same as asp/dsi_getstatus */
721 int afp_getsrvrinfo(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
722 {
723     AFPConfig *config = obj->config;
724
725     memcpy(rbuf, config->status, config->statuslen);
726     *rbuflen = config->statuslen;
727     return AFP_OK;
728 }