|
Line 83
Link Here
|
|
|
83 |
#ifndef NO_PAM |
| 84 |
#include <security/pam_appl.h> |
| 85 |
#include <security/pam_misc.h> |
| 86 |
#endif |
| 87 |
|
|
Line 95
Link Here
|
|
|
100 |
#ifndef NO_PAM |
| 101 |
static int auth_pam __P((char *)); |
| 102 |
#endif |
| 103 |
|
|
Line 198
Link Here
|
|
|
207 |
#ifndef NO_PAM |
| 208 |
/* |
| 209 |
* Attempt to authenticate the user using PAM. Returns 0 if the user is |
| 210 |
* authenticated, or 1 if not authenticated. If some sort of PAM system |
| 211 |
* error occurs (e.g., the "/etc/pam.conf" file is missing) then this |
| 212 |
* function returns -1. This can be used as an indication that we should |
| 213 |
* fall back to a different authentication mechanism. |
| 214 |
*/ |
| 215 |
static int |
| 216 |
auth_pam(char *username) |
| 217 |
{ |
| 218 |
struct passwd *pawd; |
| 219 |
pam_handle_t *pamh = NULL; |
| 220 |
const char *tmpl_user; |
| 221 |
const void *item; |
| 222 |
int rval; |
| 223 |
char *tty, *ttyn; |
| 224 |
char hostname[MAXHOSTNAMELEN]; |
| 225 |
char tname[sizeof(_PATH_TTY) + 10]; |
| 226 |
int e; |
| 227 |
|
| 228 |
static struct pam_conv conv = { misc_conv, NULL }; |
| 229 |
|
| 230 |
ttyn = ttyname(STDIN_FILENO); |
| 231 |
|
| 232 |
if (ttyn == NULL || *ttyn == '\0') { |
| 233 |
(void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY); |
| 234 |
ttyn = tname; |
| 235 |
} |
| 236 |
if ((tty = strrchr(ttyn, '/')) != NULL) |
| 237 |
++tty; |
| 238 |
else |
| 239 |
tty = ttyn; |
| 240 |
|
| 241 |
rval = gethostname(hostname, sizeof(hostname)); |
| 242 |
|
| 243 |
if (rval < 0) { |
| 244 |
syslog(LOG_ERR, "auth_pam: Failed to resolve local hostname"); |
| 245 |
return -1; |
| 246 |
} |
| 247 |
if ((e = pam_start("rshd", username, &conv, &pamh)) != PAM_SUCCESS) { |
| 248 |
syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
if ((e = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS) { |
| 252 |
syslog(LOG_ERR, "pam_set_item(PAM_TTY): %s", |
| 253 |
pam_strerror(pamh, e)); |
| 254 |
return -1; |
| 255 |
} |
| 256 |
if (hostname != NULL && |
| 257 |
(e = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS) { |
| 258 |
syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", |
| 259 |
pam_strerror(pamh, e)); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
e = pam_authenticate(pamh, 0); |
| 264 |
|
| 265 |
|
| 266 |
switch (e) { |
| 267 |
|
| 268 |
case PAM_SUCCESS: |
| 269 |
/* |
| 270 |
* With PAM we support the concept of a "template" |
| 271 |
* user. The user enters a login name which is |
| 272 |
* authenticated by PAM, usually via a remote service |
| 273 |
* such as RADIUS or TACACS+. If authentication |
| 274 |
* succeeds, a different but related "template" name |
| 275 |
* is used for setting the credentials, shell, and |
| 276 |
* home directory. The name the user enters need only |
| 277 |
* exist on the remote authentication server, but the |
| 278 |
* template name must be present in the local password |
| 279 |
* database. |
| 280 |
* |
| 281 |
* This is supported by two various mechanisms in the |
| 282 |
* individual modules. However, from the application's |
| 283 |
* point of view, the template user is always passed |
| 284 |
* back as a changed value of the PAM_USER item. |
| 285 |
*/ |
| 286 |
if ((e = pam_get_item(pamh, PAM_USER, &item)) == |
| 287 |
PAM_SUCCESS) { |
| 288 |
tmpl_user = (const char *) item; |
| 289 |
if (strcmp(username, tmpl_user) != 0) |
| 290 |
pawd = getpwnam(tmpl_user); |
| 291 |
} else |
| 292 |
syslog(LOG_ERR, "Couldn't get PAM_USER: %s", pam_strerror(pamh, e)); |
| 293 |
rval = 0; |
| 294 |
break; |
| 295 |
|
| 296 |
case PAM_AUTH_ERR: |
| 297 |
case PAM_USER_UNKNOWN: |
| 298 |
case PAM_MAXTRIES: |
| 299 |
rval = 1; |
| 300 |
break; |
| 301 |
|
| 302 |
default: |
| 303 |
syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e)); |
| 304 |
rval = -1; |
| 305 |
break; |
| 306 |
} |
| 307 |
if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { |
| 308 |
syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); |
| 309 |
rval = -1; |
| 310 |
} |
| 311 |
return rval; |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
|
Line 216
Link Here
|
|
|
333 |
int retval; |
|
Line 429
Link Here
|
|
|
547 |
|
| 548 |
#ifndef NO_PAM |
| 549 |
retval = auth_pam(locuser); |
| 550 |
|
| 551 |
if (retval < 0) { |
| 552 |
syslog(LOG_ERR,"PAM authentication failed"); |
| 553 |
} |
| 554 |
else if (retval == 1) { |
| 555 |
syslog(LOG_ERR,"User %s failed PAM authentication",locuser); |
| 556 |
exit(1); |
| 557 |
} |
| 558 |
#endif |