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. */
76 int CommandProc(struct cmd_syndesc *, void *);
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(struct cmd_syndesc *as, void *arock)
206 char name[MAXKTCNAMELEN] = "";
207 char instance[MAXKTCNAMELEN] = "";
208 char cell[MAXKTCREALMLEN] = "";
209 char realm[MAXKTCREALMLEN] = "";
210 afs_int32 serverList[MAXSERVERS];
211 char *lcell; /* local cellname */
215 struct ubik_client *conn = 0;
216 struct ktc_encryptionKey key;
217 struct ktc_encryptionKey mitkey;
218 struct ktc_encryptionKey newkey;
219 struct ktc_encryptionKey newmitkey;
221 struct ktc_token token;
224 struct passwd *pw = &pwent;
226 int insist; /* insist on good password quality */
227 int lexplicit = 0; /* servers specified explicitly */
228 int local; /* explicit cell is same a local cell */
229 int foundPassword = 0; /*Not yet, anyway */
230 int foundNewPassword = 0; /*Not yet, anyway */
231 int foundExplicitCell = 0; /*Not yet, anyway */
232 #ifdef DEFAULT_MITV4_STRINGTOKEY
234 #elif DEFAULT_AFS_STRINGTOKEY
240 /* blow away command line arguments */
241 for (i = 1; i < zero_argc; i++)
242 memset(zero_argv[i], 0, strlen(zero_argv[i]));
245 /* first determine quiet flag based on -pipe switch */
246 Pipe = (as->parms[aPIPE].items ? 1 : 0);
249 signal(SIGALRM, timedout);
254 if (code || !(lcell = ka_LocalCell())) {
255 #ifndef AFS_FREELANCE_CLIENT
257 afs_com_err(rn, code, "Can't get local cell name!");
265 afs_com_err(rn, code, "Failed to initialize Rx");
269 strcpy(instance, "");
271 /* Parse our arguments. */
273 if (as->parms[aCELL].items) {
275 * cell name explicitly mentioned; take it in if no other cell name
276 * has already been specified and if the name actually appears. If
277 * the given cell name differs from our own, we don't do a lookup.
279 foundExplicitCell = 1;
280 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
283 if (as->parms[aSERVERS].items) {
284 /* explicit server list */
287 char *ap[MAXSERVERS + 2];
289 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
293 code = ubik_ParseClientList(i, ap, serverList);
296 afs_com_err(rn, code, "could not parse server list");
302 if (as->parms[aPRINCIPAL].items) {
303 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
305 if (strlen(instance) > 0)
308 "Non-null instance (%s) may cause strange behavior.\n",
310 if (strlen(cell) > 0) {
311 if (foundExplicitCell) {
314 "%s: May not specify an explicit cell twice.\n",
318 foundExplicitCell = 1;
319 strncpy(realm, cell, sizeof(realm));
323 /* No explicit name provided: use Unix uid. */
326 if (GetUserName(userName, &userNameLen) == 0) {
329 "Can't figure out your name in local cell %s from your user id.\n",
331 fprintf(stderr, "Try providing the user name.\n");
335 pw->pw_name = userName;
337 pw = getpwuid(getuid());
341 "Can't figure out your name in local cell %s from your user id.\n",
343 fprintf(stderr, "Try providing the user name.\n");
350 if (as->parms[aPASSWORD].items) {
352 * Current argument is the desired password string. Remember it in
353 * our local buffer, and zero out the argument string - anyone can
354 * see it there with ps!
357 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
358 memset(as->parms[aPASSWORD].items->data, 0,
359 strlen(as->parms[aPASSWORD].items->data));
362 if (as->parms[aNEWPASSWORD].items) {
364 * Current argument is the new password string. Remember it in
365 * our local buffer, and zero out the argument string - anyone can
366 * see it there with ps!
368 foundNewPassword = 1;
369 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
371 memset(as->parms[aNEWPASSWORD].items->data, 0,
372 strlen(as->parms[aNEWPASSWORD].items->data));
374 #ifdef AFS_FREELANCE_CLIENT
375 if (!foundExplicitCell && !lcell) {
377 afs_com_err(rn, code, "no cell name provided");
381 if (!foundExplicitCell)
382 strcpy(realm, lcell);
383 #endif /* freelance */
385 if (code = ka_CellToRealm(realm, realm, &local)) {
387 afs_com_err(rn, code, "Can't convert cell to realm");
390 lcstring(cell, realm, sizeof(cell));
392 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
393 printf(" in cell '%s'.\n", cell);
395 /* Get the password if it wasn't provided. */
396 if (!foundPassword) {
398 getpipepass(passwd, sizeof(passwd));
400 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
401 if (code || (strlen(passwd) == 0)) {
404 memset(&mitkey, 0, sizeof(mitkey));
405 memset(&key, 0, sizeof(key));
406 memset(passwd, 0, sizeof(passwd));
408 afs_com_err(rn, code, "reading password");
413 ka_StringToKey(passwd, realm, &key);
414 des_string_to_key(passwd, &mitkey);
415 give_to_child(passwd);
417 /* Get new password if it wasn't provided. */
419 if (!foundNewPassword) {
421 getpipepass(npasswd, sizeof(npasswd));
425 read_pass(npasswd, sizeof(npasswd),
426 "New password (RETURN to abort): ", 0);
427 if (code || (strlen(npasswd) == 0)) {
433 } while (password_bad(npasswd));
436 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
441 if (strcmp(verify, npasswd) != 0) {
442 printf("Mismatch - ");
445 memset(verify, 0, sizeof(verify));
448 if (code = password_bad(npasswd)) { /* assmt here! */
449 goto no_change_no_msg;
452 if (strlen(npasswd) > 8) {
455 "%s: password too long, only the first 8 chars will be used.\n",
458 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
460 ka_StringToKey(npasswd, realm, &newkey);
461 des_string_to_key(npasswd, &newmitkey);
462 memset(npasswd, 0, sizeof(npasswd));
465 ka_ExplicitCell(realm, serverList);
467 /* Get an connection to kaserver's admin service in desired cell. Set the
468 * lifetime above the time uncertainty so that badly skewed clocks are
469 * reported when the ticket is decrypted. Then give us 10 seconds to
470 * actually get our work done if the clocks are skewed by only 14:59.
471 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
472 * interval, namely 20 minutes. */
474 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
477 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
478 &token, /*!new */ 0);
479 if (code == KABADREQUEST) {
481 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
482 ADMIN_LIFETIME, &token, /*!new */ 0);
483 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
484 /* try with only the first 8 characters incase they set their password
485 * with an old style passwd program. */
487 strncpy(pass8, passwd, 8);
489 ka_StringToKey(pass8, realm, &key);
490 memset(pass8, 0, sizeof(pass8));
491 memset(passwd, 0, sizeof(passwd));
492 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
495 /* the folks in testing really *hate* this message */
498 "Warning: only the first 8 characters of your old password were significant.\n");
514 memset(&mitkey, 0, sizeof(mitkey));
515 memset(&key, 0, sizeof(key));
516 if (code == KAUBIKCALL)
517 afs_com_err(rn, code, "(Authentication Server unavailable, try later)");
519 if (code == KABADREQUEST)
520 fprintf(stderr, "%s: Incorrect old password.\n", rn);
522 afs_com_err(rn, code, "so couldn't change password");
525 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
527 afs_com_err(rn, code, "contacting Admin Server");
531 ka_ChangePassword(pw->pw_name, instance, conn, 0,
535 ka_ChangePassword(pw->pw_name, instance, conn, 0,
537 memset(&newkey, 0, sizeof(newkey));
538 memset(&newmitkey, 0, sizeof(newmitkey));
541 reason = (char *)afs_error_message(code);
542 fprintf(stderr, "%s: Password was not changed because %s\n",
545 printf("Password changed.\n\n");
548 memset(&newkey, 0, sizeof(newkey));
549 memset(&newmitkey, 0, sizeof(newmitkey));
551 /* Might need to close down the ubik_Client connection */
553 ubik_ClientDestroy(conn);
560 no_change: /* yuck, yuck, yuck */
562 afs_com_err(rn, code, "getting new password");
564 memset(&key, 0, sizeof(key));
565 memset(npasswd, 0, sizeof(npasswd));
566 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
569 exit(code ? code : 1);