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>
34 #include <afs/com_err.h>
36 #include <afs/cellconfig.h>
46 /* This code borrowed heavily from the log program. Here is the intro comment
47 * for that program: */
50 log -- tell the Andrew Cache Manager your password
52 modified February 1986
54 Further modified in August 1987 to understand cell IDs.
58 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
61 principal is of the form 'name' or 'name@cell' which provides the
62 cellname. See the -c option below.
63 password is the user's password. This form is NOT recommended for
65 newpassword is the new password. This form is NOT recommended for
67 -c identifies cellname as the cell in which authentication is to take
69 -servers allows the explicit specification of the hosts providing
70 authentication services for the cell being used for authentication.
71 This is a debugging option and will disappear.
74 /* The following code to make use of libcmd.a also stolen from klog.c. */
79 static char **zero_argv;
80 extern int init_child(), give_to_child(), terminate_child();
90 main(argc, argv, envp)
95 struct cmd_syndesc *ts;
100 * The following signal action for AIX is necessary so that in case of a
101 * crash (i.e. core is generated) we can include the user's data section
102 * in the core dump. Unfortunately, by default, only a partial core is
103 * generated which, in many cases, isn't too useful.
105 struct sigaction nsa;
107 sigemptyset(&nsa.sa_mask);
108 nsa.sa_handler = SIG_DFL;
109 nsa.sa_flags = SA_FULLDUMP;
110 sigaction(SIGSEGV, &nsa, NULL);
117 ts = cmd_CreateSyntax(NULL, CommandProc, 0, "change user's password");
122 #define aNEWPASSWORD 3
127 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
128 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
129 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
130 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL,
131 "user's new password");
132 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
133 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
134 "explicit list of servers");
135 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
137 code = cmd_Dispatch(argc, argv);
143 getpipepass(gpbuf, len)
147 /* read a password from stdin, stop on \n or eof */
149 memset(gpbuf, 0, len);
150 for (i = 0; i < len; i++) {
152 if (tc == '\n' || tc == EOF)
160 read_pass(passwd, len, prompt, verify)
167 code = read_pw_string(passwd, len, prompt, verify);
169 getpipepass(passwd, len);
176 password_ok(newpw, insist)
181 /* see if it is reasonable, but don't get so obnoxious */
182 /* FIXME: null pointer derefence!!! */
183 (*insist)++; /* so we don't get called again */
184 if (strlen(newpw) < 6)
187 return 1; /* lie about it */
190 static char rn[] = "kpasswd"; /* Routine name */
191 static int Pipe = 0; /* reading from a pipe */
198 fprintf(stderr, "%s: timed out\n", rn);
203 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
204 CommandProc(as, arock)
206 struct cmd_syndesc *as;
208 char name[MAXKTCNAMELEN] = "";
209 char instance[MAXKTCNAMELEN] = "";
210 char cell[MAXKTCREALMLEN] = "";
211 char realm[MAXKTCREALMLEN] = "";
212 afs_int32 serverList[MAXSERVERS];
213 char *lcell; /* local cellname */
217 struct ubik_client *conn = 0;
218 struct ktc_encryptionKey key;
219 struct ktc_encryptionKey mitkey;
220 struct ktc_encryptionKey newkey;
221 struct ktc_encryptionKey newmitkey;
223 struct ktc_token token;
226 struct passwd *pw = &pwent;
228 int insist; /* insist on good password quality */
229 int lexplicit = 0; /* servers specified explicitly */
230 int local; /* explicit cell is same a local cell */
231 int foundPassword = 0; /*Not yet, anyway */
232 int foundNewPassword = 0; /*Not yet, anyway */
233 int foundExplicitCell = 0; /*Not yet, anyway */
234 #ifdef DEFAULT_MITV4_STRINGTOKEY
236 #elif DEFAULT_AFS_STRINGTOKEY
242 /* blow away command line arguments */
243 for (i = 1; i < zero_argc; i++)
244 memset(zero_argv[i], 0, strlen(zero_argv[i]));
247 /* first determine quiet flag based on -pipe switch */
248 Pipe = (as->parms[aPIPE].items ? 1 : 0);
251 signal(SIGALRM, timedout);
256 if (code || !(lcell = ka_LocalCell())) {
257 #ifndef AFS_FREELANCE_CLIENT
259 afs_com_err(rn, code, "Can't get local cell name!");
267 afs_com_err(rn, code, "Failed to initialize Rx");
271 strcpy(instance, "");
273 /* Parse our arguments. */
275 if (as->parms[aCELL].items) {
277 * cell name explicitly mentioned; take it in if no other cell name
278 * has already been specified and if the name actually appears. If
279 * the given cell name differs from our own, we don't do a lookup.
281 foundExplicitCell = 1;
282 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
285 if (as->parms[aSERVERS].items) {
286 /* explicit server list */
289 char *ap[MAXSERVERS + 2];
291 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
295 code = ubik_ParseClientList(i, ap, serverList);
298 afs_com_err(rn, code, "could not parse server list");
304 if (as->parms[aPRINCIPAL].items) {
305 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
307 if (strlen(instance) > 0)
310 "Non-null instance (%s) may cause strange behavior.\n",
312 if (strlen(cell) > 0) {
313 if (foundExplicitCell) {
316 "%s: May not specify an explicit cell twice.\n",
320 foundExplicitCell = 1;
321 strncpy(realm, cell, sizeof(realm));
325 /* No explicit name provided: use Unix uid. */
328 if (GetUserName(userName, &userNameLen) == 0) {
331 "Can't figure out your name in local cell %s from your user id.\n",
333 fprintf(stderr, "Try providing the user name.\n");
337 pw->pw_name = userName;
339 pw = getpwuid(getuid());
343 "Can't figure out your name in local cell %s from your user id.\n",
345 fprintf(stderr, "Try providing the user name.\n");
352 if (as->parms[aPASSWORD].items) {
354 * Current argument is the desired password string. Remember it in
355 * our local buffer, and zero out the argument string - anyone can
356 * see it there with ps!
359 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
360 memset(as->parms[aPASSWORD].items->data, 0,
361 strlen(as->parms[aPASSWORD].items->data));
364 if (as->parms[aNEWPASSWORD].items) {
366 * Current argument is the new password string. Remember it in
367 * our local buffer, and zero out the argument string - anyone can
368 * see it there with ps!
370 foundNewPassword = 1;
371 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
373 memset(as->parms[aNEWPASSWORD].items->data, 0,
374 strlen(as->parms[aNEWPASSWORD].items->data));
376 #ifdef AFS_FREELANCE_CLIENT
377 if (!foundExplicitCell && !lcell) {
379 afs_com_err(rn, code, "no cell name provided");
383 if (!foundExplicitCell)
384 strcpy(realm, lcell);
385 #endif /* freelance */
387 if (code = ka_CellToRealm(realm, realm, &local)) {
389 afs_com_err(rn, code, "Can't convert cell to realm");
392 lcstring(cell, realm, sizeof(cell));
394 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
395 printf(" in cell '%s'.\n", cell);
397 /* Get the password if it wasn't provided. */
398 if (!foundPassword) {
400 getpipepass(passwd, sizeof(passwd));
402 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
403 if (code || (strlen(passwd) == 0)) {
406 memset(&mitkey, 0, sizeof(mitkey));
407 memset(&key, 0, sizeof(key));
408 memset(passwd, 0, sizeof(passwd));
410 afs_com_err(rn, code, "reading password");
415 ka_StringToKey(passwd, realm, &key);
416 des_string_to_key(passwd, &mitkey);
417 give_to_child(passwd);
419 /* Get new password if it wasn't provided. */
421 if (!foundNewPassword) {
423 getpipepass(npasswd, sizeof(npasswd));
427 read_pass(npasswd, sizeof(npasswd),
428 "New password (RETURN to abort): ", 0);
429 if (code || (strlen(npasswd) == 0)) {
435 } while (password_bad(npasswd));
438 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
443 if (strcmp(verify, npasswd) != 0) {
444 printf("Mismatch - ");
447 memset(verify, 0, sizeof(verify));
450 if (code = password_bad(npasswd)) { /* assmt here! */
451 goto no_change_no_msg;
454 if (strlen(npasswd) > 8) {
457 "%s: password too long, only the first 8 chars will be used.\n",
460 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
462 ka_StringToKey(npasswd, realm, &newkey);
463 des_string_to_key(npasswd, &newmitkey);
464 memset(npasswd, 0, sizeof(npasswd));
467 ka_ExplicitCell(realm, serverList);
469 /* Get an connection to kaserver's admin service in desired cell. Set the
470 * lifetime above the time uncertainty so that badly skewed clocks are
471 * reported when the ticket is decrypted. Then give us 10 seconds to
472 * actually get our work done if the clocks are skewed by only 14:59.
473 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
474 * interval, namely 20 minutes. */
476 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
479 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
480 &token, /*!new */ 0);
481 if (code == KABADREQUEST) {
483 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
484 ADMIN_LIFETIME, &token, /*!new */ 0);
485 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
486 /* try with only the first 8 characters incase they set their password
487 * with an old style passwd program. */
489 strncpy(pass8, passwd, 8);
491 ka_StringToKey(pass8, realm, &key);
492 memset(pass8, 0, sizeof(pass8));
493 memset(passwd, 0, sizeof(passwd));
494 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
497 /* the folks in testing really *hate* this message */
500 "Warning: only the first 8 characters of your old password were significant.\n");
516 memset(&mitkey, 0, sizeof(mitkey));
517 memset(&key, 0, sizeof(key));
518 if (code == KAUBIKCALL)
519 afs_com_err(rn, code, "(Authentication Server unavailable, try later)");
521 if (code == KABADREQUEST)
522 fprintf(stderr, "%s: Incorrect old password.\n", rn);
524 afs_com_err(rn, code, "so couldn't change password");
527 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
529 afs_com_err(rn, code, "contacting Admin Server");
533 ka_ChangePassword(pw->pw_name, instance, conn, 0,
537 ka_ChangePassword(pw->pw_name, instance, conn, 0,
539 memset(&newkey, 0, sizeof(newkey));
540 memset(&newmitkey, 0, sizeof(newmitkey));
543 reason = (char *)afs_error_message(code);
544 fprintf(stderr, "%s: Password was not changed because %s\n",
547 printf("Password changed.\n\n");
550 memset(&newkey, 0, sizeof(newkey));
551 memset(&newmitkey, 0, sizeof(newmitkey));
553 /* Might need to close down the ubik_Client connection */
555 ubik_ClientDestroy(conn);
562 no_change: /* yuck, yuck, yuck */
564 afs_com_err(rn, code, "getting new password");
566 memset(&key, 0, sizeof(key));
567 memset(npasswd, 0, sizeof(npasswd));
568 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
571 exit(code ? code : 1);