]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/status.c
Revert server_notif code. We're testing something new in current, and it
[netatalk.git] / etc / afpd / status.c
1 /*
2  * $Id: status.c,v 1.4.2.4 2002-02-09 05:35:17 jmarcus Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/types.h>
17
18 #ifdef BSD4_4
19 #include <sys/param.h>
20 #ifndef USE_GETHOSTID
21 #include <sys/sysctl.h>
22 #endif /* USE_GETHOSTID */
23 #endif /* BSD4_4 */
24
25 #include <netatalk/at.h>
26 #include <netatalk/endian.h>
27 #include <atalk/dsi.h>
28 #include <atalk/atp.h>
29 #include <atalk/asp.h>
30 #include <atalk/nbp.h>
31
32 #include "globals.h"  /* includes <netdb.h> */
33 #include "status.h"
34 #include "afp_config.h"
35 #include "icon.h"
36
37 static void status_flags(char *data, const int ipok,
38                          const unsigned char passwdbits)
39 {
40     u_int16_t           status;
41
42     status = AFPSRVRINFO_COPY;
43     if (passwdbits & PASSWD_SET) /* some uams may not allow this. */
44         status |= AFPSRVRINFO_PASSWD;
45     if (passwdbits & PASSWD_NOSAVE)
46         status |= AFPSRVRINFO_NOSAVEPASSWD;
47     status |= AFPSRVRINFO_SRVSIGNATURE;
48     /* only advertise tcp/ip if we have a valid address */
49     if (ipok)
50         status |= AFPSRVRINFO_TCPIP;
51     status |= AFPSRVRINFO_SRVMSGS;
52     status |= AFPSRVRINFO_SRVNOTIFY;
53     status |= AFPSRVRINFO_FASTBOZO;
54     status = htons(status);
55     memcpy(data + AFPSTATUS_FLAGOFF, &status, sizeof(status));
56 }
57
58 static int status_server(char *data, const char *server)
59 {
60     char                *start = data;
61     char                *Obj, *Type, *Zone;
62     u_int16_t           status;
63     int                 len;
64
65     /* make room for all offsets before server name */
66     data += AFPSTATUS_PRELEN;
67
68     /* extract the obj part of the server */
69     Obj = (char *) server;
70     nbp_name(server, &Obj, &Type, &Zone);
71     len = strlen(Obj);
72     *data++ = len;
73     memcpy( data, Obj, len );
74     if ((len + 1) & 1) /* pad server name and length byte to even boundary */
75         len++;
76     data += len;
77
78     /* make room for signature and net address offset. save location of
79      * signature offset. 
80      *
81      * NOTE: technically, we don't need to reserve space for the
82      * signature and net address offsets if they're not going to be
83      * used. as there are no offsets after them, it doesn't hurt to
84      * have them specified though. so, we just do that to simplify
85      * things.  
86      */
87     len = data - start;
88     status = htons(len + AFPSTATUS_POSTLEN);
89     memcpy(start + AFPSTATUS_MACHOFF, &status, sizeof(status));
90     return len; /* return the offset to beginning of signature offset */
91 }
92
93 static void status_machine(char *data)
94 {
95     char                *start = data;
96     u_int16_t           status;
97     int                 len;
98 #ifdef AFS
99     const char          *machine = "afs";
100 #else /* !AFS */
101     const char          *machine = "unix";
102 #endif /* AFS */
103
104     memcpy(&status, start + AFPSTATUS_MACHOFF, sizeof(status));
105     data += ntohs( status );
106     len = strlen( machine );
107     *data++ = len;
108     memcpy( data, machine, len );
109     data += len;
110     status = htons(data - start);
111     memcpy(start + AFPSTATUS_VERSOFF, &status, sizeof(status));
112 }
113
114
115 /* server signature is a 16-byte quantity */
116 static u_int16_t status_signature(char *data, int *servoffset, DSI *dsi,
117                                   const char *hostname)
118 {
119     char                 *status;
120     int                  i;
121     u_int16_t            offset, sigoff;
122     long                 hostid;
123     static int           id = 0;
124 #ifdef BSD4_4
125     int                  mib[2];
126     size_t               len;
127 #endif /* BSD4_4 */
128
129     status = data;
130
131     /* get server signature offset */
132     memcpy(&offset, data + *servoffset, sizeof(offset));
133     sigoff = offset = ntohs(offset);
134
135     /* jump to server signature offset */
136     data += offset;
137
138     /* 16-byte signature consists of copies of the hostid */
139 #if defined(BSD4_4) && defined(USE_GETHOSTID)
140     mib[0] = CTL_KERN;
141     mib[1] = KERN_HOSTID;
142     len = sizeof(hostid);
143     sysctl(mib, 2, &hostid, &len, NULL, 0);
144 #else /* BSD4_4 && USE_GETHOSTID */
145     hostid = gethostid();
146 #endif /* BSD4_4 && USE_GETHOSTID */
147     if (!hostid) {
148         if (dsi)
149             hostid = dsi->server.sin_addr.s_addr;
150         else {
151             struct hostent *host;
152
153             if ((host = gethostbyname(hostname)))
154                 hostid = ((struct in_addr *) host->h_addr)->s_addr;
155         }
156     }
157
158     /* it turns out that a server signature screws up separate
159      * servers running on the same machine. to work around that, 
160      * i add in an increment */
161     hostid += id;
162     id++;
163     for (i = 0; i < 16; i += sizeof(hostid)) {
164         memcpy(data, &hostid, sizeof(hostid));
165         data += sizeof(hostid);
166     }
167
168     /* calculate net address offset */
169     *servoffset += sizeof(offset);
170     offset = htons(data - status);
171     memcpy(status + *servoffset, &offset, sizeof(offset));
172     return sigoff;
173 }
174
175 static int status_netaddress(char *data, const int servoffset,
176                              const ASP asp, const DSI *dsi,
177                              const char *fqdn)
178 {
179     char               *begin;
180     u_int16_t          offset;
181
182     begin = data;
183
184     /* get net address offset */
185     memcpy(&offset, data + servoffset, sizeof(offset));
186     data += ntohs(offset);
187
188     /* format:
189        Address count (byte) 
190        len (byte = sizeof(length + address type + address) 
191        address type (byte, ip address = 0x01, ip + port = 0x02,
192                            ddp address = 0x03, fqdn = 0x04) 
193        address (up to 254 bytes, ip = 4, ip + port = 6, ddp = 4)
194        */
195
196     /* number of addresses. this currently screws up if we have a dsi
197        connection, but we don't have the ip address. to get around this,
198        we turn off the status flag for tcp/ip. */
199     *data++ = (fqdn ? 1 : 0) + (dsi ? 1 : 0) + (asp ? 1 : 0);
200
201     /* handle DNS names */
202     if (fqdn) {
203         int len = strlen(fqdn) + 2;
204         *data++ = len;
205         *data++ = 0x04;
206         memcpy(data, fqdn, len);
207         data += len;
208     }
209
210     /* ip address */
211     if (dsi) {
212         const struct sockaddr_in *inaddr = &dsi->server;
213
214         if (inaddr->sin_port == htons(DSI_AFPOVERTCP_PORT)) {
215             *data++ = 6; /* length */
216             *data++ = 0x01; /* basic ip address */
217             memcpy(data, &inaddr->sin_addr.s_addr,
218                    sizeof(inaddr->sin_addr.s_addr));
219             data += sizeof(inaddr->sin_addr.s_addr);
220         } else {
221             /* ip address + port */
222             *data++ = 8;
223             *data++ = 0x02; /* ip address with port */
224             memcpy(data, &inaddr->sin_addr.s_addr,
225                    sizeof(inaddr->sin_addr.s_addr));
226             data += sizeof(inaddr->sin_addr.s_addr);
227             memcpy(data, &inaddr->sin_port, sizeof(inaddr->sin_port));
228             data += sizeof(inaddr->sin_port);
229         }
230     }
231
232 #ifndef NO_DDP
233     if (asp) {
234         const struct sockaddr_at *ddpaddr = atp_sockaddr(asp->asp_atp);
235
236         /* ddp address */
237         *data++ = 6;
238         *data++ = 0x03; /* ddp address */
239         memcpy(data, &ddpaddr->sat_addr.s_net, sizeof(ddpaddr->sat_addr.s_net));
240         data += sizeof(ddpaddr->sat_addr.s_net);
241         memcpy(data, &ddpaddr->sat_addr.s_node,
242                sizeof(ddpaddr->sat_addr.s_node));
243         data += sizeof(ddpaddr->sat_addr.s_node);
244         memcpy(data, &ddpaddr->sat_port, sizeof(ddpaddr->sat_port));
245         data += sizeof(ddpaddr->sat_port);
246     }
247 #endif /* ! NO_DDP */
248
249     /* return length of buffer */
250     return (data - begin);
251 }
252
253 /* returns actual offset to signature */
254 static void status_icon(char *data, const unsigned char *icondata,
255                         const int iconlen, const int sigoffset)
256 {
257     char                *start = data;
258     char                *sigdata = data + sigoffset;
259     u_int16_t           ret, status;
260
261     memcpy(&status, start + AFPSTATUS_ICONOFF, sizeof(status));
262     if ( icondata == NULL ) {
263         ret = status;
264         memset(start + AFPSTATUS_ICONOFF, 0, sizeof(status));
265     } else {
266         data += ntohs( status );
267         memcpy( data, icondata, iconlen);
268         data += iconlen;
269         ret = htons(data - start);
270     }
271
272     /* put in signature offset */
273     if (sigoffset)
274         memcpy(sigdata, &ret, sizeof(ret));
275 }
276
277 void status_init(AFPConfig *aspconfig, AFPConfig *dsiconfig,
278                  const struct afp_options *options)
279 {
280     ASP asp;
281     DSI *dsi;
282     char *status;
283     int c, sigoff;
284
285     if (!(aspconfig || dsiconfig) || !options)
286         return;
287
288     if (aspconfig) {
289         asp = aspconfig->obj.handle;
290         status = aspconfig->status;
291     } else
292         asp = NULL;
293
294     if (dsiconfig) {
295         dsi = dsiconfig->obj.handle;
296         if (!aspconfig)
297             status = dsiconfig->status;
298     } else
299         dsi = NULL;
300
301     /*
302      * These routines must be called in order -- earlier calls
303      * set the offsets for later calls.
304      *
305      * using structs is a bad idea, but that's what the original code
306      * does. solaris, in fact, will segfault with them. so, now
307      * we just use the powers of #defines and memcpy.
308      *
309      * reply block layout (offsets are 16-bit quantities):
310      * machine type offset -> AFP version count offset ->
311      * UAM count offset -> vol icon/mask offset -> flags ->
312      *
313      * server name [padded to even boundary] -> signature offset ->
314      * network address offset ->
315      *
316      * at the appropriate offsets:
317      * machine type, afp versions, uams, server signature
318      * (16-bytes), network addresses, volume icon/mask 
319      */
320
321     status_flags(status, options->fqdn ||
322                  (dsiconfig && dsi->server.sin_addr.s_addr),
323                  options->passwdbits);
324     /* returns offset to signature offset */
325     c = status_server(status, options->server ? options->server :
326                       options->hostname);
327     status_machine(status);
328     status_versions(status);
329     status_uams(status, options->uamlist);
330     if (options->flags & OPTION_CUSTOMICON)
331         status_icon(status, icon, sizeof(icon), c);
332     else
333         status_icon(status, apple_atalk_icon, sizeof(apple_atalk_icon), c);
334
335     sigoff = status_signature(status, &c, dsi, options->hostname);
336
337     /* returns length */
338     c = status_netaddress(status, c, asp, dsi, options->fqdn);
339
340
341 #ifndef NO_DDP
342     if (aspconfig) {
343         asp_setstatus(asp, status, c);
344         aspconfig->signature = status + sigoff;
345         aspconfig->statuslen = c;
346     }
347 #endif /* ! NO_DDP */
348
349     if (dsiconfig) {
350         if (aspconfig) { /* copy to dsiconfig */
351             memcpy(dsiconfig->status, status, ATP_MAXDATA);
352             status = dsiconfig->status;
353         }
354
355         if ((options->flags & OPTION_CUSTOMICON) == 0) {
356             status_icon(status, apple_tcp_icon, sizeof(apple_tcp_icon), 0);
357         }
358         dsi_setstatus(dsi, status, c);
359         dsiconfig->signature = status + sigoff;
360         dsiconfig->statuslen = c;
361     }
362 }
363
364 /* this is the same as asp/dsi_getstatus */
365 int afp_getsrvrinfo(obj, ibuf, ibuflen, rbuf, rbuflen )
366 AFPObj      *obj;
367 char    *ibuf, *rbuf;
368 int             ibuflen, *rbuflen;
369 {
370     AFPConfig *config = obj->config;
371
372     memcpy(rbuf, config->status, config->statuslen);
373     *rbuflen = config->statuslen;
374     return AFP_OK;
375 }