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.2 1987-08-22 18:39:45 wesommer
15 * User registration server.
17 * Revision 1.1 87/07/31 15:48:13 wesommer
23 static char *rcsid_reg_svr_c = "$Header$";
27 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
36 #include "ureg_proto.h"
37 #include "../../include/sms.h"
38 #include "admin_server.h"
39 #include "admin_err.h"
43 extern char *strdup();
44 extern char *malloc();
45 extern int krb_err_base;
46 extern char admin_errmsg[];
49 #define STAMP { time (&now); printf(ctime(&now)); }
60 static char retval[BUFSIZ];
64 struct sockaddr_in sin;
80 status = sms_connect();
82 com_err("reg_svr", status, " on connect");
87 com_err("reg_svr", status, " on auth");
91 sp = getservbyname("sms_ureg", "udp");
93 fprintf(stderr, "Unknown service sms_ureg/udp\n");
96 s = socket(AF_INET, SOCK_DGRAM, 0);
101 bzero((char *)&sin, sizeof(sin));
103 sin.sin_family = AF_INET;
104 sin.sin_port = sp->s_port;
105 sin.sin_addr.s_addr = INADDR_ANY;
107 if (bind(s, &sin, sizeof(sin)) < 0) {
113 addrlen = sizeof(sin);
114 bzero(retval, BUFSIZ);
115 len = recvfrom(s, buf, BUFSIZ, 0, &sin, &addrlen);
118 if (errno == EINTR) continue;
122 /* Parse a request packet */
123 status = parse_pkt(buf, len, &seqno, &message);
126 format_pkt(buf, &len, seqno, status, (char *)NULL);
127 (void) sendto(s, buf, len, 0, &sin, addrlen);
131 switch((int)message.request) {
132 case UREG_VERIFY_USER:
133 status = verify_user(&message);
135 case UREG_RESERVE_LOGIN:
136 status = reserve_user(&message);
138 case UREG_SET_PASSWORD:
139 status = set_password(&message);
143 status = UREG_UNKNOWN_REQUEST;
147 format_pkt(buf, &len, seqno, status, retval);
149 sendto(s, buf, len, 0, &sin, addrlen);
160 #define min(a,b) ((a)>(b)?(b):(a))
162 int validate_idno(message, db_mit_id, first, last)
169 static char decrypt[BUFSIZ];
171 static char hashid[14];
172 char idnumber[BUFSIZ];
178 for (i = 0; i < message->sealed_len; i++) {
179 printf("%02x ", (unsigned char)message->sealed[i]);
185 string_to_key(db_mit_id, key);
186 key_sched(key, sched);
187 pcbc_encrypt(message->sealed, decrypt, message->sealed_len, sched, key, 0);
190 for (i = 0; i < message->sealed_len; i++) {
191 printf("%02x ", (unsigned char)decrypt[i]);
194 for (i = 0; i < message->sealed_len; i++) {
195 if (isprint(decrypt[i]))
196 printf("%c ", (unsigned char)decrypt[i]);
201 (void) strncpy(idnumber, decrypt, message->sealed_len);
202 temp = decrypt + strlen(idnumber) + 1;
203 len = message->sealed_len - (temp - decrypt);
205 (void) strncpy(hashid, temp, min(len, 14));
206 temp += strlen(hashid) + 1;
207 len = message->sealed_len - (temp - decrypt);
209 if (strcmp(hashid, db_mit_id)) return 1;
210 encrypt_mitid(recrypt, idnumber, first, last);
211 if (strcmp(recrypt, db_mit_id)) return 1;
219 static int status_in_db;
221 vfy_callbk(argc, argv, p_message)
222 int argc; /* Should sanity check this.. */
226 struct msg *message = (struct msg *)p_message;
228 char *firstname, *lastname;
232 printf("Callback: %s %s %s\n", argv[8], argv[5], argv[4]);
234 if (got_one) return 0;
241 status = validate_idno(message, db_mit_id, firstname, lastname);
242 if (status) return 0; /* Nope; decryption failed */
244 status_in_db = atoi(argv[7]);
245 reg_status = status_in_db;
247 if (status_in_db != 0) {
248 (void) strcpy(retval, argv[0]);
250 user_id = atoi(argv[1]);
255 encrypt_mitid(buf, idnumber, first, last)
256 char *buf, *idnumber, *first, *last;
259 extern char *crypt();
261 #define _tolower(c) ((c)|0x60)
263 salt[0] = _tolower(last[0]);
264 salt[1] = _tolower(first[0]);
267 (void) strcpy(buf, crypt(&idnumber[2], salt));
270 int verify_user(message)
277 printf("verify_user\n");
278 argv[0] = "get_user_by_first_and_last";
279 argv[1] = message->first;
280 argv[2] = message->last;
283 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
285 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
286 if (!got_one && !status)
287 status = UREG_USER_NOT_FOUND;
289 if (status != 0) goto punt;
291 if (reg_status == 1) status = UREG_ALREADY_REGISTERED;
292 if (reg_status == 2) status = UREG_NO_PASSWD_YET;
298 reserve_user(message)
308 printf("reserve_user\n");
310 argv[0] = "gufl"; /* get_user_by_first_and_last */
311 argv[1] = message->first;
312 argv[2] = message->last;
315 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
319 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
320 if (!got_one && !status)
321 status = UREG_USER_NOT_FOUND;
323 if (status != 0) goto punt;
324 if (reg_status != 0) {
325 status = UREG_ALREADY_REGISTERED;
329 * He's made it past this phase already.
331 if (status_in_db == 2) {
335 /* Sanity check requested login name. */
336 printf("reg_misc_len = %d\n", reg_misc_len);
338 for (i = 0; i < reg_misc_len && reg_misc[i]; i++) {
339 if (!islower(reg_misc[i])) {
340 status = UREG_INVALID_UNAME;
344 if (i < 3 || i > 8) {
345 status = UREG_INVALID_UNAME;
350 /* Send request to kerberos admin_server for login name */
352 printf("get_svc_in_tkt\n");
353 status = get_svc_in_tkt("register", "kerberos", "ATHENA.MIT.EDU",
354 "changepw", "kerberos",
357 status += krb_err_base;
362 printf("admin_call\n");
363 /* send set password request to kerberos admin_server */
364 (void) sprintf(uid_buf, "%013d", user_id); /* 13 chars of placebo */
365 /* for backwards-compat. */
367 status = admin_call(ADMIN_ADD_NEW_KEY_ATTR, login, "",
371 if (status == ADMIN_SERVER_ERROR) {
372 printf("Server error: %s\n", admin_errmsg);
374 if (strcmp(admin_errmsg,
375 "Principal already in kerberos database.") ==0)
376 status = UREG_LOGIN_USED;
386 status = set_login(login, mit_id);
388 com_err("set_login", status, 0);
391 /* choose post office */
394 status = choose_pobox(login);
396 com_err("choose_pobox", status, 0);
399 /* set quota entry, create filsys */
402 status = alloc_filsys(login, SMS_FS_STUDENT, 0, 0);
404 com_err("alloc_filsys", status, 0);
410 status = create_group(login);
412 com_err("create_group", status, 0);
415 /* set filsys and status in SMS database */
418 status = set_status_filsys(reg_misc, mit_id);
420 com_err("set_filsys", status, 0);
426 printf("reserve_user returning %s\n", error_message(status));
430 set_password(message)
436 printf("set_password\n");
438 /* validate that user is who he claims to be */
440 argv[0] = "get_user_by_first_and_last";
441 argv[1] = message->first;
442 argv[2] = message->last;
445 status = sms_query_internal(3, argv, vfy_callbk, (char *)message);
447 if (status == SMS_NO_MATCH) status = UREG_USER_NOT_FOUND;
448 if (!got_one && !status)
449 status = UREG_USER_NOT_FOUND;
451 if (status != 0) goto punt;
453 /* validate that state is equal to '2' (login, but no password) */
455 if (reg_status != 2) {
456 status = UREG_NO_LOGIN_YET;
460 printf("password for %s would be set to %s\n", retval ,reg_misc);
463 status = get_svc_in_tkt("register", "kerberos", "ATHENA.MIT.EDU",
464 "changepw", "kerberos",
467 status += krb_err_base;
470 /* send set password request to kerberos admin_server */
471 status = admin_call(ADMIN_SET_KDC_PASSWORD, retval, "",
472 reg_misc, "BBBBBBBBBBBBB");
474 if (status) goto punt;
477 status = set_final_status(retval, mit_id);
479 /* reflect reply to client */
485 parse_pkt(buf, len, seqnop, messagep)
489 struct msg *messagep;
491 if (len < 4) return UREG_BROKEN_PACKET;
492 bcopy(buf, (char *)&messagep->version, sizeof(long));
493 messagep->version = ntohl(messagep->version);
494 if (messagep->version != 1) return UREG_WRONG_VERSION;
499 if (len < 4) return UREG_BROKEN_PACKET;
500 bcopy(buf, (char *)seqnop, sizeof(long));
505 if (len < 4) return UREG_BROKEN_PACKET;
506 bcopy(buf, (char *)(&messagep->request), sizeof(long));
507 messagep->request = ntohl(messagep->request);
511 messagep->first = buf;
513 for (; *buf && len > 0; --len, ++buf) continue;
514 if (len <= 0) return UREG_BROKEN_PACKET;
518 messagep->last = buf;
520 for (; *buf && len > 0; --len, ++buf) continue;
521 if (len <= 0) return UREG_BROKEN_PACKET;
525 if (len <= 0) return UREG_BROKEN_PACKET;
527 messagep->sealed = buf;
528 messagep->sealed_len = len;
533 format_pkt(buf, lenp, seqno, status, message)
540 u_long vers = htonl((u_long)1);
541 status = htonl((u_long)status);
543 bcopy((char *)&vers, buf, sizeof(long));
544 bcopy((char *)&seqno, buf+sizeof(long), sizeof(long));
545 bcopy((char *)&status, buf+ 2*sizeof(long), sizeof(long));
546 *lenp = sizeof(long) * 3;
547 (void) strcpy(buf+3*sizeof(long), message);
548 (*lenp) += strlen(message);
551 store_user(argc, argv, argp)
556 char **retv = (char **) argp;
559 for (i = 0; i < argc; i++) {
564 retv[i] = strdup(argv[i]);
571 * Set login name of user with "idnumber" to be "username"
574 set_login(username, idnumber)
582 argv[0] = "get_user_by_mitid";
585 for (i=0; i<13; i++) {
589 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
590 if (status) return status;
594 if (retv[4]) free(retv[4]);
595 retv[4] = "null"; /* No such filesystem */
597 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
599 status = sms_query("update_user", 12, retv, abort, 0);
603 for (i=1; i<12; i++) {
604 if (retv[i]) free(retv[i]);
612 * Set the status and filsys of user with username "uname" and filesys filsys.
615 set_status_filsys(username, idnumber)
623 argv[0] = "get_user_by_mitid";
626 for (i=0; i<13; i++) {
630 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
631 if (status) return status;
641 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
643 status = sms_query("update_user", 12, retv, abort, 0);
646 for (i=1; i<12; i++) {
647 if (retv[i]) free(retv[i]);
653 * Set the status and filsys of user with username "uname" and filesys filsys.
656 set_final_status(username, idnumber)
664 argv[0] = "get_user_by_mitid";
667 for (i=0; i<13; i++) {
671 status = sms_query_internal(2, argv, store_user, (char *)(retv+1));
672 if (status) return status;
679 printf("Update_user(%s, %s)\n", retv[0], retv[1]);
681 status = sms_query("update_user", 12, retv, abort, 0);
683 for (i=1; i<12; i++) {
684 if (retv[i]) free(retv[i]);
694 static char *cr[] = {
701 return sms_query_internal(2, cr, abort, 0);
707 * c-continued-statement-offset: 4
709 * c-argdecl-indent: 4
713 char *get_krbhst(a1, a2)
717 strcpy(a1, "ICARUS.MIT.EDU");
724 strcpy(a1, "ATHENA.MIT.EDU");