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