summaryrefslogtreecommitdiffstats
path: root/src/kadmin/v5server/srv_main.c
blob: ba906e3650aee4f48825314333591da5e0577002 (plain)
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
/*
 * kadmin/v5server/srv_main.c
 *
 * Copyright 1995 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * 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.  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.
 *
 */

/*
 * srv_main.c
 * Main function of administrative server which speaks new Kerberos V5
 * administrative protocol.
 */

#include <stdio.h>
#include <sys/signal.h>
#include <setjmp.h>
#include "k5-int.h"
#include "com_err.h"
#include "adm.h"
#include "adm_proto.h"

#ifdef	LANGUAGES_SUPPORTED
static const char *usage_format =	"%s: usage is %s [-a aclfile] [-d database] [-e enctype] [-m]\n\t[-k mkeytype] [-l langlist] [-p portnum] [-r realm] [-s stash] [-t timeout] [-n]\n\t[-D dbg] [-M mkeyname] [-T ktabname].\n";
static const char *getopt_string =	"a:d:e:k:l:mnp:r:t:D:M:T:";
#else	/* LANGUAGES_SUPPORTED */
static const char *usage_format =	"%s: usage is %s [-a aclfile] [-d database] [-e enctype] [-m]\n\t[-k mkeytype] [-p portnum] [-r realm] [-s stash] [-t timeout] [-n]\n\t[-D dbg] [-M mkeyname] [-T ktabname].\n";
static const char *getopt_string =	"a:d:e:k:mnp:r:t:D:M:T:";
#endif	/* LANGUAGES_SUPPORTED */
static const char *fval_not_number =	"%s: value (%s) specified for -%c is not numeric.\n";
static const char *extra_params =	"%s extra paramters beginning with %s... \n";
static const char *daemon_err =		"%s: cannot spawn and detach.\n";
static const char *grealm_err =		"%s: cannot get default realm.\n";
static const char *pinit_err = 		"%s: cannot open configuration file %s.\n";
static const char *no_memory_fmt =	"%s: cannot allocate %d bytes for %s.\n";
static const char *begin_op_msg =	"\007%s starting.";
static const char *disp_err_fmt =	"\004dispatch error.";
static const char *happy_exit_fmt =	"\007terminating normally.";
static const char *init_error_fmt =	"%s: cannot initialize %s.\n";
static const char *unh_signal_fmt =	"\007exiting on signal %d.";

static const char *proto_msg =		"protocol module";
static const char *net_msg =		"network";
static const char *output_msg =		"output";
static const char *acl_msg =		"ACLs";
static const char *key_msg =		"key and database";
static const char *server_name_msg =	"Kerberos V5 administrative server";

char *programname = (char *) NULL;
static jmp_buf	terminal_jmp;

static void
usage(prog)
    char *prog;
{
    fprintf(stderr, usage_format, prog, prog);
}

/*
 * An unhandled signal just proceeds from the setjmp() in main.
 */
static krb5_sigtype
unhandled_signal(signo)
    int signo;
{
#if	POSIX_SETJMP
    siglongjmp(terminal_jmp, signo);
#else	/* POSIX_SETJMP */
    longjmp(terminal_jmp, signo);
#endif	/* POSIX_SETJMP */
    /* NOTREACHED */
}

