1 /* $NetBSD: kpasswd.c,v 1.2 2017/01/28 21:31:45 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include "kpasswd_locl.h" 37 __RCSID("$NetBSD: kpasswd.c,v 1.2 2017/01/28 21:31:45 christos Exp $"); 38 39 static int version_flag; 40 static int help_flag; 41 static char *admin_principal_str; 42 static char *cred_cache_str; 43 44 static struct getargs args[] = { 45 { "admin-principal", 0, arg_string, &admin_principal_str, NULL, 46 NULL }, 47 { "cache", 'c', arg_string, &cred_cache_str, NULL, NULL }, 48 { "version", 0, arg_flag, &version_flag, NULL, NULL }, 49 { "help", 0, arg_flag, &help_flag, NULL, NULL } 50 }; 51 52 static void 53 usage (int ret, struct getargs *a, int num_args) 54 { 55 arg_printusage (a, num_args, NULL, "[principal ...]"); 56 exit (ret); 57 } 58 59 static int 60 change_password(krb5_context context, 61 krb5_principal principal, 62 krb5_ccache id) 63 { 64 krb5_data result_code_string, result_string; 65 int result_code; 66 krb5_error_code ret; 67 char pwbuf[BUFSIZ]; 68 char *msg, *name; 69 int aret; 70 71 krb5_data_zero (&result_code_string); 72 krb5_data_zero (&result_string); 73 74 name = msg = NULL; 75 if (principal == NULL) 76 aret = asprintf(&msg, "New password: "); 77 else { 78 ret = krb5_unparse_name(context, principal, &name); 79 if (ret) 80 krb5_err(context, 1, ret, "krb5_unparse_name"); 81 82 aret = asprintf(&msg, "New password for %s: ", name); 83 } 84 85 if (aret == -1 || msg == NULL) 86 krb5_errx (context, 1, "out of memory"); 87 88 ret = UI_UTIL_read_pw_string (pwbuf, sizeof(pwbuf), msg, 1); 89 free(msg); 90 if (name) 91 free(name); 92 if (ret != 0) { 93 return 1; 94 } 95 96 ret = krb5_set_password_using_ccache (context, id, pwbuf, 97 principal, 98 &result_code, 99 &result_code_string, 100 &result_string); 101 if (ret) { 102 krb5_warn (context, ret, "krb5_set_password_using_ccache"); 103 return 1; 104 } 105 106 printf ("%s%s%.*s\n", krb5_passwd_result_to_string(context, result_code), 107 result_string.length > 0 ? " : " : "", 108 (int)result_string.length, 109 result_string.length > 0 ? (char *)result_string.data : ""); 110 111 krb5_data_free (&result_code_string); 112 krb5_data_free (&result_string); 113 114 return ret != 0; 115 } 116 117 118 int 119 main (int argc, char **argv) 120 { 121 krb5_error_code ret; 122 krb5_context context; 123 krb5_principal principal; 124 krb5_get_init_creds_opt *opt; 125 krb5_ccache id = NULL; 126 int exit_value; 127 int optidx = 0; 128 129 setprogname(argv[0]); 130 131 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 132 usage(1, args, sizeof(args) / sizeof(args[0])); 133 if (help_flag) 134 usage(0, args, sizeof(args) / sizeof(args[0])); 135 if (version_flag) { 136 print_version(NULL); 137 return 0; 138 } 139 argc -= optidx; 140 argv += optidx; 141 142 ret = krb5_init_context (&context); 143 if (ret) 144 errx (1, "krb5_init_context failed: %d", ret); 145 146 ret = krb5_get_init_creds_opt_alloc (context, &opt); 147 if (ret) 148 krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc"); 149 150 krb5_get_init_creds_opt_set_tkt_life (opt, 300); 151 krb5_get_init_creds_opt_set_forwardable (opt, FALSE); 152 krb5_get_init_creds_opt_set_proxiable (opt, FALSE); 153 154 if (cred_cache_str) { 155 ret = krb5_cc_resolve(context, cred_cache_str, &id); 156 if (ret) 157 krb5_err (context, 1, ret, "krb5_cc_resolve"); 158 } else { 159 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id); 160 if (ret) 161 krb5_err (context, 1, ret, "krb5_cc_new_unique"); 162 } 163 164 if (cred_cache_str == NULL) { 165 krb5_principal admin_principal = NULL; 166 krb5_creds cred; 167 168 if (admin_principal_str) { 169 ret = krb5_parse_name (context, admin_principal_str, 170 &admin_principal); 171 if (ret) 172 krb5_err (context, 1, ret, "krb5_parse_name"); 173 } else if (argc == 1) { 174 ret = krb5_parse_name (context, argv[0], &admin_principal); 175 if (ret) 176 krb5_err (context, 1, ret, "krb5_parse_name"); 177 } else { 178 ret = krb5_get_default_principal (context, &admin_principal); 179 if (ret) 180 krb5_err (context, 1, ret, "krb5_get_default_principal"); 181 } 182 183 ret = krb5_get_init_creds_password (context, 184 &cred, 185 admin_principal, 186 NULL, 187 krb5_prompter_posix, 188 NULL, 189 0, 190 "kadmin/changepw", 191 opt); 192 switch (ret) { 193 case 0: 194 break; 195 case KRB5_LIBOS_PWDINTR : 196 return 1; 197 case KRB5KRB_AP_ERR_BAD_INTEGRITY : 198 case KRB5KRB_AP_ERR_MODIFIED : 199 krb5_errx(context, 1, "Password incorrect"); 200 break; 201 default: 202 krb5_err(context, 1, ret, "krb5_get_init_creds"); 203 } 204 205 krb5_get_init_creds_opt_free(context, opt); 206 207 ret = krb5_cc_initialize(context, id, admin_principal); 208 krb5_free_principal(context, admin_principal); 209 if (ret) 210 krb5_err(context, 1, ret, "krb5_cc_initialize"); 211 212 ret = krb5_cc_store_cred(context, id, &cred); 213 if (ret) 214 krb5_err(context, 1, ret, "krb5_cc_store_cred"); 215 216 krb5_free_cred_contents (context, &cred); 217 } 218 219 if (argc == 0) { 220 exit_value = change_password(context, NULL, id); 221 } else { 222 exit_value = 0; 223 224 while (argc-- > 0) { 225 226 ret = krb5_parse_name (context, argv[0], &principal); 227 if (ret) 228 krb5_err (context, 1, ret, "krb5_parse_name"); 229 230 ret = change_password(context, principal, id); 231 if (ret) 232 exit_value = 1; 233 krb5_free_principal(context, principal); 234 argv++; 235 } 236 } 237 238 if (cred_cache_str == NULL) { 239 ret = krb5_cc_destroy(context, id); 240 if (ret) 241 krb5_err (context, 1, ret, "krb5_cc_destroy"); 242 } else { 243 ret = krb5_cc_close(context, id); 244 if (ret) 245 krb5_err (context, 1, ret, "krb5_cc_close"); 246 } 247 248 krb5_free_context (context); 249 return exit_value; 250 } 251