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