Home | History | Annotate | Line # | Download | only in passwd
krb5_passwd.c revision 1.8
      1 /* $NetBSD: krb5_passwd.c,v 1.8 2000/06/20 06:00:37 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to
      8  * The NetBSD Foundation by Johan Danielsson.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  *
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * 3. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /* uses the `Kerberos Change Password Protocol' */
     39 
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 
     44 #include <err.h>
     45 
     46 #include <openssl/des.h>
     47 #include <krb5.h>
     48 
     49 #include "extern.h"
     50 
     51 static krb5_context context;
     52 static krb5_principal defprinc;
     53 static int usage = PW_USE;
     54 
     55 int
     56 krb5_init(const char *progname)
     57 {
     58     return krb5_init_context(&context);
     59 }
     60 
     61 int
     62 krb5_arg (char ch, const char *optarg)
     63 {
     64     krb5_error_code ret;
     65     switch(ch) {
     66     case '5':
     67     case 'k':
     68 	usage = PW_USE_FORCE;
     69 	return 1;
     70     case 'u':
     71 	ret = krb5_parse_name(context, optarg, &defprinc);
     72 	if(ret) {
     73 	    krb5_warn(context, ret, "%s", optarg);
     74 	    return 0;
     75 	}
     76 	return 1;
     77     }
     78     return 0;
     79 }
     80 
     81 int
     82 krb5_arg_end(void)
     83 {
     84     return usage;
     85 }
     86 
     87 void
     88 krb5_end(void)
     89 {
     90     if(defprinc)
     91 	krb5_free_principal(context, defprinc);
     92     krb5_free_context(context);
     93 }
     94 
     95 
     96 int
     97 krb5_chpw(const char *username)
     98 {
     99     krb5_error_code ret;
    100     krb5_context context;
    101     krb5_principal principal;
    102     krb5_get_init_creds_opt opt;
    103     krb5_creds cred;
    104     int result_code;
    105     krb5_data result_code_string, result_string;
    106     char pwbuf[BUFSIZ];
    107 
    108     ret = krb5_init_context (&context);
    109     if (ret) {
    110 	warnx("failed kerberos initialisation: %s",
    111 	      krb5_get_err_text(context, ret));
    112 	return 1;
    113     }
    114 
    115     krb5_get_init_creds_opt_init (&opt);
    116 
    117     krb5_get_init_creds_opt_set_tkt_life (&opt, 300);
    118     krb5_get_init_creds_opt_set_forwardable (&opt, FALSE);
    119     krb5_get_init_creds_opt_set_proxiable (&opt, FALSE);
    120 
    121     if(username != NULL) {
    122         ret = krb5_parse_name (context, username, &principal);
    123         if (ret) {
    124 	    warnx("failed to parse principal: %s",
    125 		  krb5_get_err_text(context, ret));
    126 	    return 1;
    127 	}
    128     } else
    129         principal = defprinc;
    130 
    131     ret = krb5_get_init_creds_password (context,
    132                                         &cred,
    133                                         principal,
    134                                         NULL,
    135                                         krb5_prompter_posix,
    136                                         NULL,
    137                                         0,
    138                                         "kadmin/changepw",
    139                                         &opt);
    140 
    141     switch (ret) {
    142     case 0:
    143         break;
    144     case KRB5_LIBOS_PWDINTR :
    145 	/* XXX */
    146         return 1;
    147     case KRB5KRB_AP_ERR_BAD_INTEGRITY :
    148     case KRB5KRB_AP_ERR_MODIFIED :
    149 	fprintf(stderr, "Password incorrect\n");
    150 	return 1;
    151         break;
    152     default:
    153 	warnx("failed to get credentials: %s",
    154 	      krb5_get_err_text(context, ret));
    155 	return 1;
    156     }
    157     krb5_data_zero (&result_code_string);
    158     krb5_data_zero (&result_string);
    159 
    160     /* XXX use getpass? It has a broken interface. */
    161     if(des_read_pw_string (pwbuf, sizeof(pwbuf), "New password: ", 1) != 0)
    162         return 1;
    163 
    164     ret = krb5_change_password (context, &cred, pwbuf,
    165                                 &result_code,
    166                                 &result_code_string,
    167                                 &result_string);
    168     if (ret)
    169         krb5_err (context, 1, ret, "krb5_change_password");
    170 
    171     printf ("%.*s\n", (int)result_string.length, (char *)result_string.data);
    172 
    173     krb5_data_free (&result_code_string);
    174     krb5_data_free (&result_string);
    175 
    176     krb5_free_creds_contents (context, &cred);
    177     krb5_free_context (context);
    178     return result_code;
    179 }
    180