6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
8 * Utility functions for communication with the Kerberos admin_server
10 * Original version written by Jeffery I. Schiller, January 1987
11 * Completely gutted and rewritten by Bill Sommerfeld, August 1987
16 static char *rcsid_admin_call_c = "$Header$";
19 #include <sys/errno.h>
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
30 #include "admin_err.h"
31 #include "admin_server.h"
35 extern int krb_err_base; /* Offset between com_err and kerberos codes */
36 extern int errno; /* System call error numbers */
38 extern long gethostid();
40 static u_char *strapp(); /* string append function */
42 static int inited = 0; /* are we initialized? */
43 static int admin_fd = -1; /* socket to talk to admin_server. */
45 static struct sockaddr_in admin_addr; /* address of admin server. */
46 static struct sockaddr_in my_addr; /* address bound to admin_fd. */
47 static int my_addr_len; /* size of above address. */
49 static char krbrlm[REALM_SZ]; /* Local kerberos realm */
50 static char krbhost[BUFSIZ]; /* Name of server for local realm */
51 char admin_errmsg[BUFSIZ]; /* Server error message */
54 * Initialize socket, etc. to use to talk to admin_server.
59 register int status = 0;
62 struct hostent *hp; /* host to talk to */
63 struct servent *sp; /* service to talk to */
66 if (status = get_krbrlm(krbrlm, 1)) {
67 status += krb_err_base;
75 if (status = get_krbhst(krbhost, krbrlm, 1)) {
76 status += krb_err_base;
79 hp = gethostbyname(krbhost);
81 status = ADMIN_UNKNOWN_HOST;
84 sp = getservbyname("atest3", "udp");
86 status = ADMIN_UNKNOWN_SERVICE;
89 bzero((char *)&admin_addr, sizeof(admin_addr));
90 admin_addr.sin_family = hp->h_addrtype;
91 bcopy((char *)hp->h_addr, (char *)&admin_addr.sin_addr, hp->h_length);
92 admin_addr.sin_port = sp->s_port;
99 (void) close(admin_fd);
105 * Build and transmit request to admin_server, and wait for
106 * response from server. Returns a standard error code.
110 admin_call(opcode, pname, old_passwd, new_passwd, crypt_passwd)
118 register u_char *bp; /* Pointer into buffer. */
119 u_char *ep; /* End of buffer pointer. */
121 u_char pvt_buf[BUFSIZ]; /* private message plaintext */
122 int pvt_len; /* length of valid data in pvt_buf */
124 u_char sealed_buf[BUFSIZ]; /* sealed version of private message */
125 int sealed_len; /* length of valid data in sealed_buf */
127 u_long checksum; /* quad_cksum of sealed request. */
129 C_Block sess_key; /* Session key. */
130 Key_schedule sess_sched; /* Key schedule for above. */
132 CREDENTIALS cred; /* Kerberos credentials. */
133 KTEXT_ST authent; /* Authenticator */
134 KTEXT_ST reply; /* Reply from admin_server */
135 MSG_DAT msg_data; /* Decrypted message */
136 int attempts; /* Number of retransmits so far */
138 struct sockaddr rec_addr; /* Address we got reply from */
139 int rec_addr_len; /* Length of that address */
140 int on = 1; /* ioctl argument */
144 status = admin_call_init();
145 if (status) goto bad;
151 * sealed message consists of:
152 * version number (one byte).
153 * request code (one byte).
154 * principal name (null terminated).
155 * new password (in the clear, null terminated).
156 * old password or instance (null terminated)
157 * crypt(new password, seed) (null terminated).
159 * a '\001' character.
160 * This is all sealed inside a private_message, with an
161 * authenticator tacked on in front.
165 ep = pvt_buf + sizeof(pvt_buf);
167 *bp++ = PW_SRV_VERSION;
169 if ((bp = strapp(bp, (u_char *)pname, ep)) &&
170 (bp = strapp(bp, (u_char *)new_passwd, ep)) &&
171 (bp = strapp(bp, (u_char *)old_passwd, ep)) &&
172 (bp = strapp(bp, (u_char *)crypt_passwd, ep))) {
175 pvt_len = bp - pvt_buf;
177 status = ADMIN_TOO_LONG;
182 * find our session key.
185 if (status = krb_get_cred("changepw", "kerberos", krbrlm, &cred)) {
186 status += krb_err_base;
190 bcopy((char *)cred.session, (char *)sess_key, sizeof(sess_key));
191 bzero((char *)cred.session, sizeof(sess_key)); /* lest we remember */
193 if(key_sched(sess_key, sess_sched)) {
194 status = ADMIN_BAD_KEY;
202 admin_fd = socket(admin_addr.sin_family, SOCK_DGRAM, 0);
208 bzero((char *)&my_addr, sizeof(my_addr));
210 my_addr.sin_family = admin_addr.sin_family;
211 my_addr.sin_addr.s_addr = gethostid();
213 if (bind(admin_fd, &my_addr, sizeof(my_addr)) < 0) {
218 my_addr_len = sizeof(my_addr);
220 if (getsockname(admin_fd, (struct sockaddr *)&my_addr,
226 if (ioctl(admin_fd, FIONBIO, (char *)&on) < 0) {
232 * Encrypt the message using the session key.
233 * Since this contains passwords, it must be kept from prying eyes.
236 sealed_len = krb_mk_priv(pvt_buf, sealed_buf, pvt_len,
237 sess_sched, sess_key, &my_addr,
239 if (sealed_len < 0) {
240 status = ADMIN_CANT_ENCRYPT;
245 * Checksum the cypher text, to guard against tampering in flight.
248 checksum = quad_cksum(sealed_buf, NULL, sealed_len, 0, sess_key);
251 * Make an authenticator, so the server can learn the session key
252 * and know who we are.
255 if (status = krb_mk_req(&authent, "changepw", "kerberos", krbrlm,
257 status += krb_err_base;
262 * Add the sealed message to the end of the authenticator.
265 if ((authent.length + sealed_len) > MAX_KTXT_LEN) {
266 status = ADMIN_TOO_LONG;
270 bcopy(sealed_buf, authent.dat + authent.length, sealed_len);
271 authent.length += sealed_len;
274 * send it off, and wait for a reply.
279 while (attempts++ < RETRY_LIMIT) {
280 int active; /* number of active fds (from select). */
281 fd_set set; /* interesting fd's. */
282 struct timeval timeout; /* timeout on select. */
284 if (sendto(admin_fd, (char *)authent.dat, authent.length,
285 0, &admin_addr, sizeof(admin_addr)) != authent.length) {
291 FD_SET(admin_fd, &set);
293 timeout.tv_sec = USER_TIMEOUT;
296 active = select(admin_fd+1, &set, (fd_set *)0, (fd_set *)0, &timeout);
300 } else if (active == 0) continue;
302 reply.length = recvfrom(admin_fd, (char *)reply.dat,
303 sizeof(reply.dat), 0,
304 &rec_addr, &rec_addr_len);
305 if (reply.length < 0) continue;
307 if (!bcmp(&rec_addr, &admin_addr, sizeof(admin_addr)))
308 /* the spoofers are out in force.. */
311 break; /* got one.. */
315 if (attempts > RETRY_LIMIT) {
320 if (pkt_version((&reply)) != KRB_PROT_VERSION) {
321 status = ADMIN_BAD_VERSION;
325 if ((pkt_msg_type((&reply)) & ~1) != AUTH_MSG_PRIVATE) {
327 if (*bp++ != KRB_PROT_VERSION) {
328 status = ADMIN_BAD_VERSION;
331 if (*bp++ != AUTH_MSG_ERR_REPLY) {
332 status = ADMIN_UNKNOWN_CODE;
335 bp += strlen((char *)bp) + 1; /* Skip name */
336 bp += strlen((char *)bp) + 1; /* Skip instance */
337 bp += strlen((char *)bp) + 1; /* Skip realm */
339 /* null-terminate error string */
340 reply.dat[reply.length] = '\0';
343 status = ADMIN_SERVER_ERROR;
344 strcpy(admin_errmsg, bp);
347 status = ADMIN_UNKNOWN_CODE;
351 status = krb_rd_priv(reply.dat, reply.length,
352 sess_sched, sess_key,
353 &admin_addr, &my_addr,
356 status += krb_err_base;
359 bp = msg_data.app_data;
361 if (*bp++ != PW_SRV_VERSION) {
362 status = ADMIN_BAD_VERSION;
365 if (*bp++ != INSTALL_REPLY) {
366 status = ADMIN_UNKNOWN_CODE;
371 /* fall through into cleanup */
375 * Paranoia: shred all the incriminating evidence.
376 * We'll let the caller shred the arguments.
378 bzero((char *)sess_key, sizeof(sess_key));
379 bzero((char *)sess_sched, sizeof(sess_sched));
380 bzero(pvt_buf, sizeof(pvt_buf));
383 (void) close(admin_fd);
391 * Copies characters from source to dest, returning a pointer to the
392 * point in dest after the last character copied from source.
393 * If this would be greater than end, no characters are copied, and NULL
394 * is returned instead.
397 static u_char *strapp(dest, source, end)
398 register u_char *dest, *source, *end;
400 register int length = strlen(source) + 1;
401 if (dest+length > end) return NULL;
403 bcopy(source, dest, length);
404 return dest + length;
412 * c-continued-statement-offset: 4
414 * c-argdecl-indent: 4