[OpenAFS-devel] [PATCH] PAM: allow PAM to change AFS passwords

Ulrich Hahn zrsha01@zdv.uni-tuebingen.de
Wed, 28 Feb 2001 12:58:12 +0100 (CET)


This patch allows AFS users to change their passwords using the=20
standard passwd utility (no kpasswd needed). To enable PAM to change
the AFS password the PAM definition to passwd needs the following lines
included:

auth     sufficient /lib/security/pam_afs.so try_first_path ignore_root
password sufficient /lib/security/pam_afs.so try_first_path ignore_root

The patch was tested on openafs-1.0.2, linux kernel 2.2.16, SuSE 7.0.
patch=20

Uli

: Ulrich Hahn                 email: ulrich.hahn@zdv.uni-tuebingen.de :
: Zentrum f=FCr Datenverarbeitung            Tel: + (49) 07071 29 70315 :
: Universit=E4t T=FCbingen                     FAX: + (49) 07071 29  5912 :=
=20
-----------------------------------------------------------------------

##### patch

--- openafs-1.0.2.orig/src/pam/afs_password.c=09Wed Feb 21 11:10:29 2001
+++ openafs-1.0.2/src/pam/afs_password.c=09Wed Feb 28 12:24:55 2001
@@ -9,6 +9,22 @@
=20
 #include <security/pam_appl.h>
 #include <security/pam_modules.h>
