2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* These two needed for rxgen output to work */
11 #include <afsconfig.h>
12 #include <afs/param.h>
20 #include <hcrypto/des.h>
21 #include <hcrypto/ui.h>
24 #include <rx/rxkad_convert.h>
27 #include <afs/com_err.h>
29 #include <afs/cellconfig.h>
38 /* This code borrowed heavily from the log program. Here is the intro comment
39 * for that program: */
42 log -- tell the Andrew Cache Manager your password
44 modified February 1986
46 Further modified in August 1987 to understand cell IDs.
50 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
53 principal is of the form 'name' or 'name@cell' which provides the
54 cellname. See the -c option below.
55 password is the user's password. This form is NOT recommended for
57 newpassword is the new password. This form is NOT recommended for
59 -c identifies cellname as the cell in which authentication is to take
61 -servers allows the explicit specification of the hosts providing
62 authentication services for the cell being used for authentication.
63 This is a debugging option and will disappear.
66 /* The following code to make use of libcmd.a also stolen from klog.c. */
68 int CommandProc(struct cmd_syndesc *, void *);
71 static char **zero_argv;
82 main(int argc, char *argv[], char **envp)
84 struct cmd_syndesc *ts;
89 * The following signal action for AIX is necessary so that in case of a
90 * crash (i.e. core is generated) we can include the user's data section
91 * in the core dump. Unfortunately, by default, only a partial core is
92 * generated which, in many cases, isn't too useful.
96 sigemptyset(&nsa.sa_mask);
97 nsa.sa_handler = SIG_DFL;
98 nsa.sa_flags = SA_FULLDUMP;
99 sigaction(SIGSEGV, &nsa, NULL);
106 ts = cmd_CreateSyntax(NULL, CommandProc, 0, "change user's password");
111 #define aNEWPASSWORD 3
116 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
117 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
118 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
119 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL,
120 "user's new password");
121 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
122 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
123 "explicit list of servers");
124 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
126 code = cmd_Dispatch(argc, argv);
132 getpipepass(char *gpbuf, int len)
134 /* read a password from stdin, stop on \n or eof */
136 memset(gpbuf, 0, len);
137 for (i = 0; i < len; i++) {
139 if (tc == '\n' || tc == EOF)
147 read_pass(char *passwd, int len, char *prompt, int verify)
150 code = UI_UTIL_read_pw_string(passwd, len, prompt, verify);
152 getpipepass(passwd, len);
160 password_ok(char *newpw, int *insist)
163 /* see if it is reasonable, but don't get so obnoxious */
164 /* FIXME: null pointer derefence!!! */
165 (*insist)++; /* so we don't get called again */
166 if (strlen(newpw) < 6)
169 return 1; /* lie about it */
173 static char rn[] = "kpasswd"; /* Routine name */
174 static int Pipe = 0; /* reading from a pipe */
181 fprintf(stderr, "%s: timed out\n", rn);
186 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
189 CommandProc(struct cmd_syndesc *as, void *arock)
191 char name[MAXKTCNAMELEN] = "";
192 char instance[MAXKTCNAMELEN] = "";
193 char cell[MAXKTCREALMLEN] = "";
194 char realm[MAXKTCREALMLEN] = "";
195 afs_uint32 serverList[MAXSERVERS];
196 char *lcell; /* local cellname */
200 struct ubik_client *conn = 0;
201 struct ktc_encryptionKey key;
202 struct ktc_encryptionKey mitkey;
203 struct ktc_encryptionKey newkey;
204 struct ktc_encryptionKey newmitkey;
206 struct ktc_token token;
209 struct passwd *pw = &pwent;
211 int lexplicit = 0; /* servers specified explicitly */
212 int local; /* explicit cell is same a local cell */
213 int foundPassword = 0; /*Not yet, anyway */
214 int foundNewPassword = 0; /*Not yet, anyway */
215 int foundExplicitCell = 0; /*Not yet, anyway */
216 #ifdef DEFAULT_MITV4_STRINGTOKEY
218 #elif DEFAULT_AFS_STRINGTOKEY
224 /* blow away command line arguments */
225 for (i = 1; i < zero_argc; i++)
226 memset(zero_argv[i], 0, strlen(zero_argv[i]));
229 /* first determine quiet flag based on -pipe switch */
230 Pipe = (as->parms[aPIPE].items ? 1 : 0);
233 signal(SIGALRM, timedout);
238 if (code || !(lcell = ka_LocalCell())) {
239 #ifndef AFS_FREELANCE_CLIENT
241 afs_com_err(rn, code, "Can't get local cell name!");
249 afs_com_err(rn, code, "Failed to initialize Rx");
253 strcpy(instance, "");
255 /* Parse our arguments. */
257 if (as->parms[aCELL].items) {
259 * cell name explicitly mentioned; take it in if no other cell name
260 * has already been specified and if the name actually appears. If
261 * the given cell name differs from our own, we don't do a lookup.
263 foundExplicitCell = 1;
264 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
267 if (as->parms[aSERVERS].items) {
268 /* explicit server list */
271 char *ap[MAXSERVERS + 2];
273 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
277 code = ubik_ParseClientList(i, ap, serverList);
280 afs_com_err(rn, code, "could not parse server list");
286 if (as->parms[aPRINCIPAL].items) {
287 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
289 if (strlen(instance) > 0)
292 "Non-null instance (%s) may cause strange behavior.\n",
294 if (strlen(cell) > 0) {
295 if (foundExplicitCell) {
298 "%s: May not specify an explicit cell twice.\n",
302 foundExplicitCell = 1;
303 strncpy(realm, cell, sizeof(realm));
307 /* No explicit name provided: use Unix uid. */
310 if (GetUserName(userName, &userNameLen) == 0) {
313 "Can't figure out your name in local cell %s from your user id.\n",
315 fprintf(stderr, "Try providing the user name.\n");
319 pw->pw_name = userName;
321 pw = getpwuid(getuid());
325 "Can't figure out your name in local cell %s from your user id.\n",
327 fprintf(stderr, "Try providing the user name.\n");
334 if (as->parms[aPASSWORD].items) {
336 * Current argument is the desired password string. Remember it in
337 * our local buffer, and zero out the argument string - anyone can
338 * see it there with ps!
341 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
342 memset(as->parms[aPASSWORD].items->data, 0,
343 strlen(as->parms[aPASSWORD].items->data));
346 if (as->parms[aNEWPASSWORD].items) {
348 * Current argument is the new password string. Remember it in
349 * our local buffer, and zero out the argument string - anyone can
350 * see it there with ps!
352 foundNewPassword = 1;
353 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
355 memset(as->parms[aNEWPASSWORD].items->data, 0,
356 strlen(as->parms[aNEWPASSWORD].items->data));
358 #ifdef AFS_FREELANCE_CLIENT
359 if (!foundExplicitCell && !lcell) {
361 afs_com_err(rn, code, "no cell name provided");
365 if (!foundExplicitCell)
366 strcpy(realm, lcell);
367 #endif /* freelance */
369 if ((code = ka_CellToRealm(realm, realm, &local))) {
371 afs_com_err(rn, code, "Can't convert cell to realm");
374 lcstring(cell, realm, sizeof(cell));
376 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
377 printf(" in cell '%s'.\n", cell);
379 /* Get the password if it wasn't provided. */
380 if (!foundPassword) {
382 getpipepass(passwd, sizeof(passwd));
384 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
385 if (code || (strlen(passwd) == 0)) {
388 memset(&mitkey, 0, sizeof(mitkey));
389 memset(&key, 0, sizeof(key));
390 memset(passwd, 0, sizeof(passwd));
392 afs_com_err(rn, code, "reading password");
397 ka_StringToKey(passwd, realm, &key);
398 DES_string_to_key(passwd, ktc_to_cblockptr(&mitkey));
399 give_to_child(passwd);
401 /* Get new password if it wasn't provided. */
402 if (!foundNewPassword) {
404 getpipepass(npasswd, sizeof(npasswd));
408 read_pass(npasswd, sizeof(npasswd),
409 "New password (RETURN to abort): ", 0);
410 if (code || (strlen(npasswd) == 0)) {
416 } while (password_bad(npasswd));
419 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
424 if (strcmp(verify, npasswd) != 0) {
425 printf("Mismatch - ");
428 memset(verify, 0, sizeof(verify));
431 if ((code = password_bad(npasswd))) { /* assmt here! */
432 goto no_change_no_msg;
435 if (strlen(npasswd) > 8) {
438 "%s: password too long, only the first 8 chars will be used.\n",
441 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
443 ka_StringToKey(npasswd, realm, &newkey);
444 DES_string_to_key(npasswd, ktc_to_cblockptr(&newmitkey));
445 memset(npasswd, 0, sizeof(npasswd));
448 ka_ExplicitCell(realm, serverList);
450 /* Get an connection to kaserver's admin service in desired cell. Set the
451 * lifetime above the time uncertainty so that badly skewed clocks are
452 * reported when the ticket is decrypted. Then give us 10 seconds to
453 * actually get our work done if the clocks are skewed by only 14:59.
454 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
455 * interval, namely 20 minutes. */
457 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
460 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
461 &token, /*!new */ 0);
462 if (code == KABADREQUEST) {
464 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
465 ADMIN_LIFETIME, &token, /*!new */ 0);
466 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
467 /* try with only the first 8 characters incase they set their password
468 * with an old style passwd program. */
470 strncpy(pass8, passwd, 8);
472 ka_StringToKey(pass8, realm, &key);
473 memset(pass8, 0, sizeof(pass8));
474 memset(passwd, 0, sizeof(passwd));
475 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
478 /* the folks in testing really *hate* this message */
481 "Warning: only the first 8 characters of your old password were significant.\n");
497 memset(&mitkey, 0, sizeof(mitkey));
498 memset(&key, 0, sizeof(key));
499 if (code == KAUBIKCALL)
500 afs_com_err(rn, code, "(Authentication Server unavailable, try later)");
502 if (code == KABADREQUEST)
503 fprintf(stderr, "%s: Incorrect old password.\n", rn);
505 afs_com_err(rn, code, "so couldn't change password");
508 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
510 afs_com_err(rn, code, "contacting Admin Server");
514 ka_ChangePassword(pw->pw_name, instance, conn, 0,
518 ka_ChangePassword(pw->pw_name, instance, conn, 0,
520 memset(&newkey, 0, sizeof(newkey));
521 memset(&newmitkey, 0, sizeof(newmitkey));
524 reason = (char *)afs_error_message(code);
525 fprintf(stderr, "%s: Password was not changed because %s\n",
528 printf("Password changed.\n\n");
531 memset(&newkey, 0, sizeof(newkey));
532 memset(&newmitkey, 0, sizeof(newmitkey));
534 /* Might need to close down the ubik_Client connection */
536 ubik_ClientDestroy(conn);
543 no_change: /* yuck, yuck, yuck */
545 afs_com_err(rn, code, "getting new password");
547 memset(&key, 0, sizeof(key));
548 memset(npasswd, 0, sizeof(npasswd));
549 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
552 exit(code ? code : 1);