6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
8 * Server for user registration with SMS and Kerberos.
10 * This program is a client of the SMS server and the Kerberos
11 * admin_server, and is a server for the userreg program.
14 * Revision 1.4 1987-09-04 23:33:19 wesommer
15 * Deleted test scaffolding (second oops.)
17 * Revision 1.3 87/09/03 03:05:18 wesommer
18 * Version used for userreg tests.
20 * Revision 1.2 87/08/22 18:39:45 wesommer
21 * User registration server.
23 * Revision 1.1 87/07/31 15:48:13 wesommer
29 static char *rcsid_reg_svr_c = "$Header$";
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
42 #include "ureg_proto.h"
43 #include "../../include/sms.h"
44 #include "admin_server.h"
45 #include "admin_err.h"
49 extern char *strdup();
50 extern char *malloc();
51 extern int krb_err_base;
52 extern char admin_errmsg[];
55 #define STAMP { time (&now); printf(ctime(&now)); }
66 static char retval[BUFSIZ];
70 struct sockaddr_in sin;
87 sp = getservbyname("sms_ureg", "udp");
89 fprintf(stderr, "Unknown service sms_ureg/udp\n");
92 s = socket(AF_INET, SOCK_DGRAM, 0);
97 bzero((char *)&sin, sizeof(sin));
99 sin.sin_family = AF_INET;
100 sin.sin_port = sp->s_port;
101 sin.sin_addr.s_addr = INADDR_ANY;
103 if (bind(s, &sin, sizeof(sin)) < 0) {
108 status = sms_connect();
110 com_err("reg_svr", status, " on connect");
115 com_err("reg_svr", status, " on auth");
122 addrlen = sizeof(sin);
123 bzero(retval, BUFSIZ);
124 len = recvfrom(s, buf, BUFSIZ, 0, &sin, &addrlen);
127 if (errno == EINTR) continue;
131 /* Parse a request packet */
132 status = parse_pkt(buf, len, &seqno, &message);
135 format_pkt(buf, &len, seqno, status, (char *)NULL);
136 (void) sendto(s, buf, len, 0, &sin, addrlen);
140 switch((int)message.request) {
141 case UREG_VERIFY_USER:
142 status = verify_user(&message);
144 case UREG_RESERVE_LOGIN:
145 status = reserve_user(&message);
147 case UREG_SET_PASSWORD:
148 status = set_password(&message);
152 status = UREG_UNKNOWN_REQUEST;
156 format_pkt(buf, &len, seqno, status, retval);
158 sendto(s, buf, len, 0, &sin, addrlen);
169 #define min(a,b) ((a)>(b)?(b):(a))
171 int validate_idno(message, db_mit_id, first, last)
178 static char decrypt[BUFSIZ];
180 static char hashid[14];
181 char idnumber[BUFSIZ];
187 for (i = 0; i < message->sealed_len; i++) {
188 printf("%02x ", (unsigned char)message->sealed[i]);
194 string_to_key(db_mit_id, key);
195 key_sched(key, sched);
196 pcbc_encrypt(message->sealed, decrypt, message->sealed_len, sched, key, 0);
199 for (i = 0; i < message->sealed_len; i++) {
200 printf("%02x ", (unsigned char)decrypt[i]);
203 for (i = 0; i < message->sealed_len; i++) {
204 if (isprint(decrypt[i]))
205 printf("%c ", (unsigned char)decrypt[i]);
210 (void) strncpy(idnumber, decrypt, message->sealed_len);
211 temp = decrypt + strlen(idnumber) + 1;
212 len = message->sealed_len - (temp - decrypt);
214 (void) strncpy(hashid, temp, min(len, 14));
215 temp += strlen(hashid) + 1;
216 len = message->sealed_len - (temp - decrypt);
218 if (strcmp(hashid, db_mit_id)) return 1;
219 encrypt_mitid(recrypt, idnumber, first, last);
220 if (strcmp(recrypt, db_mit_id)) return 1;
228 static int status_in_db;
230 vfy_callbk(argc, argv, p_message)
231 int argc; /* Should sanity check this.. */
235 struct msg *message = (struct msg *)p_message;
237 char *firstname, *lastname;
241 printf("Callback: %s %s %s\n", argv[8], argv[5], argv[4]);
243 if (got_one) return 0;
250 status = validate_idno(message, db_mit_id, firstname, lastname);
251 if (status) return 0; /* Nope; decryption failed */
253 status_in_db = atoi(argv[7]);
254 reg_status = status_in_db;
256 if (status_in_db != 0) {
257 (void) strcpy(retval, argv[0]);
259 user_id = atoi(argv[1]);
264 encrypt_mitid(buf, idnumber, first, last)
265 char *buf, *idnumber, *first, *last;
268 extern char *crypt();
270 #define _tolower(c) ((c)|0x60)
272 salt[0] = _tolower(last[0]);
273 salt[1] = _tolower(first[0]);
276 (void) strcpy(buf, crypt(&idnumber[2], salt));
279 int verify_user(message)
286 printf("verify_user\n");
287 argv[0] = "get_user_by_first_and_last";
288 argv[1] = message->first;
289 argv[2] = message->last;
292 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
294 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
295 if (!got_one && !status)
296 status = UREG_USER_NOT_FOUND;
298 if (status != 0) goto punt;
300 if (reg_status == 1) status = UREG_ALREADY_REGISTERED;
301 if (reg_status == 2) status = UREG_NO_PASSWD_YET;
307 reserve_user(message)
317 printf("reserve_user\n");
319 argv[0] = "gufl"; /* get_user_by_first_and_last */
320 argv[1] = message->first;
321 argv[2] = message->last;
324 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
328 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
329 if (!got_one && !status)
330 status = UREG_USER_NOT_FOUND;
332 if (status != 0) goto punt;
333 if (reg_status != 0) {
334 status = UREG_ALREADY_REGISTERED;
338 * He's made it past this phase already.
340 if (status_in_db == 2) {
344 /* Sanity check requested login name. */
345 printf("reg_misc_len = %d\n", reg_misc_len);
347 for (i = 0; i < reg_misc_len && reg_misc[i]; i++) {
348 if (!islower(reg_misc[i])) {
349 status = UREG_INVALID_UNAME;
353 if (i < 3 || i > 8) {
354 status = UREG_INVALID_UNAME;
359 /* Send request to kerberos admin_server for login name */
361 printf("get_svc_in_tkt\n");
362 status = get_svc_in_tkt("register", "kerberos", "ATHENA.MIT.EDU",
363 "changepw", "kerberos",
366 status += krb_err_base;
371 printf("admin_call\n");
372 /* send set password request to kerberos admin_server */
373 (void) sprintf(uid_buf, "%013d", user_id); /* 13 chars of placebo */
374 /* for backwards-compat. */
376 status = admin_call(ADMIN_ADD_NEW_KEY_ATTR, login, "",
380 if (status == ADMIN_SERVER_ERROR) {
381 printf("Server error: %s\n", admin_errmsg);
383 if (strcmp(admin_errmsg,
384 "Principal already in kerberos database.") ==0)
385 status = UREG_LOGIN_USED;
395 status = set_login(login, mit_id);
399 com_err("set_login", status, 0);
402 /* choose post office */
405 status = choose_pobox(login);
407 com_err("choose_pobox", status, 0);
413 status = create_group(login);
414 if (status == SMS_LIST) status = UREG_LOGIN_USED;
417 com_err("create_group", status, 0);
420 /* set quota entry, create filsys */
423 status = alloc_filsys(login, SMS_FS_STUDENT, 0, 0);
424 if (status == SMS_FILESYS_EXISTS) status = UREG_LOGIN_USED;
426 com_err("alloc_filsys", status, 0);
429 /* set filsys and status in SMS database */
432 status = set_status_filsys(reg_misc, mit_id);
434 com_err("set_filsys", status, 0);
440 printf("reserve_user returning %s\n", error_message(status));
444 set_password(message)
450 printf("set_password\n");
452 /* validate that user is who he claims to be */
454 argv[0] = "get_user_by_first_and_last";
455 argv[1] = message->first;
456 argv[2] = message->last;
459 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
461 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
462 if (!got_one && !status)
463 status = UREG_USER_NOT_FOUND;
465 if (status != 0) goto punt;
467 /* validate that state is equal to '2' (login, but no password) */
469 if (reg_status != 2) {
470 status = UREG_NO_LOGIN_YET;
474 printf("password for %s would be set to %s\n", retval ,reg_misc);
477 status = get_svc_in_tkt("register", "kerberos", "ATHENA.MIT.EDU",
478 "changepw", "kerberos",
481 status += krb_err_base;
484 /* send set password request to kerberos admin_server */
485 status = admin_call(ADMIN_SET_KDC_PASSWORD, retval, "",
486 reg_misc, "BBBBBBBBBBBBB");
488 if (status) goto punt;
491 status = set_final_status(retval, mit_id);
493 /* reflect reply to client */
499 parse_pkt(buf, len, seqnop, messagep)
503 struct msg *messagep;
505 if (len < 4) return UREG_BROKEN_PACKET;
506 bcopy(buf, (char *)&messagep->version, sizeof(long));
507 messagep->version = ntohl(messagep->version);
508 if (messagep->version != 1) return UREG_WRONG_VERSION;
513 if (len < 4) return UREG_BROKEN_PACKET;
514 bcopy(buf, (char *)seqnop, sizeof(long));
519 if (len < 4) return UREG_BROKEN_PACKET;
520 bcopy(buf, (char *)(&messagep->request), sizeof(long));
521 messagep->request = ntohl(messagep->request);
525 messagep->first = buf;
527 for (; *buf && len > 0; --len, ++buf) continue;
528 if (len <= 0) return UREG_BROKEN_PACKET;
532 messagep->last = buf;
534 for (; *buf && len > 0; --len, ++buf) continue;
535 if (len <= 0) return UREG_BROKEN_PACKET;
539 if (len <= 0) return UREG_BROKEN_PACKET;
541 messagep->sealed = buf;
542 messagep->sealed_len = len;
547 format_pkt(buf, lenp, seqno, status, message)
554 u_long vers = htonl((u_long)1);
555 status = htonl((u_long)status);
557 bcopy((char *)&vers, buf, sizeof(long));
558 bcopy((char *)&seqno, buf+sizeof(long), sizeof(long));
559 bcopy((char *)&status, buf+ 2*sizeof(long), sizeof(long));
560 *lenp = sizeof(long) * 3;
561 (void) strcpy(buf+3*sizeof(long), message);
562 (*lenp) += strlen(message);
565 store_user(argc, argv, argp)
570 char **retv = (char **) argp;
573 for (i = 0; i < argc; i++) {
578 retv[i] = strdup(argv[i]);
585 * Set login name of user with "idnumber" to be "username"
588 set_login(username, idnumber)
596 argv[0] = "get_user_by_mitid";
599 for (i=0; i<13; i++) {
603 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
604 if (status) return status;
608 if (retv[4]) free(retv[4]);
609 retv[4] = "null"; /* No such filesystem */
611 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
613 status = sms_query("update_user", 12, retv, abort, 0);
617 for (i=1; i<12; i++) {
618 if (retv[i]) free(retv[i]);
626 * Set the status and filsys of user with username "uname" and filesys filsys.
629 set_status_filsys(username, idnumber)
637 argv[0] = "get_user_by_mitid";
640 for (i=0; i<13; i++) {
644 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
645 if (status) return status;
655 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
657 status = sms_query("update_user", 12, retv, abort, 0);
660 for (i=1; i<12; i++) {
661 if (retv[i]) free(retv[i]);
667 * Set the status and filsys of user with username "uname" and filesys filsys.
670 set_final_status(username, idnumber)
678 argv[0] = "get_user_by_mitid";
681 for (i=0; i<13; i++) {
685 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
686 if (status) return status;
693 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
695 status = sms_query("update_user", 12, retv, abort, 0);
697 for (i=1; i<12; i++) {
698 if (retv[i]) free(retv[i]);
708 static char *cr[] = {
715 return sms_query_internal(2, cr, abort, 0);
721 * c-continued-statement-offset: 4
723 * c-argdecl-indent: 4