/
net-server.c
1974 lines (1745 loc) · 56 KB
/
net-server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/apputils/net-server.c - Network code for krb5 servers (kdc, kadmind) */
/*
* Copyright 1990,2000,2007,2008,2009,2010 by the Massachusetts Institute of Technology.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
#include "k5-int.h"
#include "adm_proto.h"
#include <sys/ioctl.h>
#include <syslog.h>
#include <stddef.h>
#include "port-sockets.h"
#include "socket-utils.h"
#include <gssrpc/rpc.h>
#ifdef HAVE_NETINET_IN_H
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#ifdef HAVE_SYS_SOCKIO_H
/* for SIOCGIFCONF, etc. */
#include <sys/sockio.h>
#endif
#include <sys/time.h>
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <arpa/inet.h>
#ifndef ARPHRD_ETHER /* OpenBSD breaks on multiple inclusions */
#include <net/if.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h> /* FIONBIO */
#endif
#include "fake-addrinfo.h"
#include "net-server.h"
#include <signal.h>
/* XXX */
#define KDC5_NONET (-1779992062L)
static int tcp_or_rpc_data_counter;
static int max_tcp_or_rpc_data_connections = 45;
/* Misc utility routines. */
static void
set_sa_port(struct sockaddr *addr, int port)
{
switch (addr->sa_family) {
case AF_INET:
sa2sin(addr)->sin_port = port;
break;
case AF_INET6:
sa2sin6(addr)->sin6_port = port;
break;
default:
break;
}
}
static int
ipv6_enabled()
{
static int result = -1;
if (result == -1) {
int s;
s = socket(AF_INET6, SOCK_STREAM, 0);
if (s >= 0) {
result = 1;
close(s);
} else
result = 0;
}
return result;
}
static int
setreuseaddr(int sock, int value)
{
return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
}
#if defined(IPV6_V6ONLY)
static int
setv6only(int sock, int value)
{
return setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &value, sizeof(value));
}
#endif
/* Use RFC 3542 API below, but fall back from IPV6_RECVPKTINFO to
IPV6_PKTINFO for RFC 2292 implementations. */
#ifndef IPV6_RECVPKTINFO
#define IPV6_RECVPKTINFO IPV6_PKTINFO
#endif
/* Parallel, though not standardized. */
#ifndef IP_RECVPKTINFO
#define IP_RECVPKTINFO IP_PKTINFO
#endif
static int
set_pktinfo(int sock, int family)
{
int sockopt = 1;
int option = 0, proto = 0;
switch (family) {
#if defined(IP_PKTINFO) && defined(HAVE_STRUCT_IN_PKTINFO)
case AF_INET:
proto = IPPROTO_IP;
option = IP_RECVPKTINFO;
break;
#endif
#if defined(IPV6_PKTINFO) && defined(HAVE_STRUCT_IN6_PKTINFO)
case AF_INET6:
proto = IPPROTO_IPV6;
option = IPV6_RECVPKTINFO;
break;
#endif
default:
return EINVAL;
}
if (setsockopt(sock, proto, option, &sockopt, sizeof(sockopt)))
return errno;
return 0;
}
static const char *
paddr(struct sockaddr *sa)
{
static char buf[100];
char portbuf[10];
if (getnameinfo(sa, socklen(sa),
buf, sizeof(buf), portbuf, sizeof(portbuf),
NI_NUMERICHOST|NI_NUMERICSERV))
strlcpy(buf, "<unprintable>", sizeof(buf));
else {
unsigned int len = sizeof(buf) - strlen(buf);
char *p = buf + strlen(buf);
if (len > 2+strlen(portbuf)) {
*p++ = '.';
len--;
strncpy(p, portbuf, len);
}
}
return buf;
}
/* KDC data. */
enum conn_type {
CONN_UDP, CONN_UDP_PKTINFO, CONN_TCP_LISTENER, CONN_TCP,
CONN_RPC_LISTENER, CONN_RPC,
CONN_ROUTING
};
/* Per-connection info. */
struct connection {
void *handle;
const char *prog;
enum conn_type type;
/* Connection fields (TCP or RPC) */
struct sockaddr_storage addr_s;
socklen_t addrlen;
char addrbuf[56];
krb5_fulladdr faddr;
krb5_address kaddr;
/* Incoming data (TCP) */
size_t bufsiz;
size_t offset;
char *buffer;
size_t msglen;
/* Outgoing data (TCP) */
krb5_data *response;
unsigned char lenbuf[4];
sg_buf sgbuf[2];
sg_buf *sgp;
int sgnum;
/* Crude denial-of-service avoidance support (TCP or RPC) */
time_t start_time;
/* RPC-specific fields */
SVCXPRT *transp;
int rpc_force_close;
};
#define SET(TYPE) struct { TYPE *data; size_t n, max; }
/* Start at the top and work down -- this should allow for deletions
without disrupting the iteration, since we delete by overwriting
the element to be removed with the last element. */
#define FOREACH_ELT(set,idx,vvar) \
for (idx = set.n-1; idx >= 0 && (vvar = set.data[idx], 1); idx--)
#define GROW_SET(set, incr, tmpptr) \
((set.max + incr < set.max \
|| ((set.max + incr) * sizeof(set.data[0]) / sizeof(set.data[0]) \
!= set.max + incr)) \
? 0 /* overflow */ \
: ((tmpptr = realloc(set.data, \
(set.max + incr) * sizeof(set.data[0]))) \
? (set.data = tmpptr, set.max += incr, 1) \
: 0))
/* 1 = success, 0 = failure */
#define ADD(set, val, tmpptr) \
((set.n < set.max || GROW_SET(set, 10, tmpptr)) \
? (set.data[set.n++] = val, 1) \
: 0)
#define DEL(set, idx) \
(set.data[idx] = set.data[--set.n], 0)
#define FREE_SET_DATA(set) \
(free(set.data), set.data = 0, set.max = 0, set.n = 0)
/*
* N.B.: The Emacs cc-mode indentation code seems to get confused if
* the macro argument here is one word only. So use "unsigned short"
* instead of the "u_short" we were using before.
*/
struct rpc_svc_data {
u_short port;
u_long prognum;
u_long versnum;
void (*dispatch)();
};
static SET(unsigned short) udp_port_data, tcp_port_data;
static SET(struct rpc_svc_data) rpc_svc_data;
static SET(verto_ev *) events;
verto_ctx *
loop_init(verto_ev_type types)
{
types |= VERTO_EV_TYPE_IO;
types |= VERTO_EV_TYPE_SIGNAL;
types |= VERTO_EV_TYPE_TIMEOUT;
return verto_default(NULL, types);
}
static void
do_break(verto_ctx *ctx, verto_ev *ev)
{
krb5_klog_syslog(LOG_DEBUG, _("Got signal to request exit"));
verto_break(ctx);
}
struct sighup_context {
void *handle;
void (*reset)(void *);
};
static void
do_reset(verto_ctx *ctx, verto_ev *ev)
{
struct sighup_context *sc = (struct sighup_context*) verto_get_private(ev);
krb5_klog_syslog(LOG_DEBUG, _("Got signal to reset"));
krb5_klog_reopen(get_context(sc->handle));
if (sc->reset)
sc->reset(sc->handle);
}
static void
free_sighup_context(verto_ctx *ctx, verto_ev *ev)
{
free(verto_get_private(ev));
}
krb5_error_code
loop_setup_signals(verto_ctx *ctx, void *handle, void (*reset)())
{
struct sighup_context *sc;
verto_ev *ev;
if (!verto_add_signal(ctx, VERTO_EV_FLAG_PERSIST, do_break, SIGINT) ||
!verto_add_signal(ctx, VERTO_EV_FLAG_PERSIST, do_break, SIGTERM) ||
!verto_add_signal(ctx, VERTO_EV_FLAG_PERSIST, do_break, SIGQUIT) ||
!verto_add_signal(ctx, VERTO_EV_FLAG_PERSIST, VERTO_SIG_IGN, SIGPIPE))
return ENOMEM;
ev = verto_add_signal(ctx, VERTO_EV_FLAG_PERSIST, do_reset, SIGHUP);
if (!ev)
return ENOMEM;
sc = malloc(sizeof(*sc));
if (!sc)
return ENOMEM;
sc->handle = handle;
sc->reset = reset;
verto_set_private(ev, sc, free_sighup_context);
return 0;
}
krb5_error_code
loop_add_udp_port(int port)
{
int i;
void *tmp;
u_short val;
u_short s_port = port;
if (s_port != port)
return EINVAL;
FOREACH_ELT (udp_port_data, i, val)
if (s_port == val)
return 0;
if (!ADD(udp_port_data, s_port, tmp))
return ENOMEM;
return 0;
}
krb5_error_code
loop_add_tcp_port(int port)
{
int i;
void *tmp;
u_short val;
u_short s_port = port;
if (s_port != port)
return EINVAL;
FOREACH_ELT (tcp_port_data, i, val)
if (s_port == val)
return 0;
if (!ADD(tcp_port_data, s_port, tmp))
return ENOMEM;
return 0;
}
krb5_error_code
loop_add_rpc_service(int port, u_long prognum,
u_long versnum, void (*dispatchfn)())
{
int i;
void *tmp;
struct rpc_svc_data svc, val;
svc.port = port;
if (svc.port != port)
return EINVAL;
svc.prognum = prognum;
svc.versnum = versnum;
svc.dispatch = dispatchfn;
FOREACH_ELT (rpc_svc_data, i, val) {
if (val.port == port)
return 0;
}
if (!ADD(rpc_svc_data, svc, tmp))
return ENOMEM;
return 0;
}
#define USE_AF AF_INET
#define USE_TYPE SOCK_DGRAM
#define USE_PROTO 0
#define SOCKET_ERRNO errno
#include "foreachaddr.h"
struct socksetup {
verto_ctx *ctx;
void *handle;
const char *prog;
krb5_error_code retval;
int udp_flags;
#define UDP_DO_IPV4 1
#define UDP_DO_IPV6 2
};
static void
free_connection(struct connection *conn)
{
if (!conn)
return;
if (conn->response)
krb5_free_data(get_context(conn->handle), conn->response);
if (conn->buffer)
free(conn->buffer);
if (conn->type == CONN_RPC_LISTENER && conn->transp != NULL)
svc_destroy(conn->transp);
free(conn);
}
static void
remove_event_from_set(verto_ev *ev)
{
verto_ev *tmp;
int i;
/* Remove the event from the events. */
FOREACH_ELT(events, i, tmp)
if (tmp == ev) {
DEL(events, i);
break;
}
}
static void
free_socket(verto_ctx *ctx, verto_ev *ev)
{
struct connection *conn = NULL;
fd_set fds;
int fd;
remove_event_from_set(ev);
fd = verto_get_fd(ev);
conn = verto_get_private(ev);
/* Close the file descriptor. */
krb5_klog_syslog(LOG_INFO, _("closing down fd %d"), fd);
if (fd >= 0 && (!conn || conn->type != CONN_RPC || conn->rpc_force_close))
close(fd);
/* Free the connection struct. */
if (conn) {
switch (conn->type) {
case CONN_RPC:
if (conn->rpc_force_close) {
FD_ZERO(&fds);
FD_SET(fd, &fds);
svc_getreqset(&fds);
if (FD_ISSET(fd, &svc_fdset)) {
krb5_klog_syslog(LOG_ERR,
_("descriptor %d closed but still "
"in svc_fdset"),
fd);
}
}
/* Fall through. */
case CONN_TCP:
tcp_or_rpc_data_counter--;
break;
default:
break;
}
free_connection(conn);
}
}
static verto_ev *
make_event(verto_ctx *ctx, verto_ev_flag flags, verto_callback callback,
int sock, struct connection *conn, int addevent)
{
verto_ev *ev;
void *tmp;
ev = verto_add_io(ctx, flags, callback, sock);
if (!ev) {
com_err(conn->prog, ENOMEM, _("cannot create io event"));
return NULL;
}
if (addevent) {
if (!ADD(events, ev, tmp)) {
com_err(conn->prog, ENOMEM, _("cannot save event"));
verto_del(ev);
return NULL;
}
}
verto_set_private(ev, conn, free_socket);
return ev;
}
static verto_ev *
add_fd(struct socksetup *data, int sock, enum conn_type conntype,
verto_ev_flag flags, verto_callback callback, int addevent)
{
struct connection *newconn;
#ifndef _WIN32
if (sock >= FD_SETSIZE) {
data->retval = EMFILE; /* XXX */
com_err(data->prog, 0,
_("file descriptor number %d too high"), sock);
return 0;
}
#endif
newconn = malloc(sizeof(*newconn));
if (newconn == NULL) {
data->retval = ENOMEM;
com_err(data->prog, ENOMEM,
_("cannot allocate storage for connection info"));
return 0;
}
memset(newconn, 0, sizeof(*newconn));
newconn->handle = data->handle;
newconn->prog = data->prog;
newconn->type = conntype;
return make_event(data->ctx, flags, callback, sock, newconn, addevent);
}
static void process_packet(verto_ctx *ctx, verto_ev *ev);
static void accept_tcp_connection(verto_ctx *ctx, verto_ev *ev);
static void process_tcp_connection_read(verto_ctx *ctx, verto_ev *ev);
static void process_tcp_connection_write(verto_ctx *ctx, verto_ev *ev);
static void accept_rpc_connection(verto_ctx *ctx, verto_ev *ev);
static void process_rpc_connection(verto_ctx *ctx, verto_ev *ev);
static verto_ev *
add_udp_fd(struct socksetup *data, int sock, int pktinfo)
{
return add_fd(data, sock, pktinfo ? CONN_UDP_PKTINFO : CONN_UDP,
VERTO_EV_FLAG_IO_READ |
VERTO_EV_FLAG_PERSIST |
VERTO_EV_FLAG_REINITIABLE,
process_packet, 1);
}
static verto_ev *
add_tcp_listener_fd(struct socksetup *data, int sock)
{
return add_fd(data, sock, CONN_TCP_LISTENER,
VERTO_EV_FLAG_IO_READ |
VERTO_EV_FLAG_PERSIST |
VERTO_EV_FLAG_REINITIABLE,
accept_tcp_connection, 1);
}
static verto_ev *
add_tcp_read_fd(struct socksetup *data, int sock)
{
return add_fd(data, sock, CONN_TCP,
VERTO_EV_FLAG_IO_READ | VERTO_EV_FLAG_PERSIST,
process_tcp_connection_read, 1);
}
/*
* Create a socket and bind it to addr. Ensure the socket will work with
* select(). Set the socket cloexec, reuseaddr, and if applicable v6-only.
* Does not call listen(). Returns -1 on failure after logging an error.
*/
static int
create_server_socket(struct socksetup *data, struct sockaddr *addr, int type)
{
int sock;
sock = socket(addr->sa_family, type, 0);
if (sock == -1) {
data->retval = errno;
com_err(data->prog, errno, _("Cannot create TCP server socket on %s"),
paddr(addr));
return -1;
}
set_cloexec_fd(sock);
#ifndef _WIN32 /* Windows FD_SETSIZE is a count. */
if (sock >= FD_SETSIZE) {
close(sock);
com_err(data->prog, 0, _("TCP socket fd number %d (for %s) too high"),
sock, paddr(addr));
return -1;
}
#endif
if (setreuseaddr(sock, 1) < 0) {
com_err(data->prog, errno,
_("Cannot enable SO_REUSEADDR on fd %d"), sock);
}
if (addr->sa_family == AF_INET6) {
#ifdef IPV6_V6ONLY
if (setv6only(sock, 1))
com_err(data->prog, errno,
_("setsockopt(%d,IPV6_V6ONLY,1) failed"), sock);
else
com_err(data->prog, 0, _("setsockopt(%d,IPV6_V6ONLY,1) worked"),
sock);
#else
krb5_klog_syslog(LOG_INFO, _("no IPV6_V6ONLY socket option support"));
#endif /* IPV6_V6ONLY */
}
if (bind(sock, addr, socklen(addr)) == -1) {
data->retval = errno;
com_err(data->prog, errno, _("Cannot bind server socket on %s"),
paddr(addr));
close(sock);
return -1;
}
return sock;
}
static verto_ev *
add_rpc_listener_fd(struct socksetup *data, struct rpc_svc_data *svc, int sock)
{
struct connection *conn;
verto_ev *ev;
ev = add_fd(data, sock, CONN_RPC_LISTENER,
VERTO_EV_FLAG_IO_READ |
VERTO_EV_FLAG_PERSIST |
VERTO_EV_FLAG_REINITIABLE,
accept_rpc_connection, 1);
if (ev == NULL)
return NULL;
conn = verto_get_private(ev);
conn->transp = svctcp_create(sock, 0, 0);
if (conn->transp == NULL) {
krb5_klog_syslog(LOG_ERR,
_("Cannot create RPC service: %s; continuing"),
strerror(errno));
verto_del(ev);
return NULL;
}
if (!svc_register(conn->transp, svc->prognum, svc->versnum,
svc->dispatch, 0)) {
krb5_klog_syslog(LOG_ERR,
_("Cannot register RPC service: %s; continuing"),
strerror(errno));
verto_del(ev);
return NULL;
}
return ev;
}
static verto_ev *
add_rpc_data_fd(struct socksetup *data, int sock)
{
return add_fd(data, sock, CONN_RPC,
VERTO_EV_FLAG_IO_READ | VERTO_EV_FLAG_PERSIST,
process_rpc_connection, 1);
}
static const int one = 1;
static int
setnbio(int sock)
{
return ioctlsocket(sock, FIONBIO, (const void *)&one);
}
static int
setkeepalive(int sock)
{
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
}
static int
setnolinger(int s)
{
static const struct linger ling = { 0, 0 };
return setsockopt(s, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
}
/* Returns -1 or socket fd. */
static int
setup_a_tcp_listener(struct socksetup *data, struct sockaddr *addr)
{
int sock;
sock = create_server_socket(data, addr, SOCK_STREAM);
if (sock == -1)
return -1;
if (listen(sock, 5) < 0) {
com_err(data->prog, errno,
_("Cannot listen on TCP server socket on %s"), paddr(addr));
close(sock);
return -1;
}
if (setnbio(sock)) {
com_err(data->prog, errno,
_("cannot set listening tcp socket on %s non-blocking"),
paddr(addr));
close(sock);
return -1;
}
if (setnolinger(sock)) {
com_err(data->prog, errno,
_("disabling SO_LINGER on TCP socket on %s"), paddr(addr));
close(sock);
return -1;
}
return sock;
}
static int
setup_tcp_listener_ports(struct socksetup *data)
{
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
int i, port;
memset(&sin4, 0, sizeof(sin4));
sin4.sin_family = AF_INET;
#ifdef HAVE_SA_LEN
sin4.sin_len = sizeof(sin4);
#endif
sin4.sin_addr.s_addr = INADDR_ANY;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
#ifdef SIN6_LEN
sin6.sin6_len = sizeof(sin6);
#endif
sin6.sin6_addr = in6addr_any;
FOREACH_ELT (tcp_port_data, i, port) {
int s4, s6;
set_sa_port((struct sockaddr *)&sin4, htons(port));
if (!ipv6_enabled()) {
s4 = setup_a_tcp_listener(data, (struct sockaddr *)&sin4);
if (s4 < 0)
return -1;
s6 = -1;
} else {
s4 = s6 = -1;
set_sa_port((struct sockaddr *)&sin6, htons(port));
s6 = setup_a_tcp_listener(data, (struct sockaddr *)&sin6);
if (s6 < 0)
return -1;
s4 = setup_a_tcp_listener(data, (struct sockaddr *)&sin4);
}
/* Sockets are created, prepare to listen on them. */
if (s4 >= 0) {
if (add_tcp_listener_fd(data, s4) == NULL)
close(s4);
else {
krb5_klog_syslog(LOG_INFO, _("listening on fd %d: tcp %s"),
s4, paddr((struct sockaddr *)&sin4));
}
}
if (s6 >= 0) {
if (add_tcp_listener_fd(data, s6) == NULL) {
close(s6);
s6 = -1;
} else {
krb5_klog_syslog(LOG_INFO, _("listening on fd %d: tcp %s"),
s6, paddr((struct sockaddr *)&sin6));
}
if (s4 < 0)
krb5_klog_syslog(LOG_INFO,
_("assuming IPv6 socket accepts IPv4"));
}
}
return 0;
}
static int
setup_rpc_listener_ports(struct socksetup *data)
{
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
int i;
struct rpc_svc_data svc;
memset(&sin4, 0, sizeof(sin4));
sin4.sin_family = AF_INET;
#ifdef HAVE_SA_LEN
sin4.sin_len = sizeof(sin4);
#endif
sin4.sin_addr.s_addr = INADDR_ANY;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
sin6.sin6_len = sizeof(sin6);
#endif
sin6.sin6_addr = in6addr_any;
FOREACH_ELT (rpc_svc_data, i, svc) {
int s4;
int s6;
set_sa_port((struct sockaddr *)&sin4, htons(svc.port));
s4 = create_server_socket(data, (struct sockaddr *)&sin4, SOCK_STREAM);
if (s4 < 0)
return -1;
if (add_rpc_listener_fd(data, &svc, s4) == NULL)
close(s4);
else
krb5_klog_syslog(LOG_INFO, _("listening on fd %d: rpc %s"),
s4, paddr((struct sockaddr *)&sin4));
if (ipv6_enabled()) {
set_sa_port((struct sockaddr *)&sin6, htons(svc.port));
s6 = create_server_socket(data, (struct sockaddr *)&sin6,
SOCK_STREAM);
if (s6 < 0)
return -1;
if (add_rpc_listener_fd(data, &svc, s6) == NULL)
close(s6);
else
krb5_klog_syslog(LOG_INFO, _("listening on fd %d: rpc %s"),
s6, paddr((struct sockaddr *)&sin6));
}
}
return 0;
}
#if defined(CMSG_SPACE) && defined(HAVE_STRUCT_CMSGHDR) && \
(defined(IP_PKTINFO) || defined(IPV6_PKTINFO))
union pktinfo {
#ifdef HAVE_STRUCT_IN6_PKTINFO
struct in6_pktinfo pi6;
#endif
#ifdef HAVE_STRUCT_IN_PKTINFO
struct in_pktinfo pi4;
#endif
char c;
};
static int
setup_udp_port_1(struct socksetup *data, struct sockaddr *addr,
char *haddrbuf, int pktinfo);
static void
setup_udp_pktinfo_ports(struct socksetup *data)
{
#ifdef IP_PKTINFO
{
struct sockaddr_in sa;
int r;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
#ifdef HAVE_SA_LEN
sa.sin_len = sizeof(sa);
#endif
r = setup_udp_port_1(data, (struct sockaddr *) &sa, "0.0.0.0", 4);
if (r == 0)
data->udp_flags &= ~UDP_DO_IPV4;
}
#endif
#ifdef IPV6_PKTINFO
{
struct sockaddr_in6 sa;
int r;
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
#ifdef HAVE_SA_LEN
sa.sin6_len = sizeof(sa);
#endif
r = setup_udp_port_1(data, (struct sockaddr *) &sa, "::", 6);
if (r == 0)
data->udp_flags &= ~UDP_DO_IPV6;
}
#endif
}
#else /* no pktinfo compile-time support */
static void
setup_udp_pktinfo_ports(struct socksetup *data)
{
}
#endif
static int
setup_udp_port_1(struct socksetup *data, struct sockaddr *addr,
char *haddrbuf, int pktinfo)
{
int sock = -1, i, r;
u_short port;
FOREACH_ELT (udp_port_data, i, port) {
set_sa_port(addr, htons(port));
sock = create_server_socket(data, addr, SOCK_DGRAM);
if (sock == -1)
return 1;
setnbio(sock);
#if !(defined(CMSG_SPACE) && defined(HAVE_STRUCT_CMSGHDR) && \
(defined(IP_PKTINFO) || defined(IPV6_PKTINFO)))
assert(pktinfo == 0);
#endif
if (pktinfo) {
r = set_pktinfo(sock, addr->sa_family);
if (r) {
com_err(data->prog, r,
_("Cannot request packet info for udp socket address "
"%s port %d"), haddrbuf, port);
close(sock);
return 1;
}
}
krb5_klog_syslog(LOG_INFO, _("listening on fd %d: udp %s%s"), sock,
paddr((struct sockaddr *)addr),
pktinfo ? " (pktinfo)" : "");
if (add_udp_fd (data, sock, pktinfo) == 0) {
close(sock);
return 1;
}
}
return 0;
}
static int
setup_udp_port(void *P_data, struct sockaddr *addr)
{
struct socksetup *data = P_data;
char haddrbuf[NI_MAXHOST];
int err;
if (addr->sa_family == AF_INET && !(data->udp_flags & UDP_DO_IPV4))
return 0;
#ifdef AF_INET6
if (addr->sa_family == AF_INET6 && !(data->udp_flags & UDP_DO_IPV6))
return 0;
#endif
err = getnameinfo(addr, socklen(addr), haddrbuf, sizeof(haddrbuf),
0, 0, NI_NUMERICHOST);
if (err)
strlcpy(haddrbuf, "<unprintable>", sizeof(haddrbuf));
switch (addr->sa_family) {
case AF_INET:
break;
#ifdef AF_INET6
case AF_INET6:
break;
#endif
#ifdef AF_LINK /* some BSD systems, AIX */
case AF_LINK:
return 0;
#endif
#ifdef AF_DLI /* Direct Link Interface - DEC Ultrix/OSF1 link layer? */
case AF_DLI:
return 0;
#endif
#ifdef AF_APPLETALK
case AF_APPLETALK:
return 0;
#endif
default:
krb5_klog_syslog(LOG_INFO,
_("skipping unrecognized local address family %d"),
addr->sa_family);
return 0;
}
return setup_udp_port_1(data, addr, haddrbuf, 0);
}
#ifdef HAVE_STRUCT_RT_MSGHDR
#include <net/route.h>
static void
do_network_reconfig(verto_ctx *ctx, verto_ev *ev)
{
struct connection *conn = verto_get_private(ev);
if (loop_setup_network(ctx, conn->handle, conn->prog) != 0) {
krb5_klog_syslog(LOG_ERR, _("Failed to reconfigure network, exiting"));
verto_break(ctx);
}
}
static int
routing_update_needed(struct rt_msghdr *rtm)
{
switch (rtm->rtm_type) {
case RTM_ADD: