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>
34 #include <des_prototypes.h>
35 #include <afs/com_err.h>
37 #include <afs/cellconfig.h>
49 /* This code borrowed heavily from the log program. Here is the intro comment
50 * for that program: */
53 log -- tell the Andrew Cache Manager your password
55 modified February 1986
57 Further modified in August 1987 to understand cell IDs.
61 kpasswd [user [password] [newpassword]] [-c cellname] [-servers <hostlist>]
64 principal is of the form 'name' or 'name@cell' which provides the
65 cellname. See the -c option below.
66 password is the user's password. This form is NOT recommended for
68 newpassword is the new password. This form is NOT recommended for
70 -c identifies cellname as the cell in which authentication is to take
72 -servers allows the explicit specification of the hosts providing
73 authentication services for the cell being used for authentication.
74 This is a debugging option and will disappear.
77 /* The following code to make use of libcmd.a also stolen from klog.c. */
79 int CommandProc(struct cmd_syndesc *, void *);
82 static char **zero_argv;
93 main(int argc, char *argv[], char **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(char *gpbuf, int len)
145 /* read a password from stdin, stop on \n or eof */
147 memset(gpbuf, 0, len);
148 for (i = 0; i < len; i++) {
150 if (tc == '\n' || tc == EOF)
158 read_pass(char *passwd, int len, char *prompt, int verify)
161 code = read_pw_string(passwd, len, prompt, verify);
163 getpipepass(passwd, len);
171 password_ok(char *newpw, int *insist)
174 /* see if it is reasonable, but don't get so obnoxious */
175 /* FIXME: null pointer derefence!!! */
176 (*insist)++; /* so we don't get called again */
177 if (strlen(newpw) < 6)
180 return 1; /* lie about it */
184 static char rn[] = "kpasswd"; /* Routine name */
185 static int Pipe = 0; /* reading from a pipe */
192 fprintf(stderr, "%s: timed out\n", rn);
197 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
200 CommandProc(struct cmd_syndesc *as, void *arock)
202 char name[MAXKTCNAMELEN] = "";
203 char instance[MAXKTCNAMELEN] = "";
204 char cell[MAXKTCREALMLEN] = "";
205 char realm[MAXKTCREALMLEN] = "";
206 afs_uint32 serverList[MAXSERVERS];
207 char *lcell; /* local cellname */
211 struct ubik_client *conn = 0;
212 struct ktc_encryptionKey key;
213 struct ktc_encryptionKey mitkey;
214 struct ktc_encryptionKey newkey;
215 struct ktc_encryptionKey newmitkey;
217 struct ktc_token token;
220 struct passwd *pw = &pwent;
222 int lexplicit = 0; /* servers specified explicitly */
223 int local; /* explicit cell is same a local cell */
224 int foundPassword = 0; /*Not yet, anyway */
225 int foundNewPassword = 0; /*Not yet, anyway */
226 int foundExplicitCell = 0; /*Not yet, anyway */
227 #ifdef DEFAULT_MITV4_STRINGTOKEY
229 #elif DEFAULT_AFS_STRINGTOKEY
235 /* blow away command line arguments */
236 for (i = 1; i < zero_argc; i++)
237 memset(zero_argv[i], 0, strlen(zero_argv[i]));
240 /* first determine quiet flag based on -pipe switch */
241 Pipe = (as->parms[aPIPE].items ? 1 : 0);
244 signal(SIGALRM, timedout);
249 if (code || !(lcell = ka_LocalCell())) {
250 #ifndef AFS_FREELANCE_CLIENT
252 afs_com_err(rn, code, "Can't get local cell name!");
260 afs_com_err(rn, code, "Failed to initialize Rx");
264 strcpy(instance, "");
266 /* Parse our arguments. */
268 if (as->parms[aCELL].items) {
270 * cell name explicitly mentioned; take it in if no other cell name
271 * has already been specified and if the name actually appears. If
272 * the given cell name differs from our own, we don't do a lookup.
274 foundExplicitCell = 1;
275 strncpy(realm, as->parms[aCELL].items->data, sizeof(realm));
278 if (as->parms[aSERVERS].items) {
279 /* explicit server list */
282 char *ap[MAXSERVERS + 2];
284 for (ip = as->parms[aSERVERS].items, i = 2; ip; ip = ip->next, i++)
288 code = ubik_ParseClientList(i, ap, serverList);
291 afs_com_err(rn, code, "could not parse server list");
297 if (as->parms[aPRINCIPAL].items) {
298 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
300 if (strlen(instance) > 0)
303 "Non-null instance (%s) may cause strange behavior.\n",
305 if (strlen(cell) > 0) {
306 if (foundExplicitCell) {
309 "%s: May not specify an explicit cell twice.\n",
313 foundExplicitCell = 1;
314 strncpy(realm, cell, sizeof(realm));
318 /* No explicit name provided: use Unix uid. */
321 if (GetUserName(userName, &userNameLen) == 0) {
324 "Can't figure out your name in local cell %s from your user id.\n",
326 fprintf(stderr, "Try providing the user name.\n");
330 pw->pw_name = userName;
332 pw = getpwuid(getuid());
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");
345 if (as->parms[aPASSWORD].items) {
347 * Current argument is the desired password string. Remember it in
348 * our local buffer, and zero out the argument string - anyone can
349 * see it there with ps!
352 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
353 memset(as->parms[aPASSWORD].items->data, 0,
354 strlen(as->parms[aPASSWORD].items->data));
357 if (as->parms[aNEWPASSWORD].items) {
359 * Current argument is the new password string. Remember it in
360 * our local buffer, and zero out the argument string - anyone can
361 * see it there with ps!
363 foundNewPassword = 1;
364 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
366 memset(as->parms[aNEWPASSWORD].items->data, 0,
367 strlen(as->parms[aNEWPASSWORD].items->data));
369 #ifdef AFS_FREELANCE_CLIENT
370 if (!foundExplicitCell && !lcell) {
372 afs_com_err(rn, code, "no cell name provided");
376 if (!foundExplicitCell)
377 strcpy(realm, lcell);
378 #endif /* freelance */
380 if ((code = ka_CellToRealm(realm, realm, &local))) {
382 afs_com_err(rn, code, "Can't convert cell to realm");
385 lcstring(cell, realm, sizeof(cell));
387 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
388 printf(" in cell '%s'.\n", cell);
390 /* Get the password if it wasn't provided. */
391 if (!foundPassword) {
393 getpipepass(passwd, sizeof(passwd));
395 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
396 if (code || (strlen(passwd) == 0)) {
399 memset(&mitkey, 0, sizeof(mitkey));
400 memset(&key, 0, sizeof(key));
401 memset(passwd, 0, sizeof(passwd));
403 afs_com_err(rn, code, "reading password");
408 ka_StringToKey(passwd, realm, &key);
409 des_string_to_key(passwd, ktc_to_cblockptr(&mitkey));
410 give_to_child(passwd);
412 /* Get new password if it wasn't provided. */
413 if (!foundNewPassword) {
415 getpipepass(npasswd, sizeof(npasswd));
419 read_pass(npasswd, sizeof(npasswd),
420 "New password (RETURN to abort): ", 0);
421 if (code || (strlen(npasswd) == 0)) {
427 } while (password_bad(npasswd));
430 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
435 if (strcmp(verify, npasswd) != 0) {
436 printf("Mismatch - ");
439 memset(verify, 0, sizeof(verify));
442 if ((code = password_bad(npasswd))) { /* assmt here! */
443 goto no_change_no_msg;
446 if (strlen(npasswd) > 8) {
449 "%s: password too long, only the first 8 chars will be used.\n",
452 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
454 ka_StringToKey(npasswd, realm, &newkey);
455 des_string_to_key(npasswd, ktc_to_cblockptr(&newmitkey));
456 memset(npasswd, 0, sizeof(npasswd));
459 ka_ExplicitCell(realm, serverList);
461 /* Get an connection to kaserver's admin service in desired cell. Set the
462 * lifetime above the time uncertainty so that badly skewed clocks are
463 * reported when the ticket is decrypted. Then give us 10 seconds to
464 * actually get our work done if the clocks are skewed by only 14:59.
465 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
466 * interval, namely 20 minutes. */
468 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
471 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
472 &token, /*!new */ 0);
473 if (code == KABADREQUEST) {
475 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
476 ADMIN_LIFETIME, &token, /*!new */ 0);
477 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
478 /* try with only the first 8 characters incase they set their password
479 * with an old style passwd program. */
481 strncpy(pass8, passwd, 8);
483 ka_StringToKey(pass8, realm, &key);
484 memset(pass8, 0, sizeof(pass8));
485 memset(passwd, 0, sizeof(passwd));
486 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
489 /* the folks in testing really *hate* this message */
492 "Warning: only the first 8 characters of your old password were significant.\n");
508 memset(&mitkey, 0, sizeof(mitkey));
509 memset(&key, 0, sizeof(key));
510 if (code == KAUBIKCALL)
511 afs_com_err(rn, code, "(Authentication Server unavailable, try later)");
513 if (code == KABADREQUEST)
514 fprintf(stderr, "%s: Incorrect old password.\n", rn);
516 afs_com_err(rn, code, "so couldn't change password");
519 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
521 afs_com_err(rn, code, "contacting Admin Server");
525 ka_ChangePassword(pw->pw_name, instance, conn, 0,
529 ka_ChangePassword(pw->pw_name, instance, conn, 0,
531 memset(&newkey, 0, sizeof(newkey));
532 memset(&newmitkey, 0, sizeof(newmitkey));
535 reason = (char *)afs_error_message(code);
536 fprintf(stderr, "%s: Password was not changed because %s\n",
539 printf("Password changed.\n\n");
542 memset(&newkey, 0, sizeof(newkey));
543 memset(&newmitkey, 0, sizeof(newmitkey));
545 /* Might need to close down the ubik_Client connection */
547 ubik_ClientDestroy(conn);
554 no_change: /* yuck, yuck, yuck */
556 afs_com_err(rn, code, "getting new password");
558 memset(&key, 0, sizeof(key));
559 memset(npasswd, 0, sizeof(npasswd));
560 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
563 exit(code ? code : 1);