int
main(argc, argv)
    int argc;
    char *argv[];
{
    extern int		optind;
    extern char		*optarg;
    int			option;
    krb5_error_code	error;

    int			enc_type = -1;
    int			key_type = -1;
    int			manual_entry = 0;
    krb5_boolean	mime_enabled = 0;
    int			debug_level = 0;
    int			timeout = -1;
    int			nofork = 0;
    krb5_int32		service_port = -1;
    char		*acl_file = (char *) NULL;
    char		*db_file = (char *) NULL;
    char		*language_list = (char *) NULL;
    char		*db_realm = (char *) NULL;
    char		*master_key_name = (char *) NULL;
    char		*keytab_name = (char *) NULL;
    char		*stash_name = (char *) NULL;
    krb5_deltat		maxlife = -1;
    krb5_deltat		maxrlife = -1;
    krb5_timestamp	def_expiration;
    krb5_flags		def_flags;
    krb5_boolean	exp_valid, flags_valid;
    krb5_realm_params	*rparams;

    /* Kerberatic contexts */
    krb5_context	kcontext;

    const char		*errmsg;
    int			signal_number;

    /*
     * usage is:
     *	kadmind5	[-a aclfile]		<acl file>
     *			[-d database]		<database file>
     *			[-e enctype]		<encryption type>
     *			[-k masterkeytype]	<master key type>
     *			[-l languagelist]	<language list>
     *			[-m]			<manual master key entry>
     *			[-n]			<do not fork/disassociate>
     *			[-p portnumber]		<listen on port>
     *			[-r realmname]		<realm>
     *			[-s stashfile]		<stashfile>
     *			[-t timeout]		<inactivity timeout>
     *			[-D debugmask]		<debug mask>
     *			[-M masterkeyname]	<name of master key>
     *			[-T keytabname]		<key table file>
     */
    error = 0;
    exp_valid = flags_valid = FALSE;
    while ((option = getopt(argc, argv, getopt_string)) != EOF) {
	switch (option) {
	case 'a':
	    acl_file = optarg;
	    break;
	case 'd':
	    db_file = optarg;
	    break;
	case 'e':
	    if (sscanf(optarg, "%d", &enc_type) != 1) {
		fprintf(stderr, fval_not_number, argv[0], optarg, 'e');
		error++;
	    }
	    break;
	case 'm':
	    manual_entry++;
	    break;
	case 'k':
	    if (sscanf(optarg, "%d", &key_type) != 1) {
		fprintf(stderr, fval_not_number, argv[0], optarg, 'k');
		error++;
	    }
	    break;
#ifdef	LANGUAGES_SUPPORTED
	case 'l':
	    language_list = optarg;
	    mime_enabled = 1;
	    break;
#endif	/* LANGUAGES_SUPPORTED */
	case 'n':
	    nofork++;
	    break;
	case 'p':
	    if (sscanf(optarg, "%d", &service_port) != 1) {
		fprintf(stderr, fval_not_number, argv[0], optarg, 'p');
		error++;
	    }
	    break;
	case 'r':
	    db_realm = optarg;
	    break;
	case 's':
	    stash_name = optarg;
	    break;
	case 't':
	    if (sscanf(optarg, "%d", &timeout) != 1) {
		fprintf(stderr, fval_not_number, argv[0], optarg, 't');
		error++;
	    }
	    break;
	case 'D':
	    if (sscanf(optarg, "%d", &debug_level) != 1) {
		fprintf(stderr, fval_not_number, argv[0], optarg, 'D');
		error++;
	    }
	    break;
	case 'M':
	    master_key_name = optarg;
	    break;
	case 'T':
	    keytab_name = optarg;
	    break;
	default:
	    error++;
	    break;
	}
    }
    if (optind - argc > 0) {
	fprintf(stderr, extra_params, argv[0], argv[optind+1]);
	error++;
    }
    if (error) {
	usage(argv[0]);
	return(1);
    }

#ifndef	DEBUG
    /*
     * If we're not debugging and we didn't specify -n, then detach from our
     * controlling terminal and exit.
     */
    if (!nofork && daemon(0, (manual_entry != 0))) {
	fprintf(stderr, daemon_err, argv[0]);
	perror(argv[0]);
	return(2);
    }
#endif	/* DEBUG */

    /*
     * We've come this far.  Our arguments are good.
     */
#ifndef	DEBUG
    programname = (char *) strrchr(argv[0], '/');
    if (programname)
	programname++;
    else
	programname = argv[0];
#else	/* DEBUG */
    programname = argv[0];
#endif	/* DEBUG */
    krb5_init_context(&kcontext);
    krb5_init_ets(kcontext);
    krb5_klog_init(kcontext, "admin_server", programname, 1);

    /*
     * Attempt to read the KDC profile.  If we do, then read appropriate values
     * from it and supercede values supplied on the command line.
     */
    if (!(error = krb5_read_realm_params(kcontext,
					 db_realm,
					 (char *) NULL,
					 (char *) NULL,
					 &rparams))) {
	/* Get the value for the database */
	if (rparams->realm_dbname)
	    db_file = strdup(rparams->realm_dbname);

	/* Get the value for the master key name */
	if (rparams->realm_mkey_name)
	    master_key_name = strdup(rparams->realm_mkey_name);

	/* Get the value for the master key type */
	if (rparams->realm_keytype_valid)
	    key_type = rparams->realm_keytype;

	/* Get the value for the port */
	if (rparams->realm_kadmind_port_valid)
	    service_port = rparams->realm_kadmind_port;

	/* Get the value for the encryption type */
	if (rparams->realm_enctype_valid)
	    enc_type = rparams->realm_enctype;

	/* Get the value for the stashfile */
	if (rparams->realm_stash_file)
	    stash_name = strdup(rparams->realm_stash_file);

	/* Get the value for maximum ticket lifetime. */
	if (rparams->realm_max_life_valid)
	    maxlife = rparams->realm_max_life;

	/* Get the value for maximum renewable ticket lifetime. */
	if (rparams->realm_max_rlife_valid)
	    maxrlife = rparams->realm_max_rlife;

	/* Get the value for the default principal expiration */
	if (rparams->realm_expiration_valid) {
	    def_expiration = rparams->realm_expiration;
	    exp_valid = TRUE;
	}

	/* Get the value for the default principal flags */
	if (rparams->realm_flags_valid) {
	    def_flags = rparams->realm_flags;
	    flags_valid = TRUE;
	}

	krb5_free_realm_params(kcontext, rparams);
    }

    if ((signal_number =
#if	POSIX_SETJMP
	 sigsetjmp(terminal_jmp, 1)
#else	/* POSIX_SETJMP */
	 setjmp(terminal_jmp)
#endif	/* POSIX_SETJMP */
	 ) == 0) {
#if	POSIX_SIGNALS
	struct sigaction s_action;
#endif	/* POSIX_SIGNALS */

	/*
	 * Initialize signal handling.
	 */
#if	POSIX_SIGNALS
	(void) sigemptyset(&s_action.sa_mask);
	s_action.sa_flags = 0;
	s_action.sa_handler = unhandled_signal;
	(void) sigaction(SIGINT, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGTERM, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGHUP, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGQUIT, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGPIPE, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGALRM, &s_action, (struct sigaction *) NULL);
	(void) sigaction(SIGCHLD, &s_action, (struct sigaction *) NULL);
#else	/* POSIX_SIGNALS */
	signal(SIGINT, unhandled_signal);
	signal(SIGTERM, unhandled_signal);
	signal(SIGHUP, unhandled_signal);
	signal(SIGQUIT, unhandled_signal);
	signal(SIGPIPE, unhandled_signal);
	signal(SIGALRM, unhandled_signal);
	signal(SIGCHLD, unhandled_signal);
#endif	/* POSIX_SIGNALS */

	/*
	 * Initialize our modules.
	 */
	error = key_init(kcontext, debug_level, enc_type, key_type,
			 master_key_name, manual_entry, db_file, db_realm,
			 keytab_name, stash_name);
	if (!error) {
	    error = acl_init(kcontext, debug_level, acl_file);
	    if (!error) {
		error = output_init(kcontext, debug_level,
				    language_list, mime_enabled);
		if (!error) {
		    error = net_init(kcontext, debug_level, service_port);
		    if (!error) {
			error = proto_init(kcontext, debug_level, timeout);
			admin_init(maxlife,
				   maxrlife,
				   exp_valid,
				   def_expiration,
				   flags_valid,
				   def_flags);
			if (error)
			    errmsg = proto_msg;
		    }
		    else
			errmsg = net_msg;
		}
		else
		    errmsg = output_msg;
	    }
	    else
		errmsg = acl_msg;
	}
	else
	    errmsg = key_msg;

	if (!error) {
	    /*
	     * We've successfully initialized here.
	     */
#ifndef	DEBUG
	    com_err(programname, 0, begin_op_msg, server_name_msg);
#endif	/* DEBUG */

	    /*
	     * net_dispatch() only returns when we're done for some reason.
	     */
	    error = net_dispatch(kcontext);

	    com_err(programname, error,
		    ((error) ? disp_err_fmt : happy_exit_fmt));
	}
	else {
	    /* Initialization error */
	    fprintf(stderr, init_error_fmt, programname, errmsg);
	}
    }
    else {
	/* Received an unhandled signal */
#ifndef DEBUG
	com_err(programname, 0, unh_signal_fmt, signal_number);
#endif
    }

    /* Now clean up after ourselves */
    proto_finish(kcontext, debug_level);
    net_finish(kcontext, debug_level);
    output_finish(kcontext, debug_level);
    acl_finish(kcontext, debug_level);
    key_finish(kcontext, debug_level);
    krb5_klog_close(kcontext);
    krb5_xfree(kcontext);
    exit(error);
}