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