Annotation of src/usr.bin/passwd/krb5_passwd.c, Revision 1.10
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.10 ! djm 57: int krb5_passwd(int, char **);
! 58:
1.3 hin 59: int
1.7 deraadt 60: krb5_passwd(int argc, char **argv)
1.1 deraadt 61: {
1.6 deraadt 62: krb5_data result_code_string, result_string;
63: krb5_get_init_creds_opt opt;
64: krb5_principal principal;
65: krb5_context context;
66: krb5_error_code ret;
67: char pwbuf[BUFSIZ];
68: krb5_creds cred;
69: int result_code;
1.8 biorn 70: uid_t uid;
71:
72: uid = getuid();
73: if (setresuid(uid, uid, uid)) {
74: errx(1, "can't drop privileges\n");
75: }
1.6 deraadt 76:
77: krb5_get_init_creds_opt_init (&opt);
78:
79: krb5_get_init_creds_opt_set_tkt_life (&opt, 300);
80: krb5_get_init_creds_opt_set_forwardable (&opt, FALSE);
81: krb5_get_init_creds_opt_set_proxiable (&opt, FALSE);
82:
83: ret = krb5_init_context(&context);
84: if (ret)
85: errx(1, "krb5_init_context failed: %d", ret);
86:
87: if (argv[0]) {
88: ret = krb5_parse_name(context, argv[0], &principal);
1.8 biorn 89: if (ret)
90: krb5_err(context, 1, ret, "krb5_parse_name");
91: } else {
92: ret = krb5_get_default_principal (context, &principal);
93: if (ret)
94: krb5_err (context, 1, ret, "krb5_get_default_principal");
95: }
1.6 deraadt 96:
97: ret = krb5_get_init_creds_password (context, &cred,
98: principal, NULL, krb5_prompter_posix, NULL, 0,
99: "kadmin/changepw", &opt);
100: switch (ret) {
101: case 0:
102: break;
103: case KRB5_LIBOS_PWDINTR :
104: return 1;
105: case KRB5KRB_AP_ERR_BAD_INTEGRITY :
106: case KRB5KRB_AP_ERR_MODIFIED :
107: krb5_errx(context, 1, "Password incorrect");
108: break;
109: default:
110: krb5_err(context, 1, ret, "krb5_get_init_creds");
111: }
112:
113: krb5_data_zero(&result_code_string);
114: krb5_data_zero(&result_string);
115:
1.9 millert 116: if (des_read_pw_string(pwbuf, sizeof(pwbuf), "New password:", 1) != 0)
1.6 deraadt 117: return 1;
118:
119: ret = krb5_change_password (context, &cred, pwbuf, &result_code,
120: &result_code_string, &result_string);
1.3 hin 121: if (ret)
1.6 deraadt 122: krb5_err(context, 1, ret, "krb5_change_password");
1.3 hin 123:
1.6 deraadt 124: printf("Reply from server: %.*s\n", (int)result_string.length,
1.3 hin 125: (char *)result_string.data);
126:
1.6 deraadt 127: krb5_data_free(&result_code_string);
128: krb5_data_free(&result_string);
129:
130: krb5_free_creds_contents(context, &cred);
131: krb5_free_context(context);
132: return result_code;
1.1 deraadt 133: }