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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/* #include <pwd.h> */
#include <netdb.h>
#include <shadow.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_PASSWORD
#ifndef __linux__
#include <login_cap.h>
#endif
#include <security/pam_modules.h>
#include <security/pam_appl.h>
#include <pam_mod_misc.h>
/*
* User authentication
*/
PAM_EXTERN int
pam_sm_authenticate(pam_handle_t *pamh, int flags,
int argc , const char **argv ) {
#ifndef __linux__
login_cap_t *lc;
#endif
struct spwd *pwd;
const char *pass, *crypt_pass, *user;
int pam_err;
/* identify user */
if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
PAM_LOG("Authenticating as self.");
pwd = getspnam(getlogin());
} else {
if ((pam_err = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS) {
PAM_ERROR("Authenticating with uname %s failed.", user);
return (pam_err);
}
pwd = getspnam(user);
}
PAM_LOG("Authenticating user: %s", user);
/* get password */
if (pwd != NULL) {
PAM_LOG("Doing real authentication");
pass = pwd->sp_pwdp;
if (pass[0] == '\0') {
if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
openpam_get_option(pamh, PAM_OPT_NULLOK)){
PAM_ERROR("Authentication failed. Empty passwd not allowed");
return (PAM_SUCCESS);
}
pass = "*";
}
#ifndef __linux__
lc = login_getpwclass(pwd);
#endif
} else {
PAM_LOG("Doing dummy authentication");
pass = "*";
#ifndef __linux__
lc = login_getpwclass(NULL);
#endif
}
#ifndef __linux__
prompt = login_getcapstr(lc, "passwd_prompt", NULL, NULL);
pam_err = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, prompt);
login_close(lc);
#else
pam_err = pam_get_authtok(pamh, PAM_AUTHTOK, (const char **) &pass, NULL);
#endif
PAM_LOG("Got password for user %s", user);
if (pam_err == PAM_CONV_ERR)
return (pam_err);
if (pam_err != PAM_SUCCESS)
return (PAM_AUTH_ERR);
/* check shadow */
crypt_pass = crypt(pass, pwd->sp_pwdp);
if ( strcmp(crypt_pass, pwd->sp_pwdp) != 0 ) {
PAM_ERROR("Wrong password. Authentication failed.");
pam_err = PAM_AUTH_ERR;
} else {
PAM_LOG("Authentication completed succesfully");
pam_err = PAM_SUCCESS;
}
return (pam_err);
}
PAM_EXTERN int
pam_sm_setcred(pam_handle_t *pamh , int flags ,
int argc , const char *argv[] ) {
/*
* This functions takes care of renewing/initializing
* user credentials as well as gid/uids. Someday, it
* will be completed. For now, it's not very urgent.
*/
return (PAM_SUCCESS);
}
/*
* Account Management
*/
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags ,
int argc , const char *argv[] ) {
#ifndef __linux__
login_cap_t *lc;
#endif
struct spwd *pwd;
int pam_err;
const char *user;
time_t curtime;
#ifndef __linux__
const void *rhost, *tty;
char rhostip[MAXHOSTNAMELEN] = "";
#endif
/* Sanity checks for uname,pwd,tty,host etc */
pam_err = pam_get_user(pamh, &user, NULL);
if (pam_err != PAM_SUCCESS)
return (pam_err);
if (user == NULL || (pwd = getspnam(user)) == NULL)
return (PAM_SERVICE_ERR);
#ifndef __linux__
/*
* tty/host info are provided by login classes
* and cannot be used out of the box under Linux
* for sanity checking (BSD only). May need to
* be ported/rewritten to work on Linux as well.
* Time will tell...
*/
pam_err = pam_get_item(pamh, PAM_RHOST, &rhost);
if (pam_err != PAM_SUCCESS)
return (pam_err);
pam_err = pam_get_item(pamh, PAM_TTY, &tty);
if (pam_err != PAM_SUCCESS)
return (pam_err);
#endif
if (*pwd->sp_pwdp == '\0' &&
(flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
return (PAM_NEW_AUTHTOK_REQD);
#ifndef __linux__
lc = login_getpwclass(pwd);
if (lc == NULL) {
return (PAM_SERVICE_ERR);
}
#endif
/* Check if pw_lstchg or pw_expire is set */
if (pwd->sp_lstchg || pwd->sp_expire)
curtime = time(NULL) / (60 * 60 * 24);
if (pwd->sp_expire) {
if ( (curtime > pwd->sp_expire ) && ( pwd->sp_expire != -1 ) ) {
#ifndef __linux__
login_close(lc);
#endif
return (PAM_ACCT_EXPIRED);
} else if ( ( pwd->sp_expire - curtime < pwd->sp_warn) ) {
// pam_error(pamh, "Warning: your account expires on %s",
// ctime(&pwd->pw_expire));
}
}
if (pwd->sp_lstchg == 0 ) {
return (PAM_NEW_AUTHTOK_REQD);
}
/* check all other possibilities (mostly stolen from pam_tcb) */
if ((curtime > (pwd->sp_lstchg + pwd->sp_max + pwd->sp_inact)) &&
(pwd->sp_max != -1) && (pwd->sp_inact != -1) &&
(pwd->sp_lstchg != 0))
return (PAM_ACCT_EXPIRED);
if (((pwd->sp_lstchg + pwd->sp_max) < curtime) &&
(pwd->sp_max != -1))
return (PAM_ACCT_EXPIRED);
if ((curtime - pwd->sp_lstchg > pwd->sp_max)
&& (curtime - pwd->sp_lstchg > pwd->sp_inact)
&& (curtime - pwd->sp_lstchg > pwd->sp_max + pwd->sp_inact)
&& (pwd->sp_max != -1) && (pwd->sp_inact != -1))
return (PAM_ACCT_EXPIRED);
pam_err = (PAM_SUCCESS);
#ifndef __linux__
/* validate tty/host/time */
if (!auth_hostok(lc, rhost, rhostip) ||
!auth_ttyok(lc, tty) ||
!auth_timeok(lc, time(NULL)))
pam_err = PAM_AUTH_ERR;
login_close(lc);
#endif
return (pam_err);
}
/*
* Password Management
*/
PAM_EXTERN int
pam_sm_chautok(pam_handle_t *pamh, int flags,
int argc, const char *argv[])
{
}
PAM_MODULE_ENTRY("pam_unix")
|