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>
17 #include <sys/types.h>
32 #include <afs/com_err.h>
34 #include <afs/cellconfig.h>
44 /* This code borrowed heavily from the log program. Here is the intro comment
45 * for that program: */
48 log -- tell the Andrew Cache Manager your password
50 modified February 1986
52 Further modified in August 1987 to understand cell IDs.
56 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
59 principal is of the form 'name' or 'name@cell' which provides the
60 cellname. See the -c option below.
61 password is the user's password. This form is NOT recommended for
63 newpassword is the new password. This form is NOT recommended for
65 -c identifies cellname as the cell in which authentication is to take
67 -servers allows the explicit specification of the hosts providing
68 authentication services for the cell being used for authentication.
69 This is a debugging option and will disappear.
72 /* The following code to make use of libcmd.a also stolen from klog.c. */
77 static char **zero_argv;
78 extern int init_child(), pasword_bad(), give_to_child(), terminate_child();
88 main (argc, argv, envp)
92 { struct cmd_syndesc *ts;
97 * The following signal action for AIX is necessary so that in case of a
98 * crash (i.e. core is generated) we can include the user's data section
99 * in the core dump. Unfortunately, by default, only a partial core is
100 * generated which, in many cases, isn't too useful.
102 struct sigaction nsa;
104 sigemptyset(&nsa.sa_mask);
105 nsa.sa_handler = SIG_DFL;
106 nsa.sa_flags = SA_FULLDUMP;
107 sigaction(SIGSEGV, &nsa, NULL);
114 ts = cmd_CreateSyntax((char *) 0, CommandProc, 0, "change user's password");
119 #define aNEWPASSWORD 3
124 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
125 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
126 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
127 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL, "user's new password");
128 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
129 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL, "explicit list of servers");
130 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
132 code = cmd_Dispatch(argc, argv);
137 static void getpipepass(gpbuf, len)
141 /* read a password from stdin, stop on \n or eof */
143 memset(gpbuf, 0, len);
146 if (tc == '\n' || tc == EOF) break;
152 static afs_int32 read_pass (passwd, len, prompt, verify)
158 code = read_pw_string (passwd, len, prompt, verify);
160 getpipepass (passwd, len);
166 static int password_ok (newpw, insist)
171 /* see if it is reasonable, but don't get so obnoxious */
172 (*insist)++; /* so we don't get called again */
173 if (strlen(newpw) < 6) return 0;
175 return 1; /* lie about it */
178 static char rn[] = "kpasswd"; /* Routine name */
179 static int Pipe = 0; /* reading from a pipe */
184 if (!Pipe) fprintf (stderr, "%s: timed out\n", rn);
189 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
190 CommandProc (as, arock)
192 struct cmd_syndesc *as;
194 char name[MAXKTCNAMELEN];
195 char instance[MAXKTCNAMELEN];
196 char cell[MAXKTCREALMLEN];
197 char realm[MAXKTCREALMLEN];
198 afs_int32 serverList[MAXSERVERS];
199 char *lcell; /* local cellname */
203 struct ubik_client *conn = 0;
204 struct ktc_encryptionKey key;
205 struct ktc_encryptionKey mitkey;
206 struct ktc_encryptionKey newkey;
207 struct ktc_encryptionKey newmitkey;
209 struct ktc_token token;
212 struct passwd *pw = &pwent;
214 int insist; /* insist on good password quality */
215 int lexplicit=0; /* servers specified explicitly */
216 int local; /* explicit cell is same a local cell */
217 int foundPassword = 0; /*Not yet, anyway*/
218 int foundNewPassword = 0; /*Not yet, anyway*/
219 int foundExplicitCell = 0; /*Not yet, anyway*/
220 #ifdef DEFAULT_MITV4_STRINGTOKEY
222 #elif DEFAULT_AFS_STRINGTOKEY
228 /* blow away command line arguments */
229 for (i=1; i<zero_argc; i++) memset(zero_argv[i], 0, strlen(zero_argv[i]));
232 /* first determine quiet flag based on -pipe switch */
233 Pipe = (as->parms[aPIPE].items ? 1 : 0);
236 signal(SIGALRM, timedout);
242 !(lcell = ka_LocalCell())) {
243 if (!Pipe) com_err (rn, code , "Can't get local cell name!");
249 if (!Pipe) 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);
279 if (!Pipe) com_err (rn, code, "could not parse server list");
285 if (as->parms[aPRINCIPAL].items) {
286 ka_ParseLoginName (as->parms[aPRINCIPAL].items->data,
287 name, instance, cell);
288 if (strlen (instance) > 0)
290 fprintf (stderr, "Non-null instance (%s) may cause strange behavior.\n",
292 if (strlen(cell) > 0) {
293 if (foundExplicitCell) {
295 fprintf (stderr, "%s: May not specify an explicit cell twice.\n", rn);
298 foundExplicitCell = 1;
299 strncpy (realm, cell, sizeof(realm));
303 /* No explicit name provided: use Unix uid. */
306 if (GetUserName(userName, &userNameLen) == 0) {
308 fprintf (stderr, "Can't figure out your name in local cell %s from your user id.\n", lcell);
309 fprintf (stderr, "Try providing the user name.\n");
313 pw->pw_name = userName;
315 pw = getpwuid(getuid());
318 fprintf (stderr, "Can't figure out your name in local cell %s from your user id.\n", lcell);
319 fprintf (stderr, "Try providing the user name.\n");
326 if (as->parms[aPASSWORD].items) {
328 * Current argument is the desired password string. Remember it in
329 * our local buffer, and zero out the argument string - anyone can
330 * see it there with ps!
333 strncpy (passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
334 memset(as->parms[aPASSWORD].items->data, 0, strlen(as->parms[aPASSWORD].items->data));
337 if (as->parms[aNEWPASSWORD].items) {
339 * Current argument is the new password string. Remember it in
340 * our local buffer, and zero out the argument string - anyone can
341 * see it there with ps!
343 foundNewPassword = 1;
344 strncpy (npasswd, as->parms[aNEWPASSWORD].items->data,
346 memset(as->parms[aNEWPASSWORD].items->data, 0, strlen(as->parms[aNEWPASSWORD].items->data));
349 if (!foundExplicitCell) strcpy (realm, lcell);
350 if (code = ka_CellToRealm (realm, realm, &local)) {
351 if (!Pipe) com_err (rn, code, "Can't convert cell to realm");
354 lcstring (cell, realm, sizeof(cell));
356 ka_PrintUserID ("Changing password for '", pw->pw_name, instance, "'");
357 printf (" in cell '%s'.\n", cell);
359 /* Get the password if it wasn't provided. */
360 if (!foundPassword) {
361 if (Pipe) getpipepass (passwd, sizeof(passwd));
363 code = read_pass (passwd, sizeof(passwd), "Old password: ", 0);
364 if (code || (strlen (passwd) == 0)) {
365 if (code) code = KAREADPW;
366 memset(&mitkey, 0, sizeof(mitkey));
367 memset(&key, 0, sizeof(key));
368 memset(passwd, 0, sizeof(passwd));
369 if (code) com_err (rn, code, "reading password");
374 ka_StringToKey (passwd, realm, &key);
375 des_string_to_key(passwd, &mitkey);
376 give_to_child(passwd);
378 /* Get new password if it wasn't provided. */
380 if (!foundNewPassword) {
382 getpipepass (npasswd, sizeof(npasswd));
385 code = read_pass (npasswd, sizeof(npasswd),
386 "New password (RETURN to abort): ", 0);
387 if (code || (strlen (npasswd) == 0)) {
393 } while (password_bad (npasswd));
395 code = read_pass (verify, sizeof(verify),
396 "Retype new password: ", 0);
401 if (strcmp (verify, npasswd) != 0) {
402 printf ("Mismatch - ");
405 memset(verify, 0, sizeof(verify));
408 if (code = password_bad (npasswd)) { /* assmt here! */
409 goto no_change_no_msg;
413 if (strlen(npasswd) > 8) {
416 "%s: password too long, only the first 8 chars will be used.\n",
420 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
422 ka_StringToKey (npasswd, realm, &newkey);
423 des_string_to_key(npasswd, &newmitkey);
424 memset(npasswd, 0, sizeof(npasswd));
426 if (lexplicit) ka_ExplicitCell (realm, serverList);
428 /* Get an connection to kaserver's admin service in desired cell. Set the
429 * lifetime above the time uncertainty so that badly skewed clocks are
430 * reported when the ticket is decrypted. Then give us 10 seconds to
431 * actually get our work done if the clocks are skewed by only 14:59.
432 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
433 * interval, namely 20 minutes. */
435 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
437 code = ka_GetAdminToken (pw->pw_name, instance, realm,
438 &key, ADMIN_LIFETIME, &token, /*!new*/0);
439 if (code == KABADREQUEST) {
440 code = ka_GetAdminToken (pw->pw_name, instance, realm,
441 &mitkey, ADMIN_LIFETIME, &token, /*!new*/0);
442 if ((code == KABADREQUEST) && (strlen (passwd) > 8)) {
443 /* try with only the first 8 characters incase they set their password
444 * with an old style passwd program. */
446 strncpy (pass8, passwd, 8);
448 ka_StringToKey (pass8, realm, &key);
449 memset(pass8, 0, sizeof(pass8));
450 memset(passwd, 0, sizeof(passwd));
451 code = ka_GetAdminToken (pw->pw_name, instance, realm,
452 &key, ADMIN_LIFETIME, &token, /*!new*/0);
454 /* the folks in testing really *hate* this message */
456 fprintf (stderr, "Warning: only the first 8 characters of your old password were significant.\n");
472 memset(&mitkey, 0, sizeof(mitkey));
473 memset(&key, 0, sizeof(key));
474 if (code == KAUBIKCALL) com_err (rn, code, "(Authentication Server unavailable, try later)");
476 if (code == KABADREQUEST)
477 fprintf(stderr, "%s: Incorrect old password.\n", rn);
479 com_err (rn, code, "so couldn't change password");
482 code = ka_AuthServerConn (realm, KA_MAINTENANCE_SERVICE, &token, &conn);
483 if (code) com_err (rn, code, "contacting Admin Server");
486 code = ka_ChangePassword (pw->pw_name, instance, conn, 0, &newmitkey);
488 code = ka_ChangePassword (pw->pw_name, instance, conn, 0, &newkey);
489 memset(&newkey, 0, sizeof(newkey));
490 memset(&newmitkey, 0, sizeof(newmitkey));
493 reason = (char *) error_message(code);
494 fprintf (stderr, "%s: Password was not changed because %s\n", rn, reason);
496 else printf("Password changed.\n\n");
499 memset(&newkey, 0, sizeof(newkey));
500 memset(&newmitkey, 0, sizeof(newmitkey));
502 /* Might need to close down the ubik_Client connection */
504 ubik_ClientDestroy(conn);
511 no_change: /* yuck, yuck, yuck */
512 if (code) com_err (rn, code, "getting new password");
514 memset(&key, 0, sizeof(key));
515 memset(npasswd, 0, sizeof(npasswd));
516 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name, cell);
518 exit (code ? code : 1);