1 |
#include "pamwrapper.h" |
2 |
|
3 |
#include <string> |
4 |
#include <vector> |
5 |
|
6 |
using std::string; |
7 |
using std::vector; |
8 |
|
9 |
extern "C" int conversation(int num_msg, const pam_message **msg, |
10 |
pam_response **resp, void *appdata_prt) |
11 |
{ |
12 |
|
13 |
if (num_msg <= 0) |
14 |
return PAM_CONV_ERR; |
15 |
|
16 |
PamWrapper *wrap = (PamWrapper*) appdata_prt; |
17 |
|
18 |
pam_response* reply = static_cast<pam_response*> |
19 |
(::calloc(num_msg, sizeof(pam_response))); |
20 |
if (!reply) |
21 |
return PAM_SYSTEM_ERR; |
22 |
|
23 |
for (int replies = 0; replies < num_msg; replies++) { |
24 |
switch (msg[replies]->msg_style) { |
25 |
case PAM_PROMPT_ECHO_OFF: { |
26 |
string tmp = wrap->promptEchoOff(msg[replies]->msg); |
27 |
reply[replies].resp = (char*) tmp.c_str(); |
28 |
reply[replies].resp_retcode = PAM_SUCCESS; |
29 |
break; |
30 |
} |
31 |
case PAM_PROMPT_ECHO_ON: { |
32 |
string tmp = wrap->promptEchoOn(msg[replies]->msg); |
33 |
reply[replies].resp = (char*) tmp.c_str(); |
34 |
reply[replies].resp_retcode = PAM_SUCCESS; |
35 |
break; |
36 |
} |
37 |
case PAM_ERROR_MSG: { |
38 |
wrap->errorMsg(msg[replies]->msg); |
39 |
reply[replies].resp_retcode = PAM_SUCCESS; |
40 |
break; |
41 |
} |
42 |
case PAM_TEXT_INFO: { |
43 |
wrap->textInfo(msg[replies]->msg); |
44 |
reply[replies].resp_retcode = PAM_SUCCESS; |
45 |
break; |
46 |
} |
47 |
default: |
48 |
::free(reply); |
49 |
return PAM_CONV_ERR; |
50 |
} |
51 |
} |
52 |
|
53 |
*resp = reply; |
54 |
return PAM_SUCCESS; |
55 |
} |
56 |
|
57 |
PamWrapper::PamWrapper(string service ) |
58 |
{ |
59 |
m_pamh = NULL; |
60 |
m_isStarted = false; |
61 |
m_service = service; |
62 |
m_conv.conv = conversation; |
63 |
m_conv.appdata_ptr = (void*)this; |
64 |
} |
65 |
|
66 |
PamWrapper::~PamWrapper() |
67 |
{ |
68 |
if ( m_isStarted ) |
69 |
end(); |
70 |
} |
71 |
|
72 |
int PamWrapper::start(string username) |
73 |
{ |
74 |
m_isStarted = true; |
75 |
if (username.size() == 0) |
76 |
return pam_start(m_service.c_str(), NULL, &m_conv, &m_pamh); |
77 |
else |
78 |
return pam_start(m_service.c_str(), username.c_str(), &m_conv, &m_pamh); |
79 |
} |
80 |
|
81 |
int PamWrapper::end(int status) |
82 |
{ |
83 |
m_isStarted = false; |
84 |
return pam_end(m_pamh,status); |
85 |
} |
86 |
|
87 |
int PamWrapper::authenticate( int flags ) |
88 |
{ |
89 |
return pam_authenticate(m_pamh, flags); |
90 |
} |
91 |
|
92 |
int PamWrapper::account(int flags ) |
93 |
{ |
94 |
return pam_acct_mgmt(m_pamh, flags); |
95 |
} |
96 |
|
97 |
int PamWrapper::setCred(int flags) |
98 |
{ |
99 |
return pam_setcred(m_pamh,flags); |
100 |
} |
101 |
|
102 |
int PamWrapper::openSession(int flags) |
103 |
{ |
104 |
return pam_open_session(m_pamh, flags); |
105 |
} |
106 |
|
107 |
int PamWrapper::closeSession(int flags) |
108 |
{ |
109 |
return pam_close_session(m_pamh, flags); |
110 |
} |
111 |
|
112 |
int PamWrapper::changeAuthToken(int flags) |
113 |
{ |
114 |
return pam_chauthtok(m_pamh, flags); |
115 |
} |
116 |
|
117 |
string PamWrapper::strError(int err) |
118 |
{ |
119 |
return pam_strerror(m_pamh, err); |
120 |
} |
121 |
|
122 |
int PamWrapper::putEnv(string nameValue) |
123 |
{ |
124 |
return pam_putenv(m_pamh, nameValue.c_str() ); |
125 |
} |
126 |
|
127 |
string PamWrapper::getEnv(string name) |
128 |
{ |
129 |
const char *buf = pam_getenv(m_pamh, name.c_str() ); |
130 |
return (buf != NULL) ? string(buf) : string("NULL"); |
131 |
} |
132 |
|
133 |
|
134 |
int PamWrapper::failDelay(unsigned int usec) |
135 |
{ |
136 |
#ifdef PAM_FAIL_DELAY |
137 |
return pam_fail_delay(m_pamh, usec); |
138 |
#else |
139 |
#warning PAM_FAIL_DELAY not defined |
140 |
return 0; |
141 |
#endif //PAM_FAIL_DELAY |
142 |
} |
143 |
|
144 |
|
145 |
int PamWrapper::setItem(int item_type, const void *item) |
146 |
{ |
147 |
return pam_set_item(m_pamh, item_type, item); |
148 |
} |
149 |
|
150 |
int PamWrapper::getItem(int item_type, const void **item) |
151 |
{ |
152 |
return pam_get_item(m_pamh, item_type, item); |
153 |
} |
154 |
|
155 |
bool PamWrapper::checkUser(string username, bool checkAccount) |
156 |
{ |
157 |
this->start(username); |
158 |
int auth = this->authenticate(); |
159 |
int acc = checkAccount ? this->account() : PAM_SUCCESS; |
160 |
this->end(); |
161 |
return (auth == PAM_SUCCESS && acc == PAM_SUCCESS); |
162 |
} |
163 |
|
164 |
/* |
165 |
vector<string> PamWrapper::getEnvList() |
166 |
{ |
167 |
vector<string> output; |
168 |
char **buf = pam_getenvlist(m_pamh); |
169 |
|
170 |
for (char **ptr = buf; ptr != NULL; ptr++) { |
171 |
string tmp = string(*ptr); |
172 |
output.push_back(tmp); |
173 |
} |
174 |
std::free(buf); |
175 |
|
176 |
return output; |
177 |
} |
178 |
*/ |