2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 __RCSID("$Heimdal: ipropd_slave.c 22211 2007-12-07 19:27:27Z lha $"
37 "$NetBSD: ipropd_slave.c,v 1.2 2008/03/22 08:37:12 mlelstv Exp $");
39 static krb5_log_facility
*log_facility
;
40 static char *server_time_lost
= "5 min";
41 static int time_before_lost
;
42 const char *slave_str
= NULL
;
45 connect_to_master (krb5_context context
, const char *master
,
49 struct sockaddr_in addr
;
52 fd
= socket (AF_INET
, SOCK_STREAM
, 0);
54 krb5_err (context
, 1, errno
, "socket AF_INET");
55 memset (&addr
, 0, sizeof(addr
));
56 addr
.sin_family
= AF_INET
;
58 addr
.sin_port
= krb5_getportbyname (context
,
61 if (addr
.sin_port
== 0) {
65 port
= strtol (port_str
, &ptr
, 10);
66 if (port
== 0 && ptr
== port_str
)
67 krb5_errx (context
, 1, "bad port `%s'", port_str
);
68 addr
.sin_port
= htons(port
);
71 addr
.sin_port
= krb5_getportbyname (context
, IPROP_SERVICE
,
74 he
= roken_gethostbyname (master
);
76 krb5_errx (context
, 1, "gethostbyname: %s", hstrerror(h_errno
));
77 memcpy (&addr
.sin_addr
, he
->h_addr
, sizeof(addr
.sin_addr
));
78 if(connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
79 krb5_err (context
, 1, errno
, "connect");
84 get_creds(krb5_context context
, const char *keytab_str
,
85 krb5_ccache
*cache
, const char *serverhost
)
88 krb5_principal client
;
90 krb5_get_init_creds_opt
*init_opts
;
95 if (keytab_str
== NULL
) {
96 ret
= krb5_kt_default_name (context
, keytab_buf
, sizeof(keytab_buf
));
98 krb5_err (context
, 1, ret
, "krb5_kt_default_name");
99 keytab_str
= keytab_buf
;
102 ret
= krb5_kt_resolve(context
, keytab_str
, &keytab
);
104 krb5_err(context
, 1, ret
, "%s", keytab_str
);
107 ret
= krb5_sname_to_principal (context
, slave_str
, IPROP_NAME
,
108 KRB5_NT_SRV_HST
, &client
);
109 if (ret
) krb5_err(context
, 1, ret
, "krb5_sname_to_principal");
111 ret
= krb5_get_init_creds_opt_alloc(context
, &init_opts
);
112 if (ret
) krb5_err(context
, 1, ret
, "krb5_get_init_creds_opt_alloc");
114 asprintf (&server
, "%s/%s", IPROP_NAME
, serverhost
);
116 krb5_errx (context
, 1, "malloc: no memory");
118 ret
= krb5_get_init_creds_keytab(context
, &creds
, client
, keytab
,
119 0, server
, init_opts
);
121 krb5_get_init_creds_opt_free(context
, init_opts
);
122 if(ret
) krb5_err(context
, 1, ret
, "krb5_get_init_creds");
124 ret
= krb5_kt_close(context
, keytab
);
125 if(ret
) krb5_err(context
, 1, ret
, "krb5_kt_close");
127 ret
= krb5_cc_gen_new(context
, &krb5_mcc_ops
, cache
);
128 if(ret
) krb5_err(context
, 1, ret
, "krb5_cc_gen_new");
130 ret
= krb5_cc_initialize(context
, *cache
, client
);
131 if(ret
) krb5_err(context
, 1, ret
, "krb5_cc_initialize");
133 ret
= krb5_cc_store_cred(context
, *cache
, &creds
);
134 if(ret
) krb5_err(context
, 1, ret
, "krb5_cc_store_cred");
138 ihave (krb5_context context
, krb5_auth_context auth_context
,
139 int fd
, uint32_t version
)
146 sp
= krb5_storage_from_mem (buf
, 8);
147 krb5_store_int32 (sp
, I_HAVE
);
148 krb5_store_int32 (sp
, version
);
149 krb5_storage_free (sp
);
153 ret
= krb5_write_priv_message(context
, auth_context
, &fd
, &data
);
155 krb5_err (context
, 1, ret
, "krb5_write_priv_message");
159 receive_loop (krb5_context context
,
161 kadm5_server_context
*server_context
)
170 * Seek to the current version of the local database.
173 int32_t len
, timestamp
, tmp
;
176 if(krb5_ret_int32 (sp
, &vers
) != 0)
178 krb5_ret_int32 (sp
, ×tamp
);
179 krb5_ret_int32 (sp
, &tmp
);
181 krb5_ret_int32 (sp
, &len
);
182 if (vers
<= server_context
->log_context
.version
)
183 krb5_storage_seek(sp
, len
+ 8, SEEK_CUR
);
184 } while(vers
<= server_context
->log_context
.version
);
187 * Read up rest of the entires into the memory...
189 left
= krb5_storage_seek (sp
, -16, SEEK_CUR
);
190 right
= krb5_storage_seek (sp
, 0, SEEK_END
);
191 buf
= malloc (right
- left
);
192 if (buf
== NULL
&& (right
- left
) != 0)
193 krb5_errx (context
, 1, "malloc: no memory");
196 * ...and then write them out to the on-disk log.
198 krb5_storage_seek (sp
, left
, SEEK_SET
);
199 krb5_storage_read (sp
, buf
, right
- left
);
200 sret
= write (server_context
->log_context
.log_fd
, buf
, right
-left
);
201 if (sret
!= right
- left
)
202 krb5_err(context
, 1, errno
, "Failed to write log to disk");
203 ret
= fsync (server_context
->log_context
.log_fd
);
205 krb5_err(context
, 1, errno
, "Failed to sync log to disk");
209 * Go back to the startpoint and start to commit the entires to
212 krb5_storage_seek (sp
, left
, SEEK_SET
);
215 int32_t len
, len2
, timestamp
, tmp
;
219 if(krb5_ret_int32 (sp
, &vers
) != 0)
221 ret
= krb5_ret_int32 (sp
, ×tamp
);
222 if (ret
) krb5_errx(context
, 1, "entry %ld: too short", (long)vers
);
223 ret
= krb5_ret_int32 (sp
, &tmp
);
224 if (ret
) krb5_errx(context
, 1, "entry %ld: too short", (long)vers
);
226 ret
= krb5_ret_int32 (sp
, &len
);
227 if (ret
) krb5_errx(context
, 1, "entry %ld: too short", (long)vers
);
229 krb5_errx(context
, 1, "log is corrupted, "
230 "negative length of entry version %ld: %ld",
231 (long)vers
, (long)len
);
232 cur
= krb5_storage_seek(sp
, 0, SEEK_CUR
);
234 krb5_warnx (context
, "replaying entry %d", (int)vers
);
236 ret
= kadm5_log_replay (server_context
,
239 char *s
= krb5_get_error_message(server_context
->context
, ret
);
241 "kadm5_log_replay: %ld. Lost entry entry, "
242 "Database out of sync ?: %s (%d)",
243 (long)vers
, s
? s
: "unknown error", ret
);
249 * Make sure the krb5_log_replay does the right thing wrt
250 * reading out data from the sp.
252 cur2
= krb5_storage_seek(sp
, 0, SEEK_CUR
);
253 if (cur
+ len
!= cur2
)
254 krb5_errx(context
, 1,
255 "kadm5_log_reply version: %ld didn't read the whole entry",
259 if (krb5_ret_int32 (sp
, &len2
) != 0)
260 krb5_errx(context
, 1, "entry %ld: postamble too short", (long)vers
);
261 if(krb5_ret_int32 (sp
, &vers2
) != 0)
262 krb5_errx(context
, 1, "entry %ld: postamble too short", (long)vers
);
265 krb5_errx(context
, 1, "entry %ld: len != len2", (long)vers
);
267 krb5_errx(context
, 1, "entry %ld: vers != vers2", (long)vers
);
274 server_context
->log_context
.version
= vers
;
278 receive (krb5_context context
,
280 kadm5_server_context
*server_context
)
284 ret
= server_context
->db
->hdb_open(context
,
286 O_RDWR
| O_CREAT
, 0600);
288 krb5_err (context
, 1, ret
, "db->open");
290 receive_loop (context
, sp
, server_context
);
292 ret
= server_context
->db
->hdb_close (context
, server_context
->db
);
294 krb5_err (context
, 1, ret
, "db->close");
298 send_im_here (krb5_context context
, int fd
,
299 krb5_auth_context auth_context
)
305 ret
= krb5_data_alloc (&data
, 4);
307 krb5_err (context
, 1, ret
, "send_im_here");
309 sp
= krb5_storage_from_data (&data
);
311 krb5_errx (context
, 1, "krb5_storage_from_data");
312 krb5_store_int32(sp
, I_AM_HERE
);
313 krb5_storage_free(sp
);
315 ret
= krb5_write_priv_message(context
, auth_context
, &fd
, &data
);
316 krb5_data_free(&data
);
319 krb5_err (context
, 1, ret
, "krb5_write_priv_message");
323 receive_everything (krb5_context context
, int fd
,
324 kadm5_server_context
*server_context
,
325 krb5_auth_context auth_context
)
336 krb5_warnx(context
, "receive complete database");
338 asprintf(&dbname
, "%s-NEW", server_context
->db
->hdb_name
);
339 ret
= hdb_create(context
, &mydb
, dbname
);
341 krb5_err(context
,1, ret
, "hdb_create");
344 ret
= hdb_set_master_keyfile (context
,
345 mydb
, server_context
->config
.stash_file
);
347 krb5_err(context
,1, ret
, "hdb_set_master_keyfile");
349 /* I really want to use O_EXCL here, but given that I can't easily clean
350 up on error, I won't */
351 ret
= mydb
->hdb_open(context
, mydb
, O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
353 krb5_err (context
, 1, ret
, "db->open");
357 ret
= krb5_read_priv_message(context
, auth_context
, &fd
, &data
);
360 krb5_err (context
, 1, ret
, "krb5_read_priv_message");
362 sp
= krb5_storage_from_data (&data
);
364 krb5_errx (context
, 1, "krb5_storage_from_data");
365 krb5_ret_int32 (sp
, &opcode
);
366 if (opcode
== ONE_PRINC
) {
370 krb5_storage_free(sp
);
372 fake_data
.data
= (char *)data
.data
+ 4;
373 fake_data
.length
= data
.length
- 4;
375 memset(&entry
, 0, sizeof(entry
));
377 ret
= hdb_value2entry (context
, &fake_data
, &entry
.entry
);
379 krb5_err (context
, 1, ret
, "hdb_value2entry");
380 ret
= mydb
->hdb_store(server_context
->context
,
384 krb5_err (context
, 1, ret
, "hdb_store");
386 hdb_free_entry (context
, &entry
);
387 krb5_data_free (&data
);
388 } else if (opcode
== NOW_YOU_HAVE
)
391 krb5_errx (context
, 1, "strange opcode %d", opcode
);
392 } while (opcode
== ONE_PRINC
);
394 if (opcode
!= NOW_YOU_HAVE
)
395 krb5_errx (context
, 1, "receive_everything: strange %d", opcode
);
397 krb5_ret_int32 (sp
, &vno
);
398 krb5_storage_free(sp
);
400 ret
= kadm5_log_reinit (server_context
);
402 krb5_err(context
, 1, ret
, "kadm5_log_reinit");
404 ret
= kadm5_log_set_version (server_context
, vno
- 1);
406 krb5_err (context
, 1, ret
, "kadm5_log_set_version");
408 ret
= kadm5_log_nop (server_context
);
410 krb5_err (context
, 1, ret
, "kadm5_log_nop");
412 krb5_data_free (&data
);
414 ret
= mydb
->hdb_rename (context
, mydb
, server_context
->db
->hdb_name
);
416 krb5_err (context
, 1, ret
, "db->rename");
418 ret
= mydb
->hdb_close (context
, mydb
);
420 krb5_err (context
, 1, ret
, "db->close");
422 ret
= mydb
->hdb_destroy (context
, mydb
);
424 krb5_err (context
, 1, ret
, "db->destroy");
426 krb5_warnx(context
, "receive complete database, version %ld", (long)vno
);
429 static char *config_file
;
431 static int version_flag
;
432 static int help_flag
;
433 static char *keytab_str
;
434 static char *port_str
;
435 static int detach_from_console
= 0;
437 static struct getargs args
[] = {
438 { "config-file", 'c', arg_string
, &config_file
},
439 { "realm", 'r', arg_string
, &realm
},
440 { "keytab", 'k', arg_string
, &keytab_str
,
441 "keytab to get authentication from", "kspec" },
442 { "time-lost", 0, arg_string
, &server_time_lost
,
443 "time before server is considered lost", "time" },
444 { "port", 0, arg_string
, &port_str
,
445 "port ipropd-slave will connect to", "port"},
446 { "detach", 0, arg_flag
, &detach_from_console
,
447 "detach from console" },
448 { "hostname", 0, arg_string
, &slave_str
,
449 "hostname of slave (if not same as hostname)", "hostname" },
450 { "version", 0, arg_flag
, &version_flag
},
451 { "help", 0, arg_flag
, &help_flag
}
454 static int num_args
= sizeof(args
) / sizeof(args
[0]);
457 main(int argc
, char **argv
)
460 krb5_context context
;
461 krb5_auth_context auth_context
;
463 kadm5_server_context
*server_context
;
464 kadm5_config_params conf
;
467 krb5_principal server
;
473 optidx
= krb5_program_setup(&context
, argc
, argv
, args
, num_args
, NULL
);
476 krb5_std_usage(0, args
, num_args
);
484 if (config_file
== NULL
) {
485 asprintf(&config_file
, "%s/kdc.conf", hdb_db_dir(context
));
486 if (config_file
== NULL
)
487 errx(1, "out of memory");
490 ret
= krb5_prepend_config_files_default(config_file
, &files
);
492 krb5_err(context
, 1, ret
, "getting configuration files");
494 ret
= krb5_set_config_files(context
, files
);
495 krb5_free_config_files(files
);
497 krb5_err(context
, 1, ret
, "reading configuration files");
503 krb5_std_usage(1, args
, num_args
);
507 if (detach_from_console
)
510 krb5_openlog (context
, "ipropd-slave", &log_facility
);
511 krb5_set_warn_dest(context
, log_facility
);
513 ret
= krb5_kt_register(context
, &hdb_kt_ops
);
515 krb5_err(context
, 1, ret
, "krb5_kt_register");
517 time_before_lost
= parse_time (server_time_lost
, "s");
518 if (time_before_lost
< 0)
519 krb5_errx (context
, 1, "couldn't parse time: %s", server_time_lost
);
521 memset(&conf
, 0, sizeof(conf
));
523 conf
.mask
|= KADM5_CONFIG_REALM
;
526 ret
= kadm5_init_with_password_ctx (context
,
533 krb5_err (context
, 1, ret
, "kadm5_init_with_password_ctx");
535 server_context
= (kadm5_server_context
*)kadm_handle
;
537 ret
= kadm5_log_init (server_context
);
539 krb5_err (context
, 1, ret
, "kadm5_log_init");
541 get_creds(context
, keytab_str
, &ccache
, master
);
543 master_fd
= connect_to_master (context
, master
, port_str
);
545 ret
= krb5_sname_to_principal (context
, master
, IPROP_NAME
,
546 KRB5_NT_SRV_HST
, &server
);
548 krb5_err (context
, 1, ret
, "krb5_sname_to_principal");
551 ret
= krb5_sendauth (context
, &auth_context
, &master_fd
,
552 IPROP_VERSION
, NULL
, server
,
553 AP_OPTS_MUTUAL_REQUIRED
, NULL
, NULL
,
554 ccache
, NULL
, NULL
, NULL
);
556 krb5_err (context
, 1, ret
, "krb5_sendauth");
558 krb5_warnx(context
, "ipropd-slave started at version: %ld",
559 (long)server_context
->log_context
.version
);
561 ihave (context
, auth_context
, master_fd
,
562 server_context
->log_context
.version
);
564 while (exit_flag
== 0) {
571 if (master_fd
>= FD_SETSIZE
)
572 krb5_errx (context
, 1, "fd too large");
575 FD_SET(master_fd
, &readset
);
577 to
.tv_sec
= time_before_lost
;
580 ret
= select (master_fd
+ 1,
581 &readset
, NULL
, NULL
, &to
);
586 krb5_err (context
, 1, errno
, "select");
589 krb5_errx (context
, 1, "server didn't send a message "
590 "in %d seconds", time_before_lost
);
592 ret
= krb5_read_priv_message(context
, auth_context
, &master_fd
, &out
);
595 krb5_err (context
, 1, ret
, "krb5_read_priv_message");
597 sp
= krb5_storage_from_mem (out
.data
, out
.length
);
598 krb5_ret_int32 (sp
, &tmp
);
601 receive (context
, sp
, server_context
);
602 ihave (context
, auth_context
, master_fd
,
603 server_context
->log_context
.version
);
605 case TELL_YOU_EVERYTHING
:
606 receive_everything (context
, master_fd
, server_context
,
610 send_im_here (context
, master_fd
, auth_context
);
617 krb5_warnx (context
, "Ignoring command %d", tmp
);
620 krb5_storage_free (sp
);
621 krb5_data_free (&out
);
624 if(exit_flag
== SIGXCPU
)
625 krb5_warnx(context
, "%s CPU time limit exceeded", getprogname());
626 else if(exit_flag
== SIGINT
|| exit_flag
== SIGTERM
)
627 krb5_warnx(context
, "%s terminated", getprogname());
629 krb5_warnx(context
, "%s unexpected exit reason: %ld",
630 getprogname(), (long)exit_flag
);