]> andersk Git - moira.git/blobdiff - reg_svr/reg_svr.c
dereference NULL bug
[moira.git] / reg_svr / reg_svr.c
index f080a347638e4dd3d802b1e79697851c75302a67..72e410c3c28fc1ad56f74d0caabb93ffbd0bc42a 100644 (file)
@@ -3,13 +3,15 @@
  *      $Author$
  *      $Header$
  *
- *      Copyright (C) 1987 by the Massachusetts Institute of Technology
+ *      Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
+ *     For copying and distribution information, please see the file
+ *     <mit-copyright.h>.
  *
- *      Server for user registration with SMS and Kerberos.
+ *      Server for user registration with Moira and Kerberos.
  *
  *      This program is a client of the Kerberos admin_server and a
  *      server for the userreg program.  It is not a client of the
- *      SMS server as it is linked with libsmsglue which bypasses
+ *      Moira server as it is linked with libmoiraglue which bypasses
  *      the network protocol.
  */
 
 static char *rcsid_reg_svr_c = "$Header$";
 #endif lint
 
+#include <mit-copyright.h>
 #include <stdio.h>
 #include <strings.h>
 #include <ctype.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/file.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
 #include <krb.h>
 #include <des.h>
+#include <kadm.h>
+#include <kadm_err.h>
+#include <krb_err.h>
 #include <errno.h>
-#include <ctype.h>
-#include "ureg_err.h"
-#include "ureg_proto.h"
-#include "sms.h"
-#include "sms_app.h"
-#include "infodefs.h"
-#include "admin_server.h"
-#include "admin_err.h"
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-#ifndef FALSE
-#define FALSE 0
-#endif
+#include "moira.h"
+#include "moira_site.h"
+#include "reg_svr.h"
 
-#define FAIL_INST "reg_svr"    /* Instance for failure zephyrgrams */
-
-#define CUR_UREG_VERSION 1     /* Version for the register protocol */
-#define SUCCESS 0              /* General purpose success code */
-#define FAILURE 1              /* To use when any non-zero number will work */
-#define min(a,b) ((a)>(b)?(b):(a))
-#define MIN_UNAME 3            /* Username must be between three and */
-#define MAX_UNAME 8            /*    eight characters long. */
-#define CRYPT_LEN 14           /* crypt() returns a 13 char string */
-#define LOGIN_LEN MAX_UNAME + 1        /* Leave room for a null */
-#define UID_LEN 7              /* Allow room for a 16 bit number */
-
-#define DEBUG
-/* This MUST be correct!  There isn't a good way of getting this without
-   hardcoding that would not make testing impractical. */
-
-extern char *strdup();
-extern char *malloc();
-extern int krb_err_base;
 extern char admin_errmsg[];
 
-extern char *whoami;           /* Name of program - used by libraries */
-extern int errno;              /* Unix error number */
-
-/* This structure holds information from the SMS database that will be
-   worth holding on to.  An instance of it appears in the formatted 
-   packet structure. */
-struct db_data
-{
-    char mit_id[CRYPT_LEN];    /* Encrypted MIT ID */
-    int reg_status;            /* Registration status */
-    char uid[UID_LEN];         /* Reserved uid */
-    char login[LOGIN_LEN];     /* Login (username) */
-};
-
-/* This structure stores information sent over in the packet in a 
-   more convenient format and also stores some information obtained 
-   from the database that will be needed for each transaction.  It
-   initialized from format_pkt() and find_user(). */
-struct msg
-{    
-    U_32BIT version;           /* User registration protocol version */
-    U_32BIT request;           /* Request */
-    char *first;               /* First name */
-    char *last;                        /* Last name */
-    char *encrypted;           /* Encrypted information in packet */
-    int encrypted_len;         /* Length of encrypted information in packet */
-    char *leftover;            /* Leftover information sent in the packet */
-    int leftover_len;          /* Length of leftover information */
-    struct db_data db;         /* Information from the SMS database */
-};
-
-static char krbhst[BUFSIZ];    /* kerberos server name */
-static char krbrealm[REALM_SZ];        /* kerberos realm name */
-
+void reg_com_err_hook();
 
 main(argc,argv)
   int argc;
   char *argv[];
 {
-    struct servent *sp;                /* Service info from /etc/services */
-    int s;                     /* Socket descriptor */
-    struct sockaddr_in sin;    /* Internet style socket address */
-    int addrlen;               /* Size of socket address (sin) */
-    char packet[BUFSIZ];       /* Buffer for packet transmission */
-    int pktlen;                        /* Length of packet */
-    U_32BIT seqno;             /* Sequence number for packet transmission */
     struct msg message;                /* Storage for parsed packet */
     int status = SUCCESS;      /* Error status */
     char retval[BUFSIZ];       /* Buffer to hold return message for client */
     
-    void failure_alert();      /* Log an unexplainable failure */
-    int parse_pkt();           /* Parse a packet from the client */
-    int format_pkt();          /* Prepare a packet to send to client*/
-    int verify_user();         /* Make sure user is allowed to register */
-    int reserve_user();                /* Reserve a login for this user */
-    int set_password();                /* Set this user's password */
+    void req_initialize();     /* Initialize request layer */
+    void get_request();                /* Get a request */
+    void report();             /* Respond to a request */
 
+    /* Initialize */
     whoami = argv[0];
-
+    
     /* Error messages sent one line at a time */
     setlinebuf(stderr);
+    setlinebuf(stdout);
+    set_com_err_hook(reg_com_err_hook);
     
-    /* Initialize user registration error table for com_err */
+    /* Initialize com_err error tables */
     init_ureg_err_tbl();
+    init_krb_err_tbl();
+    init_kadm_err_tbl();
+    initialize_gdss_error_table();
     
-    /* Get service information from /etc/services */
-    if ((sp = getservbyname("sms_ureg", "udp")) == NULL) 
-    {
-       com_err(whoami, errno, " unknown service sms_ureg/udp");
-       exit(1);
-    }
+    /* Use com_err or output to stderr for all log messages. */    
+#ifdef DEBUG
+    com_err(whoami, 0, "*** Debugging messages enabled. ***");
+#endif DEBUG
     
-    /* Get an internet style datagram socket */
-    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) 
+    /* Set the name of our kerberos ticket file */
+    krb_set_tkt_string("/tmp/tkt_ureg");
+
+    /* Connect to the Moira server */
+    if ((status = mr_connect(MOIRA_SERVER)) != MR_SUCCESS) 
     {
-       com_err(whoami,errno," socket");
+       com_err(whoami, status, " on mr_connect");
        exit(1);
     }
-    bzero((char *)&sin,(int)sizeof(sin));
     