+#include <syslog.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pwd.h>
+#include <unistd.h>
+#include <afs/param.h>
+#include <sys/param.h>
+#include <afs/kautils.h>
+#include "afs_message.h"
+#include "afs_util.h"
+#include "afs_pam_msg.h"
+#include <signal.h>
+#include <sys/wait.h>
+#include <errno.h>
+
+#define RET(x) { retcode =3D (x); goto out; }
=20
 extern int
 pam_sm_chauthtok(
@@ -17,5 +33,267 @@
 =09int=09=09argc,
 =09const char=09**argv)
 {
-    return PAM_PERM_DENIED;
+    int retcode =3D PAM_SUCCESS;
+    int errcode =3D PAM_SUCCESS;
+    int code;
+    int origmask;
+    int logmask =3D LOG_UPTO(LOG_INFO);
+    int nowarn =3D 0;
+    int use_first_pass =3D 0;
+    int try_first_pass =3D 0;
+    int ignore_root =3D 0;
+    int got_authtok =3D 0;=09/* got PAM_AUTHTOK upon entry */
+    int torch_password =3D 1;
+    int i;
+    char my_password_buf[256];
+    char instance[256];
+    char realm[256];
+    char cell[256];
+    char *localcell;
+    char *user =3D NULL, *password =3D NULL;
+    char *new_password =3D NULL, *verify_password =3D NULL;
+    char upwd_buf[2048];=09/* size is a guess. */
+    char*=09reason =3D NULL;
+    struct ktc_encryptionKey oldkey, newkey;
+    struct ktc_token token;
+    struct ubik_client *conn =3D 0;
+    struct pam_conv *pam_convp =3D NULL;
+    struct passwd unix_pwd, *upwd =3D NULL;
+
+#ifndef AFS_SUN56_ENV
+    openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
+#endif
+    origmask =3D setlogmask(logmask);
+
+    /*
+     * Parse the user options.  Log an error for any unknown options.
+     *
+     * Todo options: PAM_SILENT
+     */
+    for (i =3D 0; i < argc; i++) {
+=09if (=09   strcasecmp(argv[i], "debug"=09       ) =3D=3D 0) {
+=09    logmask |=3D LOG_MASK(LOG_DEBUG);
+=09    (void) setlogmask(logmask);
+=09} else if (strcasecmp(argv[i], "nowarn"=09       ) =3D=3D 0) {
+=09    nowarn =3D 1;
+=09} else if (strcasecmp(argv[i], "use_first_pass") =3D=3D 0) {
+=09    use_first_pass =3D 1;
+=09} else if (strcasecmp(argv[i], "try_first_pass") =3D=3D 0) {
+=09    try_first_pass =3D 1;
+=09} else if (strcasecmp(argv[i], "ignore_root"   ) =3D=3D 0) {
+=09    ignore_root =3D 1;
+=09} else {
+=09    pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
+=09}
+    }
+
+    if (use_first_pass) try_first_pass =3D 0;
+
+    pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_=
first_pass);
+    pam_afs_syslog(LOG_DEBUG, PAMAFS_PAMERROR, flags);
+
+    /* Try to get the user-interaction info, if available. */
+    errcode =3D pam_get_item(pamh, PAM_CONV, (const void **) &pam_convp);
+    if (errcode !=3D PAM_SUCCESS) {
+=09pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
+=09pam_convp =3D NULL;
+    }
+
+    /* Who are we trying to authenticate here? */
+    if ((errcode =3D pam_get_user(pamh, (const char **)&user, "AFS usernam=
e: ")) !=3D PAM_SUCCESS) {
+=09pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
+=09RET(PAM_USER_UNKNOWN);
+    }
+
+    pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
+
+    /*
+     * If the user has a "local" (or via nss, possibly nss_dce) pwent,
+     * and its uid=3D=3D0, and "ignore_root" was given in pam.conf,
+     * ignore the user.
+     */
+#if=09defined(AFS_HPUX_ENV)
+#if     defined(AFS_HPUX110_ENV)
+    i =3D getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
+#else   /* AFS_HPUX110_ENV */
+    i =3D getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
+    if ( i =3D=3D 0 )=09=09=09/* getpwnam_r success */
+=09upwd =3D &unix_pwd;=20
+#endif  /* else AFS_HPUX110_ENV */
+    if (ignore_root && i =3D=3D 0  && upwd->pw_uid =3D=3D 0) {
+=09pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
+=09RET(PAM_AUTH_ERR);
+    }
+#else
+#ifdef AFS_LINUX20_ENV
+    upwd =3D getpwnam(user);
+#else
+    upwd =3D getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
+#endif
+    if (ignore_root && upwd !=3D NULL && upwd->pw_uid =3D=3D 0) {
+=09pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
+=09RET(PAM_AUTH_ERR);
+    }
+#endif
+
+    errcode =3D pam_get_item(pamh, PAM_AUTHTOK, (const void **) &password)=
;
+    if (errcode !=3D PAM_SUCCESS || password =3D=3D NULL) {
+=09if (use_first_pass) {
+=09    pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
+=09    RET(PAM_AUTH_ERR);
+=09}
+=09password =3D NULL;=09/* In case it isn't already NULL */
+=09pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
+    } else if (password[0] =3D=3D '\0') {
+=09/* Actually we *did* get one but it was empty. */
+=09torch_password =3D 0;
+=09pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
+=09RET(PAM_NEW_AUTHTOK_REQD);
+    } else {
+=09pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
+=09torch_password =3D 0;
+=09got_authtok =3D 1;
+    }
+    if (!(use_first_pass || try_first_pass)) {
+=09password =3D NULL;
+    }
+
+    if (password =3D=3D NULL) {
+=09torch_password =3D 1;
+=09if (use_first_pass)
+=09    RET(PAM_AUTH_ERR);=09/* shouldn't happen */
+=09if (try_first_pass)
+=09    try_first_pass =3D 0;=09
+=09if (pam_convp =3D=3D NULL || pam_convp->conv =3D=3D NULL) {
+=09    pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
+=09    RET(PAM_AUTH_ERR);
+=09}
+
+=09errcode =3D pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
+=09if (errcode !=3D PAM_SUCCESS || password =3D=3D NULL) {
+=09    pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
+=09    RET(PAM_AUTH_ERR);
+=09}
+=09if (password[0] =3D=3D '\0') {
+=09    pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
+=09    RET(PAM_NEW_AUTHTOK_REQD);
+=09}
+
+=09/*
+         * We aren't going to free the password later (we will wipe it,
+         * though), because the storage for it if we get it from other
+         * paths may belong to someone else.  Since we do need to free
+         * this storage, copy it to a buffer that won't need to be freed
+         * later, and free this storage now.
+         */
+        strncpy(my_password_buf, password, sizeof(my_password_buf));
+        my_password_buf[sizeof(my_password_buf)-1] =3D '\0';
+        memset(password, 0, strlen(password));
+        free(password);
+        password =3D my_password_buf;
+    }
+
+    if ( (code =3D ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH=
_DOSETPAG,
+=09=09=09=09    user, /* kerberos name */
+=09=09=09=09    (char *)0, /* instance */
+=09=09=09=09    (char *)0, /* realm */
+=09=09=09=09    password, /* password */
+=09=09=09=09    0, /* spare 2 */
+=09=09=09=09    &reason /* error string */ )) !=3D0 ) {
+=09pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
+        RET(PAM_AUTH_ERR);
+    }
+    torch_password =3D 0;
+    pam_set_item(pamh, PAM_AUTHTOK, password);
+    pam_set_item(pamh, PAM_OLDAUTHTOK, password);
+    if (flags & PAM_PRELIM_CHECK) {
+       /* only auth check was requested, so return success here */
+       return(PAM_SUCCESS);
+    }
+    if (!(flags & PAM_UPDATE_AUTHTOK)) {
+       /* these lines are never executed ... */
+       /* UPDATE_AUTHTOK flag is required, return with error */
+       pam_afs_syslog(LOG_ERR, PAMAFS_FLAGS, "PAM_UPDATE_AUTHTOK");
+       RET(PAM_AUTH_ERR);
+    }
+
+    /* get the new passwd and verify it */
+    errcode =3D pam_afs_prompt(pam_convp, &new_password, 0, PAMAFS_NEW_PWD=
_PROMPT);
+    if (errcode !=3D PAM_SUCCESS || new_password =3D=3D NULL) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
+        RET(PAM_AUTH_ERR);
+    }
+    if (new_password[0] =3D=3D '\0') {
+        pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
+        RET(PAM_AUTH_ERR);
+    }
+    errcode =3D pam_afs_prompt(pam_convp, &verify_password, 0, PAMAFS_VERI=
FY_PWD_PROMPT);
+    if (errcode !=3D PAM_SUCCESS || verify_password =3D=3D NULL) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
+=09memset(new_password, 0, strlen(new_password));
+        RET(PAM_AUTH_ERR);
+    }
+    if (verify_password[0] =3D=3D '\0') {
+        pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
+=09memset(new_password, 0, strlen(new_password));
+        RET(PAM_AUTH_ERR);
+    }
+    if (strcmp(new_password, verify_password) !=3D 0) {
+        pam_afs_syslog(LOG_INFO, PAMAFS_NE_PASSWORD);
+=09memset(new_password, 0, strlen(new_password));
+=09memset(verify_password, 0, strlen(verify_password));
+        RET(PAM_AUTH_ERR);
+    }
+    memset(verify_password, 0, strlen(verify_password));
+    /* checking password length and quality is up to other PAM modules */=
=20
+
+    /* set the new password */
+    if ((code =3D ka_Init(0)) !=3D 0) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
+        RET(PAM_AUTH_ERR);
+    }
+    if ((code =3D rx_Init(0)) !=3D 0) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
+        RET(PAM_AUTH_ERR);
+    }
+    strcpy(instance,"");
+    if ((localcell =3D ka_LocalCell()) =3D=3D NULL) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_NOCELLNAME);
+        RET(PAM_AUTH_ERR);
+    }
+    strcpy(realm,localcell);
+    strcpy(cell,realm);
+    /* oldkey is not used in ka_ChangePassword (only for ka_auth) */
+    ka_StringToKey(password, realm, &oldkey);
+    ka_StringToKey(new_password, realm, &newkey);
+    if ((code =3D ka_GetAdminToken(user, instance, realm, &oldkey, 20, &to=
ken, 0)) !=3D 0) {
+        pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
+        RET(PAM_AUTH_ERR);
+    }
+    if ((code =3D ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token,=
 &conn)) !=3D 0) {=20
+        pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
+        RET(PAM_AUTH_ERR);
+    }
+    if ((code =3D ka_ChangePassword(user,     /* kerberos name */
+=09   =09                  instance, /* instance */
+=09=09=09          conn,     /* conn */
+=09=09=09          0,        /* old password unused */
+=09=09=09          &newkey   /* new password */ )) !=3D 0) {
+=09pam_afs_syslog(LOG_ERR, PAMAFS_KAPASS_FAIL);
+        memset(new_password, 0, strlen(new_password));
+        RET(PAM_AUTH_ERR);
+    } else {
+        pam_set_item(pamh, PAM_AUTHTOK, new_password);
+        RET(PAM_SUCCESS);
+    }
+=09
+ out:
+    if (password && torch_password) {
+        memset(password, 0, strlen(password));
+    }
+    (void) setlogmask(origmask);
+#ifndef AFS_SUN56_ENV
+    closelog();
+#endif
+    return retcode;
 }
