-
Notifications
You must be signed in to change notification settings - Fork 4
/
PAM.cpp
275 lines (231 loc) · 8.28 KB
/
PAM.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
/* SLiM - Simple Login Manager
Copyright (C) 2007 Martin Parm
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include <string>
#include <iostream>
#include "PAM.h"
namespace PAM {
Exception::Exception(pam_handle_t* _pam_handle,
const std::string& _func_name,
int _errnum):
errnum(_errnum),
errstr(pam_strerror(_pam_handle, _errnum)),
func_name(_func_name)
{}
Exception::~Exception(void){}
Auth_Exception::Auth_Exception(pam_handle_t* _pam_handle,
const std::string& _func_name,
int _errnum):
Exception(_pam_handle, _func_name, _errnum){}
Cred_Exception::Cred_Exception(pam_handle_t* _pam_handle,
const std::string& _func_name,
int _errnum):
Exception(_pam_handle, _func_name, _errnum){}
int Authenticator::_end(void){
int result=pam_end(pam_handle, last_result);
pam_handle=0;
return result;
}
Authenticator::Authenticator(conversation* conv, void* data):
pam_handle(0),
last_result(PAM_SUCCESS)
{
pam_conversation.conv=conv;
pam_conversation.appdata_ptr=data;
}
Authenticator::~Authenticator(void){
if (pam_handle) _end();
}
void Authenticator::start(const std::string& service){
switch((last_result=pam_start(service.c_str(), NULL, &pam_conversation, &pam_handle))){
default:
throw Exception(pam_handle, "pam_start()", last_result);
case PAM_SUCCESS:
break;
}
return;
}
void Authenticator::end(void){
switch((last_result=_end())){
default:
throw Exception(pam_handle, "pam_end()", last_result);
case PAM_SUCCESS:
break;
}
return;
}
void Authenticator::set_item(const Authenticator::ItemType item, const void* value){
switch((last_result=pam_set_item(pam_handle, item, value))){
default:
_end();
throw Exception(pam_handle, "pam_set_item()", last_result);
case PAM_SUCCESS:
break;
}
return;
}
const void* Authenticator::get_item(const Authenticator::ItemType item){
const void* data;
switch ((last_result=pam_get_item(pam_handle, item, &data))){
default:
case PAM_SYSTEM_ERR:
#ifdef __LIBPAM_VERSION
case PAM_BAD_ITEM:
#endif
_end();
throw Exception(pam_handle, "pam_get_item()", last_result);
case PAM_PERM_DENIED: // The value of item was NULL
case PAM_SUCCESS:
break;
}
return data;
}
#ifdef __LIBPAM_VERSION
void Authenticator::fail_delay(const unsigned int micro_sec){
switch((last_result=pam_fail_delay(pam_handle, micro_sec))){
default:
_end();
throw Exception(pam_handle, "fail_delay()", last_result);
case PAM_SUCCESS:
break;
}
return;
}
#endif
void Authenticator::authenticate(void){
switch((last_result=pam_authenticate(pam_handle, 0))){
default:
case PAM_ABORT:
case PAM_AUTHINFO_UNAVAIL:
_end();
throw Exception(pam_handle, "pam_authenticate()", last_result);
case PAM_USER_UNKNOWN:
case PAM_MAXTRIES:
case PAM_CRED_INSUFFICIENT:
case PAM_AUTH_ERR:
throw Auth_Exception(pam_handle, "pam_authentication()", last_result);
case PAM_SUCCESS:
break;
}
switch((last_result=pam_acct_mgmt(pam_handle, PAM_SILENT))){
// The documentation and implementation of Linux PAM differs:
// PAM_NEW_AUTHTOKEN_REQD is described in the documentation but
// don't exists in the actual implementation. This issue needs
// to be fixes at some point.
default:
//case PAM_NEW_AUTHTOKEN_REQD:
case PAM_ACCT_EXPIRED:
case PAM_USER_UNKNOWN:
_end();
throw Exception(pam_handle, "pam_acct_mgmt()", last_result);
case PAM_AUTH_ERR:
case PAM_PERM_DENIED:
throw Auth_Exception(pam_handle, "pam_acct_mgmt()", last_result);
case PAM_SUCCESS:
break;
};
return;
}
void Authenticator::open_session(void){
switch((last_result=pam_setcred(pam_handle, PAM_ESTABLISH_CRED))){
default:
case PAM_CRED_ERR:
case PAM_CRED_UNAVAIL:
_end();
throw Exception(pam_handle, "pam_setcred()", last_result);
case PAM_CRED_EXPIRED:
case PAM_USER_UNKNOWN:
throw Cred_Exception(pam_handle, "pam_setcred()", last_result);
case PAM_SUCCESS:
break;
}
switch((last_result=pam_open_session(pam_handle, 0))){
// The documentation and implementation of Linux PAM differs:
// PAM_SESSION_ERROR is described in the documentation but
// don't exists in the actual implementation. This issue needs
// to be fixes at some point.
default:
//case PAM_SESSION_ERROR:
pam_setcred(pam_handle, PAM_DELETE_CRED);
_end();
throw Exception(pam_handle, "pam_open_session()", last_result);
case PAM_SUCCESS:
break;
};
return;
}
void Authenticator::close_session(void){
switch((last_result=pam_close_session(pam_handle, 0))){
// The documentation and implementation of Linux PAM differs:
// PAM_SESSION_ERROR is described in the documentation but
// don't exists in the actual implementation. This issue needs
// to be fixes at some point.
default:
//case PAM_SESSION_ERROR:
pam_setcred(pam_handle, PAM_DELETE_CRED);
_end();
throw Exception(pam_handle, "pam_close_session", last_result);
case PAM_SUCCESS:
break;
};
switch((last_result=pam_setcred(pam_handle, PAM_DELETE_CRED))){
default:
case PAM_CRED_ERR:
case PAM_CRED_UNAVAIL:
case PAM_CRED_EXPIRED:
case PAM_USER_UNKNOWN:
_end();
throw Exception(pam_handle, "pam_setcred()", last_result);
case PAM_SUCCESS:
break;
}
return;
}
void Authenticator::setenv(const std::string& key, const std::string& value){
std::string name_value = key+"="+value;
switch((last_result=pam_putenv(pam_handle, name_value.c_str()))){
default:
case PAM_PERM_DENIED:
case PAM_ABORT:
case PAM_BUF_ERR:
#ifdef __LIBPAM_VERSION
case PAM_BAD_ITEM:
#endif
_end();
throw Exception(pam_handle, "pam_putenv()", last_result);
case PAM_SUCCESS:
break;
};
return;
}
void Authenticator::delenv(const std::string& key){
switch((last_result=pam_putenv(pam_handle, key.c_str()))){
default:
case PAM_PERM_DENIED:
case PAM_ABORT:
case PAM_BUF_ERR:
#ifdef __LIBPAM_VERSION
case PAM_BAD_ITEM:
#endif
_end();
throw Exception(pam_handle, "pam_putenv()", last_result);
case PAM_SUCCESS:
break;
};
return;
}
const char* Authenticator::getenv(const std::string& key){
return pam_getenv(pam_handle, key.c_str());
}
char** Authenticator::getenvlist(void){
return pam_getenvlist(pam_handle);
}
};
std::ostream& operator<<( std::ostream& os, const PAM::Exception& e){
os << e.func_name << ": " << e.errstr;
return os;
}