-    sin.sin_family = AF_INET;
-    sin.sin_port = sp->s_port;
-    sin.sin_addr.s_addr = INADDR_ANY;
-    
-    /* Bind a name to the socket */
-    if (bind(s, &sin, sizeof(sin)) < 0) 
+    /* Authorize, telling the server who you are */
+    if ((status = mr_auth(whoami)) != MR_SUCCESS) 
     {
-       com_err(whoami,errno," bind");
+       com_err(whoami, status, " on mr_auth");
        exit(1);
     }
     
-    /* Connect to the SMS server */
-    if ((status = sms_connect()) != SMS_SUCCESS) 
-    {
-       com_err(whoami, status, " on connect");
+    journal = fopen(REGJOURNAL, "a");
+    if (journal == NULL) {
+       com_err(whoami, errno, " while opening journal file");
        exit(1);
     }
     
-    /* Authorize, telling the server who you are */
-    if ((status = sms_auth(whoami)) != SMS_SUCCESS) 
-    {
-       com_err(whoami, status, " on auth");
-       exit(1);
-    }
-
-    if (status = get_krbrlm(krbrealm, 1)) {
-       status += krb_err_base;
-       com_err(whoami, status, " fetching kerberos realm");
-       exit(1);
-    }
-
-    if (status = get_krbhst(krbhst, krbrealm, 1)) {
-       status += krb_err_base;
-       com_err(whoami, status, " fetching kerberos hostname");
-       exit(1);
-    } else {
-       char *s;
-       for (s = krbhst; *s && *s != '.'; s++)
-           if (isupper(*s))
-               *s = tolower(*s);
-       *s = 0;
-    }
-
-    /* Use com_err or output to stderr for all log messages. */    
-#ifdef DEBUG
-    fprintf(stderr,"*** Debugging messages enabled. ***\n");
-#endif DEBUG
-
+    /* Allow request layer to initialize */
+    req_initialize();
+    
     /* Sit around waiting for requests from the client. */
     for (;;) 
     {
-       com_err(whoami, 0, "*** Ready for next request ***");
-       addrlen = sizeof(sin);
-       bzero(retval, BUFSIZ);
-       /* Receive a packet */
-       if ((pktlen = recvfrom(s,packet,sizeof(packet),0,&sin,&addrlen)) < 0) 
-       {
-           com_err(whoami,errno," recvfrom");
-           /* Don't worry if error is interrupted system call. */
-           if (errno == EINTR) continue;
-           exit(1);
-       }
+       get_request(&message);
        
-       /* Parse a request packet */
-       if ((status = parse_pkt(packet, pktlen, &seqno, &message)) != SUCCESS) 
-       {
-           /* If error, format packet to send back to the client */
-           pktlen = sizeof(packet);
-           (void) format_pkt(packet, &pktlen, seqno, status, (char *)NULL);
-           /* Report the error the the client */
-           (void)  sendto(s, packet, pktlen, 0, &sin, addrlen);
-           continue;
-       }
-       
-       /* do action */
        switch((int)message.request) 
        {
          case UREG_VERIFY_USER:
@@ -230,106 +111,57 @@ main(argc,argv)
            status = reserve_user(&message,retval);
            break;
          case UREG_SET_PASSWORD:
+         case UREG_GET_KRB:
            status = set_password(&message,retval);
            break;
-           
+         case UREG_SET_IDENT:
+           status = set_identity(&message,retval);
+           break;
+         case UREG_GET_SECURE:
+           status = get_secure(&message,retval);
+           break;
+         case UREG_SET_SECURE:
+           status = set_secure(&message,retval);
+           break;
          default:
            status = UREG_UNKNOWN_REQUEST;
-           failure_alert("Unknown request %d from userreg.",
-                         message.request);
+           critical_alert(FAIL_INST,"Unknown request %d from userreg.",
+                          message.request);
            break;
        }
        
        /* Report what happened to client */
-       pktlen = sizeof(packet);
-       if (format_pkt(packet, &pktlen, seqno, status, retval))
-       {
-           com_err(whoami,0,"Client error message was truncated.");
-       }
-       (void) sendto(s, packet, pktlen, 0, &sin, addrlen);
+       report(status, retval);
     }
 }
 
-/* This is necessary so that this server can know where to put its
-   tickets. */
-char *tkt_string()
-{
-    return("/tmp/tkt_ureg");
-}
-
-void failure_alert(msg, args)
-  char *msg;                   /* Text of message, printf format */
-                               /* args = arguments, printf style */
-  /* This routine takes care of logging critical error from userreg
-     in the appropriate way. */
-{
-    critical_alert(FAIL_INST, msg, args);
-    com_err(whoami, 0, msg, args);
-}
-
-int format_pkt(packet, pktlenp, seqno, cl_status, message)
-  char *packet;                        /* Packet buffer */
-  int *pktlenp;                        /* Pointer to packet size */
-  U_32BIT seqno;               /* Sequence number */
-  int cl_status;               /* Error status to return to client */
-  char *message;               /* Error message to return to client */
-  /* This routine prepares a packet to send back to the client.  A 
-     non-zero return status means that the client error message was 
-     truncated. */
-{
-    int len;                   /* Amount of message to send */
-    int status = SUCCESS;      /* Return status */
-
-    /* Convert byte order to network byte order */
-    U_32BIT vers = htonl((U_32BIT)CUR_UREG_VERSION);
-    cl_status = htonl((U_32BIT)cl_status);
-    /* Put current user registration protocol version into the packet */
-    bcopy((char *)&vers, packet, sizeof(U_32BIT));
-    /* Put sequence number into the packet */
-    bcopy((char *)&seqno, packet+sizeof(U_32BIT), sizeof(U_32BIT));
-    /* Put error status into the packet */
-    bcopy((char *)&cl_status, packet+ 2*sizeof(U_32BIT), sizeof(U_32BIT));
-    
-    /* Find out how much of the message to copy; truncate if too short. */
-    /* How much room is there left? */
-    len = *pktlenp - sizeof(U_32BIT)*3;
-    if (len < strlen(message) + 1) /* Room for null terminator */
-    {
-       status = FAILURE;       /* Message was truncated */
-       /* Truncate the message */
-       message[len-1] = NULL;
-    }
-
-    /* Copy the message into the packet */
-    (void) strcpy(packet+3*sizeof(U_32BIT), message);
-    *pktlenp = 3*sizeof(U_32BIT) + strlen(message);
-    
-    return status;
-}
-
 int parse_encrypted(message,data)
   struct msg *message;         /* Formatted packet */
