Annotation of src/usr.bin/passwd/krb5_passwd.c, Revision 1.6
1.3 hin 1: /*
2: * Copyright (c) 1997-2000 Kungliga Tekniska Högskolan
1.6 ! deraadt 3: * (Royal Institute of Technology, Stockholm, Sweden).
! 4: * All rights reserved.
1.3 hin 5: *
1.6 ! deraadt 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
1.3 hin 9: *
1.6 ! deraadt 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
1.3 hin 12: *
1.6 ! deraadt 13: * 2. Redistributions in binary form must reproduce the above copyright
! 14: * notice, this list of conditions and the following disclaimer in the
! 15: * documentation and/or other materials provided with the distribution.
1.1 deraadt 16: *
1.6 ! deraadt 17: * 3. Neither the name of the Institute 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.
1.1 deraadt 20: *
1.6 ! deraadt 21: * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
1.1 deraadt 32: */
33:
1.4 hin 34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <string.h>
37: #include <sys/types.h>
38: #include <fcntl.h>
39: #include <sys/uio.h>
40: #include <unistd.h>
41: #include <pwd.h>
42: #include <sys/time.h>
43: #include <sys/select.h>
44: #include <sys/socket.h>
45: #include <netinet/in.h>
46: #include <arpa/inet.h>
47: #include <netdb.h>
48: #include <errno.h>
49: #include <dlfcn.h>
50: #include <util.h>
51: #include <err.h>
1.5 hugh 52: #include <des.h>
1.4 hin 53: #include <kerberosV/krb5.h>
1.1 deraadt 54:
1.4 hin 55: /* RCSID("$KTH: kpasswd.c,v 1.23 2000/12/31 07:48:34 assar Exp $"); */
1.1 deraadt 56:
1.3 hin 57: int
1.4 hin 58: krb5_passwd (int argc, char **argv)
1.1 deraadt 59: {
1.6 ! deraadt 60: krb5_data result_code_string, result_string;
! 61: krb5_get_init_creds_opt opt;
! 62: krb5_principal principal;
! 63: krb5_context context;
! 64: krb5_error_code ret;
! 65: char pwbuf[BUFSIZ];
! 66: krb5_creds cred;
! 67: int result_code;
! 68:
! 69: krb5_get_init_creds_opt_init (&opt);
! 70:
! 71: krb5_get_init_creds_opt_set_tkt_life (&opt, 300);
! 72: krb5_get_init_creds_opt_set_forwardable (&opt, FALSE);
! 73: krb5_get_init_creds_opt_set_proxiable (&opt, FALSE);
! 74:
! 75: ret = krb5_init_context(&context);
! 76: if (ret)
! 77: errx(1, "krb5_init_context failed: %d", ret);
! 78:
! 79: if (argv[0]) {
! 80: ret = krb5_parse_name(context, argv[0], &principal);
! 81: if (ret)
! 82: krb5_err(context, 1, ret, "krb5_parse_name");
! 83: } else
! 84: principal = NULL;
! 85:
! 86: ret = krb5_get_init_creds_password (context, &cred,
! 87: principal, NULL, krb5_prompter_posix, NULL, 0,
! 88: "kadmin/changepw", &opt);
! 89: switch (ret) {
! 90: case 0:
! 91: break;
! 92: case KRB5_LIBOS_PWDINTR :
! 93: return 1;
! 94: case KRB5KRB_AP_ERR_BAD_INTEGRITY :
! 95: case KRB5KRB_AP_ERR_MODIFIED :
! 96: krb5_errx(context, 1, "Password incorrect");
! 97: break;
! 98: default:
! 99: krb5_err(context, 1, ret, "krb5_get_init_creds");
! 100: }
! 101:
! 102: krb5_data_zero(&result_code_string);
! 103: krb5_data_zero(&result_string);
! 104:
! 105: if (des_read_pw_string(pwbuf, sizeof(pwbuf), "New password: ", 1) != 0)
! 106: return 1;
! 107:
! 108: ret = krb5_change_password (context, &cred, pwbuf, &result_code,
! 109: &result_code_string, &result_string);
1.3 hin 110: if (ret)
1.6 ! deraadt 111: krb5_err(context, 1, ret, "krb5_change_password");
1.3 hin 112:
1.6 ! deraadt 113: printf("Reply from server: %.*s\n", (int)result_string.length,
1.3 hin 114: (char *)result_string.data);
115:
1.6 ! deraadt 116: krb5_data_free(&result_code_string);
! 117: krb5_data_free(&result_string);
! 118:
! 119: krb5_free_creds_contents(context, &cred);
! 120: krb5_free_context(context);
! 121: return result_code;
1.1 deraadt 122: }