xref: /freebsd/crypto/heimdal/kdc/hpropd.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Copyright (c) 1997-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 "hprop.h"
35 
36 RCSID("$Id: hpropd.c,v 1.32 2001/02/20 01:44:50 assar Exp $");
37 
38 #ifdef KRB4
39 static des_cblock mkey4;
40 static des_key_schedule msched4;
41 
42 static char *
43 time2str(time_t t)
44 {
45     static char buf[128];
46     strftime(buf, sizeof(buf), "%Y%m%d%H%M", gmtime(&t));
47     return buf;
48 }
49 
50 static int
51 dump_krb4(krb5_context context, hdb_entry *ent, int fd)
52 {
53     char name[ANAME_SZ];
54     char instance[INST_SZ];
55     char realm[REALM_SZ];
56     char buf[1024];
57     char *p;
58     int i;
59     int ret;
60     char *princ_name;
61     Event *modifier;
62     krb5_realm *realms;
63     int cmp;
64 
65     ret = krb5_524_conv_principal(context, ent->principal,
66 				  name, instance, realm);
67     if (ret) {
68 	krb5_unparse_name(context, ent->principal, &princ_name);
69 	krb5_warn(context, ret, "%s", princ_name);
70 	free(princ_name);
71 	return -1;
72     }
73 
74     ret = krb5_get_default_realms (context, &realms);
75     if (ret) {
76 	krb5_warn(context, ret, "krb5_get_default_realms");
77 	return -1;
78     }
79 
80     cmp = strcmp (realms[0], ent->principal->realm);
81     krb5_free_host_realm (context, realms);
82     if (cmp != 0)
83         return -1;
84 
85     snprintf (buf, sizeof(buf), "%s %s ", name,
86 	      (strlen(instance) != 0) ? instance : "*");
87 
88     if (ent->max_life) {
89 	asprintf(&p, "%d", krb_time_to_life(0, *ent->max_life));
90 	strcat(buf, p);
91 	free(p);
92     } else
93 	strcat(buf, "255");
94     strcat(buf, " ");
95 
96     i = 0;
97     while (i < ent->keys.len &&
98 	   ent->keys.val[i].key.keytype != KEYTYPE_DES)
99 	++i;
100 
101     if (i == ent->keys.len) {
102 	krb5_warnx(context, "No DES key for %s.%s", name, instance);
103 	return -1;
104     }
105 
106     if (ent->keys.val[i].mkvno)
107 	asprintf(&p, "%d ", *ent->keys.val[i].mkvno);
108     else
109 	asprintf(&p, "%d ", 1);
110     strcat(buf, p);
111     free(p);
112 
113     asprintf(&p, "%d ", ent->kvno);
114     strcat(buf, p);
115     free(p);
116 
117     asprintf(&p, "%d ", 0); /* Attributes are always 0*/
118     strcat(buf, p);
119     free(p);
120 
121     {
122 	u_int32_t *key = ent->keys.val[i].key.keyvalue.data;
123 	kdb_encrypt_key((des_cblock*)key, (des_cblock*)key,
124 			&mkey4, msched4, DES_ENCRYPT);
125 	asprintf(&p, "%x %x ", (int)htonl(*key), (int)htonl(*(key+1)));
126 	strcat(buf, p);
127 	free(p);
128     }
129 
130     if (ent->valid_end == NULL)
131 	strcat(buf, time2str(60*60*24*365*50)); /* no expiration */
132     else
133 	strcat(buf, time2str(*ent->valid_end));
134     strcat(buf, " ");
135 
136     if (ent->modified_by == NULL)
137 	modifier = &ent->created_by;
138     else
139 	modifier = ent->modified_by;
140 
141     ret = krb5_524_conv_principal(context, modifier->principal,
142 				  name, instance, realm);
143     if (ret) {
144 	krb5_unparse_name(context, modifier->principal, &princ_name);
145 	krb5_warn(context, ret, "%s", princ_name);
146 	free(princ_name);
147 	return -1;
148     }
149     asprintf(&p, "%s %s %s\n", time2str(modifier->time),
150 	     (strlen(name) != 0) ? name : "*",
151 	     (strlen(instance) != 0) ? instance : "*");
152     strcat(buf, p);
153     free(p);
154 
155     ret = write(fd, buf, strlen(buf));
156     if (ret == -1)
157 	krb5_warnx(context, "write");
158     return 0;
159 }
160 #endif /* KRB4 */
161 
162 static int inetd_flag = -1;
163 static int help_flag;
164 static int version_flag;
165 static int print_dump;
166 static char *database = HDB_DEFAULT_DB;
167 static int from_stdin;
168 #ifdef KRB4
169 static int v4dump;
170 #endif
171 static char *ktname = NULL;
172 
173 struct getargs args[] = {
174     { "database", 'd', arg_string, &database, "database", "file" },
175     { "stdin",    'n', arg_flag, &from_stdin, "read from stdin" },
176     { "print",	    0, arg_flag, &print_dump, "print dump to stdout" },
177     { "inetd",	   'i',	arg_negative_flag,	&inetd_flag,
178       "Not started from inetd" },
179     { "keytab",   'k',	arg_string, &ktname,	"keytab to use for authentication", "keytab" },
180 #ifdef KRB4
181     { "v4dump",       '4',  arg_flag, &v4dump, "create v4 type DB" },
182 #endif
183     { "version",    0, arg_flag, &version_flag, NULL, NULL },
184     { "help",    'h',  arg_flag, &help_flag, NULL, NULL}
185 };
186 
187 static int num_args = sizeof(args) / sizeof(args[0]);
188 
189 static void
190 usage(int ret)
191 {
192     arg_printusage (args, num_args, NULL, "");
193     exit (ret);
194 }
195 
196 int
197 main(int argc, char **argv)
198 {
199     krb5_error_code ret;
200     krb5_context context;
201     krb5_auth_context ac = NULL;
202     krb5_principal c1, c2;
203     krb5_authenticator authent;
204     krb5_keytab keytab;
205     int fd;
206     HDB *db;
207     int optind = 0;
208     char *tmp_db;
209     krb5_log_facility *fac;
210     int nprincs;
211 #ifdef KRB4
212     int e;
213     int fd_out = -1;
214 #endif
215 
216     setprogname(argv[0]);
217 
218     ret = krb5_init_context(&context);
219     if(ret)
220 	exit(1);
221 
222     ret = krb5_openlog(context, "hpropd", &fac);
223     if(ret)
224 	;
225     krb5_set_warn_dest(context, fac);
226 
227     if(getarg(args, num_args, argc, argv, &optind))
228 	usage(1);
229 
230 #ifdef KRB4
231     if (v4dump && database == HDB_DEFAULT_DB)
232        database = "/var/kerberos/524_dump";
233 #endif /* KRB4 */
234 
235     if(help_flag)
236 	usage(0);
237     if(version_flag) {
238 	print_version(NULL);
239 	exit(0);
240     }
241 
242     argc -= optind;
243     argv += optind;
244 
245     if (argc != 0)
246 	usage(1);
247 
248     if(from_stdin)
249 	fd = STDIN_FILENO;
250     else {
251 	struct sockaddr_storage ss;
252 	struct sockaddr *sa = (struct sockaddr *)&ss;
253 	socklen_t sin_len = sizeof(ss);
254 	char addr_name[256];
255 	krb5_ticket *ticket;
256 	char *server;
257 
258 	fd = STDIN_FILENO;
259 	if (inetd_flag == -1) {
260 	    if (getpeername (fd, sa, &sin_len) < 0)
261 		inetd_flag = 0;
262 	    else
263 		inetd_flag = 1;
264 	}
265 	if (!inetd_flag) {
266 	    mini_inetd (krb5_getportbyname (context, "hprop", "tcp",
267 					    HPROP_PORT));
268 	}
269 	sin_len = sizeof(ss);
270 	if(getpeername(fd, sa, &sin_len) < 0)
271 	    krb5_err(context, 1, errno, "getpeername");
272 
273 	if (inet_ntop(sa->sa_family,
274 		      socket_get_address (sa),
275 		      addr_name,
276 		      sizeof(addr_name)) == NULL)
277 	    strlcpy (addr_name, "unknown address",
278 			     sizeof(addr_name));
279 
280 	krb5_log(context, fac, 0, "Connection from %s", addr_name);
281 
282 	ret = krb5_kt_register(context, &hdb_kt_ops);
283 	if(ret)
284 	    krb5_err(context, 1, ret, "krb5_kt_register");
285 
286 	if (ktname != NULL) {
287 	    ret = krb5_kt_resolve(context, ktname, &keytab);
288 	    if (ret)
289 		krb5_err (context, 1, ret, "krb5_kt_resolve %s", ktname);
290 	} else {
291 	    ret = krb5_kt_default (context, &keytab);
292 	    if (ret)
293 		krb5_err (context, 1, ret, "krb5_kt_default");
294 	}
295 
296 	ret = krb5_recvauth(context, &ac, &fd, HPROP_VERSION, NULL,
297 			    0, keytab, &ticket);
298 	if(ret)
299 	    krb5_err(context, 1, ret, "krb5_recvauth");
300 
301 	ret = krb5_unparse_name(context, ticket->server, &server);
302 	if (ret)
303 	    krb5_err(context, 1, ret, "krb5_unparse_name");
304 	if (strncmp(server, "hprop/", 5) != 0)
305 	    krb5_errx(context, 1, "ticket not for hprop (%s)", server);
306 
307 	free(server);
308 	krb5_free_ticket (context, ticket);
309 
310 	ret = krb5_auth_getauthenticator(context, ac, &authent);
311 	if(ret)
312 	    krb5_err(context, 1, ret, "krb5_auth_getauthenticator");
313 
314 	ret = krb5_make_principal(context, &c1, NULL, "kadmin", "hprop", NULL);
315 	if(ret)
316 	    krb5_err(context, 1, ret, "krb5_make_principal");
317 	principalname2krb5_principal(&c2, authent->cname, authent->crealm);
318 	if(!krb5_principal_compare(context, c1, c2)) {
319 	    char *s;
320 	    krb5_unparse_name(context, c2, &s);
321 	    krb5_errx(context, 1, "Unauthorized connection from %s", s);
322 	}
323 	krb5_free_principal(context, c1);
324 	krb5_free_principal(context, c2);
325 
326 	ret = krb5_kt_close(context, keytab);
327 	if(ret)
328 	    krb5_err(context, 1, ret, "krb5_kt_close");
329     }
330 
331     if(!print_dump) {
332 	asprintf(&tmp_db, "%s~", database);
333 #ifdef KRB4
334 	if (v4dump) {
335 	    fd_out = open(tmp_db, O_WRONLY | O_CREAT | O_TRUNC, 0600);
336 	    if (fd_out == -1)
337 		krb5_errx(context, 1, "%s", strerror(errno));
338 	}
339 	else
340 #endif /* KRB4 */
341 	{
342 	    ret = hdb_create(context, &db, tmp_db);
343 	    if(ret)
344 		krb5_err(context, 1, ret, "hdb_create(%s)", tmp_db);
345 	    ret = db->open(context, db, O_RDWR | O_CREAT | O_TRUNC, 0600);
346 	    if(ret)
347 		krb5_err(context, 1, ret, "hdb_open(%s)", tmp_db);
348 	}
349     }
350 
351 #ifdef KRB4
352     if (v4dump) {
353 	e = kdb_get_master_key(0, &mkey4, msched4);
354 	if(e)
355 	    krb5_errx(context, 1, "kdb_get_master_key: %s",
356 		      krb_get_err_text(e));
357     }
358 #endif /* KRB4 */
359 
360     nprincs = 0;
361     while(1){
362 	krb5_data data;
363 	hdb_entry entry;
364 
365 	if(from_stdin) {
366 	    ret = krb5_read_message(context, &fd, &data);
367 	    if(ret != 0 && ret != HEIM_ERR_EOF)
368 		krb5_err(context, 1, ret, "krb5_read_message");
369 	} else {
370 	    ret = krb5_read_priv_message(context, ac, &fd, &data);
371 	    if(ret)
372 		krb5_err(context, 1, ret, "krb5_read_priv_message");
373 	}
374 
375 	if(ret == HEIM_ERR_EOF || data.length == 0) {
376 	    if(!from_stdin) {
377 		data.data = NULL;
378 		data.length = 0;
379 		krb5_write_priv_message(context, ac, &fd, &data);
380 	    }
381 	    if(!print_dump) {
382 #ifdef KRB4
383 		if (v4dump) {
384 		    ret = rename(tmp_db, database);
385 		    if (ret)
386 			krb5_errx(context, 1, "rename");
387 		    ret = close(fd_out);
388 		    if (ret)
389 			krb5_errx(context, 1, "close");
390 		} else
391 #endif /* KRB4 */
392 		{
393 		    ret = db->rename(context, db, database);
394 		    if(ret)
395 			krb5_err(context, 1, ret, "db_rename");
396 		    ret = db->close(context, db);
397 		    if(ret)
398 			krb5_err(context, 1, ret, "db_close");
399 		}
400 	    }
401 	    break;
402 	}
403 	ret = hdb_value2entry(context, &data, &entry);
404 	if(ret)
405 	    krb5_err(context, 1, ret, "hdb_value2entry");
406 	if(print_dump)
407 	    hdb_print_entry(context, db, &entry, stdout);
408 	else {
409 #ifdef KRB4
410 	    if (v4dump) {
411 		ret = dump_krb4(context, &entry, fd_out);
412 		if(!ret) nprincs++;
413 	    }
414 	    else
415 #endif /* KRB4 */
416 	    {
417 		ret = db->store(context, db, 0, &entry);
418 		if(ret == HDB_ERR_EXISTS) {
419 		    char *s;
420 		    krb5_unparse_name(context, entry.principal, &s);
421 		    krb5_warnx(context, "Entry exists: %s", s);
422 		    free(s);
423 		} else if(ret)
424 		    krb5_err(context, 1, ret, "db_store");
425 		else
426 		    nprincs++;
427 	    }
428 	}
429 	hdb_free_entry(context, &entry);
430     }
431     if (!print_dump)
432 	krb5_log(context, fac, 0, "Received %d principals", nprincs);
433     exit(0);
434 }
435