1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <ttyent.h>
#include <pwd.h>
#define PAM_SM_ACCOUNT
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_mod_misc.h>
#define TTY_PREFIX "/dev/"
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh, int flags,
int argc, const char * argv[])
{
struct passwd *pwd;
struct ttyent *ttyinfo;
const char *user;
const char *tty;
int pam_err;
if ( ( (pam_err = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS )
|| ( user == NULL ) ) {
PAM_ERROR("Error recovering username.");
return (pam_err);
}
if ( (pwd = getpwnam(user)) == NULL ) {
PAM_ERROR("Could not get passwd entry for user [%s]",user);
return (PAM_SERVICE_ERR);
}
if ( pwd->pw_uid != 0 ) {
/* secure tty applies only to root */
return (PAM_SUCCESS);
}
if ( (pam_err = pam_get_item(pamh, PAM_TTY,(void *) &tty) ) != PAM_SUCCESS ) {
return (pam_err);
}
if (tty != NULL && strncmp(TTY_PREFIX, tty, sizeof(TTY_PREFIX)) == 0) {
PAM_LOG("tty starts with " TTY_PREFIX);
/* skip prefix */
tty = (const char *)tty + sizeof(TTY_PREFIX) - 1;
}
/*
* Linux-PAM, before checking the actual tty,
* opens /etc/securettys to check if it's world
* writable or not a normal file and only continues
* if neither is correct. Sounds like a good idea -
* maybe it should be done here as well...
*/
if ( tty != NULL && (ttyinfo = getttynam(tty)) != NULL &&
(ttyinfo->ty_status & TTY_SECURE) != 0)
return (PAM_SUCCESS);
PAM_ERROR("Access denied: tty %s is not secure", tty);
return (PAM_AUTH_ERR);
}
PAM_MODULE_ENTRY("pam_securetty");
|