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>
18 #include <sys/types.h>
40 #include <afs/com_err.h>
42 #include <afs/cellconfig.h>
52 /* This code borrowed heavily from the log program. Here is the intro comment
53 * for that program: */
56 log -- tell the Andrew Cache Manager your password
58 modified February 1986
60 Further modified in August 1987 to understand cell IDs.
64 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
67 principal is of the form 'name' or 'name@cell' which provides the
68 cellname. See the -c option below.
69 password is the user's password. This form is NOT recommended for
71 newpassword is the new password. This form is NOT recommended for
73 -c identifies cellname as the cell in which authentication is to take
75 -servers allows the explicit specification of the hosts providing
76 authentication services for the cell being used for authentication.
77 This is a debugging option and will disappear.
80 /* The following code to make use of libcmd.a also stolen from klog.c. */
85 static char **zero_argv;
86 extern int init_child(), give_to_child(), terminate_child();
96 main(argc, argv, envp)
101 struct cmd_syndesc *ts;
106 * The following signal action for AIX is necessary so that in case of a
107 * crash (i.e. core is generated) we can include the user's data section
108 * in the core dump. Unfortunately, by default, only a partial core is
109 * generated which, in many cases, isn't too useful.
111 struct sigaction nsa;
113 sigemptyset(&nsa.sa_mask);
114 nsa.sa_handler = SIG_DFL;
115 nsa.sa_flags = SA_FULLDUMP;
116 sigaction(SIGSEGV, &nsa, NULL);
123 ts = cmd_CreateSyntax(NULL, CommandProc, 0, "change user's password");
128 #define aNEWPASSWORD 3
133 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
134 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
135 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
136 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL,
137 "user's new password");
138 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
139 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
140 "explicit list of servers");
141 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
143 code = cmd_Dispatch(argc, argv);
149 getpipepass(gpbuf, len)
153 /* read a password from stdin, stop on \n or eof */
155 memset(gpbuf, 0, len);
156 for (i = 0; i < len; i++) {
158 if (tc == '\n' || tc == EOF)
166 read_pass(passwd, len, prompt, verify)
173 code = read_pw_string(passwd, len, prompt, verify);
175 getpipepass(passwd, len);
182 password_ok(newpw, insist)
187 /* see if it is reasonable, but don't get so obnoxious */
188 /* FIXME: null pointer derefence!!! */
189 (*insist)++; /* so we don't get called again */
190 if (strlen(newpw) < 6)
193 return 1; /* lie about it */
196 static char rn[] = "kpasswd"; /* Routine name */
197 static int Pipe = 0; /* reading from a pipe */
204 fprintf(stderr, "%s: timed out\n", rn);
209 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
210 CommandProc(as, arock)
212 struct cmd_syndesc *as;
214 char name[MAXKTCNAMELEN] = "";
215 char instance[MAXKTCNAMELEN] = "";
216 char cell[MAXKTCREALMLEN] = "";
217 char realm[MAXKTCREALMLEN] = "";
218 afs_int32 serverList[MAXSERVERS];
219 char *lcell; /* local cellname */
223 struct ubik_client *conn = 0;
224 struct ktc_encryptionKey key;
225 struct ktc_encryptionKey mitkey;
226 struct ktc_encryptionKey newkey;
227 struct ktc_encryptionKey newmitkey;
229 struct ktc_token token;
232 struct passwd *pw = &pwent;
234 int insist; /* insist on good password quality */
235 int lexplicit = 0; /* servers specified explicitly */
236 int local; /* explicit cell is same a local cell */
237 int foundPassword = 0; /*Not yet, anyway */
238 int foundNewPassword = 0; /*Not yet, anyway */
239 int foundExplicitCell = 0; /*Not yet, anyway */
240 #ifdef DEFAULT_MITV4_STRINGTOKEY
242 #elif DEFAULT_AFS_STRINGTOKEY
248 /* blow away command line arguments */
249 for (i = 1; i < zero_argc; i++)
250 memset(zero_argv[i], 0, strlen(zero_argv[i]));
253 /* first determine quiet flag based on -pipe switch */
254 Pipe = (as->parms[aPIPE].items ? 1 : 0);
257 signal(SIGALRM, timedout);
262 if (code || !(lcell = ka_LocalCell())) {
263 #ifndef AFS_FREELANCE_CLIENT
265 com_err(rn, code, "Can't get local cell name!");
273 com_err(rn, code, "Failed to initialize Rx");
277 strcpy(instance, "");
279 /* Parse our arguments. */
281 if (as->parms[aCELL].items) {
283 * cell name explicitly mentioned; take it in if no other cell name
284 * has already been specified and if the name actually appears. If
285 * the given cell name differs from our own, we don't do a lookup.
287 foundExplicitCell = 1;
288 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
291 if (as->parms[aSERVERS].items) {
292 /* explicit server list */
295 char *ap[MAXSERVERS + 2];
297 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
301 code = ubik_ParseClientList(i, ap, serverList);
304 com_err(rn, code, "could not parse server list");
310 if (as->parms[aPRINCIPAL].items) {
311 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
313 if (strlen(instance) > 0)
316 "Non-null instance (%s) may cause strange behavior.\n",
318 if (strlen(cell) > 0) {
319 if (foundExplicitCell) {
322 "%s: May not specify an explicit cell twice.\n",
326 foundExplicitCell = 1;
327 strncpy(realm, cell, sizeof(realm));
331 /* No explicit name provided: use Unix uid. */
334 if (GetUserName(userName, &userNameLen) == 0) {
337 "Can't figure out your name in local cell %s from your user id.\n",
339 fprintf(stderr, "Try providing the user name.\n");
343 pw->pw_name = userName;
345 pw = getpwuid(getuid());
349 "Can't figure out your name in local cell %s from your user id.\n",
351 fprintf(stderr, "Try providing the user name.\n");
358 if (as->parms[aPASSWORD].items) {
360 * Current argument is the desired password string. Remember it in
361 * our local buffer, and zero out the argument string - anyone can
362 * see it there with ps!
365 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
366 memset(as->parms[aPASSWORD].items->data, 0,
367 strlen(as->parms[aPASSWORD].items->data));
370 if (as->parms[aNEWPASSWORD].items) {
372 * Current argument is the new password string. Remember it in
373 * our local buffer, and zero out the argument string - anyone can
374 * see it there with ps!
376 foundNewPassword = 1;
377 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
379 memset(as->parms[aNEWPASSWORD].items->data, 0,
380 strlen(as->parms[aNEWPASSWORD].items->data));
382 #ifdef AFS_FREELANCE_CLIENT
383 if (!foundExplicitCell && !lcell) {
385 com_err(rn, code, "no cell name provided");
389 if (!foundExplicitCell)
390 strcpy(realm, lcell);
391 #endif /* freelance */
393 if (code = ka_CellToRealm(realm, realm, &local)) {
395 com_err(rn, code, "Can't convert cell to realm");
398 lcstring(cell, realm, sizeof(cell));
400 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
401 printf(" in cell '%s'.\n", cell);
403 /* Get the password if it wasn't provided. */
404 if (!foundPassword) {
406 getpipepass(passwd, sizeof(passwd));
408 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
409 if (code || (strlen(passwd) == 0)) {
412 memset(&mitkey, 0, sizeof(mitkey));
413 memset(&key, 0, sizeof(key));
414 memset(passwd, 0, sizeof(passwd));
416 com_err(rn, code, "reading password");
421 ka_StringToKey(passwd, realm, &key);
422 des_string_to_key(passwd, &mitkey);
423 give_to_child(passwd);
425 /* Get new password if it wasn't provided. */
427 if (!foundNewPassword) {
429 getpipepass(npasswd, sizeof(npasswd));
433 read_pass(npasswd, sizeof(npasswd),
434 "New password (RETURN to abort): ", 0);
435 if (code || (strlen(npasswd) == 0)) {
441 } while (password_bad(npasswd));
444 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
449 if (strcmp(verify, npasswd) != 0) {
450 printf("Mismatch - ");
453 memset(verify, 0, sizeof(verify));
456 if (code = password_bad(npasswd)) { /* assmt here! */
457 goto no_change_no_msg;
460 if (strlen(npasswd) > 8) {
463 "%s: password too long, only the first 8 chars will be used.\n",
466 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
468 ka_StringToKey(npasswd, realm, &newkey);
469 des_string_to_key(npasswd, &newmitkey);
470 memset(npasswd, 0, sizeof(npasswd));
473 ka_ExplicitCell(realm, serverList);
475 /* Get an connection to kaserver's admin service in desired cell. Set the
476 * lifetime above the time uncertainty so that badly skewed clocks are
477 * reported when the ticket is decrypted. Then give us 10 seconds to
478 * actually get our work done if the clocks are skewed by only 14:59.
479 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
480 * interval, namely 20 minutes. */
482 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
485 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
486 &token, /*!new */ 0);
487 if (code == KABADREQUEST) {
489 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
490 ADMIN_LIFETIME, &token, /*!new */ 0);
491 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
492 /* try with only the first 8 characters incase they set their password
493 * with an old style passwd program. */
495 strncpy(pass8, passwd, 8);
497 ka_StringToKey(pass8, realm, &key);
498 memset(pass8, 0, sizeof(pass8));
499 memset(passwd, 0, sizeof(passwd));
500 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
503 /* the folks in testing really *hate* this message */
506 "Warning: only the first 8 characters of your old password were significant.\n");
522 memset(&mitkey, 0, sizeof(mitkey));
523 memset(&key, 0, sizeof(key));
524 if (code == KAUBIKCALL)
525 com_err(rn, code, "(Authentication Server unavailable, try later)");
527 if (code == KABADREQUEST)
528 fprintf(stderr, "%s: Incorrect old password.\n", rn);
530 com_err(rn, code, "so couldn't change password");
533 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
535 com_err(rn, code, "contacting Admin Server");
539 ka_ChangePassword(pw->pw_name, instance, conn, 0,
543 ka_ChangePassword(pw->pw_name, instance, conn, 0,
545 memset(&newkey, 0, sizeof(newkey));
546 memset(&newmitkey, 0, sizeof(newmitkey));
549 reason = (char *)error_message(code);
550 fprintf(stderr, "%s: Password was not changed because %s\n",
553 printf("Password changed.\n\n");
556 memset(&newkey, 0, sizeof(newkey));
557 memset(&newmitkey, 0, sizeof(newmitkey));
559 /* Might need to close down the ubik_Client connection */
561 ubik_ClientDestroy(conn);
568 no_change: /* yuck, yuck, yuck */
570 com_err(rn, code, "getting new password");
572 memset(&key, 0, sizeof(key));
573 memset(npasswd, 0, sizeof(npasswd));
574 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
577 exit(code ? code : 1);