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(), pasword_bad(), 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 (*insist)++; /* so we don't get called again */
189 if (strlen(newpw) < 6)
192 return 1; /* lie about it */
195 static char rn[] = "kpasswd"; /* Routine name */
196 static int Pipe = 0; /* reading from a pipe */
203 fprintf(stderr, "%s: timed out\n", rn);
208 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
209 CommandProc(as, arock)
211 struct cmd_syndesc *as;
213 char name[MAXKTCNAMELEN];
214 char instance[MAXKTCNAMELEN];
215 char cell[MAXKTCREALMLEN];
216 char realm[MAXKTCREALMLEN];
217 afs_int32 serverList[MAXSERVERS];
218 char *lcell; /* local cellname */
222 struct ubik_client *conn = 0;
223 struct ktc_encryptionKey key;
224 struct ktc_encryptionKey mitkey;
225 struct ktc_encryptionKey newkey;
226 struct ktc_encryptionKey newmitkey;
228 struct ktc_token token;
231 struct passwd *pw = &pwent;
233 int insist; /* insist on good password quality */
234 int lexplicit = 0; /* servers specified explicitly */
235 int local; /* explicit cell is same a local cell */
236 int foundPassword = 0; /*Not yet, anyway */
237 int foundNewPassword = 0; /*Not yet, anyway */
238 int foundExplicitCell = 0; /*Not yet, anyway */
239 #ifdef DEFAULT_MITV4_STRINGTOKEY
241 #elif DEFAULT_AFS_STRINGTOKEY
247 /* blow away command line arguments */
248 for (i = 1; i < zero_argc; i++)
249 memset(zero_argv[i], 0, strlen(zero_argv[i]));
252 /* first determine quiet flag based on -pipe switch */
253 Pipe = (as->parms[aPIPE].items ? 1 : 0);
256 signal(SIGALRM, timedout);
261 if (code || !(lcell = ka_LocalCell())) {
262 #ifndef AFS_FREELANCE_CLIENT
264 com_err(rn, code, "Can't get local cell name!");
272 com_err(rn, code, "Failed to initialize Rx");
276 strcpy(instance, "");
278 /* Parse our arguments. */
280 if (as->parms[aCELL].items) {
282 * cell name explicitly mentioned; take it in if no other cell name
283 * has already been specified and if the name actually appears. If
284 * the given cell name differs from our own, we don't do a lookup.
286 foundExplicitCell = 1;
287 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
290 if (as->parms[aSERVERS].items) {
291 /* explicit server list */
294 char *ap[MAXSERVERS + 2];
296 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
300 code = ubik_ParseClientList(i, ap, serverList);
303 com_err(rn, code, "could not parse server list");
309 if (as->parms[aPRINCIPAL].items) {
310 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
312 if (strlen(instance) > 0)
315 "Non-null instance (%s) may cause strange behavior.\n",
317 if (strlen(cell) > 0) {
318 if (foundExplicitCell) {
321 "%s: May not specify an explicit cell twice.\n",
325 foundExplicitCell = 1;
326 strncpy(realm, cell, sizeof(realm));
330 /* No explicit name provided: use Unix uid. */
333 if (GetUserName(userName, &userNameLen) == 0) {
336 "Can't figure out your name in local cell %s from your user id.\n",
338 fprintf(stderr, "Try providing the user name.\n");
342 pw->pw_name = userName;
344 pw = getpwuid(getuid());
348 "Can't figure out your name in local cell %s from your user id.\n",
350 fprintf(stderr, "Try providing the user name.\n");
357 if (as->parms[aPASSWORD].items) {
359 * Current argument is the desired password string. Remember it in
360 * our local buffer, and zero out the argument string - anyone can
361 * see it there with ps!
364 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
365 memset(as->parms[aPASSWORD].items->data, 0,
366 strlen(as->parms[aPASSWORD].items->data));
369 if (as->parms[aNEWPASSWORD].items) {
371 * Current argument is the new password string. Remember it in
372 * our local buffer, and zero out the argument string - anyone can
373 * see it there with ps!
375 foundNewPassword = 1;
376 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
378 memset(as->parms[aNEWPASSWORD].items->data, 0,
379 strlen(as->parms[aNEWPASSWORD].items->data));
381 #ifdef AFS_FREELANCE_CLIENT
382 if (!foundExplicitCell && !lcell) {
384 com_err(rn, code, "no cell name provided");
388 if (!foundExplicitCell)
389 strcpy(realm, lcell);
390 #endif /* freelance */
392 if (code = ka_CellToRealm(realm, realm, &local)) {
394 com_err(rn, code, "Can't convert cell to realm");
397 lcstring(cell, realm, sizeof(cell));
399 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
400 printf(" in cell '%s'.\n", cell);
402 /* Get the password if it wasn't provided. */
403 if (!foundPassword) {
405 getpipepass(passwd, sizeof(passwd));
407 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
408 if (code || (strlen(passwd) == 0)) {
411 memset(&mitkey, 0, sizeof(mitkey));
412 memset(&key, 0, sizeof(key));
413 memset(passwd, 0, sizeof(passwd));
415 com_err(rn, code, "reading password");
420 ka_StringToKey(passwd, realm, &key);
421 des_string_to_key(passwd, &mitkey);
422 give_to_child(passwd);
424 /* Get new password if it wasn't provided. */
426 if (!foundNewPassword) {
428 getpipepass(npasswd, sizeof(npasswd));
432 read_pass(npasswd, sizeof(npasswd),
433 "New password (RETURN to abort): ", 0);
434 if (code || (strlen(npasswd) == 0)) {
440 } while (password_bad(npasswd));
443 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
448 if (strcmp(verify, npasswd) != 0) {
449 printf("Mismatch - ");
452 memset(verify, 0, sizeof(verify));
455 if (code = password_bad(npasswd)) { /* assmt here! */
456 goto no_change_no_msg;
459 if (strlen(npasswd) > 8) {
462 "%s: password too long, only the first 8 chars will be used.\n",
465 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
467 ka_StringToKey(npasswd, realm, &newkey);
468 des_string_to_key(npasswd, &newmitkey);
469 memset(npasswd, 0, sizeof(npasswd));
472 ka_ExplicitCell(realm, serverList);
474 /* Get an connection to kaserver's admin service in desired cell. Set the
475 * lifetime above the time uncertainty so that badly skewed clocks are
476 * reported when the ticket is decrypted. Then give us 10 seconds to
477 * actually get our work done if the clocks are skewed by only 14:59.
478 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
479 * interval, namely 20 minutes. */
481 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
484 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
485 &token, /*!new */ 0);
486 if (code == KABADREQUEST) {
488 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
489 ADMIN_LIFETIME, &token, /*!new */ 0);
490 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
491 /* try with only the first 8 characters incase they set their password
492 * with an old style passwd program. */
494 strncpy(pass8, passwd, 8);
496 ka_StringToKey(pass8, realm, &key);
497 memset(pass8, 0, sizeof(pass8));
498 memset(passwd, 0, sizeof(passwd));
499 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
502 /* the folks in testing really *hate* this message */
505 "Warning: only the first 8 characters of your old password were significant.\n");
521 memset(&mitkey, 0, sizeof(mitkey));
522 memset(&key, 0, sizeof(key));
523 if (code == KAUBIKCALL)
524 com_err(rn, code, "(Authentication Server unavailable, try later)");
526 if (code == KABADREQUEST)
527 fprintf(stderr, "%s: Incorrect old password.\n", rn);
529 com_err(rn, code, "so couldn't change password");
532 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
534 com_err(rn, code, "contacting Admin Server");
538 ka_ChangePassword(pw->pw_name, instance, conn, 0,
542 ka_ChangePassword(pw->pw_name, instance, conn, 0,
544 memset(&newkey, 0, sizeof(newkey));
545 memset(&newmitkey, 0, sizeof(newmitkey));
548 reason = (char *)error_message(code);
549 fprintf(stderr, "%s: Password was not changed because %s\n",
552 printf("Password changed.\n\n");
555 memset(&newkey, 0, sizeof(newkey));
556 memset(&newmitkey, 0, sizeof(newmitkey));
558 /* Might need to close down the ubik_Client connection */
560 ubik_ClientDestroy(conn);
567 no_change: /* yuck, yuck, yuck */
569 com_err(rn, code, "getting new password");
571 memset(&key, 0, sizeof(key));
572 memset(npasswd, 0, sizeof(npasswd));
573 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
576 exit(code ? code : 1);