home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / passwd / krb_passwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  8.1 KB  |  317 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)krb_passwd.c    5.4 (Berkeley) 3/1/91";
  36. #endif /* not lint */
  37.  
  38. #ifdef KERBEROS
  39.  
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #include <netinet/in.h>
  45. #include <kerberosIV/des.h>
  46. #include <kerberosIV/krb.h>
  47. #include <netdb.h>
  48. #include <signal.h>
  49. #include <pwd.h>
  50. #include <errno.h>
  51. #include <stdio.h>
  52. #include "kpasswd_proto.h"
  53. #include <string.h>
  54. #include <stdlib.h>
  55.  
  56. #define    PROTO    "tcp"
  57.  
  58. static struct timeval timeout = { CLIENT_KRB_TIMEOUT, 0 };
  59. static struct kpasswd_data proto_data;
  60. static des_cblock okey;
  61. static Key_schedule osched;
  62. KTEXT_ST ticket;
  63. Key_schedule random_schedule;
  64. long authopts;
  65. char realm[REALM_SZ], krbhst[MAX_HSTNM];
  66. int sock;
  67.  
  68. krb_passwd()
  69. {
  70.     struct servent *se;
  71.     struct hostent *host;
  72.     struct sockaddr_in sin;
  73.     CREDENTIALS cred;
  74.     fd_set readfds;
  75.     int rval;
  76.     char pass[_PASSWORD_LEN], password[_PASSWORD_LEN];
  77.     static void finish();
  78.  
  79.     static struct rlimit rl = { 0, 0 };
  80.  
  81.     (void)signal(SIGHUP, SIG_IGN);
  82.     (void)signal(SIGINT, SIG_IGN);
  83.     (void)signal(SIGTSTP, SIG_IGN);
  84.  
  85.     if (setrlimit(RLIMIT_CORE, &rl) < 0) {
  86.         (void)fprintf(stderr,
  87.             "passwd: setrlimit: %s\n", strerror(errno));
  88.         return(1);
  89.     }
  90.  
  91.     if ((se = getservbyname(SERVICE, PROTO)) == NULL) {
  92.         (void)fprintf(stderr,
  93.             "passwd: couldn't find entry for service %s/%s\n",
  94.             SERVICE, PROTO);
  95.         return(1);
  96.     }
  97.  
  98.     if ((rval = krb_get_lrealm(realm,1)) != KSUCCESS) {
  99.         (void)fprintf(stderr,
  100.             "passwd: couldn't get local Kerberos realm: %s\n",
  101.             krb_err_txt[rval]);
  102.         return(1);
  103.     }
  104.  
  105.     if ((rval = krb_get_krbhst(krbhst, realm, 1)) != KSUCCESS) {
  106.         (void)fprintf(stderr,
  107.             "passwd: couldn't get Kerberos host: %s\n",
  108.             krb_err_txt[rval]);
  109.         return(1);
  110.     }
  111.  
  112.     if ((host = gethostbyname(krbhst)) == NULL) {
  113.         (void)fprintf(stderr,
  114.             "passwd: couldn't get host entry for krb host %s\n",
  115.             krbhst);
  116.         return(1);
  117.     }
  118.  
  119.     sin.sin_family = host->h_addrtype;
  120.     bcopy(host->h_addr, (char *) &sin.sin_addr, host->h_length);
  121.     sin.sin_port = se->s_port;
  122.  
  123.     if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  124.         (void)fprintf(stderr, "passwd: socket: %s\n", strerror(errno));
  125.         return(1);
  126.     }
  127.  
  128.     if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  129.         (void)fprintf(stderr, "passwd: connect: %s\n", strerror(errno));
  130.         (void)close(sock);
  131.         return(1);
  132.     }
  133.  
  134.     rval = krb_sendauth(
  135.         authopts,        /* NOT mutual */
  136.         sock,
  137.         &ticket,        /* (filled in) */
  138.         SERVICE,
  139.         krbhst,            /* instance (krbhst) */
  140.         realm,            /* dest realm */
  141.         (u_long) getpid(),    /* checksum */
  142.         NULL,            /* msg data */
  143.         NULL,            /* credentials */ 
  144.         NULL,            /* schedule */
  145.         NULL,            /* local addr */
  146.         NULL,            /* foreign addr */
  147.         "KPWDV0.1"
  148.     );
  149.  
  150.     if (rval != KSUCCESS) {
  151.         (void)fprintf(stderr, "passwd: Kerberos sendauth error: %s\n",
  152.             krb_err_txt[rval]);
  153.         return(1);
  154.     }
  155.  
  156.     krb_get_cred("krbtgt", realm, realm, &cred);
  157.  
  158.     (void)printf("Changing Kerberos password for %s.%s@%s.\n",
  159.         cred.pname, cred.pinst, realm);
  160.  
  161.     if (des_read_pw_string(pass,
  162.         sizeof(pass)-1, "Old Kerberos password:", 0)) {
  163.         (void)fprintf(stderr,
  164.             "passwd: error reading old Kerberos password\n");
  165.         return(1);
  166.     }
  167.  
  168.     (void)des_string_to_key(pass, okey);
  169.     (void)des_key_sched(okey, osched);
  170.     (void)des_set_key(okey, osched);
  171.  
  172.     /* wait on the verification string */
  173.  
  174.     FD_ZERO(&readfds);
  175.     FD_SET(sock, &readfds);
  176.  
  177.     rval =
  178.         select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
  179.  
  180.     if ((rval < 1) || !FD_ISSET(sock, &readfds)) {
  181.         if(rval == 0) {
  182.             (void)fprintf(stderr, "passwd: timed out (aborted)\n");
  183.             cleanup();
  184.             return(1);
  185.         }
  186.         (void)fprintf(stderr, "passwd: select failed (aborted)\n");
  187.         cleanup();
  188.         return(1);
  189.     }
  190.  
  191.     /* read verification string */
  192.  
  193.     if (des_read(sock, &proto_data, sizeof(proto_data)) !=
  194.         sizeof(proto_data)) {
  195.         (void)fprintf(stderr,
  196.             "passwd: couldn't read verification string (aborted)\n");
  197.         cleanup();
  198.         return(1);
  199.     }
  200.  
  201.     (void)signal(SIGHUP, finish);
  202.     (void)signal(SIGINT, finish);
  203.  
  204.     if (strcmp(SECURE_STRING, proto_data.secure_msg) != 0) {
  205.         cleanup();
  206.         /* don't complain loud if user just hit return */
  207.         if (pass == NULL || (!*pass))
  208.             return(0);
  209.         (void)fprintf(stderr, "Sorry\n");
  210.         return(1);
  211.     }
  212.  
  213.     (void)des_key_sched(proto_data.random_key, random_schedule);
  214.     (void)des_set_key(proto_data.random_key, random_schedule);
  215.     (void)bzero(pass, sizeof(pass));
  216.  
  217.     if (des_read_pw_string(pass,
  218.         sizeof(pass)-1, "New Kerberos password:", 0)) {
  219.         (void)fprintf(stderr,
  220.             "passwd: error reading new Kerberos password (aborted)\n");
  221.         cleanup();
  222.         return(1);
  223.     }
  224.  
  225.     if (des_read_pw_string(password,
  226.         sizeof(password)-1, "Retype new Kerberos password:", 0)) {
  227.         (void)fprintf(stderr,
  228.             "passwd: error reading new Kerberos password (aborted)\n");
  229.         cleanup();
  230.         return(1);
  231.     }
  232.  
  233.     if (strcmp(password, pass) != 0) {
  234.         (void)fprintf(stderr,
  235.             "passwd: password mismatch (aborted)\n");
  236.         cleanup();
  237.         return(1);
  238.     }
  239.  
  240.     if (strlen(pass) == 0)
  241.         (void)printf("using NULL password\n");
  242.  
  243.     send_update(sock, password, SECURE_STRING);
  244.  
  245.     /* wait for ACK */
  246.  
  247.     FD_ZERO(&readfds);
  248.     FD_SET(sock, &readfds);
  249.  
  250.     rval =
  251.         select(sock + 1, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout);
  252.     if ((rval < 1) || !FD_ISSET(sock, &readfds)) {
  253.         if(rval == 0) {
  254.             (void)fprintf(stderr,
  255.                 "passwd: timed out reading ACK (aborted)\n");
  256.             cleanup();
  257.             exit(1);
  258.         }
  259.         (void)fprintf(stderr, "passwd: select failed (aborted)\n");
  260.         cleanup();
  261.         exit(1);
  262.     }
  263.     recv_ack(sock);
  264.     cleanup();
  265.     exit(0);
  266. }
  267.  
  268. send_update(dest, pwd, str)
  269.     int dest;
  270.     char *pwd, *str;
  271. {
  272.     static struct update_data ud;
  273.  
  274.     (void)strncpy(ud.secure_msg, str, _PASSWORD_LEN);
  275.     (void)strncpy(ud.pw, pwd, sizeof(ud.pw));
  276.     if (des_write(dest, &ud, sizeof(ud)) != sizeof(ud)) {
  277.         (void)fprintf(stderr,
  278.             "passwd: couldn't write pw update (abort)\n");
  279.         bzero((char *)&ud, sizeof(ud));
  280.         cleanup();
  281.         exit(1);
  282.     }
  283. }
  284.  
  285. recv_ack(remote)
  286.     int remote;
  287. {
  288.     int cc;
  289.     char buf[BUFSIZ];
  290.  
  291.     cc = des_read(remote, buf, sizeof(buf));
  292.     if (cc <= 0) {
  293.         (void)fprintf(stderr,
  294.             "passwd: error reading acknowledgement (aborted)\n");
  295.         cleanup();
  296.         exit(1);
  297.     }
  298.     (void)printf("%s", buf);
  299. }
  300.  
  301. cleanup()
  302. {
  303.     (void)bzero((char *)&proto_data, sizeof(proto_data));
  304.     (void)bzero((char *)okey, sizeof(okey));
  305.     (void)bzero((char *)osched, sizeof(osched));
  306.     (void)bzero((char *)random_schedule, sizeof(random_schedule));
  307. }
  308.  
  309. static void
  310. finish()
  311. {
  312.     (void)close(sock);
  313.     exit(1);
  314. }
  315.  
  316. #endif /* KERBEROS */
  317.