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