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 <afs/param.h>
12 #include <afsconfig.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 */
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++) bzero (zero_argv[i], 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 bzero (as->parms[aPASSWORD].items->data,
335 strlen(as->parms[aPASSWORD].items->data));
338 if (as->parms[aNEWPASSWORD].items) {
340 * Current argument is the new password string. Remember it in
341 * our local buffer, and zero out the argument string - anyone can
342 * see it there with ps!
344 foundNewPassword = 1;
345 strncpy (npasswd, as->parms[aNEWPASSWORD].items->data,
347 bzero (as->parms[aNEWPASSWORD].items->data,
348 strlen(as->parms[aNEWPASSWORD].items->data));
351 if (!foundExplicitCell) strcpy (realm, lcell);
352 if (code = ka_CellToRealm (realm, realm, &local)) {
353 if (!Pipe) com_err (rn, code, "Can't convert cell to realm");
356 lcstring (cell, realm, sizeof(cell));
358 ka_PrintUserID ("Changing password for '", pw->pw_name, instance, "'");
359 printf (" in cell '%s'.\n", cell);
361 /* Get the password if it wasn't provided. */
362 if (!foundPassword) {
363 if (Pipe) getpipepass (passwd, sizeof(passwd));
365 code = read_pass (passwd, sizeof(passwd), "Old password: ", 0);
366 if (code || (strlen (passwd) == 0)) {
367 if (code) code = KAREADPW;
368 bzero (&mitkey, sizeof(mitkey));
369 bzero (&key, sizeof(key));
370 bzero (passwd, sizeof(passwd));
371 if (code) com_err (rn, code, "reading password");
376 ka_StringToKey (passwd, realm, &key);
377 des_string_to_key(passwd, &mitkey);
378 give_to_child(passwd);
380 /* Get new password if it wasn't provided. */
382 if (!foundNewPassword) {
384 getpipepass (npasswd, sizeof(npasswd));
387 code = read_pass (npasswd, sizeof(npasswd),
388 "New password (RETURN to abort): ", 0);
389 if (code || (strlen (npasswd) == 0)) {
395 } while (password_bad (npasswd));
397 code = read_pass (verify, sizeof(verify),
398 "Retype new password: ", 0);
403 if (strcmp (verify, npasswd) != 0) {
404 printf ("Mismatch - ");
407 bzero (verify, sizeof(verify));
410 if (code = password_bad (npasswd)) { /* assmt here! */
411 goto no_change_no_msg;
415 if (strlen(npasswd) > 8) {
418 "%s: password too long, only the first 8 chars will be used.\n",
422 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
424 ka_StringToKey (npasswd, realm, &newkey);
425 des_string_to_key(npasswd, &newmitkey);
426 bzero (npasswd, sizeof(npasswd));
428 if (lexplicit) ka_ExplicitCell (realm, serverList);
430 /* Get an connection to kaserver's admin service in desired cell. Set the
431 * lifetime above the time uncertainty so that badly skewed clocks are
432 * reported when the ticket is decrypted. Then give us 10 seconds to
433 * actually get our work done if the clocks are skewed by only 14:59.
434 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
435 * interval, namely 20 minutes. */
437 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
439 code = ka_GetAdminToken (pw->pw_name, instance, realm,
440 &key, ADMIN_LIFETIME, &token, /*!new*/0);
441 if (code == KABADREQUEST) {
442 code = ka_GetAdminToken (pw->pw_name, instance, realm,
443 &mitkey, ADMIN_LIFETIME, &token, /*!new*/0);
444 if ((code == KABADREQUEST) && (strlen (passwd) > 8)) {
445 /* try with only the first 8 characters incase they set their password
446 * with an old style passwd program. */
448 strncpy (pass8, passwd, 8);
450 ka_StringToKey (pass8, realm, &key);
451 bzero (pass8, sizeof(pass8));
452 bzero (passwd, sizeof(passwd));
453 code = ka_GetAdminToken (pw->pw_name, instance, realm,
454 &key, ADMIN_LIFETIME, &token, /*!new*/0);
456 /* the folks in testing really *hate* this message */
458 fprintf (stderr, "Warning: only the first 8 characters of your old password were significant.\n");
474 bzero (&mitkey, sizeof(mitkey));
475 bzero (&key, sizeof(key));
476 if (code == KAUBIKCALL) com_err (rn, code, "(Authentication Server unavailable, try later)");
478 if (code == KABADREQUEST)
479 fprintf(stderr, "%s: Incorrect old password.\n", rn);
481 com_err (rn, code, "so couldn't change password");
484 code = ka_AuthServerConn (realm, KA_MAINTENANCE_SERVICE, &token, &conn);
485 if (code) com_err (rn, code, "contacting Admin Server");
488 code = ka_ChangePassword (pw->pw_name, instance, conn, 0, &newmitkey);
490 code = ka_ChangePassword (pw->pw_name, instance, conn, 0, &newkey);
491 bzero (&newkey, sizeof(newkey));
492 bzero (&newmitkey, sizeof(newmitkey));
495 reason = (char *) error_message(code);
496 fprintf (stderr, "%s: Password was not changed because %s\n", rn, reason);
498 else printf("Password changed.\n\n");
501 bzero (&newkey, sizeof(newkey));
502 bzero (&newmitkey, sizeof(newmitkey));
504 /* Might need to close down the ubik_Client connection */
506 ubik_ClientDestroy(conn);
513 no_change: /* yuck, yuck, yuck */
514 if (code) com_err (rn, code, "getting new password");
516 bzero (&key, sizeof(key));
517 bzero (npasswd, sizeof(npasswd));
518 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name, cell);
520 exit (code ? code : 1);