]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/heimdal/kadmin/kadm_conn.c
import of heimdal 0.3f
[FreeBSD/FreeBSD.git] / crypto / heimdal / kadmin / kadm_conn.c
1 /*
2  * Copyright (c) 2000 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "kadmin_locl.h"
35 #ifdef HAVE_SYS_WAIT_H
36 #include <sys/wait.h>
37 #endif
38
39 RCSID("$Id: kadm_conn.c,v 1.13 2001/05/16 22:06:44 assar Exp $");
40
41 struct kadm_port {
42     char *port;
43     unsigned short def_port;
44     struct kadm_port *next;
45 } *kadm_ports;
46
47 static void
48 add_kadm_port(krb5_context context, const char *service, unsigned int port)
49 {
50     struct kadm_port *p;
51     p = malloc(sizeof(*p));
52     if(p == NULL) {
53         krb5_warnx(context, "failed to allocate %lu bytes\n", 
54                    (unsigned long)sizeof(*p));
55         return;
56     }
57     
58     p->port = strdup(service);
59     p->def_port = port;
60
61     p->next = kadm_ports;
62     kadm_ports = p;
63 }
64
65 static void
66 add_standard_ports (krb5_context context)
67 {
68     add_kadm_port(context, "kerberos-adm", 749);
69 #ifdef KRB4
70     add_kadm_port(context, "kerberos-master", 751);
71 #endif
72 }
73
74 /*
75  * parse the set of space-delimited ports in `str' and add them.
76  * "+" => all the standard ones
77  * otherwise it's port|service[/protocol]
78  */
79
80 void
81 parse_ports(krb5_context context, const char *str)
82 {
83     char p[128];
84
85     while(strsep_copy(&str, " \t", p, sizeof(p)) != -1) {
86         if(strcmp(p, "+") == 0)
87             add_standard_ports(context);
88         else
89             add_kadm_port(context, p, 0);
90     }
91 }
92
93 static pid_t pgrp;
94 sig_atomic_t term_flag, doing_useful_work;
95
96 static RETSIGTYPE
97 sigchld(int sig)
98 {
99     int status;
100     waitpid(-1, &status, 0);
101     SIGRETURN(0);
102 }
103
104 static RETSIGTYPE
105 terminate(int sig)
106 {
107     if(getpid() == pgrp) {
108         /* parent */
109         term_flag = 1;
110         signal(sig, SIG_IGN);
111         killpg(pgrp, sig);
112     } else {
113         /* child */
114         if(doing_useful_work)
115             term_flag = 1;
116         else
117             exit(0);
118     }
119     SIGRETURN(0);
120 }
121
122 static int
123 spawn_child(krb5_context context, int *socks, int num_socks, int this_sock)
124 {
125     int e, i;
126     struct sockaddr_storage __ss;
127     struct sockaddr *sa = (struct sockaddr *)&__ss;
128     socklen_t sa_size = sizeof(__ss);
129     int s;
130     pid_t pid;
131     krb5_address addr;
132     char buf[128];
133     size_t buf_len;
134
135     s = accept(socks[this_sock], sa, &sa_size);
136     if(s < 0) {
137         krb5_warn(context, errno, "accept");
138         return 1;
139     }
140     e = krb5_sockaddr2address(context, sa, &addr);
141     if(e)
142         krb5_warn(context, e, "krb5_sockaddr2address");
143     else {
144         e = krb5_print_address (&addr, buf, sizeof(buf), 
145                                 &buf_len);
146         if(e) 
147             krb5_warn(context, e, "krb5_print_address");
148         else
149             krb5_warnx(context, "connection from %s", buf);
150         krb5_free_address(context, &addr);
151     }
152     
153     pid = fork();
154     if(pid == 0) {
155         for(i = 0; i < num_socks; i++)
156             close(socks[i]);
157         dup2(s, STDIN_FILENO);
158         dup2(s, STDOUT_FILENO);
159         if(s != STDIN_FILENO && s != STDOUT_FILENO)
160             close(s);
161         return 0;
162     } else {
163         close(s);
164     }
165     return 1;
166 }
167
168 static int
169 wait_for_connection(krb5_context context,
170                     int *socks, int num_socks)
171 {
172     int i, e;
173     fd_set orig_read_set, read_set;
174     int max_fd = -1;
175     
176     FD_ZERO(&orig_read_set);
177     
178     for(i = 0; i < num_socks; i++) {
179         if (socks[i] >= FD_SETSIZE)
180             errx (1, "fd too large");
181         FD_SET(socks[i], &orig_read_set);
182         max_fd = max(max_fd, socks[i]);
183     }
184     
185     pgrp = getpid();
186
187     if(setpgid(0, pgrp) < 0)
188         err(1, "setpgid");
189
190     signal(SIGTERM, terminate);
191     signal(SIGINT, terminate);
192     signal(SIGCHLD, sigchld);
193
194     while (term_flag == 0) {
195         read_set = orig_read_set;
196         e = select(max_fd + 1, &read_set, NULL, NULL, NULL);
197         if(e < 0) {
198             if(errno != EINTR)
199                 krb5_warn(context, errno, "select");
200         } else if(e == 0)
201             krb5_warnx(context, "select returned 0");
202         else {
203             for(i = 0; i < num_socks; i++) {
204                 if(FD_ISSET(socks[i], &read_set))
205                     if(spawn_child(context, socks, num_socks, i) == 0)
206                         return 0;
207             }
208         }
209     }
210     signal(SIGCHLD, SIG_IGN);
211     while(1) {
212         int status;
213         pid_t pid;
214         pid = waitpid(-1, &status, 0);
215         if(pid == -1 && errno == ECHILD)
216             break;
217     }
218     exit(0);
219 }
220
221
222 int
223 start_server(krb5_context context)
224 {
225     int e;
226     struct kadm_port *p;
227
228     int *socks = NULL, *tmp;
229     int num_socks = 0;
230     int i;
231
232     for(p = kadm_ports; p; p = p->next) {
233         struct addrinfo hints, *ai, *ap;
234         char portstr[32];
235         memset (&hints, 0, sizeof(hints));
236         hints.ai_flags    = AI_PASSIVE;
237         hints.ai_socktype = SOCK_STREAM;
238
239         e = getaddrinfo(NULL, p->port, &hints, &ai);
240         if(e) {
241             snprintf(portstr, sizeof(portstr), "%u", p->def_port);
242             e = getaddrinfo(NULL, portstr, &hints, &ai);
243         }
244
245         if(e) {
246             krb5_warn(context, krb5_eai_to_heim_errno(e, errno),
247                       "%s", portstr);
248             continue;
249         }
250         i = 0;
251         for(ap = ai; ap; ap = ap->ai_next) 
252             i++;
253         tmp = realloc(socks, (num_socks + i) * sizeof(*socks));
254         if(tmp == NULL) {
255             krb5_warnx(context, "failed to reallocate %lu bytes", 
256                        (unsigned long)(num_socks + i) * sizeof(*socks));
257             continue;
258         }
259         socks = tmp;
260         for(ap = ai; ap; ap = ap->ai_next) {
261             int one = 1;
262             int s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
263             if(s < 0) {
264                 krb5_warn(context, errno, "socket");
265                 continue;
266             }
267 #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT)
268             if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&one,
269                           sizeof(one)) < 0)
270                 krb5_warn(context, errno, "setsockopt");
271 #endif
272             if (bind (s, ap->ai_addr, ap->ai_addrlen) < 0) {
273                 krb5_warn(context, errno, "bind");
274                 close(s);
275                 continue;
276             }
277             if (listen (s, SOMAXCONN) < 0) {
278                 krb5_warn(context, errno, "listen");
279                 close(s);
280                 continue;
281             }
282             socks[num_socks++] = s;
283         }
284         freeaddrinfo (ai);
285     }
286     if(num_socks == 0)
287         krb5_errx(context, 1, "no sockets to listen to - exiting");
288     return wait_for_connection(context, socks, num_socks);
289 }