Imported Upstream version 2.6.0
[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 /*
48  * Interface functions
49  */
50 static void     bsd_connect(const char *, char *(*)(char *, void *), 
51                         void (*)(void *, security_handle_t *, security_status_t),
52                         void *, void *);
53 static void     bsd_accept(const struct security_driver *,
54                         char *(*)(char *, void *),
55                         int, int,
56                         void (*)(security_handle_t *, pkt_t *),
57                         void *);
58 static void     bsd_close(void *);
59 static void *   bsd_stream_server(void *);
60 static int      bsd_stream_accept(void *);
61 static void *   bsd_stream_client(void *, int);
62 static void     bsd_stream_close(void *);
63 static int      bsd_stream_auth(void *);
64 static int      bsd_stream_id(void *);
65 static void     bsd_stream_read(void *, void (*)(void *, void *, ssize_t), void *);
66 static ssize_t  bsd_stream_read_sync(void *, void **);
67 static void     bsd_stream_read_cancel(void *);
68
69 /*
70  * This is our interface to the outside world
71  */
72 const security_driver_t bsd_security_driver = {
73     "BSD",
74     bsd_connect,
75     bsd_accept,
76     bsd_close,
77     udpbsd_sendpkt,
78     udp_recvpkt,
79     udp_recvpkt_cancel,
80     bsd_stream_server,
81     bsd_stream_accept,
82     bsd_stream_client,
83     bsd_stream_close,
84     bsd_stream_auth,
85     bsd_stream_id,
86     tcp_stream_write,
87     bsd_stream_read,
88     bsd_stream_read_sync,
89     bsd_stream_read_cancel,
90     sec_close_connection_none,
91     NULL,
92     NULL
93 };
94
95 /*
96  * This is data local to the datagram socket.  We have one datagram
97  * per process, so it is global.
98  */
99 static udp_handle_t netfd4;
100 static udp_handle_t netfd6;
101 static int not_init4 = 1;
102 static int not_init6 = 1;
103
104 /* generate new handles from here */
105 static int newhandle = 0;
106
107 /*
108  * These are the internal helper functions
109  */
110 static void     stream_read_callback(void *);
111 static void     stream_read_sync_callback(void *);
112
113 /*
114  * Setup and return a handle outgoing to a client
115  */
116
117 static void
118 bsd_connect(
119     const char *        hostname,
120     char *              (*conf_fn)(char *, void *),
121     void                (*fn)(void *, security_handle_t *, security_status_t),
122     void *              arg,
123     void *              datap)
124 {
125     struct sec_handle *bh;
126     struct servent *se;
127     in_port_t port = 0;
128     struct timeval sequence_time;
129     int sequence;
130     char *handle;
131     int result;
132     struct addrinfo *res, *res_addr;
133     char *canonname;
134     int result_bind;
135
136     assert(hostname != NULL);
137
138     (void)conf_fn;      /* Quiet unused parameter warning */
139     (void)datap;        /* Quiet unused parameter warning */
140
141     bh = alloc(SIZEOF(*bh));
142     bh->proto_handle=NULL;
143     security_handleinit(&bh->sech, &bsd_security_driver);
144
145     result = resolve_hostname(hostname, SOCK_DGRAM, &res, &canonname);
146     if(result != 0) {
147         dbprintf(_("resolve_hostname(%s): %s\n"), hostname, gai_strerror(result));
148         security_seterror(&bh->sech, _("resolve_hostname(%s): %s\n"), hostname,
149                           gai_strerror(result));
150         (*fn)(arg, &bh->sech, S_ERROR);
151         return;
152     }
153     if (canonname == NULL) {
154         dbprintf(_("resolve_hostname(%s) did not return a canonical name\n"), hostname);
155         security_seterror(&bh->sech,
156                 _("resolve_hostname(%s) did not return a canonical name\n"), hostname);
157         (*fn)(arg, &bh->sech, S_ERROR);
158        return;
159     }
160     if (res == NULL) {
161         dbprintf(_("resolve_hostname(%s): no results\n"), hostname);
162         security_seterror(&bh->sech,
163                 _("resolve_hostname(%s): no results\n"), hostname);
164         (*fn)(arg, &bh->sech, S_ERROR);
165        amfree(canonname);
166        return;
167     }
168
169     for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
170 #ifdef WORKING_IPV6
171         /* IPv6 socket already bound */
172         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 0) {
173             break;
174         }
175         /*
176          * Only init the IPv6 socket once
177          */
178         if (res_addr->ai_addr->sa_family == AF_INET6 && not_init6 == 1) {
179             uid_t euid;
180             dgram_zero(&netfd6.dgram);
181
182             euid = geteuid();
183             set_root_privs(1);
184             result_bind = dgram_bind(&netfd6.dgram,
185                                      res_addr->ai_addr->sa_family, &port);
186             set_root_privs(0);
187             if (result_bind != 0) {
188                 continue;
189             }
190             netfd6.handle = NULL;
191             netfd6.pkt.body = NULL;
192             netfd6.recv_security_ok = &bsd_recv_security_ok;
193             netfd6.prefix_packet = &bsd_prefix_packet;
194             /*
195              * We must have a reserved port.  Bomb if we didn't get one.
196              */
197             if (port >= IPPORT_RESERVED) {
198                 security_seterror(&bh->sech,
199                     _("unable to bind to a reserved port (got port %u)"),
200                     (unsigned int)port);
201                 (*fn)(arg, &bh->sech, S_ERROR);
202                 freeaddrinfo(res);
203                 amfree(canonname);
204                 return;
205             }
206             not_init6 = 0;
207             bh->udp = &netfd6;
208             break;
209         }
210 #endif
211
212         /* IPv4 socket already bound */
213         if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 0) {
214             break;
215         }
216
217         /*
218          * Only init the IPv4 socket once
219          */
220         if (res_addr->ai_addr->sa_family == AF_INET && not_init4 == 1) {
221             uid_t euid;
222             dgram_zero(&netfd4.dgram);
223
224             euid = geteuid();
225             set_root_privs(1);
226             result_bind = dgram_bind(&netfd4.dgram,
227                                      res_addr->ai_addr->sa_family, &port);
228             set_root_privs(0);
229             if (result_bind != 0) {
230                 continue;
231             }
232             netfd4.handle = NULL;
233             netfd4.pkt.body = NULL;
234             netfd4.recv_security_ok = &bsd_recv_security_ok;
235             netfd4.prefix_packet = &bsd_prefix_packet;
236             /*
237              * We must have a reserved port.  Bomb if we didn't get one.
238              */
239             if (port >= IPPORT_RESERVED) {
240                 security_seterror(&bh->sech,
241                     "unable to bind to a reserved port (got port %u)",
242                     (unsigned int)port);
243                 (*fn)(arg, &bh->sech, S_ERROR);
244                 freeaddrinfo(res);
245                 amfree(canonname);
246                 return;
247             }
248             not_init4 = 0;
249             bh->udp = &netfd4;
250             break;
251         }
252     }
253
254     if (res_addr == NULL) {
255         dbprintf(_("Can't bind a socket to connect to %s\n"), hostname);
256         security_seterror(&bh->sech,
257                 _("Can't bind a socket to connect to %s\n"), hostname);
258         (*fn)(arg, &bh->sech, S_ERROR);
259        amfree(canonname);
260        return;
261     }
262
263 #ifdef WORKING_IPV6
264     if (res_addr->ai_addr->sa_family == AF_INET6)
265         bh->udp = &netfd6;
266     else
267 #endif
268         bh->udp = &netfd4;
269
270     auth_debug(1, _("Resolved hostname=%s\n"), canonname);
271     if ((se = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
272         port = AMANDA_SERVICE_DEFAULT;
273     else
274         port = (in_port_t)ntohs(se->s_port);
275     amanda_gettimeofday(&sequence_time);
276     sequence = (int)sequence_time.tv_sec ^ (int)sequence_time.tv_usec;
277     handle=alloc(15);
278     g_snprintf(handle, 14, "000-%08x",  (unsigned)newhandle++);
279     if (udp_inithandle(bh->udp, bh, canonname,
280         (struct sockaddr_storage *)res_addr->ai_addr, port, handle, sequence) < 0) {
281         (*fn)(arg, &bh->sech, S_ERROR);
282         amfree(bh->hostname);
283         amfree(bh);
284     }
285     else {
286         (*fn)(arg, &bh->sech, S_OK);
287     }
288     amfree(handle);
289     amfree(canonname);
290
291     freeaddrinfo(res);
292 }
293
294 /*
295  * Setup to accept new incoming connections
296  */
297 static void
298 bsd_accept(
299     const struct security_driver *      driver,
300     char       *(*conf_fn)(char *, void *),
301     int         in,
302     int         out,
303     void        (*fn)(security_handle_t *, pkt_t *),
304     void       *datap)
305 {
306
307     assert(in >= 0 && out >= 0);
308     assert(fn != NULL);
309
310     (void)out;  /* Quiet unused parameter warning */
311     (void)driver; /* Quiet unused parameter warning */
312     (void)conf_fn;
313     (void)datap;
314
315     /*
316      * We assume in and out point to the same socket, and just use
317      * in.
318      */
319     dgram_socket(&netfd4.dgram, in);
320     dgram_socket(&netfd6.dgram, in);
321
322     /*
323      * Assign the function and return.  When they call recvpkt later,
324      * the recvpkt callback will call this function when it discovers
325      * new incoming connections
326      */
327     netfd4.accept_fn = fn;
328     netfd4.recv_security_ok = &bsd_recv_security_ok;
329     netfd4.prefix_packet = &bsd_prefix_packet;
330     netfd4.driver = &bsd_security_driver;
331
332     udp_addref(&netfd4, &udp_netfd_read_callback);
333 }
334
335 /*
336  * Frees a handle allocated by the above
337  */
338 static void
339 bsd_close(
340     void *      cookie)
341 {
342     struct sec_handle *bh = cookie;
343
344     if(bh->proto_handle == NULL) {
345         return;
346     }
347
348     auth_debug(1, _("bsd: close handle '%s'\n"), bh->proto_handle);
349
350     udp_recvpkt_cancel(bh);
351     if(bh->next) {
352         bh->next->prev = bh->prev;
353     }
354     else {
355         if (!not_init6 && netfd6.bh_last == bh)
356             netfd6.bh_last = bh->prev;
357         else
358             netfd4.bh_last = bh->prev;
359     }
360     if(bh->prev) {
361         bh->prev->next = bh->next;
362     }
363     else {
364         if (!not_init6 && netfd6.bh_first == bh)
365             netfd6.bh_first = bh->next;
366         else
367             netfd4.bh_first = bh->next;
368     }
369
370     amfree(bh->proto_handle);
371     amfree(bh->hostname);
372     amfree(bh);
373 }
374
375 /*
376  * Create the server end of a stream.  For bsd, this means setup a tcp
377  * socket for receiving a connection.
378  */
379 static void *
380 bsd_stream_server(
381     void *      h)
382 {
383     struct sec_stream *bs = NULL;
384     struct sec_handle *bh = h;
385
386     assert(bh != NULL);
387
388     bs = alloc(SIZEOF(*bs));
389     security_streaminit(&bs->secstr, &bsd_security_driver);
390     bs->socket = stream_server(bh->udp->peer.ss_family, &bs->port,
391                                (size_t)STREAM_BUFSIZE, (size_t)STREAM_BUFSIZE,
392                                0);
393     if (bs->socket < 0) {
394         security_seterror(&bh->sech,
395             _("can't create server stream: %s"), strerror(errno));
396         amfree(bs);
397         return (NULL);
398     }
399     bs->fd = -1;
400     bs->ev_read = NULL;
401     return (bs);
402 }
403
404 /*
405  * Accepts a new connection on unconnected streams.  Assumes it is ok to
406  * block on accept()
407  */
408 static int
409 bsd_stream_accept(
410     void *      s)
411 {
412     struct sec_stream *bs = s;
413
414     assert(bs != NULL);
415     assert(bs->socket != -1);
416     assert(bs->fd < 0);
417
418     bs->fd = stream_accept(bs->socket, 30, STREAM_BUFSIZE, STREAM_BUFSIZE);
419     if (bs->fd < 0) {
420         security_stream_seterror(&bs->secstr,
421             _("can't accept new stream connection: %s"), strerror(errno));
422         return (-1);
423     }
424     return (0);
425 }
426
427 /*
428  * Return a connected stream
429  */
430 static void *
431 bsd_stream_client(
432     void *      h,
433     int         id)
434 {
435     struct sec_stream *bs = NULL;
436     struct sec_handle *bh = h;
437 #ifdef DUMPER_SOCKET_BUFFERING
438     int rcvbuf = SIZEOF(bs->databuf) * 2;
439 #endif
440
441     assert(bh != NULL);
442
443     bs = alloc(SIZEOF(*bs));
444     security_streaminit(&bs->secstr, &bsd_security_driver);
445     bs->fd = stream_client(bh->hostname, (in_port_t)id,
446         STREAM_BUFSIZE, STREAM_BUFSIZE, &bs->port, 0);
447     if (bs->fd < 0) {
448         security_seterror(&bh->sech,
449             _("can't connect stream to %s port %d: %s"), bh->hostname,
450             id, strerror(errno));
451         amfree(bs);
452         return (NULL);
453     }
454     bs->socket = -1;    /* we're a client */
455     bs->ev_read = NULL;
456 #ifdef DUMPER_SOCKET_BUFFERING
457     setsockopt(bs->fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, SIZEOF(rcvbuf));
458 #endif
459     return (bs);
460 }
461
462 /*
463  * Close and unallocate resources for a stream
464  */
465 static void
466 bsd_stream_close(
467     void *      s)
468 {
469     struct sec_stream *bs = s;
470
471     assert(bs != NULL);
472
473     if (bs->fd != -1)
474         aclose(bs->fd);
475     if (bs->socket != -1)
476         aclose(bs->socket);
477     bsd_stream_read_cancel(bs);
478     amfree(bs);
479 }
480
481 /*
482  * Authenticate a stream.  bsd streams have no authentication
483  */
484 static int
485 bsd_stream_auth(
486     void *      s)
487 {
488     (void)s;            /* Quiet unused parameter warning */
489
490     return (0); /* success */
491 }
492
493 /*
494  * Returns the stream id for this stream.  This is just the local port.
495  */
496 static int
497 bsd_stream_id(
498     void *      s)
499 {
500     struct sec_stream *bs = s;
501
502     assert(bs != NULL);
503
504     return ((int)bs->port);
505 }
506
507 /*
508  * Submit a request to read some data.  Calls back with the given function
509  * and arg when completed.
510  */
511 static void
512 bsd_stream_read(
513     void *      s,
514     void        (*fn)(void *, void *, ssize_t),
515     void *      arg)
516 {
517     struct sec_stream *bs = s;
518
519     /*
520      * Only one read request can be active per stream.
521      */
522     if (bs->ev_read != NULL)
523         event_release(bs->ev_read);
524
525     bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD, stream_read_callback, bs);
526     bs->fn = fn;
527     bs->arg = arg;
528 }
529
530 /*
531  * Read a chunk of data to a stream.  Blocks until completion.
532  */
533 static ssize_t
534 bsd_stream_read_sync(
535     void *      s,
536     void **     buf)
537 {
538     struct sec_stream *bs = s;
539
540     assert(bs != NULL);
541
542     /*
543      * Only one read request can be active per stream.
544      */
545     if(bs->ev_read != NULL) {
546         return -1;
547     }
548     bs->ev_read = event_register((event_id_t)bs->fd, EV_READFD,
549                         stream_read_sync_callback, bs);
550     event_wait(bs->ev_read);
551     *buf = bs->databuf;
552     return (bs->len);
553 }
554
555
556 /*
557  * Callback for bsd_stream_read_sync
558  */
559 static void
560 stream_read_sync_callback(
561     void *      s)
562 {
563     struct sec_stream *bs = s;
564     ssize_t n;
565
566     assert(bs != NULL);
567
568     auth_debug(1, _("bsd: stream_read_callback_sync: fd %d\n"), bs->fd);
569
570     /*
571      * Remove the event first, in case they reschedule it in the callback.
572      */
573     bsd_stream_read_cancel(bs);
574     do {
575         n = read(bs->fd, bs->databuf, sizeof(bs->databuf));
576     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
577     if (n < 0)
578         security_stream_seterror(&bs->secstr, strerror(errno));
579     bs->len = n;
580 }
581
582 /*
583  * Cancel a previous stream read request.  It's ok if we didn't
584  * have a read scheduled.
585  */
586 static void
587 bsd_stream_read_cancel(
588     void *      s)
589 {
590     struct sec_stream *bs = s;
591
592     assert(bs != NULL);
593     if (bs->ev_read != NULL) {
594         event_release(bs->ev_read);
595         bs->ev_read = NULL;
596     }
597 }
598
599 /*
600  * Callback for bsd_stream_read
601  */
602 static void
603 stream_read_callback(
604     void *      arg)
605 {
606     struct sec_stream *bs = arg;
607     ssize_t n;
608
609     assert(bs != NULL);
610
611     /*
612      * Remove the event first, in case they reschedule it in the callback.
613      */
614     bsd_stream_read_cancel(bs);
615     do {
616         n = read(bs->fd, bs->databuf, SIZEOF(bs->databuf));
617     } while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN)));
618
619     if (n < 0)
620         security_stream_seterror(&bs->secstr, strerror(errno));
621
622     (*bs->fn)(bs->arg, bs->databuf, n);
623 }