-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.c
262 lines (224 loc) · 6.9 KB
/
public.c
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
258
259
260
261
262
/*
* The public APIs of the pam-afs-session PAM module.
*
* Provides the public pam_sm_authenticate, pam_sm_setcred,
* pam_sm_open_session, pam_sm_close_session, and pam_sm_chauthtok functions.
* These must all be specified in the same file to work with the symbol export
* and linking mechanism used in OpenPAM, since OpenPAM will mark them all as
* static functions and export a function table instead.
*
* Written by Russ Allbery <[email protected]>
* Copyright 2011
* The Board of Trustees of the Leland Stanford Junior University
* Copyright 2005, 2006, 2007, 2008, 2009 Russ Allbery <[email protected]>
* Copyright 2005 Andres Salomon <[email protected]>
* Copyright 1999, 2000 Frank Cusack <[email protected]>
*
* See LICENSE for licensing terms.
*/
/* Get prototypes for all of the functions. */
#define PAM_SM_ACCOUNT
#define PAM_SM_AUTH
#define PAM_SM_PASSWORD
#define PAM_SM_SESSION
#include <config.h>
#include <portable/system.h>
#include <portable/pam.h>
#include <internal.h>
#include <pam-util/args.h>
#include <pam-util/logging.h>
/*
* The main PAM interface for authorization checking.
*/
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
struct pam_args *args;
int pamret;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_AUTH_ERR;
goto done;
}
pamret = pamk5_context_fetch(args);
ENTRY(args, flags);
/*
* Succeed if the user did not use krb5 to login. Ideally, we should
* probably fail and require that the user set up policy properly in their
* PAM configuration, but it's not common for the user to do so and that's
* not how other krb5 PAM modules work. If we don't do this, root logins
* with the system root password fail, which is a bad failure mode.
*/
if (pamret != PAM_SUCCESS || args->config->ctx == NULL) {
pamret = PAM_IGNORE;
putil_debug(args, "skipping non-Kerberos login");
goto done;
}
pamret = pamk5_account(args);
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/*
* The main PAM interface for authentication. We also do authorization checks
* here, since many applications don't call pam_acct_mgmt.
*/
PAM_EXTERN int
pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
struct pam_args *args;
int pamret;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_SERVICE_ERR;
goto done;
}
ENTRY(args, flags);
pamret = pamk5_authenticate(args);
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/*
* The main PAM interface, in the auth stack, for establishing credentials
* obtained during authentication.
*/
PAM_EXTERN int
pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
struct pam_args *args;
bool refresh = false;
int pamret, allow;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_SERVICE_ERR;
goto done;
}
ENTRY(args, flags);
/*
* Special case. Just free the context data, which will destroy the
* ticket cache as well.
*/
if (flags & PAM_DELETE_CRED) {
pamret = pam_set_data(pamh, "pam_krb5", NULL, NULL);
if (pamret != PAM_SUCCESS)
putil_err_pam(args, pamret, "cannot clear context data");
goto done;
}
/*
* Reinitialization requested, which means that rather than creating a new
* ticket cache and setting KRB5CCNAME, we should figure out the existing
* ticket cache and just refresh its tickets.
*/
if (flags & (PAM_REINITIALIZE_CRED | PAM_REFRESH_CRED))
refresh = true;
if (refresh && (flags & PAM_ESTABLISH_CRED)) {
putil_err(args, "requested establish and refresh at the same time");
pamret = PAM_SERVICE_ERR;
goto done;
}
allow = PAM_REINITIALIZE_CRED | PAM_REFRESH_CRED | PAM_ESTABLISH_CRED;
if (!(flags & allow)) {
putil_err(args, "invalid pam_setcred flags %d", flags);
pamret = PAM_SERVICE_ERR;
goto done;
}
/* Do the work. */
pamret = pamk5_setcred(args, refresh);
/*
* Never return PAM_IGNORE from pam_setcred since this can confuse the
* Linux PAM library, at least for applications that call pam_setcred
* without pam_authenticate (possibly because authentication was done
* some other way), when used with jumps with the [] syntax. Since we
* do nothing in this case, and since the stack is already frozen from
* the auth group, success makes sense.
*
* Don't return an error here or the PAM stack will fail if pam-krb5 is
* used with [success=ok default=1], since jumps are treated as required
* during the second pass with pam_setcred.
*/
if (pamret == PAM_IGNORE)
pamret = PAM_SUCCESS;
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/*
* The main PAM interface for password changing.
*/
PAM_EXTERN int
pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
struct pam_args *args;
int pamret;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_AUTHTOK_ERR;
goto done;
}
pamret = pamk5_context_fetch(args);
ENTRY(args, flags);
/* We only support password changes. */
if (!(flags & PAM_UPDATE_AUTHTOK) && !(flags & PAM_PRELIM_CHECK)) {
putil_err(args, "invalid pam_chauthtok flags %d", flags);
pamret = PAM_AUTHTOK_ERR;
goto done;
}
pamret = pamk5_password(args, (flags & PAM_PRELIM_CHECK) != 0);
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/*
* The main PAM interface for opening a session.
*/
PAM_EXTERN int
pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
struct pam_args *args;
int pamret;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_SERVICE_ERR;
goto done;
}
ENTRY(args, flags);
pamret = pamk5_setcred(args, 0);
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/*
* The main PAM interface for closing a session.
*/
PAM_EXTERN int
pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
struct pam_args *args;
int pamret;
args = pamk5_init(pamh, flags, argc, argv);
if (args == NULL) {
pamret = PAM_SERVICE_ERR;
goto done;
}
ENTRY(args, flags);
pamret = pam_set_data(pamh, "pam_krb5", NULL, NULL);
if (pamret != PAM_SUCCESS)
putil_err_pam(args, pamret, "cannot clear context data");
done:
EXIT(args, pamret);
pamk5_free(args);
return pamret;
}
/* OpenPAM uses this macro to set up a table of entry points. */
#ifdef PAM_MODULE_ENTRY
PAM_MODULE_ENTRY("pam_krb5");
#endif