1 /* Copyright (C) 1999, 1989 Transarc Corporation - All rights reserved */
3 * For copyright information, see IPL which you accepted in order to
4 * download this software.
8 /* These two needed for rxgen output to work */
11 #include <sys/types.h>
26 #include <afs/com_err.h>
28 #include <afs/cellconfig.h>
38 /* This code borrowed heavily from the log program. Here is the intro comment
39 * for that program: */
42 log -- tell the Andrew Cache Manager your password
44 modified February 1986
46 Further modified in August 1987 to understand cell IDs.
50 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
53 principal is of the form 'name' or 'name@cell' which provides the
54 cellname. See the -c option below.
55 password is the user's password. This form is NOT recommended for
57 newpassword is the new password. This form is NOT recommended for
59 -c identifies cellname as the cell in which authentication is to take
61 -servers allows the explicit specification of the hosts providing
62 authentication services for the cell being used for authentication.
63 This is a debugging option and will disappear.
66 /* The following code to make use of libcmd.a also stolen from klog.c. */
71 static char **zero_argv;
72 extern int init_child(), pasword_bad(), give_to_child(), terminate_child();
82 main (argc, argv, envp)
86 { struct cmd_syndesc *ts;
91 * The following signal action for AIX is necessary so that in case of a
92 * crash (i.e. core is generated) we can include the user's data section
93 * in the core dump. Unfortunately, by default, only a partial core is
94 * generated which, in many cases, isn't too useful.
98 sigemptyset(&nsa.sa_mask);
99 nsa.sa_handler = SIG_DFL;
100 nsa.sa_flags = SA_FULLDUMP;
101 sigaction(SIGSEGV, &nsa, NULL);
108 ts = cmd_CreateSyntax((char *) 0, CommandProc, 0, "change user's password");
113 #define aNEWPASSWORD 3
118 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
119 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
120 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
121 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL, "user's new password");
122 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
123 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL, "explicit list of servers");
124 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
126 code = cmd_Dispatch(argc, argv);
131 static void getpipepass(gpbuf, len)
135 /* read a password from stdin, stop on \n or eof */
140 if (tc == '\n' || tc == EOF) break;
146 static afs_int32 read_pass (passwd, len, prompt, verify)
152 code = read_pw_string (passwd, len, prompt, verify);
154 getpipepass (passwd, len);
160 static int password_ok (newpw, insist)
165 /* see if it is reasonable, but don't get so obnoxious */
166 (*insist)++; /* so we don't get called again */
167 if (strlen(newpw) < 6) return 0;
169 return 1; /* lie about it */
172 static char rn[] = "kpasswd"; /* Routine name */
173 static int Pipe = 0; /* reading from a pipe */
178 if (!Pipe) fprintf (stderr, "%s: timed out\n", rn);
183 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
184 CommandProc (as, arock)
186 struct cmd_syndesc *as;
188 char name[MAXKTCNAMELEN];
189 char instance[MAXKTCNAMELEN];
190 char cell[MAXKTCREALMLEN];
191 char realm[MAXKTCREALMLEN];
192 afs_int32 serverList[MAXSERVERS];
193 char *lcell; /* local cellname */
197 struct ubik_client *conn = 0;
198 struct ktc_encryptionKey key;
199 struct ktc_encryptionKey newkey;
201 struct ktc_token token;
204 struct passwd *pw = &pwent;
206 int insist; /* insist on good password quality */
207 int lexplicit=0; /* servers specified explicitly */
208 int local; /* explicit cell is same a local cell */
209 int foundPassword = 0; /*Not yet, anyway*/
210 int foundNewPassword = 0; /*Not yet, anyway*/
211 int foundExplicitCell = 0; /*Not yet, anyway*/
213 /* blow away command line arguments */
214 for (i=1; i<zero_argc; i++) bzero (zero_argv[i], strlen(zero_argv[i]));
217 /* first determine quiet flag based on -pipe switch */
218 Pipe = (as->parms[aPIPE].items ? 1 : 0);
221 signal(SIGALRM, timedout);
227 !(lcell = ka_LocalCell())) {
228 if (!Pipe) com_err (rn, code , "Can't get local cell name!");
234 if (!Pipe) com_err (rn, code , "Failed to initialize Rx");
238 strcpy (instance, "");
240 /* Parse our arguments. */
242 if (as->parms[aCELL].items) {
244 * cell name explicitly mentioned; take it in if no other cell name
245 * has already been specified and if the name actually appears. If
246 * the given cell name differs from our own, we don't do a lookup.
248 foundExplicitCell = 1;
249 strncpy (realm, as->parms[aCELL].items->data, sizeof(realm));
252 if (as->parms[aSERVERS].items) {
253 /* explicit server list */
256 char *ap[MAXSERVERS+2];
258 for (ip = as->parms[aSERVERS].items, i=2; ip; ip=ip->next, i++)
262 code = ubik_ParseClientList(i, ap, serverList);
264 if (!Pipe) com_err (rn, code, "could not parse server list");
270 if (as->parms[aPRINCIPAL].items) {
271 ka_ParseLoginName (as->parms[aPRINCIPAL].items->data,
272 name, instance, cell);
273 if (strlen (instance) > 0)
275 fprintf (stderr, "Non-null instance (%s) may cause strange behavior.\n",
277 if (strlen(cell) > 0) {
278 if (foundExplicitCell) {
280 fprintf (stderr, "%s: May not specify an explicit cell twice.\n", rn);
283 foundExplicitCell = 1;
284 strncpy (realm, cell, sizeof(realm));
288 /* No explicit name provided: use Unix uid. */
291 if (GetUserName(userName, &userNameLen) == 0) {
293 fprintf (stderr, "Can't figure out your name in local cell %s from your user id.\n", lcell);
294 fprintf (stderr, "Try providing the user name.\n");
298 pw->pw_name = userName;
300 pw = getpwuid(getuid());
303 fprintf (stderr, "Can't figure out your name in local cell %s from your user id.\n", lcell);
304 fprintf (stderr, "Try providing the user name.\n");
311 if (as->parms[aPASSWORD].items) {
313 * Current argument is the desired password string. Remember it in
314 * our local buffer, and zero out the argument string - anyone can
315 * see it there with ps!
318 strncpy (passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
319 bzero (as->parms[aPASSWORD].items->data,
320 strlen(as->parms[aPASSWORD].items->data));
323 if (as->parms[aNEWPASSWORD].items) {
325 * Current argument is the new password string. Remember it in
326 * our local buffer, and zero out the argument string - anyone can
327 * see it there with ps!
329 foundNewPassword = 1;
330 strncpy (npasswd, as->parms[aNEWPASSWORD].items->data,
332 bzero (as->parms[aNEWPASSWORD].items->data,
333 strlen(as->parms[aNEWPASSWORD].items->data));
336 if (!foundExplicitCell) strcpy (realm, lcell);
337 if (code = ka_CellToRealm (realm, realm, &local)) {
338 if (!Pipe) com_err (rn, code, "Can't convert cell to realm");
341 lcstring (cell, realm, sizeof(cell));
343 ka_PrintUserID ("Changing password for '", pw->pw_name, instance, "'");
344 printf (" in cell '%s'.\n", cell);
346 /* Get the password if it wasn't provided. */
347 if (!foundPassword) {
348 if (Pipe) getpipepass (passwd, sizeof(passwd));
350 code = read_pass (passwd, sizeof(passwd), "Old password: ", 0);
351 if (code || (strlen (passwd) == 0)) {
352 if (code) code = KAREADPW;
353 bzero (&key, sizeof(key));
354 bzero (passwd, sizeof(passwd));
355 if (code) com_err (rn, code, "reading password");
360 ka_StringToKey (passwd, realm, &key);
361 give_to_child(passwd);
363 /* Get new password if it wasn't provided. */
365 if (!foundNewPassword) {
367 getpipepass (npasswd, sizeof(npasswd));
370 code = read_pass (npasswd, sizeof(npasswd),
371 "New password (RETURN to abort): ", 0);
372 if (code || (strlen (npasswd) == 0)) {
378 } while (password_bad (npasswd));
380 code = read_pass (verify, sizeof(verify),
381 "Retype new password: ", 0);
386 if (strcmp (verify, npasswd) != 0) {
387 printf ("Mismatch - ");
390 bzero (verify, sizeof(verify));
393 if (code = password_bad (npasswd)) { /* assmt here! */
394 goto no_change_no_msg;
398 if (strlen(npasswd) > 8) {
401 "%s: password too long, only the first 8 chars will be used.\n",
405 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
407 ka_StringToKey (npasswd, realm, &newkey);
408 bzero (npasswd, sizeof(npasswd));
410 if (lexplicit) ka_ExplicitCell (realm, serverList);
412 /* Get an connection to kaserver's admin service in desired cell. Set the
413 * lifetime above the time uncertainty so that badly skewed clocks are
414 * reported when the ticket is decrypted. Then give us 10 seconds to
415 * actually get our work done if the clocks are skewed by only 14:59.
416 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
417 * interval, namely 20 minutes. */
419 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
421 code = ka_GetAdminToken (pw->pw_name, instance, realm,
422 &key, ADMIN_LIFETIME, &token, /*!new*/0);
424 if ((code == KABADREQUEST) && (strlen (passwd) > 8)) {
425 /* try with only the first 8 characters incase they set their password
426 * with an old style passwd program. */
428 strncpy (pass8, passwd, 8);
430 ka_StringToKey (pass8, realm, &key);
431 bzero (pass8, sizeof(pass8));
432 bzero (passwd, sizeof(passwd));
433 code = ka_GetAdminToken (pw->pw_name, instance, realm,
434 &key, ADMIN_LIFETIME, &token, /*!new*/0);
436 /* the folks in testing really *hate* this message */
438 fprintf (stderr, "Warning: only the first 8 characters of your old password were significant.\n");
443 bzero (&key, sizeof(key));
444 if (code == KAUBIKCALL) com_err (rn, code, "(Authentication Server unavailable, try later)");
446 if (code == KABADREQUEST)
447 fprintf(stderr, "%s: Incorrect old password.\n", rn);
449 com_err (rn, code, "so couldn't change password");
452 code = ka_AuthServerConn (realm, KA_MAINTENANCE_SERVICE, &token, &conn);
453 if (code) com_err (rn, code, "contacting Admin Server");
455 code = ka_ChangePassword (pw->pw_name, instance, conn, 0, &newkey);
456 bzero (&newkey, sizeof(newkey));
459 reason = (char *) error_message(code);
460 fprintf (stderr, "%s: Password was not changed because %s\n", rn, reason);
462 else printf("Password changed.\n\n");
465 bzero (&newkey, sizeof(newkey));
467 /* Might need to close down the ubik_Client connection */
469 ubik_ClientDestroy(conn);
476 no_change: /* yuck, yuck, yuck */
477 if (code) com_err (rn, code, "getting new password");
479 bzero (&key, sizeof(key));
480 bzero (npasswd, sizeof(npasswd));
481 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name, cell);
483 exit (code ? code : 1);