|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright (c) 2002 Michael Steffens. All rights reserved. |
| 3 |
* |
| 4 |
* Derived from auth-pam.c, |
| 5 |
* Copyright (c) 2000 Damien Miller. All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/* |
| 29 |
* This program provides a workaround for sshd presently not supporting |
| 30 |
* user changes of expired passwords when running in privsep mode. |
| 31 |
* |
| 32 |
* When encountering an expired authentication token, the non-privileged |
| 33 |
* session daemon can delegate change to ssh-chauthtok-helper, which |
| 34 |
* should be installed suid root for performing this task. |
| 35 |
* |
| 36 |
* It is somewhat similar to the system passwd program, but dedicatedly |
| 37 |
* talks to modules configured for PAM service "sshd", rather than those |
| 38 |
* configured for passwd. These might be the same, but can also be |
| 39 |
* different. |
| 40 |
* |
| 41 |
* By nature this helper program can also be invoked by local users |
| 42 |
* directly, just as the passwd program can. But in any case |
| 43 |
* |
| 44 |
* - non-root users can only change their own authentication tokens. |
| 45 |
* |
| 46 |
* - root users can change authentication tokens for others, but only |
| 47 |
* with providing the old token. |
| 48 |
* |
| 49 |
* The second restriction is meant to be a fuse against the helper program |
| 50 |
* being accidentally invoked by a privileged daemon... |
| 51 |
* |
| 52 |
* The ssh-chauthtok-helper program unfortunately can't safely deal |
| 53 |
* with the PAM_RUSER and PAM_RHOST items, because there is no way of |
| 54 |
* preventing local users to feed in arbitrary values for these. |
| 55 |
* They are being left undefined here, relying on modules and their |
| 56 |
* configuration to decide whether to accept requests from unknown users |
| 57 |
* at unknown hosts. (Always provided they have authenticated for the |
| 58 |
* local account, of course!) |
| 59 |
*/ |
| 60 |
|
| 61 |
#include "includes.h" |
| 62 |
|
| 63 |
#ifdef USE_PAM |
| 64 |
|
| 65 |
#include "xmalloc.h" |
| 66 |
#include "log.h" |
| 67 |
#include "readpass.h" |
| 68 |
|
| 69 |
#define SSHD_PAM_SERVICE "sshd" |
| 70 |
|
| 71 |
#ifdef HAVE___PROGNAME |
| 72 |
extern char *__progname; |
| 73 |
#else |
| 74 |
char *__progname; |
| 75 |
#endif |
| 76 |
|
| 77 |
int null_pam_conv; |
| 78 |
|
| 79 |
/* |
| 80 |
* This is a downstripped version of do_pam_conversation() in pam-auth.c, |
| 81 |
* with the INITIAL_LOGIN mode removed. At the time this program can be |
| 82 |
* run we already are authenticated and do have STDIN available. |
| 83 |
* The null_pam_conv flag enforces modules to be silent even if |
| 84 |
* they do not honor the PAM_SILENT flag. We just don't need to tell |
| 85 |
* the user again that passwords are expired when calling pam_acct_mgmt(), |
| 86 |
* as this has already been done by the calling daemon. |
| 87 |
*/ |
| 88 |
static int do_pam_conversation(int num_msg, const struct pam_message **msg, |
| 89 |
struct pam_response **resp, void *appdata_ptr) |
| 90 |
{ |
| 91 |
struct pam_response *reply; |
| 92 |
int count; |
| 93 |
char buf[1024]; |
| 94 |
|
| 95 |
/* PAM will free this later */ |
| 96 |
reply = xmalloc(num_msg * sizeof(*reply)); |
| 97 |
|
| 98 |
for (count = 0; count < num_msg; count++) { |
| 99 |
/* |
| 100 |
* stdio is connected, so interact directly |
| 101 |
*/ |
| 102 |
switch(PAM_MSG_MEMBER(msg, count, msg_style)) { |
| 103 |
case PAM_PROMPT_ECHO_ON: |
| 104 |
if (null_pam_conv) { |
| 105 |
xfree(reply); |
| 106 |
return PAM_CONV_ERR; |
| 107 |
} |
| 108 |
fputs(PAM_MSG_MEMBER(msg, count, msg), stderr); |
| 109 |
fgets(buf, sizeof(buf), stdin); |
| 110 |
reply[count].resp = xstrdup(buf); |
| 111 |
reply[count].resp_retcode = PAM_SUCCESS; |
| 112 |
break; |
| 113 |
case PAM_PROMPT_ECHO_OFF: |
| 114 |
if (null_pam_conv) { |
| 115 |
xfree(reply); |
| 116 |
return PAM_CONV_ERR; |
| 117 |
} |
| 118 |
/* |
| 119 |
* Lets's be a bit more paranoid than the original and |
| 120 |
* avoid the read_passphrase() wrapper. There is no |
| 121 |
* need to care for non-tty, and eventually call |
| 122 |
* ssh-askpass, as we don't have X11-forwarding at |
| 123 |
* this point anyway. |
| 124 |
*/ |
| 125 |
if (readpassphrase(PAM_MSG_MEMBER(msg, count, msg), |
| 126 |
buf, sizeof(buf), RPP_REQUIRE_TTY)) { |
| 127 |
reply[count].resp = xstrdup(buf); |
| 128 |
} else { |
| 129 |
reply[count].resp = xstrdup(""); |
| 130 |
} |
| 131 |
memset(buf, 'x', sizeof(buf)); |
| 132 |
reply[count].resp_retcode = PAM_SUCCESS; |
| 133 |
break; |
| 134 |
case PAM_ERROR_MSG: |
| 135 |
case PAM_TEXT_INFO: |
| 136 |
if (!null_pam_conv && PAM_MSG_MEMBER(msg, count, msg) != NULL) |
| 137 |
fprintf(stderr, "%s\n", |
| 138 |
PAM_MSG_MEMBER(msg, count, msg)); |
| 139 |
reply[count].resp = xstrdup(""); |
| 140 |
reply[count].resp_retcode = PAM_SUCCESS; |
| 141 |
break; |
| 142 |
default: |
| 143 |
xfree(reply); |
| 144 |
return PAM_CONV_ERR; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
*resp = reply; |
| 149 |
|
| 150 |
return PAM_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
int main(int argc, char *argv[]) { |
| 154 |
struct passwd *pw; |
| 155 |
uid_t uid; |
| 156 |
uid_t euid; |
| 157 |
struct pam_conv conv = { |
| 158 |
(int (*)())do_pam_conversation, |
| 159 |
NULL |
| 160 |
}; |
| 161 |
pam_handle_t *pamh; |
| 162 |
int pam_retval; |
| 163 |
int retval = 0; |
| 164 |
|
| 165 |
__progname = get_progname(argv[0]); |
| 166 |
log_init(__progname, SYSLOG_LEVEL_INFO, |
| 167 |
SYSLOG_FACILITY_AUTH, 0); |
| 168 |
|
| 169 |
uid = getuid(); |
| 170 |
euid = geteuid(); |
| 171 |
if (euid != 0) |
| 172 |
fatal("Cannot change authtok using effective uid %ld", (long)euid); |
| 173 |
|
| 174 |
switch (argc) { |
| 175 |
case 2: |
| 176 |
if (!(pw = getpwnam(argv[1]))) |
| 177 |
fatal("Cannot get uid of user %.200s: %.200s", |
| 178 |
argv[1], strerror(errno)); |
| 179 |
if (uid && pw->pw_uid != uid) |
| 180 |
fatal("Cannot chauthtok for user %.200s using uid %ld", |
| 181 |
argv[1], (long)uid); |
| 182 |
break; |
| 183 |
default: |
| 184 |
fprintf(stderr, "Usage: ssh-chauthtok-helper <user>\n"); |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
/* drop root real uid when changing authtok for another user */ |
| 189 |
if (!uid && setuid(uid = pw->pw_uid)) |
| 190 |
fatal("Cannot setuid %ld: %.200s", (long)uid, strerror(errno)); |
| 191 |
|
| 192 |
null_pam_conv = 1; |
| 193 |
|
| 194 |
pam_retval = pam_start(SSHD_PAM_SERVICE, pw->pw_name, &conv, &pamh); |
| 195 |
switch (pam_retval) { |
| 196 |
case PAM_SUCCESS: |
| 197 |
break; |
| 198 |
default: |
| 199 |
fatal("PAM initialisation failed[%d]: %.200s", |
| 200 |
pam_retval, PAM_STRERROR(pamh, pam_retval)); |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
/* |
| 205 |
* Some PAM implemetations (HP-UX) require pam_acct_mgmt() to |
| 206 |
* be invoked before they can identify expired tokens on |
| 207 |
* pam_chauthtok(). |
| 208 |
*/ |
| 209 |
pam_retval = pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK); |
| 210 |
switch (pam_retval) { |
| 211 |
case PAM_SUCCESS: |
| 212 |
/* |
| 213 |
* We were called for changing expired authentication |
| 214 |
* tokens and found nothing to do! The caller should not |
| 215 |
* consider this success... |
| 216 |
*/ |
| 217 |
log("No expired authentication tokens found"); |
| 218 |
retval = 255; |
| 219 |
break; |
| 220 |
case PAM_NEW_AUTHTOK_REQD: |
| 221 |
break; |
| 222 |
default: |
| 223 |
fatal("PAM pam_acct_mgmt failed[%d]: %.200s", |
| 224 |
pam_retval, PAM_STRERROR(pamh, pam_retval)); |
| 225 |
break; |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* pam_chauthtok() will perform authentication of its own, if required. |
| 230 |
* For password change it will prompt for the old password only if |
| 231 |
* the real uid is non-zero and the effective uid is zero. It doesn't |
| 232 |
* work with the effective uid being non-zero, which is why this |
| 233 |
* program exists... |
| 234 |
*/ |
| 235 |
if (!retval) { |
| 236 |
null_pam_conv = 0; |
| 237 |
pam_retval = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 238 |
switch (pam_retval) { |
| 239 |
case PAM_SUCCESS: |
| 240 |
break; |
| 241 |
default: |
| 242 |
log("PAM pam_chauthtok failed[%d]: %.200s", |
| 243 |
pam_retval, PAM_STRERROR(pamh, pam_retval)); |
| 244 |
break; |
| 245 |
} |
| 246 |
retval = pam_retval; |
| 247 |
null_pam_conv = 1; |
| 248 |
} |
| 249 |
|
| 250 |
pam_retval = pam_end(pamh, pam_retval); |
| 251 |
switch (pam_retval) { |
| 252 |
case PAM_SUCCESS: |
| 253 |
break; |
| 254 |
default: |
| 255 |
log("PAM pam_end failed[%d]: %.200s", |
| 256 |
pam_retval, PAM_STRERROR(pamh, pam_retval)); |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
return retval; |
| 261 |
} |
| 262 |
|
| 263 |
#else /* USE_PAM */ |
| 264 |
|
| 265 |
int main(int argc, char *argv[]) { |
| 266 |
fprintf(stderr, "Sorry, no PAM support...\n"); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
#endif /* USE_PAM */ |