Imported Upstream version 2.5.2p1
[debian/amanda] / common-src / bsd-security.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: bsd-security.c,v 1.75 2006/07/19 17:41:14 martinea Exp $
28  *
29  * "BSD" security module
30  */
31
32 #include "amanda.h"
33 #include "util.h"
34 #include "clock.h"
35 #include "dgram.h"
36 #include "event.h"
37 #include "packet.h"
38 #include "security.h"
39 #include "security-util.h"
40 #include "stream.h"
41 #include "version.h"
42
43 #ifndef SO_RCVBUF
44 #undef DUMPER_SOCKET_BUFFERING
45 #endif
46
47 #ifdef BSD_SECURITY                                             /* { */
48
49 /*
50  * Change the following from #undef to #define to cause detailed logging
51  * of the security steps, e.g. into /tmp/amanda/amandad*debug.
52  */
53 #undef SHOW_SECURITY_DETAIL
54
55 /*
56  * Interface functions
57  */
58 static void     bsd_connect(const char *, char *(*)(char *, void *), 
59                         void (*)(void *, security_handle_t *, security_status_t),
60                         void *, void *);
61 static void     bsd_accept(const struct security_driver *, int, int,
62                         void (*)(security_handle_t *, pkt_t *));
63 static void     bsd_close(void *);
64 static void *   bsd_stream_server(void *);
65 static int      bsd_stream_accept(void *);
66 static void *   bsd_stream_client(void *, int);
67 static void     bsd_stream_close(void *);
68 static int      bsd_stream_auth(void *);
69 static int      bsd_stream_id(void *);
70 static void     bsd_stream_read(void *, void (*)(void *, void *, ssize_t), void *);
71 static ssize_t  bsd_stream_read_sync(void *, void **);
72 static void     bsd_stream_read_cancel(void *);
73
74 /*
75  * This is our interface to the outside world
76  */
77 const security_driver_t bsd_security_driver = {
78     "BSD",
79     bsd_connect,
80     bsd_accept,
81     bsd_close,
82     udpbsd_sendpkt,
83     udp_recvpkt,
84     udp_recvpkt_cancel,
85     bsd_stream_server,
86     bsd_stream_accept,
87     bsd_stream_client,
88     bsd_stream_close,
89     bsd_stream_auth,
90     bsd_stream_id,
91     tcp_stream_write,
92     bsd_stream_read,
93     bsd_stream_read_sync,
94     bsd_stream_read_cancel,
95     sec_close_connection_none,
96     NULL,
97     NULL
98 };
99
100 /*
101  * This is data local to the datagram socket.  We have one datagram
102  * per process, so it is global.
103  */
104 static udp_handle_t netfd4;
105 static udp_handle_t netfd6;
106 static int not_init4 = 1;
107 static int not_init6 = 1;
108
109 /* generate new handles from here */
110 static int newhandle = 0;
111
112 /*
113  * These are the internal helper functions
114  */
115 static void     stream_read_callback(void *);
116 static void     stream_read_sync_callback(void *);
117
118 /*
119  * Setup and return a handle outgoing to a client
120  */
121
122 static void
123 bsd_connect(
124     const char *        hostname,
125     char *              (*conf_fn)(char *, void *),
126     void                (*fn)(void *, security_handle_t *, security_status_t),
127     void *              arg,
128     void *              datap)
129 {
130     struct sec_handle *bh;
131     struct servent *se;
132     in_port_t port = 0;
133     struct timeval sequence_time;
134     amanda_timezone dontcare;
135     int sequence;
136     char *handle;
137     int result;
138     struct addrinfo hints;
139     struct addrinfo *res = NULL;
140
141     assert(hostname != NULL);
142
143     (void)conf_fn;      /* Quiet unused parameter warning */
144     (void)datap;        /* Quiet unused parameter warning */
145
146     bh = alloc(SIZEOF(*bh));
147     bh->proto_handle=NULL;
148     security_handleinit(&bh->sech, &bsd_security_driver);
149
150     /*
151      * Only init the socket once
152      */
153 #ifdef WORKING_IPV6
154     hints.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ALL;
155     hints.ai_family = AF_INET6;
156 #else
157     hints.ai_flags = AI_CANONNAME;
158     hints.ai_family = AF_INET;
159 #endif
160     hints.ai_socktype = SOCK_DGRAM;
161     hints.ai_protocol = IPPROTO_UDP;
162     hints.ai_addrlen = 0;
163     hints.ai_addr = NULL;
164     hints.ai_canonname = NULL;
165     hints.ai_next = NULL;
166     result = getaddrinfo(hostname, NULL, &hints, &res);
167 #ifdef WORKING_IPV6
168     if (result != 0) {
169         hints.ai_flags = AI_CANONNAME;
170         hints.ai_family = AF_UNSPEC;
171         result = getaddrinfo(hostname, NULL, &hints, &res);
172     }
173 #endif
174     if(result != 0) {
175         dbprintf(("getaddrinfo(%s): %s\n", hostname, gai_strerror(result)));
176         security_seterror(&bh->sech, "getaddrinfo(%s): %s\n", hostname,
177                           gai_strerror(result));
178         (*fn)(arg, &bh->sech, S_ERROR);
179         return;
180     }
181     if (res->ai_canonname == NULL) {
182         dbprintf(("getaddrinfo(%s) did not return a canonical name\n", hostname));
183         security_seterror(&bh->sech,
184                 _("getaddrinfo(%s) did not return a canonical name\n"), hostname);
185         (*fn)(arg, &bh->sech, S_ERROR);
186        return;
187     }
188
189 #ifdef WORKING_IPV6
190     if (res->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
191         uid_t euid;
192         dgram_zero(&netfd6.dgram);
193
194         euid = geteuid();
195         seteuid((uid_t)0);
196         dgram_bind(&netfd6.dgram, res->ai_addr->sa_family, &port);
197         seteuid(euid);
198         netfd6.handle = NULL;
199         netfd6.pkt.body = NULL;
200         netfd6.recv_security_ok = &bsd_recv_security_ok;
201         netfd6.prefix_packet = &bsd_prefix_packet;
202         /*
203          * We must have a reserved port.  Bomb if we didn't get one.
204          */
205         if (port >= IPPORT_RESERVED) {
206             security_seterror(&bh->sech,
207                 "unable to bind to a reserved port (got port %u)",
208                 (unsigned int)port);
209             (*fn)(arg, &bh->sech, S_ERROR);
210             return;
211         }
212         not_init6 = 0;
213         bh->udp = &netfd6;
214     }
215 #endif
216
217     if (res->ai_addr->sa_family == AF_INET && not_init4 == 1) {
218         uid_t euid;
219         dgram_zero(&netfd4.dgram);
220
221         euid = geteuid();
222         seteuid((uid_t)0);
223         dgram_bind(&netfd4.dgram, res->ai_addr->sa_family, &port);
224         seteuid(euid);
225         netfd4.handle = NULL;
226         netfd4.pkt.body = NULL;
227         netfd4.recv_security_ok = &bsd_recv_security_ok;
228         netfd4.prefix_packet = &bsd_prefix_packet;
229         /*
230          * We must have a reserved port.  Bomb if we didn't get one.
231          */
232         if (port >= IPPORT_RESERVED) {
233             security_seterror(&bh->sech,
234                 "unable to bind to a reserved port (got port %u)",
235                 (unsigned int)port);
236             (*fn)(arg, &bh->sech, S_ERROR);
237             return;
238         }
239         not_init4 = 0;
240         bh->udp = &netfd4;
241     }
242
243 #ifdef WORKING_IPV6
244     if (res->ai_addr->sa_family == AF_INET6)
245         bh->udp = &netfd6;
246     else
247 #endif
248         bh->udp = &netfd4;
249
250     auth_debug(1, ("Resolved hostname=%s\n", res->ai_canonname));
251     if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
252         port = AMANDA_SERVICE_DEFAULT;
253     else
254         port = (in_port_t)ntohs(se->s_port);
255     amanda_gettimeofday(&sequence_time, &dontcare);
256     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
257     handle=alloc(15);
258     snprintf(handle, 14, "000-%08x",  (unsigned)newhandle++);
259     if (udp_inithandle(bh->udp, bh, res->ai_canonname,
260         (struct sockaddr_storage *)res->ai_addr, port, handle, sequence) < 0) {
261         (*fn)(arg, &bh->sech, S_ERROR);
262         amfree(bh->hostname);
263         amfree(bh);
264     }
265     else {
266         (*fn)(arg, &bh->sech, S_OK);
267     }
268     amfree(handle);
269
270     freeaddrinfo(res);
271 }
272
273 /*
274  * Setup to accept new incoming connections
275  */
276 static void
277 bsd_accept(
278     const struct security_driver *      driver,
279     int         in,
280     int         out,
281     void        (*fn)(security_handle_t *, pkt_t *))
282 {
283
284     assert(in >= 0 && out >= 0);
285     assert(fn != NULL);
286
287     (void)out;  /* Quiet unused parameter warning */
288     (void)driver; /* Quiet unused parameter warning */
289
290     /*
291      * We assume in and out point to the same socket, and just use
292      * in.
293      */
294     dgram_socket(&netfd4.dgram, in);
295     dgram_socket(&netfd6.dgram, in);
296
297     /*
298      * Assign the function and return.  When they call recvpkt later,
299      * the recvpkt callback will call this function when it discovers
300      * new incoming connections
301      */
302     netfd4.accept_fn = fn;
303     netfd4.recv_security_ok = &bsd_recv_security_ok;
304     netfd4.prefix_packet = &bsd_prefix_packet;
305     netfd4.driver = &bsd_security_driver;
306
307     udp_addref(&netfd4, &udp_netfd_read_callback);
308 }
309
310 /*
311  * Frees a handle allocated by the above
312  */
313 static void
314 bsd_close(
315     void *      cookie)
316 {
317     struct sec_handle *bh = cookie;
318
319     if(bh->proto_handle == NULL) {
320         return;
321     }
322
323     auth_debug(1, ("%s: bsd: close handle '%s'\n",
324                    debug_prefix_time(NULL), bh->proto_handle));
325
326     udp_recvpkt_cancel(bh);
327     if(bh->next) {
328         bh->next->prev = bh->prev;
329     }
330     else {
331         if (!not_init6 && netfd6.bh_last == bh)
332             netfd6.bh_last = bh->prev;
333         else
334             netfd4.bh_last = bh->prev;
335     }
336     if(bh->prev) {
337         bh->prev->next = bh->next;
338     }
339     else {
340         if (!not_init6 && netfd6.bh_first == bh)
341             netfd6.bh_first = bh->next;
342         else
343             netfd4.bh_first = bh->next;
344     }
345
346     amfree(bh->proto_handle);
347     amfree(bh->hostname);
348     amfree(bh);
349 }
350
351 /*
352  * Create the server end of a stream.  For bsd, this means setup a tcp
353  * socket for receiving a connection.
354  */
355 static void *
356 bsd_stream_server(
357     void *      h)
358 {
359     struct sec_stream *bs = NULL;
360     struct sec_handle *bh = h;
361
362     assert(bh != NULL);
363
364     bs = alloc(SIZEOF(*bs));
365     security_streaminit(&bs->secstr, &bsd_security_driver);
366     bs->socket = stream_server(&bs->port, (size_t)STREAM_BUFSIZE, 
367                         (size_t)STREAM_BUFSIZE, 0);
368     if (bs->socket < 0) {
369         security_seterror(&bh->sech,
370             "can't create server stream: %s", strerror(errno));
371         amfree(bs);
372         return (NULL);
373     }
374     bs->fd = -1;
375     bs->ev_read = NULL;
376     return (bs);
377 }
378
379 /*
380  * Accepts a new connection on unconnected streams.  Assumes it is ok to
381  * block on accept()
382  */
383 static int
384 bsd_stream_accept(
385     void *      s)
386 {
387     struct sec_stream *bs = s;
388
389     assert(bs != NULL);
390     assert(bs->socket != -1);
391     assert(bs->fd < 0);
392
393     bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
394     if (bs->fd < 0) {
395         security_stream_seterror(&bs->secstr,
396             "can't accept new stream connection: %s", strerror(errno));
397         return (-1);
398     }
399     return (0);
400 }
401
402 /*
403  * Return a connected stream
404  */
405 static void *
406 bsd_stream_client(
407     void *      h,
408     int         id)
409 {
410     struct sec_stream *bs = NULL;
411     struct sec_handle *bh = h;
412 #ifdef DUMPER_SOCKET_BUFFERING
413     int rcvbuf = SIZEOF(bs->databuf) * 2;
414 #endif
415
416     assert(bh != NULL);
417
418     bs = alloc(SIZEOF(*bs));
419     security_streaminit(&bs->secstr, &bsd_security_driver);
420     bs->fd = stream_client(bh->hostname, (in_port_t)id,
421         STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
422     if (bs->fd < 0) {
423         security_seterror(&bh->sech,
424             "can't connect stream to %s port %d: %s", bh->hostname,
425             id, strerror(errno));
426         amfree(bs);
427         return (NULL);
428     }
429     bs->socket = -1;    /* we're a client */
430     bs->ev_read = NULL;
431 #ifdef DUMPER_SOCKET_BUFFERING
432     setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
433 #endif
434     return (bs);
435 }
436
437 /*
438  * Close and unallocate resources for a stream
439  */
440 static void
441 bsd_stream_close(
442     void *      s)
443 {
444     struct sec_stream *bs = s;
445
446     assert(bs != NULL);
447
448     if (bs->fd != -1)
449         aclose(bs->fd);
450     if (bs->socket != -1)
451         aclose(bs->socket);
452     bsd_stream_read_cancel(bs);
453     amfree(bs);
454 }
455
456 /*
457  * Authenticate a stream.  bsd streams have no authentication
458  */
459 static int
460 bsd_stream_auth(
461     void *      s)
462 {
463     (void)s;            /* Quiet unused parameter warning */
464
465     return (0); /* success */
466 }
467
468 /*
469  * Returns the stream id for this stream.  This is just the local port.
470  */
471 static int
472 bsd_stream_id(
473     void *      s)
474 {
475     struct sec_stream *bs = s;
476
477     assert(bs != NULL);
478
479     return ((int)bs->port);
480 }
481
482 /*
483  * Submit a request to read some data.  Calls back with the given function
484  * and arg when completed.
485  */
486 static void
487 bsd_stream_read(
488     void *      s,
489     void        (*fn)(void *, void *, ssize_t),
490     void *      arg)
491 {
492     struct sec_stream *bs = s;
493
494     /*
495      * Only one read request can be active per stream.
496      */
497     if (bs->ev_read != NULL)
498         event_release(bs->ev_read);
499
500     bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
501     bs->fn = fn;
502     bs->arg = arg;
503 }
504
505 /*
506  * Read a chunk of data to a stream.  Blocks until completion.
507  */
508 static ssize_t
509 bsd_stream_read_sync(
510     void *      s,
511     void **     buf)
512 {
513     struct sec_stream *bs = s;
514
515     assert(bs != NULL);
516
517     /*
518      * Only one read request can be active per stream.
519      */
520     if(bs->ev_read != NULL) {
521         return -1;
522     }
523     bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
524                         stream_read_sync_callback, bs);
525     event_wait(bs->ev_read);
526     *buf = bs->databuf;
527     return (bs->len);
528 }
529
530
531 /*
532  * Callback for bsd_stream_read_sync
533  */
534 static void
535 stream_read_sync_callback(
536     void *      s)
537 {
538     struct sec_stream *bs = s;
539     ssize_t n;
540
541     assert(bs != NULL);
542
543     auth_debug(1, ("%s: bsd: stream_read_callback_sync: fd %d\n",
544                    debug_prefix_time(NULL), bs->fd));
545
546     /*
547      * Remove the event first, in case they reschedule it in the callback.
548      */
549     bsd_stream_read_cancel(bs);
550     do {
551         n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
552     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
553     if (n < 0)
554         security_stream_seterror(&bs->secstr, strerror(errno));
555     bs->len = n;
556 }
557
558 /*
559  * Cancel a previous stream read request.  It's ok if we didn't
560  * have a read scheduled.
561  */
562 static void
563 bsd_stream_read_cancel(
564     void *      s)
565 {
566     struct sec_stream *bs = s;
567
568     assert(bs != NULL);
569     if (bs->ev_read != NULL) {
570         event_release(bs->ev_read);
571         bs->ev_read = NULL;
572     }
573 }
574
575 /*
576  * Callback for bsd_stream_read
577  */
578 static void
579 stream_read_callback(
580     void *      arg)
581 {
582     struct sec_stream *bs = arg;
583     ssize_t n;
584
585     assert(bs != NULL);
586
587     /*
588      * Remove the event first, in case they reschedule it in the callback.
589      */
590     bsd_stream_read_cancel(bs);
591     do {
592         n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
593     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
594
595     if (n < 0)
596         security_stream_seterror(&bs->secstr, strerror(errno));
597
598     (*bs->fn)(bs->arg, bs->databuf, n);
599 }
600
601 #endif  /* BSD_SECURITY */                                      /* } */