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