summaryrefslogtreecommitdiffstats
path: root/src/kadmin/v5client/network.c
blob: 2c8b5b844fea898db4c55fda74527a9fceac6061 (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
/*
 * kadmin/v5client/network.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.
 *
 */

/*
 * network.c	- Handle network and protocol related functions for kadmin5.
 */
#include "k5-int.h"
#include "com_err.h"
#include "adm.h"
#include "adm_proto.h"
#include "kadmin5.h"

/*
 * Own storage.
 */
static int		server_socket = -1;
static krb5_auth_context *server_auth_context = (krb5_auth_context *) NULL;
static krb5_ccache	server_ccache = (krb5_ccache) NULL;
static krb5_boolean	server_active = 0;
static krb5_error_code	server_stat = 0;
static krb5_boolean	server_op_in_prog = 0;

/*
 * Static strings.
 */
static const char *kadmin_server_name	= "kadmin-server";
static const char *proto_serv_supp_msg	= "server response follows:";
static const char *proto_serv_end_msg	= "end of server response.";
static const char *proto_cmd_unsupp_fmt	= "(%s) %s protocol command not supported by server";
static const char *proto_pw_unacc_fmt	= "(%s) password unacceptable to server";
static const char *proto_bad_pw_fmt	= "(%s) bad password entered";
static const char *proto_in_tkt_fmt	= "(%s) not an initial ticket";
static const char *proto_cant_chg_fmt	= "(%s) cannot change password";
static const char *proto_lang_uns_fmt	= "(%s) language not supported";
static const char *proto_p_exists_fmt	= "(%s) principal already exists";
static const char *proto_p_notexist_fmt	= "(%s) principal does not exist";
static const char *proto_no_auth_fmt	= "(%s) not authorized for this operation";
static const char *proto_bad_opt_fmt	= "(%s) option not recognized by server";
static const char *proto_value_req_fmt	= "(%s) value required for option";
static const char *proto_system_err_fmt	= "(%s) remote system error";
static const char *proto_ufo_err_fmt	= "- (%s) protocol command %s returned unexpected error %d";
static const char *net_conn_err_fmt	= "- %s: cannot connect to server";

/*
 * print_proto_sreply()	- Print server's error reply strings.
 */
void
print_proto_sreply(ncomps, complist)
    krb5_int32	ncomps;
    krb5_data	*complist;
{
    int i;

    if (ncomps > 0) {
	for (i=0; i<ncomps; i++)
	    com_err(kadmin_server_name, 0, complist[i].data);
    }
}

/*
 * print_proto_error()	- Print protocol error message if appropriate.
 */
void
print_proto_error(cmd, cstat, ncomps, complist)
    char	*cmd;
    krb5_int32	cstat;
    krb5_int32	ncomps;
    krb5_data	*complist;
{
    switch (cstat) {
    case KRB5_ADM_SUCCESS:
	break;
    case KRB5_ADM_CMD_UNKNOWN:
	com_err(programname, 0, proto_cmd_unsupp_fmt, requestname, cmd);
	break;
    case KRB5_ADM_PW_UNACCEPT:
	com_err(programname, 0, proto_pw_unacc_fmt, requestname);
	break;
    case KRB5_ADM_BAD_PW:
	com_err(programname, 0, proto_bad_pw_fmt, requestname);
	break;
    case KRB5_ADM_NOT_IN_TKT:
	com_err(programname, 0, proto_in_tkt_fmt, requestname);
	break;
    case KRB5_ADM_CANT_CHANGE:
	com_err(programname, 0, proto_cant_chg_fmt, requestname);
	break;
    case KRB5_ADM_LANG_NOT_SUPPORTED:
	com_err(programname, 0, proto_lang_uns_fmt, requestname);
	break;
    case KRB5_ADM_P_ALREADY_EXISTS:
	com_err(programname, 0, proto_p_exists_fmt, requestname);
	break;
    case KRB5_ADM_P_DOES_NOT_EXIST:
	com_err(programname, 0, proto_p_notexist_fmt, requestname);
	break;
    case KRB5_ADM_NOT_AUTHORIZED:
	com_err(programname, 0, proto_no_auth_fmt, requestname);
	break;
    case KRB5_ADM_BAD_OPTION:
	com_err(programname, 0, proto_bad_opt_fmt, requestname);
	break;
    case KRB5_ADM_VALUE_REQUIRED:
	com_err(programname, 0, proto_value_req_fmt, requestname);
	break;
    case KRB5_ADM_SYSTEM_ERROR:
	com_err(programname, 0, proto_system_err_fmt, requestname);
	break;
    default:
	com_err(programname, cstat, proto_ufo_err_fmt, requestname,
		cmd, cstat);
	break;
    }
    if (cstat != KRB5_ADM_SUCCESS)
	print_proto_sreply(ncomps, complist);
}

/*
 * net_connect()	- Connect to the administrative server if not already
 *			  connected or a separate connection is required for
 *			  each transaction.
 */
static krb5_error_code
net_connect()
{
    krb5_error_code	kret = 0;

    /*
     * Drop the connection if we were in the middle of something before.
     */
    if (server_op_in_prog)
	net_disconnect(1);

    if (!multiple || !server_active) {
	char opassword[KRB5_ADM_MAX_PASSWORD_LEN];

	if (!(kret = server_stat = krb5_adm_connect(kcontext,
						    principal_name,
						    password_prompt,
						    opassword,
						    &server_socket,
						    &server_auth_context,
						    &server_ccache))) {
	    server_active = 1;
	    memset(opassword, 0, KRB5_ADM_MAX_PASSWORD_LEN);
	}
	else
	    com_err(programname, kret, net_conn_err_fmt, requestname);
    }
    return(kret);
}

/*
 * kadmin_disconnect()	- Disconnect from the server.  If there has been
 *			  a server error, just close the socket.  Otherwise
 *			  engage in the disconnection protocol.
 */
void
net_disconnect(force)
    krb5_boolean	force;
{
    /*
     * Only need to do this if we think the connection is active.
     */
    if (server_active) {
	/*
	 * Disconnect if:
	 *	1) this is a one-time-only connection.
	 *	2) there was an error on the connection.
	 *	3) somebody's forcing the disconnection.
	 */
	if (!multiple || (server_stat != 0) || force) {
	    /* If the connection is still good, then send a QUIT command */
	    if ((server_stat == 0) && !server_op_in_prog) {
		krb5_data	quit_data;
		krb5_int32	quit_status;
		krb5_int32	quit_ncomps;
		krb5_data	*quit_reply;

		quit_data.data = KRB5_ADM_QUIT_CMD;
		quit_data.length = strlen(quit_data.data);
		if (!(server_stat = krb5_send_adm_cmd(kcontext,
						      &server_socket,
						      server_auth_context,
						      1,
						      &quit_data)))
		    server_stat = krb5_read_adm_reply(kcontext,
						      &server_socket,
						      server_auth_context,
						      &quit_status,
						      &quit_ncomps,
						      &quit_reply);
		if (!server_stat) {
		    print_proto_error(KRB5_ADM_QUIT_CMD,
				      quit_status,
				      quit_ncomps,
				      quit_reply);
		    krb5_free_adm_data(kcontext, quit_ncomps, quit_reply);
		}
	    }
	    /* Break down the connection */
	    krb5_adm_disconnect(kcontext,
				&server_socket,
				server_auth_context,
				server_ccache);

	    /* Clean up our state. */
	    server_socket = -1;
	    server_auth_context = (krb5_auth_context *) NULL;
	    server_ccache = (krb5_ccache) NULL;
	    server_active = 0;
	    server_op_in_prog = 0;
	    server_stat = EINVAL;
	}
    }
}

/*
 * net_do_proto()	- Perform a protocol request and return the results.
 */
krb5_error_code
net_do_proto(cmd, arg1, arg2, nargs, argp, rstatp, ncompp, complistp)
    char	*cmd;
    char	*arg1;
    char	*arg2;
    krb5_int32	nargs;
    krb5_data	*argp;
    krb5_int32	*rstatp;
    krb5_int32	*ncompp;
    krb5_data	**complistp;
{
    krb5_error_code	kret;
    krb5_int32		nprotoargs;
    krb5_data		*protoargs;

    /* Connect to the server, if necessary */
    if (!(kret = net_connect())) {

	/* Figure out how many things we need to prepend to the arguments */
	nprotoargs = nargs + 1;
	if (arg1)
	    nprotoargs++;
	if (arg2)
	    nprotoargs++;

	/* Get the space for the new argument list */
	if (protoargs = (krb5_data *) malloc((size_t) nprotoargs *
					     sizeof(krb5_data))) {
	    int	index = 0;

	    /* Copy in the command */
	    protoargs[index].data = cmd;
	    protoargs[index].length = strlen(cmd);
	    index++;

	    /* Copy in the optional arguments */
	    if (arg1) {
		protoargs[index].data = arg1;
		protoargs[index].length = strlen(arg1);
		index++;
	    }
	    if (arg2) {
		protoargs[index].data = arg2;
		protoargs[index].length = strlen(arg2);
		index++;
	    }

	    /* Copy in the argument list */
	    memcpy(&protoargs[index], argp,
		   (size_t) (nargs*sizeof(krb5_data)));

	    server_op_in_prog = 1;
	    /*
	     * Now send the command.
	     */
	    if (!(kret = server_stat = krb5_send_adm_cmd(kcontext,
							 &server_socket,
							 server_auth_context,
							 nprotoargs,
							 protoargs))) {
		/*
		 * If that was successful, then try to read the reply.
		 */
		kret = server_stat = krb5_read_adm_reply(kcontext,
							 &server_socket,
							 server_auth_context,
							 rstatp,
							 ncompp,
							 complistp);
		print_proto_error(cmd, *rstatp, *ncompp, *complistp);
	    }
	    server_op_in_prog = 0;
	    free(protoargs);
	}
	else
	    kret = ENOMEM;
	net_disconnect(0);
    }
    return(kret);
}