-  struct db_data *data;                /* Data from the SMS database */
+  struct db_data *data;                /* Data from the Moira database */
 /* This routine makes sure that the ID from the database matches
    the ID sent accross in the packet.  The information in the packet
    was created in the following way:
 
+   The database used to contain encrypted IDs.  Now we don't encrypt
+   them in the database, although there are still some encrypted IDs
+   there.
+
    The plain text ID number was encrypted via EncryptID() resulting
-   in the form that would appear in the SMS database.  This is
+   in the form that would appear in the Moira database.  This is
    concatinated to the plain text ID so that the ID string contains plain
    text ID followed by a null followed by the encrypted ID.  Other
    information such as the username or password is appended.  The whole
    thing is then DES encrypted using the encrypted ID as the source of
    the key.
 
-   This routine tries each encrypted ID in the database that belongs
+   This routine tries each ID in the database that belongs
    to someone with this user's first and last name and tries to 
    decrypt the packet with this information.  If it succeeds, it returns 
    zero and initializes all the fields of the formatted packet structure
    that depend on the encrypted information. */
 {
-    C_Block key;               /* The key for DES en/decryption */
-    Key_schedule sched;                /* En/decryption schedule */
+    des_cblock key;            /* The key for DES en/decryption */
+    des_key_schedule sched;    /* En/decryption schedule */
     static char decrypt[BUFSIZ];   /* Buffer to hold decrypted information */
     long decrypt_len;          /* Length of decypted ID information */
     char recrypt[14];          /* Buffer to hold re-encrypted information */
@@ -340,7 +172,7 @@ int parse_encrypted(message,data)
     int status = SUCCESS;      /* Error status */
     
 #ifdef DEBUG
-    com_err(whoami,0,"Entering parse_encrypted");
+    com_err(whoami, 0, "Entering parse_encrypted");
 #endif
 
     /* Make the decrypted information length the same as the encrypted
@@ -348,14 +180,22 @@ int parse_encrypted(message,data)
        because of the DES encryption routines. */
     decrypt_len = (long)message->encrypted_len;
     
-    /* Get key from the one-way encrypted ID in the SMS database */
-    string_to_key(data->mit_id, key);
+    /* Get key from the possibly one-way encrypted ID in the Moira database */
+    if (data->mit_id[0] >= '0' && data->mit_id[0] <= '9') {
+       char buf[32];
+
+       EncryptID(buf, data->mit_id, message->first, message->last);
+       des_string_to_key(buf, key);
+    } else
+      des_string_to_key(data->mit_id, key);
+
     /* Get schedule from key */
-    key_sched(key, sched);
+    des_key_sched(key, sched);
     /* Decrypt information from packet using this key.  Since decrypt_len
        is an integral multiple of eight bytes, it will probably be null-
        padded. */
-    pcbc_encrypt(message->encrypted,decrypt, decrypt_len, sched, key, DECRYPT);
+    des_pcbc_encrypt(message->encrypted, decrypt, decrypt_len,
+                    sched, key, DES_DECRYPT);
     
     /* Extract the plain text and encrypted ID fields from the decrypted
        packet information. */
@@ -366,6 +206,15 @@ int parse_encrypted(message,data)
        correct key, there is no guarantee that a null will occur
        anywhere in the string. */
     (void) strncpy(idnumber,decrypt,(int)decrypt_len);
+    /* Check that the idnumber of a mismatched decryption doesn't overflow
+     * the buffer.
+     */
+    if (strlen(idnumber) != 9) {
+#ifdef DEBUG
+       com_err(whoami, 0, "idnumber wrong size, probable user mismatch\n");
+#endif
+       return(FAILURE);
+    }
     /* Point temp to the end of the plain text ID number. */
     temp = decrypt + strlen(idnumber) + 1;
     /* Find out how much more packet there is. */
@@ -379,14 +228,10 @@ int parse_encrypted(message,data)
     /* Find out how much more room there is. */
     len = message->encrypted_len - (temp - decrypt);
     
-    /* Now compare encrypted ID's don't match. */
-    if (strcmp(hashid, data->mit_id)) status = FAILURE;
-    if (status == SUCCESS)
-    {
-       EncryptID(recrypt, idnumber, message->first, message->last);
-       /* Now compare encrypted plain text to ID from database. */
-       if (strcmp(recrypt, data->mit_id)) status = FAILURE;
-    }
+    /* Now compare encrypted ID and clear text ID for a match. */
+    if (strcmp(hashid, data->mit_id) &&
+       strcmp(idnumber, data->mit_id))
+      status = FAILURE;
     
     if (status == SUCCESS)
     {
@@ -395,7 +240,7 @@ int parse_encrypted(message,data)
        message->leftover = temp;
        message->leftover_len = len;
        /* Since we know we have the right user, fill in the information 
-          from the SMS database. */
+          from the Moira database. */
        message->db.reg_status = data->reg_status;
        (void) strncpy(message->db.uid,data->uid, sizeof(message->db.uid));
        (void) strncpy(message->db.mit_id,data->mit_id, 
@@ -405,37 +250,38 @@ int parse_encrypted(message,data)
     
 #ifdef DEBUG
     if (status)
-       com_err(whoami,status," parse_encrypted failed.");
+       com_err(whoami, status, " in parse_encrypted");
     else
-       com_err(whoami,status,"parse_encrypted succeeded.");
+       com_err(whoami, status, "parse_encrypted succeeded");
 #endif
 
     return status;
 }
 
 int db_callproc(argc,argv,queue)
-  int argc;                    /* Number of arguments returned by SMS */
-  char *argv[];                        /* Arguments returned by SMS */
+  int argc;                    /* Number of arguments returned by Moira */
+  char *argv[];                        /* Arguments returned by Moira */
   struct save_queue *queue;    /* Queue to save information in */
-/* This function is called by sms_query after each tuple found.  It is
+/* This function is called by mr_query after each tuple found.  It is
    used by find_user to cache information about each user found.  */
 {
     struct db_data *data;      /* Structure to store the information in */
     int status = SUCCESS;      /* Error status */
     
 #ifdef DEBUG
-    com_err(whoami,0,"Entering db_callproc.");
+    com_err(whoami, 0, "Entering db_callproc.");
 #endif
 
     if (argc != U_END)
     {
-       failure_alert
-           ("Wrong number of arguments returned from get_user_by_name.");
-       status = SMS_ABORT;
+       critical_alert
+           (FAIL_INST,
+            "Wrong number of arguments returned from get_user_account_by_name.");
+       status = MR_ABORT;
     }
     else
     {
-       /* extract the needed information from the results of the SMS query */
+       /* extract the needed information from the results of the Moira query */
        data = (struct db_data *)malloc(sizeof(struct db_data));
        data->reg_status = atoi(argv[U_STATE]);
        (void) strncpy(data->login,argv[U_NAME],sizeof(data->login));
@@ -457,7 +303,7 @@ int db_callproc(argc,argv,queue)
 int find_user(message)
   struct msg *message;         /* Formatted packet structure */
 /* This routine verifies that a user is allowed to register by finding
-   him/her in the SMS database.  It returns the status of the SMS
+   him/her in the Moira database.  It returns the status of the Moira
    query that it calls. */
 {
 #define GUBN_ARGS 2            /* Arguements needed by get_user_by_name */
@@ -466,7 +312,7 @@ int find_user(message)
     char *q_argv[GUBN_ARGS];   /* Arguments to query */
     int status = SUCCESS;      /* Query return status */
 
-    struct save_queue *queue;  /* Queue to hold SMS data */
+    struct save_queue *queue;  /* Queue to hold Moira data */
     struct db_data *data;      /* Structure for data for one tuple */
     short verified = FALSE;    /* Have we verified the user? */
 
@@ -476,8 +322,8 @@ int find_user(message)
     
     if (status == SUCCESS)
     {
-       /* Get ready to make an SMS query */
-       q_name = "get_user_by_name";
+       /* Get ready to make an Moira query */
+       q_name = "get_user_account_by_name";
        q_argc = GUBN_ARGS;     /* #defined in this routine */
        q_argv[0] = message->first;
        q_argv[1] = message->last;
@@ -486,13 +332,13 @@ int find_user(message)
        queue = sq_create();
        
        /* Do it */
-       status = sms_query(q_name,q_argc,q_argv,db_callproc,(char *)queue);
+       status = mr_query(q_name,q_argc,q_argv,db_callproc,(char *)queue);
        
 #ifdef DEBUG
        fprintf(stderr," %d returned by get_user_by_name\n",status);
 #endif
        
-       if (status == SMS_SUCCESS) 
+       if (status == MR_SUCCESS) 
        {
            /* Traverse the list, freeing data as we go.  If sq_get_data()
               returns zero if there is no more data on the queue. */
@@ -521,169 +367,6 @@ int find_user(message)
     return status;
 }
 
-/* The ureg_validate_char variable and routine were taken verbatim 
-   out of server/qsupport.qc where they are called
-   validate_chars.  At some point, it may be desirable
-   to put this functionality in one place. */
-
-/* ureg_validate_char: verify that there are no illegal characters in
- * the string.  Legal characters are printing chars other than 
- * ", *, ?, \, [ and ].
- */
-static int illegalchars[] = {
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^@ - ^O */
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^P - ^_ */
-    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* SPACE - / */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 0 - ? */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* @ - O */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, /* P - _ */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ` - o */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* p - ^? */
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-};
-
-ureg_validate_char(s)
-register char *s;
-{
-    while (*s)
-      if (illegalchars[*s++])
-       return(FAILURE);
-    return(SUCCESS);
-}
-
-parse_pkt(packet, pktlen, seqnop, message)
-  char *packet;
-  int pktlen;
-  U_32BIT *seqnop;
-  struct msg *message;
-  /* This routine checks a packet and puts the information in it in
-     a structure if it is valid. */
-{
-    int status = SUCCESS;      /* Error status */
-
-    com_err(whoami,0,"Packet received");
-
-    if (pktlen < sizeof(U_32BIT)) status = UREG_BROKEN_PACKET;
-    if (status == SUCCESS)
-    {
-       /* Extract the user registration protocol version from the packet */
-       bcopy(packet, (char *)&message->version, sizeof(long));
-       /* Convert byte order from network to host */
-       message->version = ntohl(message->version);
-       /* Verify version */
-       if (message->version != CUR_UREG_VERSION) 
-           status = UREG_WRONG_VERSION;
-    }
-
-    if (status == SUCCESS)
-    {
-       packet += sizeof(U_32BIT);
-       pktlen -= sizeof(U_32BIT);
-       
-       if (pktlen < sizeof(U_32BIT))
-           status = UREG_BROKEN_PACKET;
-    }
-
-    if (status == SUCCESS)
-    {
-       /* Extract the sequence number from the packet */
-       bcopy(packet, (char *)seqnop, sizeof(long));
-       
-       packet += sizeof(U_32BIT);
-       pktlen -= sizeof(U_32BIT);
-       
-       if (pktlen < sizeof(U_32BIT))
-           status = UREG_BROKEN_PACKET;
-    }
-
-    if (status == SUCCESS)
-    {
-       /* Extract the request from the packet */
-       bcopy(packet, (char *)(&message->request), sizeof(U_32BIT));
-       message->request = ntohl(message->request);
-       packet += sizeof(U_32BIT);
-       pktlen -= sizeof(U_32BIT);
-       
-       /* Make sure that the packet contains only valid characters up 
-          to the next null */
-       if (ureg_validate_char(packet) != SUCCESS)
-       {
-           com_err(whoami,0,"Packet contains invalid characters.");
-           status = UREG_USER_NOT_FOUND;
-       }
-       else
-       {
-           /* Extract first name from the packet */
-           message->first = packet;
-           
-           /* Scan forward until null appears in the packet or there
-              is no more packet! */
-           for (; *packet && pktlen > 0; --pktlen, ++packet) continue;
-           if (pktlen <= 0) 
-               status = UREG_BROKEN_PACKET;
-       }
-    }
-    
-    if (status == SUCCESS)
-    {
-       /* Skip over the null */
-       packet++, pktlen--;
-       
-       if (ureg_validate_char(packet) != SUCCESS)
-       {
-           com_err(whoami,0,"Packet contains invalid characters.");
-           status = UREG_USER_NOT_FOUND;
-       }
-       else
-       {
-           /* Extract last name from the packet */
-           message->last = packet;
-           
-           for (; *packet && pktlen > 0; --pktlen, ++packet) continue;
-           if (pktlen <= 0)
-               status = UREG_BROKEN_PACKET;
-       }
-    }
-
-    if (status == SUCCESS)
-    {
-       packet++, pktlen--;
-       
-       if (pktlen <= 0)
-           status = UREG_BROKEN_PACKET;
-    }
-
-    /* Extract encrypted information from packet */
-    message->encrypted = packet;
-    message->encrypted_len = pktlen;
-    
-    if (status == SUCCESS)
-    {
-#ifdef DEBUG
-       com_err(whoami,status,"%s\n%s%d\n%s%d\n%s%s\n%s%s",
-               "Packet parsed successfully.  Packet contains:",
-               "   Protocol version: ",message->version,
-               "   Request: ",message->request,
-               "   First name: ",message->first,
-               "   Last name: ",message->last);
-#else /* DEBUG */
-       com_err(whoami,status,"Request %d for %s %s",message->request,
-               message->first,message->last);
-#endif DEBUG    
-    }
-    else
-       com_err(whoami,status," - parse packet failed.");
-
-    return status;
-}
-
 int verify_user(message,retval)
   struct msg *message;
   char *retval;
@@ -695,7 +378,7 @@ int verify_user(message,retval)
     int status = SUCCESS;      /* Return status */
 
     /* Log that we are about to veryify user */
-    com_err(whoami,0,"verify_user %s %s",message->first,message->last);
+    com_err(whoami, 0, "verifying user %s %s",message->first,message->last);
 
     /* Figure out what user (if any) can be found based on the
        encrypted information in the packet.  (See the comment on 
@@ -703,10 +386,10 @@ int verify_user(message,retval)
 
     status = find_user(message);
 
-    /* If SMS coudn't find the user */
-    if (status == SMS_NO_MATCH) 
+    /* If Moira coudn't find the user */
+    if (status == MR_NO_MATCH) 
        status = UREG_USER_NOT_FOUND;
-    else if (status == SMS_SUCCESS)
+    else if (status == MR_SUCCESS)
     {
        /* If the information sent over in the packet did not point to a
           valid user, the mit_id field in the formatted packet structure
@@ -734,11 +417,19 @@ int verify_user(message,retval)
              case US_NOT_ALLOWED:
                status = UREG_NOT_ALLOWED;
                break;
-
+             case US_ENROLLED:
+               status = UREG_ENROLLED;
+               break;
+             case US_ENROLL_NOT_ALLOWED:
+               status = UREG_ENROLL_NOT_ALLOWED;
+               break;
+           case US_HALF_ENROLLED:
+               status = UREG_HALF_ENROLLED;
+               break;
              default:
                status = UREG_MISC_ERROR;
-               failure_alert("Bad user state for %s %s.",
-                             message->first,message->last);
+               critical_alert(FAIL_INST,"Bad user state %d for login %s.",
+                              message->db.reg_status, message->db.login);
                break;
            }
            /* Set retval to the login name so that the client can use
@@ -747,27 +438,64 @@ int verify_user(message,retval)
        }
     }
     
-    com_err(whoami,status," returned from verify_user");
+    if (status)
+       com_err(whoami, status, " returned from verify_user");
+    else
+       com_err(whoami, 0, "User verified");
 
     return status;
 }
        
-int ureg_get_tkt()
+int ureg_kadm_init()
 {
-    int status = SUCCESS;      /* Return status */
+    unsigned int status = SUCCESS;      /* Return status */
+    static char krbrealm[REALM_SZ];     /* kerberos realm name */
+    static char hostbuf[BUFSIZ], *host;         /* local hostname in principal fmt */
+    static int inited = 0;
+    char *p;
+
+#ifdef DEBUG
+    com_err(whoami, 0, "Entering ureg_kadm_init");
+#endif DEBUG
+
+    if (!inited) {
+       inited++;
+       bzero(krbrealm, sizeof(krbrealm));
+       if (status = krb_get_lrealm(krbrealm, 1)) {
+           status += krb_err_base;
+           com_err(whoami, status, " fetching kerberos realm");
+           exit(1);
+       }
+       if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
+         com_err(whoami, errno, "getting local hostname");
+       host = canonicalize_hostname(strsave(hostbuf));
+       for (p = host; *p && *p != '.'; p++)
+         if (isupper(*p))
+           *p = tolower(*p);
+       *p = 0;
+    }
 
     /* Get keys for interacting with Kerberos admin server. */
     /* principal, instance, realm, service, service instance, life, file */
-    if (status = get_svc_in_tkt("register", "sms", krbrealm, "changepw", 
-                               krbhst, 1, KEYFILE))
+    if (status = krb_get_svc_in_tkt(MOIRA_SNAME, host, krbrealm, PWSERV_NAME,
+                                   KADM_SINST, 1, KEYFILE))
        status += krb_err_base;
-
+    
+    if (status != SUCCESS)
+       com_err(whoami, status, " while get admin tickets");
 #ifdef DEBUG
-    if (status == SUCCESS)
-       com_err(whoami,status,"Succeeded in getting tickets.");
-    else
-       com_err(whoami,status,"Failed to get tickets.");
+    else {
+       com_err(whoami, status, "Succeeded in getting admin tickets");
+    }
 #endif
+
+    if (status == SUCCESS) {
+       if ((status = kadm_init_link(PWSERV_NAME, KADM_SINST, krbrealm)) !=
+           KADM_SUCCESS) {
+           com_err(whoami, status, " while initializing kadmin connection");
+       }
+    }
+
     return status;
 }
 
@@ -778,48 +506,89 @@ int null_callproc(argc,argv,message)
   /* This routine is a null callback that should be used for queries that
      do not return tuples.  If it ever gets called, something is wrong. */
 {
-    failure_alert("Something returned from an update query.");
+    critical_alert(FAIL_INST,"Something returned from an update query.");
     return FAILURE;
 }
 
-int do_admin_call(login, passwd, uid)
-  char *login;                 /* Requested kerberos principal */
-  char *passwd;                        /* Requested password */
 char *uid;                   /* Uid of user who owns this principal */
-  /* This routine gets tickets, makes the appropriate call to admin_call,
-     and destroys tickets. */
+/*
+ * This routine reserves a principal in kerberos by setting up a 
+ * principal with a random initial key.
+ */
+int reserve_krb(login)
+  char *login;
 {
-    int status;                        /* Error status */
-    char uid_buf[20];          /* Holds uid for kerberos */
+    int status = SUCCESS;
+    Kadm_vals new;
+    des_cblock key;
+    u_long *lkey = (u_long *)key;
 
-    com_err(whoami,0,"Entering do_admin_call");
+#ifdef DEBUG
+    com_err(whoami, 0, "Entering reserve_krb");
+#endif DEBUG
 
-    if ((status = ureg_get_tkt()) == SUCCESS)
-    {
-       /* Try to reserve kerberos principal.  To do this, send a 
-          password request and a null password.  It will only succeed
-          if there is no principal or the principal exists and has no 
-          password. */
-       /* 13 chars of placebo for backwards-compatability - the admin
-          server protocol reqires this. */
-       bzero(uid_buf,sizeof(uid_buf));
-       (void) sprintf(uid_buf, "%13s", uid);
+    if ((status = ureg_kadm_init()) == SUCCESS) {
+       bzero((char *)&new, sizeof(new));
+       SET_FIELD(KADM_DESKEY, new.fields);
+       SET_FIELD(KADM_NAME, new.fields);
        
-       if ((status = admin_call(ADMIN_ADD_NEW_KEY_ATTR, login, 
-                                "", passwd, uid_buf)) != KSUCCESS)
-       {
-           com_err(whoami,status," server error: %s",admin_errmsg);
-           
-           if (strcmp(admin_errmsg,
-                      "Principal already in kerberos database.") == 0)
-               status = UREG_KRB_TAKEN;
-           failure_alert("%s is known to Kerberos but not SMS.", login);
+       (void) des_random_key(key);
+       new.key_low = htonl(lkey[0]);
+       new.key_high = htonl(lkey[1]);
+       strcpy(new.name, login);
+       
+       com_err(whoami, 0, "Creating kerberos principal for %s", login);
+       status = kadm_add(&new);
+       if (status != KADM_SUCCESS) 
+           com_err(whoami, status, " while reserving principal");
+       
+       bzero((char *)&new, sizeof(new));
+    }
+
+    dest_tkt();
+
+    return(status);
+}
+
+/*
+ * This routine reserves a principal in kerberos by setting up a 
+ * principal with a random initial key.
+ */
+int setpass_krb(login, password)
+  char *login;
+  char *password;
+{
+    int status = SUCCESS;
+    Kadm_vals new;
+    des_cblock key;
+    u_long *lkey = (u_long *)key;
+
+    if ((status = ureg_kadm_init()) == SUCCESS) {
+       bzero((char *)&new, sizeof(new));
+       SET_FIELD(KADM_DESKEY, new.fields);
+       SET_FIELD(KADM_NAME, new.fields);
+       
+       (void) des_string_to_key(password, key);
+       new.key_low = htonl(lkey[0]);
+       new.key_high = htonl(lkey[1]);
+       strcpy(new.name, login);
+       
+       com_err(whoami, 0, "Setting password for %s", login);
+       /* First arguement is not used if user has modify privileges */
+       if ((status = kadm_mod(&new, &new)) != KADM_SUCCESS) {
+           if (status == KADM_NOENTRY) {
+               com_err(whoami, 0, 
+                       "kerberos principal doesn't exist; creating");
+               if ((status = kadm_add(&new)) != KADM_SUCCESS)
+                   com_err(whoami, status, 
+                           " while creating kerberos principal");
+           }
+           else
+               com_err(whoami, status, " while setting password");
        }
     }
     
     dest_tkt();
-    com_err(whoami,status," returned from do_adin_call");
-    return status;
+    return(status);
 }
 
 int reserve_user(message,retval)
@@ -827,15 +596,15 @@ int reserve_user(message,retval)
   char *retval;
 {
     int q_argc;                        /* Number of arguments to query */
-    char *q_argv[3];           /* Arguments to SMS query */
-    char *q_name;              /* Name of SMS query */
+    char *q_argv[3];           /* Arguments to Moira query */
+    char *q_name;              /* Name of Moira query */
     int status = SUCCESS;      /* General purpose error status */
     char fstype_buf[7];                /* Buffer to hold fs_type, a 16 bit number */
     char *login;               /* The login name the user wants */
     register int i;            /* A counter */
 
     /* Log that we are about to reserve a user. */
-    com_err(whoami, 0, "reserve_user %s %s", 
+    com_err(whoami, 0, "reserving user %s %s", 
            message->first, message->last);
     
     /* Check to make sure that we can verify this user. */
@@ -845,20 +614,21 @@ int reserve_user(message,retval)
        login = message->leftover;
 
        /* Check the login name for validity.  The login name is currently
-          is allowed to contain lowercase letters and numbers in any
-          position and underscore characters and periods in any position
-          but the first. */
+          is allowed to contain lowercase letters in any position and 
+          and numbers and underscore characters in any position but the 
+          first. */
        if ((strlen(login) < MIN_UNAME) || (strlen(login) > MAX_UNAME))
            status = UREG_INVALID_UNAME;
     }
     if (status == SUCCESS)
-       if ((login[0] == '.') || (login[1] == '_'))
+       if ((login[0] == '_') || isdigit(login[0]))
            status = UREG_INVALID_UNAME;
+    
     if (status == SUCCESS)
     {
        for (i = 0; i < strlen(login); i++)
-           if (!islower(login[i]) && !isdigit(login[i]) &&
-               (login[i] != '_') && (login[i] != '.'))
+           if (!islower(login[i]) && !isdigit(login[i]) && 
+               (login[i] != '_'))
            {
                status = UREG_INVALID_UNAME;
                break;
@@ -868,68 +638,93 @@ int reserve_user(message,retval)
     {
        /* Now that we have a valid user with a valid login... */
 
-       /* First, try to reserve the user in SMS. */
-       (void) sprintf(fstype_buf,"%d",SMS_FS_STUDENT);
+       /* First, try to reserve the user in Moira. */
+       (void) sprintf(fstype_buf,"%d",MR_FS_STUDENT);
        q_name = "register_user";
        q_argv[0] = message->db.uid;
        q_argv[1] = login;
        q_argv[2] = fstype_buf;
        q_argc = 3;
-       status = sms_query(q_name,q_argc,q_argv,null_callproc,(char *)0);
+       status = mr_query(q_name,q_argc,q_argv,null_callproc,(char *)0);
        switch (status)
        {
-         case SMS_SUCCESS:
+         case MR_SUCCESS:
            status = SUCCESS;
            break;
-         case SMS_IN_USE:
+         case MR_IN_USE:
            status = UREG_LOGIN_USED;
            break;
+         case MR_DEADLOCK:
+           status = UREG_MISC_ERROR;
+           break;
          default:
+           critical_alert(FAIL_INST,"%s returned from register_user.",
+                          error_message(status));
            status = UREG_MISC_ERROR;
-           failure_alert("%s returned from register_user.",
-                         error_message(status));
            break;
        }
     }
+
     if (status == SUCCESS)
     {
-       /* SMS login was successfully created; try to reserve kerberos
-          principal. */
-       /* If this routine fails, store the login in the retval so
-          that it can be used in the client-side error message. */
-       if ((status = do_admin_call(login, "", message->db.uid)) != SUCCESS)
+       /* 
+        * Moira login was successfully created; try to reserve kerberos
+        * principal. 
+        *
+        * If this routine fails, store the login in the retval so
+        * that it can be used in the client-side error message.
+        */
+       if ((status = reserve_krb(login)) != SUCCESS)
            (void) strcpy(retval, login);
     }
 
-    com_err(whoami, status, " returned from reserve_user");
+    if (status)
+       com_err(whoami, status, " returned from reserve_user");
+    else {
+       com_err(whoami, 0, "User reserved");
+    }
     
     return status;
 }
 
-int set_final_status(login)
-  char *login;
+int set_final_status(message)
+struct msg *message;
     /* This routine updates a user's registration status to fully 
        registered. */
 {
-    char *q_name;              /* Name of SMS query */
-    int q_argc;                        /* Number of arguments for SMS query */
+    char *login;
+    char *q_name;              /* Name of Moira query */
+    int q_argc;                        /* Number of arguments for Moira query */
     char *q_argv[2];           /* Arguments to get user by uid */
     char state[7];             /* Can hold a 16 bit integer */
     int status;                        /* Error status */
 
-    com_err(whoami, 0, "Setting final status for %s", login);
+    if (message->request == UREG_SET_PASSWORD)
+      (void) sprintf(state,"%d",US_REGISTERED);
+    else if (message->db.reg_status == US_NO_LOGIN_YET)
+      (void) sprintf(state,"%d",US_ENROLLED);
+    else
+      (void) sprintf(state,"%d",US_ENROLL_NOT_ALLOWED);
+
+    login = message->db.login;
+    com_err(whoami, 0, "Setting final status for %s to %s", login, state);
 
-    (void) sprintf(state,"%d",US_REGISTERED);
     q_name = "update_user_status";
     q_argc = 2;
     q_argv[0] = login;
     q_argv[1] = state;
-    if ((status = sms_query(q_name, q_argc, q_argv, null_callproc,
-                           (char *)0)) != SMS_SUCCESS)
-       failure_alert("%s returned from update_user_status.",
-                     error_message(status));
-    
-    com_err(whoami,status," returned from set_final_status");
+    if ((status = mr_query(q_name, q_argc, q_argv, null_callproc,
+                           (char *)0)) != MR_SUCCESS) {
+       if (status == MR_DEADLOCK)
+         status = UREG_MISC_ERROR;
+       else
+         critical_alert(FAIL_INST,"%s returned from update_user_status.",
+                        error_message(status));
+    }
+    if (status)
+       com_err(whoami, status, " returned from set_final_status");
+    else
+       com_err(whoami, 0, "Final status set");
     return status;
 }
 
@@ -942,7 +737,7 @@ int set_password(message,retval)
     int status = SUCCESS;      /* Return status */
     char *passwd;              /* User's password */
 
-    com_err(whoami, 0, " set_password %s %s",
+    com_err(whoami, 0, "setting password %s %s",
            message->first, message->last);
 
     status = verify_user(message,retval);
@@ -951,34 +746,321 @@ int set_password(message,retval)
        is that he exists and has no password. */
     if (status == SUCCESS)
        status = UREG_NO_LOGIN_YET;
-    if (status == UREG_NO_PASSWD_YET)
+    if (((int)message->request == UREG_SET_PASSWORD &&
+        status == UREG_NO_PASSWD_YET) ||
+       ((int)message->request == UREG_GET_KRB &&
+        status == UREG_HALF_ENROLLED))
     {
        /* User is in proper state for this transaction. */
        
        passwd = message->leftover;
        
        /* Set password. */
-       if ((status = do_admin_call(message->db.login, 
-                                   passwd, message->db.uid)) != SUCCESS)
+       if ((status = setpass_krb(message->db.login, passwd)) != SUCCESS)
            /* If failure, allow login name to be used in client 
               error message */
            (void) strcpy(retval,message->db.login);
        else
            /* Otherwise, mark user as finished. */
-           status = set_final_status(message->db.login);
+           status = set_final_status(message);
     }
-    com_err(whoami, status, " returned from set_passwd");
-    
+
+    if (status)
+       com_err(whoami, status, " returned from set_passwd");
+    else
+       com_err(whoami, 0, "Password set");
+
     return status;
 }
+
+
+int getuserinfo(argc, argv, qargv)
+int argc;
+char **argv;
+char **qargv;
+{
+    int status = SUCCESS;
+    int  i;
+
+    if (argc != U_END) {
+       critical_alert(FAIL_INST,
+                      "Wrong number of args returned from get_user_by_uid");
+       status = MR_ABORT;
+    } else {
+       qargv[U_NAME] = strsave(argv[U_NAME]);
+       for (i = 1; i < U_MODTIME; i++)
+         qargv[i+1] = strsave(argv[i]);
+       qargv[U_MODTIME+1] = NULL;
+    }
+    return(status);
+}
+
+
+int set_identity(message,retval)
+struct msg *message;
+char *retval;
+{
+    int q_argc;                        /* Number of arguments to query */
+    char *q_argv[U_END];       /* Arguments to Moira query */
+    char *q_name;              /* Name of Moira query */
+    int status = SUCCESS;      /* General purpose error status */
+    char fstype_buf[7];                /* Buffer to hold fs_type, a 16 bit number */
+    char *login;               /* The login name the user wants */
+    register int i;            /* A counter */
+
+    /* Log that we are about to reserve a user. */
+    com_err(whoami, 0, "setting identity %s %s", 
+           message->first, message->last);
     
-/*
- * Local Variables:
- * mode: c
- * c-argdecl-indent: 2
- * c-brace-offset: -4
- * c-continued-statement-offset: 4
- * c-indent-level: 4
- * c-label-offset: -2
- * End:
+    /* Check to make sure that we can verify this user. */
+    status = verify_user(message,retval);
+    if (status == SUCCESS || status == UREG_NOT_ALLOWED)
+    {
+       status = SUCCESS;
+       /* Get the requested login name from leftover packet information. */
+       login = message->leftover;
+
+       /* Check the login name for validity.  The login name is currently
+          is allowed to contain lowercase letters in any position and 
+          and numbers and underscore characters in any position but the 
+          first. */
+       if ((strlen(login) < MIN_UNAME) || (strlen(login) > MAX_UNAME))
+           status = UREG_INVALID_UNAME;
+    }
+    if (status == SUCCESS)
+       if ((login[0] == '_') || isdigit(login[0]))
+           status = UREG_INVALID_UNAME;
+    if (status == SUCCESS)
+    {
+       for (i = 0; i < strlen(login); i++)
+           if (!islower(login[i]) && !isdigit(login[i]) && 
+               (login[i] != '_'))
+           {
+               status = UREG_INVALID_UNAME;
+               break;
+           }
+    }
+    if (status == SUCCESS)
+    {
+       /* Now that we have a valid user with a valid login... */
+
+       q_argv[0] = message->db.uid;
+       status = mr_query("get_user_account_by_uid", 1, q_argv,
+                         getuserinfo, q_argv);
+       if (status != SUCCESS) {
+           com_err(whoami, status, " while getting user info");
+           return(status);
+       }
+       q_argv[U_NAME+1] = login;
+       q_argv[U_STATE+1] = "7";
+       q_argv[U_SIGNATURE+1] = "";
+       status = mr_query("update_user_account", U_MODTIME+1, q_argv,
+                          null_callproc, NULL);
+       switch (status)
+       {
+         case MR_SUCCESS:
+           status = SUCCESS;
+           break;
+         case MR_IN_USE:
+           status = UREG_LOGIN_USED;
+           break;
+         case MR_DEADLOCK:
+           status = UREG_MISC_ERROR;
+           break;
+         default:
+           critical_alert(FAIL_INST,"%s returned from update_user_account.",
+                          error_message(status));
+           status = UREG_MISC_ERROR;
+           break;
+       }
+    }
+    if (status == SUCCESS)
+    {
+       /* Moira login was successfully created; try to reserve kerberos
+          principal. */
+       /* If this routine fails, store the login in the retval so
+          that it can be used in the client-side error message. */
+       if ((status = reserve_krb(login)) != SUCCESS)
+           (void) strcpy(retval, login);
+    }
+
+    if (status)
+       com_err(whoami, status, " returned from set_identity");
+    else
+       com_err(whoami, 0, "Identity set");
+    
+    return status;
+}
+
+
+void reg_com_err_hook(whoami, code, fmt, pvar)
+       char *whoami;
+       int code;
+       char *fmt;
+       caddr_t pvar;
+{
+       if (whoami) {
+               fputs(whoami, stderr);
+               fputs(": ", stderr);
+       }
+       if (code) {
+               fputs(error_message(code), stderr);
+       }
+       if (fmt) {
+               _doprnt(fmt, pvar, stderr);
+       }
+       putc('\n', stderr);
+       fflush(stderr);
+}
+
+
+/* Find out of someone's secure instance password is set.
+ * Returns UREG_ALREADY_REGISTERED if set, SUCCESS (0) if not.
  */
+
+int get_secure(message, retval)
+struct msg *message;
+char *retval;
+{
+    int status;
+    char *argv[U_END];
+
+    com_err(whoami, 0, "checking status of secure password for %s",
+           message->first);
+    argv[0] = message->first;
+    status = mr_query("get_user_account_by_login", 1, argv, getuserinfo, argv);
+    if (status != SUCCESS) {
+       com_err(whoami, status, " while getting user info");
+       return(status);
+    }
+    if (atoi(argv[U_SECURE + 1]))
+      return UREG_ALREADY_REGISTERED;
+    return SUCCESS;
+}
+
+
+/* Set someone's secure instance password. */
+
+int set_secure(message, retval)
+struct msg *message;
+char *retval;
+{
+    int status, i;
+    char *argv[U_END], hostbuf[256], *bp, *p, buf[512], *passwd, *id;
+    KTEXT_ST creds;
+    AUTH_DAT auth;
+    C_Block key;
+    Key_schedule keys;
+    Kadm_vals kv;
+    u_long *lkey = (u_long *)key;
+    struct timeval now;
+    static int inited = 0;
+    static char *host;
+
+    if (!inited) {
+       inited++;
+       if (gethostname(hostbuf, sizeof(hostbuf)) < 0)
+         com_err(whoami, errno, "getting local hostname");
+       host = strsave(krb_get_phost(hostbuf));
+    }
+
+    com_err(whoami, 0, "setting secure passwd for %s", message->first);
+    argv[0] = message->first;
+    status = mr_query("get_user_account_by_login", 1, argv, getuserinfo, argv);
+    if (status != SUCCESS) {
+       com_err(whoami, status, " while getting user info");
+       return(status);
+    }
+    if (atoi(argv[U_SECURE + 1])) {
+       com_err(whoami, UREG_ALREADY_REGISTERED, "in set_secure()");
+       return UREG_ALREADY_REGISTERED;
+    }
+
+    bp = message->encrypted;
+    /* round up to word boundary */
+    bp = (char *)((((u_long)bp + 3) >> 2) << 2);
+    
+    creds.length = ntohl(*((int *)bp));
+    bp += sizeof(int);
+    bcopy(bp, creds.dat, creds.length);
+    creds.mbz = 0;
+    bp += creds.length;
+
+#ifdef DEBUG
+    com_err(whoami, 0, "Cred: length %d", creds.length);
+    for (i = 0; i < creds.length; i += 16)
+      com_err(whoami, 0, " %02x %02x %02x %02x %02x %02x %02x %02x  %02x %02x %02x %02x %02x %02x %02x %02x",
+             creds.dat[i+0], creds.dat[i+1], creds.dat[i+2], creds.dat[i+3],
+             creds.dat[i+4], creds.dat[i+5], creds.dat[i+6], creds.dat[i+7],
+             creds.dat[i+8], creds.dat[i+9], creds.dat[i+10], creds.dat[i+11],
+             creds.dat[i+12], creds.dat[i+13], creds.dat[i+14], creds.dat[i+15]);
+#endif /* DEBUG */
+    
+    status = krb_rd_req(&creds, "changepw", host, cur_req_sender(),
+                       &auth, "");
+    if (status) {
+       status += krb_err_base;
+       com_err(whoami, status, " verifying credentials in set_secure()");
+       return(status);
+    }
+
+    message->leftover_len = ntohl(*((int*)(bp)));
+    bp += sizeof(int);
+    message->leftover = bp;
+
+    des_key_sched(auth.session, keys);
+    des_pcbc_encrypt(message->leftover, buf, message->leftover_len,
+                    keys, auth.session, 0);
+
+    id = buf;
+    passwd = index(buf, ',');
+    *passwd++ = 0;
+#ifdef DEBUG
+    com_err(whoami, 0, "Got id: %s, passwd %d chars", id, strlen(passwd));
+#endif
+
+    if (strcmp(id, argv[U_MITID + 1])) {
+       char buf[32];
+
+       EncryptID(buf, id, argv[U_FIRST+1], argv[U_LAST+1]);
+       if (strcmp(buf, argv[U_MITID + 1])) {
+           status = UREG_USER_NOT_FOUND;
+           com_err(whoami, status, "IDs mismatch: %s (%s), %s", id, buf,
+                   argv[U_MITID + 1]);
+           return status;
+       }
+    }
+
+    /* now do actual password setting stuff */
+
+    if ((status = ureg_kadm_init()) != SUCCESS) {
+       com_err(whoami, status, "initing kadm stuff");
+       return(status);
+    }
+
+    bzero((char *)&kv, sizeof(kv));
+    SET_FIELD(KADM_DESKEY, kv.fields);
+    SET_FIELD(KADM_NAME, kv.fields);
+    SET_FIELD(KADM_INST, kv.fields);
+    (void) des_string_to_key(passwd, key);
+    kv.key_low = htonl(lkey[0]);
+    kv.key_high = htonl(lkey[1]);
+    strcpy(kv.name, message->first);
+    strcpy(kv.instance, "extra");
+
+    if ((status = kadm_add(&kv)) != KADM_SUCCESS) {
+       com_err(whoami, status, " while creating kerberos principal");
+       return(status);
+    }
+
+    argv[0] = message->first;
+    argv[1] = buf;
+    gettimeofday(&now, NULL);
+    sprintf(buf, "%d", now.tv_sec);
+    status = mr_query("update_user_security_status", 2, argv, getuserinfo, argv);
+    if (status != SUCCESS) {
+       com_err(whoami, status, " while updating user status");
+       return(status);
+    }
+    return SUCCESS;
+}
This page took 0.091851 seconds and 4 git commands to generate.