--- openafs-1.0.2.orig/src/pam/afs_message.c=09Thu Feb 22 12:20:09 2001
+++ openafs-1.0.2/src/pam/afs_message.c=09Wed Feb 28 12:35:59 2001
@@ -36,7 +36,7 @@
     "AFS not available",=09=09=09/* 10: AFS_UNAVAIL=09*/
     "AFS error code 0x%x",=09=09=09/* 11: AFS_ERROR=09*/
     "AFS Authentication succeeded.\n",=09=09/* 12: LOGIN_OK=09=09*/
-    "AFS Authentication failed for user %s. %s\n",
+    "AFS Authentication failed for user %s %s\n",
 =09=09=09=09=09=09/* 13: LOGIN_FAILED=09*/
     "AFS PAM error, code=3D%d",=09=09=09/* 14: PAMERROR=09=09*/
     "AFS uid exceeds OS bounds.\n",=09=09/* 15: UID_OVERFLOW=09*/
@@ -64,6 +64,14 @@
     "AFS ReInitializing creds for user %s\n",=09/* 31: REINITCRED=09*/
     "AFS Failed to set PASSWORD_EXPIRES for user %s\n",
 =09=09=09=09=09=09/* 32: PASSEXPFAIL      */
+    "",
+    "",
+    "New AFS Password: ",                       /* 35: NEW_PWD_PROMPT   */
+    "New AFS Password (again): ",               /* 36: VERIFY_PWD_PROMPT *=
/
+    "Failed to change AFS password",            /* 37: KRBPASS_FAIL     */
+    "Missing PAM flag: %s",                     /* 38: FLAGS            */
+    "ka error, code=3D%d",                        /* 39: KAERROR          =
*/
+    "Passwords are not equal"                   /* 40: NE_PASSWORD      */
 };
