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>
31 #include "admin_err.h"
32 #include "admin_server.h"
36 extern int krb_err_base; /* Offset between com_err and kerberos codes */
37 extern int errno; /* System call error numbers */
39 extern long gethostid();
41 static u_char *strapp(); /* string append function */
43 static int inited = 0; /* are we initialized? */
44 static int admin_fd = -1; /* socket to talk to admin_server. */
46 static struct sockaddr_in admin_addr; /* address of admin server. */
47 static struct sockaddr_in my_addr; /* address bound to admin_fd. */
48 static int my_addr_len; /* size of above address. */
50 static char krbrlm[REALM_SZ]; /* Local kerberos realm */
51 static char krbhost[BUFSIZ]; /* Name of server for local realm */
52 char admin_errmsg[BUFSIZ]; /* Server error message */
55 * Initialize socket, etc. to use to talk to admin_server.
60 register int status = 0;
63 struct hostent *hp; /* host to talk to */
64 struct servent *sp; /* service to talk to */
67 if (status = get_krbrlm(krbrlm, 1)) {
68 status += krb_err_base;
76 if (status = get_krbhst(krbhost, krbrlm, 1)) {
77 status += krb_err_base;
80 hp = gethostbyname(krbhost);
82 status = ADMIN_UNKNOWN_HOST;
85 sp = getservbyname("atest3", "udp");
87 status = ADMIN_UNKNOWN_SERVICE;
90 bzero((char *)&admin_addr, sizeof(admin_addr));
91 admin_addr.sin_family = hp->h_addrtype;
92 bcopy((char *)hp->h_addr, (char *)&admin_addr.sin_addr, hp->h_length);
93 admin_addr.sin_port = sp->s_port;
95 /* lowercase & truncate hostname becuase it will be used as an
100 for (s = krbhost; *s && *s != '.'; s++)
110 (void) close(admin_fd);
116 * Build and transmit request to admin_server, and wait for
117 * response from server. Returns a standard error code.
121 admin_call(opcode, pname, old_passwd, new_passwd, crypt_passwd)
129 register u_char *bp; /* Pointer into buffer. */
130 u_char *ep; /* End of buffer pointer. */
132 u_char pvt_buf[BUFSIZ]; /* private message plaintext */
133 int pvt_len; /* length of valid data in pvt_buf */
135 u_char sealed_buf[BUFSIZ]; /* sealed version of private message */
136 int sealed_len; /* length of valid data in sealed_buf */
138 u_long checksum; /* quad_cksum of sealed request. */
140 C_Block sess_key; /* Session key. */
141 Key_schedule sess_sched; /* Key schedule for above. */
143 CREDENTIALS cred; /* Kerberos credentials. */
144 KTEXT_ST authent; /* Authenticator */
145 KTEXT_ST reply; /* Reply from admin_server */
146 MSG_DAT msg_data; /* Decrypted message */
147 int attempts; /* Number of retransmits so far */
149 struct sockaddr rec_addr; /* Address we got reply from */
150 int rec_addr_len; /* Length of that address */
151 int on = 1; /* ioctl argument */
155 status = admin_call_init();
156 if (status) goto bad;
162 * sealed message consists of:
163 * version number (one byte).
164 * request code (one byte).
165 * principal name (null terminated).
166 * new password (in the clear, null terminated).
167 * old password or instance (null terminated)
168 * crypt(new password, seed) (null terminated).
170 * a '\001' character.
171 * This is all sealed inside a private_message, with an
172 * authenticator tacked on in front.
176 ep = pvt_buf + sizeof(pvt_buf);
178 *bp++ = PW_SRV_VERSION;
180 if ((bp = strapp(bp, (u_char *)pname, ep)) &&
181 (bp = strapp(bp, (u_char *)new_passwd, ep)) &&
182 (bp = strapp(bp, (u_char *)old_passwd, ep)) &&
183 (bp = strapp(bp, (u_char *)crypt_passwd, ep))) {
186 pvt_len = bp - pvt_buf;
188 status = ADMIN_TOO_LONG;
193 * find our session key.
196 if (status = krb_get_cred("changepw", krbhost, krbrlm, &cred)) {
197 status += krb_err_base;
201 bcopy((char *)cred.session, (char *)sess_key, sizeof(sess_key));
202 bzero((char *)cred.session, sizeof(sess_key)); /* lest we remember */
204 if(key_sched(sess_key, sess_sched)) {
205 status = ADMIN_BAD_KEY;
213 admin_fd = socket(admin_addr.sin_family, SOCK_DGRAM, 0);
219 bzero((char *)&my_addr, sizeof(my_addr));
221 my_addr.sin_family = admin_addr.sin_family;
222 my_addr.sin_addr.s_addr = gethostid();
224 if (bind(admin_fd, &my_addr, sizeof(my_addr)) < 0) {
229 my_addr_len = sizeof(my_addr);
231 if (getsockname(admin_fd, (struct sockaddr *)&my_addr,
237 if (ioctl(admin_fd, FIONBIO, (char *)&on) < 0) {
243 * Encrypt the message using the session key.
244 * Since this contains passwords, it must be kept from prying eyes.
247 sealed_len = krb_mk_priv(pvt_buf, sealed_buf, pvt_len,
248 sess_sched, sess_key, &my_addr,
250 if (sealed_len < 0) {
251 status = ADMIN_CANT_ENCRYPT;
256 * Checksum the cypher text, to guard against tampering in flight.
259 checksum = quad_cksum(sealed_buf, NULL, sealed_len, 0, sess_key);
262 * Make an authenticator, so the server can learn the session key
263 * and know who we are.
266 if (status = krb_mk_req(&authent, "changepw", krbhost, krbrlm,
268 status += krb_err_base;
273 * Add the sealed message to the end of the authenticator.
276 if ((authent.length + sealed_len) > MAX_KTXT_LEN) {
277 status = ADMIN_TOO_LONG;
281 bcopy(sealed_buf, authent.dat + authent.length, sealed_len);
282 authent.length += sealed_len;
285 * send it off, and wait for a reply.
290 while (attempts++ < RETRY_LIMIT) {
291 int active; /* number of active fds (from select). */
292 fd_set set; /* interesting fd's. */
293 struct timeval timeout; /* timeout on select. */
295 if (sendto(admin_fd, (char *)authent.dat, authent.length,
296 0, &admin_addr, sizeof(admin_addr)) != authent.length) {
302 FD_SET(admin_fd, &set);
304 timeout.tv_sec = USER_TIMEOUT;
307 active = select(admin_fd+1, &set, (fd_set *)0, (fd_set *)0, &timeout);
311 } else if (active == 0) continue;
313 reply.length = recvfrom(admin_fd, (char *)reply.dat,
314 sizeof(reply.dat), 0,
315 &rec_addr, &rec_addr_len);
316 if (reply.length < 0) continue;
318 if (!bcmp(&rec_addr, &admin_addr, sizeof(admin_addr)))
319 /* the spoofers are out in force.. */
322 break; /* got one.. */
326 if (attempts > RETRY_LIMIT) {
331 if (pkt_version((&reply)) != KRB_PROT_VERSION) {
332 status = ADMIN_BAD_VERSION;
336 if ((pkt_msg_type((&reply)) & ~1) != AUTH_MSG_PRIVATE) {
338 if (*bp++ != KRB_PROT_VERSION) {
339 status = ADMIN_BAD_VERSION;
342 if (*bp++ != AUTH_MSG_ERR_REPLY) {
343 status = ADMIN_UNKNOWN_CODE;
346 bp += strlen((char *)bp) + 1; /* Skip name */
347 bp += strlen((char *)bp) + 1; /* Skip instance */
348 bp += strlen((char *)bp) + 1; /* Skip realm */
350 /* null-terminate error string */
351 reply.dat[reply.length] = '\0';
354 status = ADMIN_SERVER_ERROR;
355 strcpy(admin_errmsg, bp);
358 status = ADMIN_UNKNOWN_CODE;
362 status = krb_rd_priv(reply.dat, reply.length,
363 sess_sched, sess_key,
364 &admin_addr, &my_addr,
367 status += krb_err_base;
370 bp = msg_data.app_data;
372 if (*bp++ != PW_SRV_VERSION) {
373 status = ADMIN_BAD_VERSION;
376 if (*bp++ != INSTALL_REPLY) {
377 status = ADMIN_UNKNOWN_CODE;
382 /* fall through into cleanup */
386 * Paranoia: shred all the incriminating evidence.
387 * We'll let the caller shred the arguments.
389 bzero((char *)sess_key, sizeof(sess_key));
390 bzero((char *)sess_sched, sizeof(sess_sched));
391 bzero(pvt_buf, sizeof(pvt_buf));
394 (void) close(admin_fd);
402 * Copies characters from source to dest, returning a pointer to the
403 * point in dest after the last character copied from source.
404 * If this would be greater than end, no characters are copied, and NULL
405 * is returned instead.
408 static u_char *strapp(dest, source, end)
409 register u_char *dest, *source, *end;
411 register int length = strlen(source) + 1;
412 if (dest+length > end) return NULL;
414 bcopy(source, dest, length);
415 return dest + length;
423 * c-continued-statement-offset: 4
425 * c-argdecl-indent: 4