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>
20 #include <hcrypto/des.h>
21 #include <hcrypto/ui.h>
24 #include <rx/rxkad_convert.h>
27 #include <afs/com_err.h>
29 #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. */
68 int CommandProc(struct cmd_syndesc *, void *);
71 static char **zero_argv;
82 main(int argc, char *argv[], char **envp)
84 struct cmd_syndesc *ts;
89 * The following signal action for AIX is necessary so that in case of a
90 * crash (i.e. core is generated) we can include the user's data section
91 * in the core dump. Unfortunately, by default, only a partial core is
92 * generated which, in many cases, isn't too useful.
96 sigemptyset(&nsa.sa_mask);
97 nsa.sa_handler = SIG_DFL;
98 nsa.sa_flags = SA_FULLDUMP;
99 sigaction(SIGSEGV, &nsa, NULL);
106 ts = cmd_CreateSyntax(NULL, CommandProc, 0, 0, "change user's password");
111 #define aNEWPASSWORD 3
116 cmd_AddParm(ts, "-x", CMD_FLAG, CMD_OPTIONAL, "(obsolete, noop)");
117 cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_OPTIONAL, "user name");
118 cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_OPTIONAL, "user's password");
119 cmd_AddParm(ts, "-newpassword", CMD_SINGLE, CMD_OPTIONAL,
120 "user's new password");
121 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
122 cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL,
123 "explicit list of servers");
124 cmd_AddParm(ts, "-pipe", CMD_FLAG, CMD_OPTIONAL, "silent operation");
126 code = cmd_Dispatch(argc, argv);
132 getpipepass(char *gpbuf, int len)
134 /* read a password from stdin, stop on \n or eof */
136 memset(gpbuf, 0, len);
137 for (i = 0; i < len; i++) {
139 if (tc == '\n' || tc == EOF)
147 read_pass(char *passwd, int len, char *prompt, int verify)
150 code = UI_UTIL_read_pw_string(passwd, len, prompt, verify);
152 getpipepass(passwd, len);
158 static char rn[] = "kpasswd"; /* Routine name */
159 static int Pipe = 0; /* reading from a pipe */
166 fprintf(stderr, "%s: timed out\n", rn);
171 char passwd[BUFSIZ], npasswd[BUFSIZ], verify[BUFSIZ];
174 CommandProc(struct cmd_syndesc *as, void *arock)
176 char name[MAXKTCNAMELEN] = "";
177 char instance[MAXKTCNAMELEN] = "";
178 char cell[MAXKTCREALMLEN] = "";
179 char realm[MAXKTCREALMLEN] = "";
180 afs_uint32 serverList[MAXSERVERS];
181 char *lcell; /* local cellname */
185 struct ubik_client *conn = 0;
186 struct ktc_encryptionKey key;
187 struct ktc_encryptionKey mitkey;
188 struct ktc_encryptionKey newkey;
189 struct ktc_encryptionKey newmitkey;
191 struct ktc_token token;
194 struct passwd *pw = &pwent;
196 int lexplicit = 0; /* servers specified explicitly */
197 int local; /* explicit cell is same a local cell */
198 int foundPassword = 0; /*Not yet, anyway */
199 int foundNewPassword = 0; /*Not yet, anyway */
200 int foundExplicitCell = 0; /*Not yet, anyway */
201 #ifdef DEFAULT_MITV4_STRINGTOKEY
203 #elif DEFAULT_AFS_STRINGTOKEY
209 /* blow away command line arguments */
210 for (i = 1; i < zero_argc; i++)
211 memset(zero_argv[i], 0, strlen(zero_argv[i]));
214 /* first determine quiet flag based on -pipe switch */
215 Pipe = (as->parms[aPIPE].items ? 1 : 0);
218 signal(SIGALRM, timedout);
223 if (code || !(lcell = ka_LocalCell())) {
224 #ifndef AFS_FREELANCE_CLIENT
226 afs_com_err(rn, code, "Can't get local cell name!");
234 afs_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);
265 afs_com_err(rn, code, "could not parse server list");
271 if (as->parms[aPRINCIPAL].items) {
272 ka_ParseLoginName(as->parms[aPRINCIPAL].items->data, name, instance,
274 if (strlen(instance) > 0)
277 "Non-null instance (%s) may cause strange behavior.\n",
279 if (strlen(cell) > 0) {
280 if (foundExplicitCell) {
283 "%s: May not specify an explicit cell twice.\n",
287 foundExplicitCell = 1;
288 strncpy(realm, cell, sizeof(realm));
292 /* No explicit name provided: use Unix uid. */
295 if (GetUserName(userName, &userNameLen) == 0) {
298 "Can't figure out your name in local cell %s from your user id.\n",
300 fprintf(stderr, "Try providing the user name.\n");
304 pw->pw_name = userName;
306 pw = getpwuid(getuid());
310 "Can't figure out your name in local cell %s from your user id.\n",
312 fprintf(stderr, "Try providing the user name.\n");
319 if (as->parms[aPASSWORD].items) {
321 * Current argument is the desired password string. Remember it in
322 * our local buffer, and zero out the argument string - anyone can
323 * see it there with ps!
326 strncpy(passwd, as->parms[aPASSWORD].items->data, sizeof(passwd));
327 memset(as->parms[aPASSWORD].items->data, 0,
328 strlen(as->parms[aPASSWORD].items->data));
331 if (as->parms[aNEWPASSWORD].items) {
333 * Current argument is the new password string. Remember it in
334 * our local buffer, and zero out the argument string - anyone can
335 * see it there with ps!
337 foundNewPassword = 1;
338 strncpy(npasswd, as->parms[aNEWPASSWORD].items->data,
340 memset(as->parms[aNEWPASSWORD].items->data, 0,
341 strlen(as->parms[aNEWPASSWORD].items->data));
343 #ifdef AFS_FREELANCE_CLIENT
344 if (!foundExplicitCell && !lcell) {
346 afs_com_err(rn, code, "no cell name provided");
350 if (!foundExplicitCell)
351 strcpy(realm, lcell);
352 #endif /* freelance */
354 if ((code = ka_CellToRealm(realm, realm, &local))) {
356 afs_com_err(rn, code, "Can't convert cell to realm");
359 lcstring(cell, realm, sizeof(cell));
361 ka_PrintUserID("Changing password for '", pw->pw_name, instance, "'");
362 printf(" in cell '%s'.\n", cell);
364 /* Get the password if it wasn't provided. */
365 if (!foundPassword) {
367 getpipepass(passwd, sizeof(passwd));
369 code = read_pass(passwd, sizeof(passwd), "Old password: ", 0);
370 if (code || (strlen(passwd) == 0)) {
373 memset(&mitkey, 0, sizeof(mitkey));
374 memset(&key, 0, sizeof(key));
375 memset(passwd, 0, sizeof(passwd));
377 afs_com_err(rn, code, "reading password");
382 ka_StringToKey(passwd, realm, &key);
383 DES_string_to_key(passwd, ktc_to_cblockptr(&mitkey));
384 give_to_child(passwd);
386 /* Get new password if it wasn't provided. */
387 if (!foundNewPassword) {
389 getpipepass(npasswd, sizeof(npasswd));
393 read_pass(npasswd, sizeof(npasswd),
394 "New password (RETURN to abort): ", 0);
395 if (code || (strlen(npasswd) == 0)) {
401 } while (password_bad(npasswd));
404 read_pass(verify, sizeof(verify), "Retype new password: ", 0);
409 if (strcmp(verify, npasswd) != 0) {
410 printf("Mismatch - ");
413 memset(verify, 0, sizeof(verify));
416 if ((code = password_bad(npasswd))) { /* assmt here! */
417 goto no_change_no_msg;
420 if (strlen(npasswd) > 8) {
423 "%s: password too long, only the first 8 chars will be used.\n",
426 npasswd[8] = 0; /* in case the password was exactly 8 chars long */
428 ka_StringToKey(npasswd, realm, &newkey);
429 DES_string_to_key(npasswd, ktc_to_cblockptr(&newmitkey));
430 memset(npasswd, 0, sizeof(npasswd));
433 ka_ExplicitCell(realm, serverList);
435 /* Get an connection to kaserver's admin service in desired cell. Set the
436 * lifetime above the time uncertainty so that badly skewed clocks are
437 * reported when the ticket is decrypted. Then give us 10 seconds to
438 * actually get our work done if the clocks are skewed by only 14:59.
439 * NOTE: Kerberos lifetime encoding will round this up to next 5 minute
440 * interval, namely 20 minutes. */
442 #define ADMIN_LIFETIME (KTC_TIME_UNCERTAINTY+1)
445 ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME,
446 &token, /*!new */ 0);
447 if (code == KABADREQUEST) {
449 ka_GetAdminToken(pw->pw_name, instance, realm, &mitkey,
450 ADMIN_LIFETIME, &token, /*!new */ 0);
451 if ((code == KABADREQUEST) && (strlen(passwd) > 8)) {
452 /* try with only the first 8 characters incase they set their password
453 * with an old style passwd program. */
455 strncpy(pass8, passwd, 8);
457 ka_StringToKey(pass8, realm, &key);
458 memset(pass8, 0, sizeof(pass8));
459 memset(passwd, 0, sizeof(passwd));
460 code = ka_GetAdminToken(pw->pw_name, instance, realm, &key, ADMIN_LIFETIME, &token, /*!new */
475 memset(&mitkey, 0, sizeof(mitkey));
476 memset(&key, 0, sizeof(key));
477 if (code == KAUBIKCALL)
478 afs_com_err(rn, code, "(Authentication Server unavailable, try later)");
480 if (code == KABADREQUEST)
481 fprintf(stderr, "%s: Incorrect old password.\n", rn);
483 afs_com_err(rn, code, "so couldn't change password");
486 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token, &conn);
488 afs_com_err(rn, code, "contacting Admin Server");
492 ka_ChangePassword(pw->pw_name, instance, conn, 0,
496 ka_ChangePassword(pw->pw_name, instance, conn, 0,
498 memset(&newkey, 0, sizeof(newkey));
499 memset(&newmitkey, 0, sizeof(newmitkey));
502 reason = (char *)afs_error_message(code);
503 fprintf(stderr, "%s: Password was not changed because %s\n",
506 printf("Password changed.\n\n");
509 memset(&newkey, 0, sizeof(newkey));
510 memset(&newmitkey, 0, sizeof(newmitkey));
512 /* Might need to close down the ubik_Client connection */
514 ubik_ClientDestroy(conn);
521 no_change: /* yuck, yuck, yuck */
523 afs_com_err(rn, code, "getting new password");
525 memset(&key, 0, sizeof(key));
526 memset(npasswd, 0, sizeof(npasswd));
527 printf("Password for '%s' in cell '%s' unchanged.\n\n", pw->pw_name,
530 exit(code ? code : 1);