=20
 static int num_fallbacks =3D sizeof(fallback_messages)/sizeof(char *);
--- openafs-1.0.2.orig/src/pam/afs_message.c=09Thu Feb 22 12:20:09 2001
+++ openafs-1.0.2/src/pam/afs_message.c=09Wed Feb 28 12:35:59 2001
@@ -36,7 +36,7 @@
     "AFS not available",=09=09=09/* 10: AFS_UNAVAIL=09*/
     "AFS error code 0x%x",=09=09=09/* 11: AFS_ERROR=09*/
     "AFS Authentication succeeded.\n",=09=09/* 12: LOGIN_OK=09=09*/
-    "AFS Authentication failed for user %s. %s\n",
+    "AFS Authentication failed for user %s %s\n",
 =09=09=09=09=09=09/* 13: LOGIN_FAILED=09*/
     "AFS PAM error, code=3D%d",=09=09=09/* 14: PAMERROR=09=09*/
     "AFS uid exceeds OS bounds.\n",=09=09/* 15: UID_OVERFLOW=09*/
@@ -64,6 +64,14 @@
     "AFS ReInitializing creds for user %s\n",=09/* 31: REINITCRED=09*/
     "AFS Failed to set PASSWORD_EXPIRES for user %s\n",
 =09=09=09=09=09=09/* 32: PASSEXPFAIL      */
+    "",
+    "",
+    "New AFS Password: ",                       /* 35: NEW_PWD_PROMPT   */
+    "New AFS Password (again): ",               /* 36: VERIFY_PWD_PROMPT *=
/
+    "Failed to change AFS password",            /* 37: KRBPASS_FAIL     */
+    "Missing PAM flag: %s",                     /* 38: FLAGS            */
+    "ka error, code=3D%d",                        /* 39: KAERROR          =
*/
+    "Passwords are not equal"                   /* 40: NE_PASSWORD      */
 };
=20
 static int num_fallbacks =3D sizeof(fallback_messages)/sizeof(char *);


#### end patch