6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
10 * Utility functions for communication with the Kerberos admin_server
12 * Original version written by Jeffery I. Schiller, January 1987
13 * Completely gutted and rewritten by Bill Sommerfeld, August 1987
18 static char *rcsid_admin_call_c = "$Header$";
21 #include <mit-copyright.h>
22 #include <sys/errno.h>
23 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
34 #include "admin_err.h"
35 #include "admin_server.h"
40 extern int errno; /* System call error numbers */
42 extern long gethostid();
44 static u_char *strapp(); /* string append function */
46 static int inited = 0; /* are we initialized? */
47 static int admin_fd = -1; /* socket to talk to admin_server. */
49 static struct sockaddr_in admin_addr; /* address of admin server. */
50 static struct sockaddr_in my_addr; /* address bound to admin_fd. */
51 static int my_addr_len; /* size of above address. */
53 static char krbrlm[REALM_SZ]; /* Local kerberos realm */
54 static char krbhost[BUFSIZ]; /* Name of server for local realm */
55 char admin_errmsg[BUFSIZ]; /* Server error message */
58 * Initialize socket, etc. to use to talk to admin_server.
63 register int status = 0;
66 struct hostent *hp; /* host to talk to */
67 struct servent *sp; /* service to talk to */
69 initialize_kadm_error_table();
70 if (status = krb_get_lrealm(krbrlm, 1)) {
71 status += ERROR_TABLE_BASE_krb;
79 if (status = krb_get_krbhst(krbhost, krbrlm, 1)) {
80 status += ERROR_TABLE_BASE_krb;
83 hp = gethostbyname(krbhost);
85 status = ADMIN_UNKNOWN_HOST;
88 sp = getservbyname("atest3", "udp");
90 status = ADMIN_UNKNOWN_SERVICE;
93 bzero((char *)&admin_addr, sizeof(admin_addr));
94 admin_addr.sin_family = hp->h_addrtype;
95 bcopy((char *)hp->h_addr, (char *)&admin_addr.sin_addr, hp->h_length);
96 admin_addr.sin_port = sp->s_port;
98 /* lowercase & truncate hostname becuase it will be used as an
103 for (s = krbhost; *s && *s != '.'; s++)
113 (void) close(admin_fd);
119 * Build and transmit request to admin_server, and wait for
120 * response from server. Returns a standard error code.
124 admin_call(opcode, pname, old_passwd, new_passwd, crypt_passwd)
132 register u_char *bp; /* Pointer into buffer. */
133 u_char *ep; /* End of buffer pointer. */
135 u_char pvt_buf[BUFSIZ]; /* private message plaintext */
136 int pvt_len; /* length of valid data in pvt_buf */
138 u_char sealed_buf[BUFSIZ]; /* sealed version of private message */
139 int sealed_len; /* length of valid data in sealed_buf */
141 u_long checksum; /* quad_cksum of sealed request. */
143 C_Block sess_key; /* Session key. */
144 Key_schedule sess_sched; /* Key schedule for above. */
146 CREDENTIALS cred; /* Kerberos credentials. */
147 KTEXT_ST authent; /* Authenticator */
148 KTEXT_ST reply; /* Reply from admin_server */
149 MSG_DAT msg_data; /* Decrypted message */
150 int attempts; /* Number of retransmits so far */
152 struct sockaddr rec_addr; /* Address we got reply from */
153 int rec_addr_len; /* Length of that address */
154 int on = 1; /* ioctl argument */
158 status = admin_call_init();
159 if (status) goto bad;
165 * sealed message consists of:
166 * version number (one byte).
167 * request code (one byte).
168 * principal name (null terminated).
169 * new password (in the clear, null terminated).
170 * old password or instance (null terminated)
171 * crypt(new password, seed) (null terminated).
173 * a '\001' character.
174 * This is all sealed inside a private_message, with an
175 * authenticator tacked on in front.
179 ep = pvt_buf + sizeof(pvt_buf);
181 *bp++ = PW_SRV_VERSION;
183 if ((bp = strapp(bp, (u_char *)pname, ep)) &&
184 (bp = strapp(bp, (u_char *)new_passwd, ep)) &&
185 (bp = strapp(bp, (u_char *)old_passwd, ep)) &&
186 (bp = strapp(bp, (u_char *)crypt_passwd, ep))) {
189 pvt_len = bp - pvt_buf;
191 status = ADMIN_TOO_LONG;
196 * find our session key.
199 if (status = krb_get_cred("changepw", krbhost, krbrlm, &cred)) {
200 status += ERROR_TABLE_BASE_krb;
204 bcopy((char *)cred.session, (char *)sess_key, sizeof(sess_key));
205 bzero((char *)cred.session, sizeof(sess_key)); /* lest we remember */
207 if(key_sched(sess_key, sess_sched)) {
208 status = ADMIN_BAD_KEY;
216 admin_fd = socket(admin_addr.sin_family, SOCK_DGRAM, 0);
222 bzero((char *)&my_addr, sizeof(my_addr));
224 my_addr.sin_family = admin_addr.sin_family;
225 my_addr.sin_addr.s_addr = gethostid();
227 if (bind(admin_fd, &my_addr, sizeof(my_addr)) < 0) {
232 my_addr_len = sizeof(my_addr);
234 if (getsockname(admin_fd, (struct sockaddr *)&my_addr,
240 if (ioctl(admin_fd, FIONBIO, (char *)&on) < 0) {
246 * Encrypt the message using the session key.
247 * Since this contains passwords, it must be kept from prying eyes.
250 sealed_len = krb_mk_priv(pvt_buf, sealed_buf, pvt_len,
251 sess_sched, sess_key, &my_addr,
253 if (sealed_len < 0) {
254 status = ADMIN_CANT_ENCRYPT;
259 * Checksum the cypher text, to guard against tampering in flight.
262 checksum = quad_cksum(sealed_buf, NULL, sealed_len, 0, sess_key);
265 * Make an authenticator, so the server can learn the session key
266 * and know who we are.
269 if (status = krb_mk_req(&authent, "changepw", krbhost, krbrlm,
271 status += ERROR_TABLE_BASE_krb;
276 * Add the sealed message to the end of the authenticator.
279 if ((authent.length + sealed_len) > MAX_KTXT_LEN) {
280 status = ADMIN_TOO_LONG;
284 bcopy(sealed_buf, authent.dat + authent.length, sealed_len);
285 authent.length += sealed_len;
288 * send it off, and wait for a reply.
293 while (attempts++ < RETRY_LIMIT) {
294 int active; /* number of active fds (from select). */
295 fd_set set; /* interesting fd's. */
296 struct timeval timeout; /* timeout on select. */
298 if (sendto(admin_fd, (char *)authent.dat, authent.length,
299 0, &admin_addr, sizeof(admin_addr)) != authent.length) {
305 FD_SET(admin_fd, &set);
307 timeout.tv_sec = USER_TIMEOUT;
310 active = select(admin_fd+1, &set, (fd_set *)0, (fd_set *)0, &timeout);
314 } else if (active == 0) continue;
316 reply.length = recvfrom(admin_fd, (char *)reply.dat,
317 sizeof(reply.dat), 0,
318 &rec_addr, &rec_addr_len);
319 if (reply.length < 0) continue;
321 if (!bcmp(&rec_addr, &admin_addr, sizeof(admin_addr)))
322 /* the spoofers are out in force.. */
325 break; /* got one.. */
329 if (attempts > RETRY_LIMIT) {
334 if (pkt_version((&reply)) != KRB_PROT_VERSION) {
335 status = ADMIN_BAD_VERSION;
339 if ((pkt_msg_type((&reply)) & ~1) != AUTH_MSG_PRIVATE) {
341 if (*bp++ != KRB_PROT_VERSION) {
342 status = ADMIN_BAD_VERSION;
345 if (*bp++ != AUTH_MSG_ERR_REPLY) {
346 status = ADMIN_UNKNOWN_CODE;
349 bp += strlen((char *)bp) + 1; /* Skip name */
350 bp += strlen((char *)bp) + 1; /* Skip instance */
351 bp += strlen((char *)bp) + 1; /* Skip realm */
353 /* null-terminate error string */
354 reply.dat[reply.length] = '\0';
357 status = ADMIN_SERVER_ERROR;
358 strcpy(admin_errmsg, bp);
361 status = ADMIN_UNKNOWN_CODE;
365 status = krb_rd_priv(reply.dat, reply.length,
366 sess_sched, sess_key,
367 &admin_addr, &my_addr,
370 status += ERROR_TABLE_BASE_krb;
373 bp = msg_data.app_data;
375 if (*bp++ != PW_SRV_VERSION) {
376 status = ADMIN_BAD_VERSION;
379 if (*bp++ != INSTALL_REPLY) {
380 status = ADMIN_UNKNOWN_CODE;
385 /* fall through into cleanup */
389 * Paranoia: shred all the incriminating evidence.
390 * We'll let the caller shred the arguments.
392 bzero((char *)sess_key, sizeof(sess_key));
393 bzero((char *)sess_sched, sizeof(sess_sched));
394 bzero(pvt_buf, sizeof(pvt_buf));
397 (void) close(admin_fd);
405 * Copies characters from source to dest, returning a pointer to the
406 * point in dest after the last character copied from source.
407 * If this would be greater than end, no characters are copied, and NULL
408 * is returned instead.
411 static u_char *strapp(dest, source, end)
412 register u_char *dest, *source, *end;
414 register int length = strlen(source) + 1;
415 if (dest+length > end) return NULL;
417 bcopy(source, dest, length);
418 return dest + length;