1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
*
* Openvision retains the copyright to derivative works of
* this source code. Do *NOT* create a derivative of this
* source code before consulting with your legal department.
* Do *NOT* integrate *ANY* of this source code into another
* product before consulting with your legal department.
*
* For further information, read the top-level Openvision
* copyright which is contained in the top-level MIT Kerberos
* copyright.
*
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
*
*/
/*
* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
*/
#include <stdio.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <time.h>
#include <locale.h>
#include <kadm5/admin.h>
#include "admin_internal.h"
#include <krb5.h>
#include <strings.h>
#define string_text error_message
const char *chpw_error_message(kadm5_ret_t code);
/*
* Function: kadm5_chpass_principal_util
*
* Purpose: Wrapper around chpass_principal. We can read new pw, change pw and return useful messages
*
* Arguments:
*
* princ (input) a krb5b_principal structure for the
* principal whose password we should change.
*
* new_password (input) NULL or a null terminated string with the
* the principal's desired new password. If new_password
* is NULL then this routine will read a new password.
*
* pw_ret (output) if non-NULL, points to a static buffer
* containing the new password (if password is prompted
* internally), or to the new_password argument (if
* that is non-NULL). If the former, then the buffer
* is only valid until the next call to the function,
* and the caller should be sure to zero it when
* it is no longer needed.
*
* msg_ret (output) a useful message is copied here.
*
* <return value> exit status of 0 for success, else the com err code
* for the last significant routine called.
*
* Requires:
*
* A msg_ret should point to a buffer large enough for the messasge.
*
* Effects:
*
* Modifies:
*
*
*/
kadm5_ret_t _kadm5_chpass_principal_util(void *server_handle,
void *lhandle,
krb5_principal princ,
char *new_pw,
char **ret_pw,
char *msg_ret,
unsigned int msg_len)
{
int code, code2;
unsigned int pwsize;
static char buffer[255];
char *new_password;
kadm5_principal_ent_rec princ_ent;
kadm5_policy_ent_rec policy_ent;
krb5_chgpwd_prot passwd_protocol;
_KADM5_CHECK_HANDLE(server_handle);
if (ret_pw)
*ret_pw = NULL;
if (new_pw != NULL) {
new_password = new_pw;
} else { /* read the password */
krb5_context context;
if ((code = (int) kadm5_init_krb5_context(&context)) == 0) {
pwsize = sizeof(buffer);
code = krb5_read_password(context, KADM5_PW_FIRST_PROMPT,
KADM5_PW_SECOND_PROMPT,
buffer, &pwsize);
krb5_free_context(context);
}
if (code == 0)
new_password = buffer;
else {
#ifdef ZEROPASSWD
memset(buffer, 0, sizeof(buffer));
#endif
if (code == KRB5_LIBOS_BADPWDMATCH) {
(void) strncpy(msg_ret, string_text(CHPASS_UTIL_NEW_PASSWORD_MISMATCH),
msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(code);
} else {
(void) strncpy(msg_ret, error_message(code), msg_len - 1);
(void) strncat(msg_ret, " ", msg_len - 1);
(void) strncat(msg_ret, string_text(CHPASS_UTIL_WHILE_READING_PASSWORD),
msg_len - 1);
(void) strncat(msg_ret, string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(code);
}
}
if (pwsize == 0) {
#ifdef ZEROPASSWD
memset(buffer, 0, sizeof(buffer));
#endif
strncpy(msg_ret, string_text(CHPASS_UTIL_NO_PASSWORD_READ), msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(KRB5_LIBOS_CANTREADPWD); /* could do better */
}
}
if (ret_pw)
*ret_pw = new_password;
passwd_protocol = _kadm5_get_kpasswd_protocol(server_handle);
if (passwd_protocol == KRB5_CHGPWD_CHANGEPW_V2) {
kadm5_ret_t srvr_rsp_code;
krb5_data srvr_msg;
srvr_msg.length = 0;
srvr_msg.data = NULL;
code = kadm5_chpass_principal_v2(server_handle, princ,
new_password,
&srvr_rsp_code,
&srvr_msg);
if (srvr_rsp_code) {
sprintf(msg_ret, "%s%s%.*s\n",
chpw_error_message(srvr_rsp_code),
srvr_msg.length? ": " : "",
srvr_msg.length,
srvr_msg.data ? srvr_msg.data : "");
return (srvr_rsp_code);
}
return (code);
} else if (passwd_protocol == KRB5_CHGPWD_RPCSEC) {
code = kadm5_chpass_principal(server_handle, princ,
new_password);
#ifdef ZEROPASSWD
if (!ret_pw)
memset(buffer, 0, sizeof(buffer)); /* in case we read a new password */
#endif
if (code == KADM5_OK) {
strncpy(msg_ret, string_text(CHPASS_UTIL_PASSWORD_CHANGED), msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(0);
}
if ((code != KADM5_PASS_Q_TOOSHORT) &&
(code != KADM5_PASS_REUSE) &&(code != KADM5_PASS_Q_CLASS) &&
(code != KADM5_PASS_Q_DICT) && (code != KADM5_PASS_TOOSOON)) {
/* Can't get more info for other errors */
sprintf(buffer, "%s %s", error_message(code),
string_text(CHPASS_UTIL_WHILE_TRYING_TO_CHANGE));
sprintf(msg_ret, "%s\n%s\n", string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
buffer);
return(code);
}
/* Ok, we have a password quality error. Return a good message */
if (code == KADM5_PASS_REUSE) {
strncpy(msg_ret, string_text(CHPASS_UTIL_PASSWORD_REUSE), msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(code);
}
if (code == KADM5_PASS_Q_DICT) {
strncpy(msg_ret, string_text(CHPASS_UTIL_PASSWORD_IN_DICTIONARY),
msg_len - 1);
msg_ret[msg_len - 1] = '\0';
return(code);
}
/* Look up policy for the remaining messages */
code2 = kadm5_get_principal (lhandle, princ, &princ_ent,
KADM5_PRINCIPAL_NORMAL_MASK);
if (code2 != 0) {
strncpy(msg_ret, error_message(code2), msg_len - 1);
strncat(msg_ret, " ", msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, string_text(CHPASS_UTIL_GET_PRINC_INFO), msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, "\n", msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, error_message(code), msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, " ", msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, string_text(CHPASS_UTIL_WHILE_TRYING_TO_CHANGE),
msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, "\n\n", msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, "\n", msg_len - 1 - strlen(msg_ret));
msg_ret[msg_len - 1] = '\0';
return(code);
}
if ((princ_ent.aux_attributes & KADM5_POLICY) == 0) {
strncpy(msg_ret, error_message(code), msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, " ", msg_len - 1 - strlen(msg_ret));
strncpy(msg_ret, string_text(CHPASS_UTIL_NO_POLICY_YET_Q_ERROR),
msg_len - 1 - strlen(msg_ret));
strncat(msg_ret, "\n\n", msg_len - 1 - strlen(msg_ret));
strncpy(msg_ret, string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
msg_len - 1 - strlen(msg_ret));
msg_ret[msg_len - 1] = '\0';
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
return(code);
}
code2 = kadm5_get_policy(lhandle, princ_ent.policy,
&policy_ent);
if (code2 != 0) {
sprintf(msg_ret, "%s %s\n%s %s\n\n%s\n ", error_message(code2),
string_text(CHPASS_UTIL_GET_POLICY_INFO),
error_message(code),
string_text(CHPASS_UTIL_WHILE_TRYING_TO_CHANGE),
string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED));
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
return(code);
}
if (code == KADM5_PASS_Q_TOOSHORT) {
sprintf(msg_ret, string_text(CHPASS_UTIL_PASSWORD_TOO_SHORT),
policy_ent.pw_min_length);
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
(void) kadm5_free_policy_ent(lhandle, &policy_ent);
return(code);
}
if (code == KADM5_PASS_Q_CLASS) {
sprintf(msg_ret, string_text(CHPASS_UTIL_TOO_FEW_CLASSES),
policy_ent.pw_min_classes);
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
(void) kadm5_free_policy_ent(lhandle, &policy_ent);
return(code);
}
if (code == KADM5_PASS_TOOSOON) {
time_t until;
char *time_string, *ptr;
until = princ_ent.last_pwd_change + policy_ent.pw_min_life;
time_string = ctime(&until);
if (*(ptr = &time_string[strlen(time_string)-1]) == '\n')
*ptr = '\0';
sprintf(msg_ret, string_text(CHPASS_UTIL_PASSWORD_TOO_SOON),
time_string);
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
(void) kadm5_free_policy_ent(lhandle, &policy_ent);
return(code);
} else {
/* We should never get here, but just in case ... */
sprintf(buffer, "%s %s", error_message(code),
string_text(CHPASS_UTIL_WHILE_TRYING_TO_CHANGE));
sprintf(msg_ret, "%s\n%s\n", string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
buffer);
(void) kadm5_free_principal_ent(lhandle, &princ_ent);
(void) kadm5_free_policy_ent(lhandle, &policy_ent);
return(code);
}
} else {
sprintf(msg_ret, "%s\n%s\n",
string_text(CHPASS_UTIL_PASSWORD_NOT_CHANGED),
"Password protocol in krb5.conf is not supported\n");
return (-1);
}
}
/*
* krb5_chpw_result_code_string
*
* convert the return code received from the password server
* to a human-readable string.
*/
const char *
chpw_error_message(kadm5_ret_t result_code)
{
switch (result_code) {
case KRB5_KPASSWD_MALFORMED:
return (dgettext(TEXT_DOMAIN, "Malformed request error"));
case KRB5_KPASSWD_HARDERROR:
return (dgettext(TEXT_DOMAIN, "Server error"));
case KRB5_KPASSWD_AUTHERROR:
return (dgettext(TEXT_DOMAIN, "Authentication error"));
case KRB5_KPASSWD_SOFTERROR:
return (dgettext(TEXT_DOMAIN, "Password change rejected"));
case KRB5_KPASSWD_ACCESSDENIED:
return (dgettext(TEXT_DOMAIN,
"Not authorized to change password"));
case KRB5_KPASSWD_BAD_VERSION:
return (dgettext(TEXT_DOMAIN, "Protocol version unsupported"));
case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
return (dgettext(TEXT_DOMAIN,
"initial flag required in changepw request"));
case KRB5_KPASSWD_POLICY_REJECT:
return (dgettext(TEXT_DOMAIN, "new password fails policy"));
case KRB5_KPASSWD_BAD_PRINCIPAL:
return (dgettext(TEXT_DOMAIN,
"target principal does not exist for "
"changepw request"));
case KRB5_KPASSWD_ETYPE_NOSUPP:
return (dgettext(TEXT_DOMAIN,
"changepw request key sequence has an "
"unsupported Etype"));
default:
return (dgettext(TEXT_DOMAIN, "Password change failed"));
}
}
|