-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_pam.c
219 lines (175 loc) · 4.74 KB
/
auth_pam.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
/*
* PAM authentication routines.
*/
#include "params.h"
#if (AUTH_PAM || AUTH_PAM_USERPASS) && !VIRTUAL_ONLY
#define _XOPEN_SOURCE 4
#define _XOPEN_SOURCE_EXTENDED
#define _XOPEN_VERSION 4
#define _XPG4_2
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <security/pam_appl.h>
#if (defined(__sun) || defined(__hpux)) && \
!defined(LINUX_PAM) && !defined(_OPENPAM)
#define lo_const /* Sun's PAM doesn't use const here */
#else
#define lo_const const
#endif
typedef lo_const void *pam_item_t;
#if USE_LIBPAM_USERPASS
#include <security/pam_userpass.h>
#else
#if AUTH_PAM_USERPASS
#include <security/pam_client.h>
#ifndef PAM_BP_RCONTROL
/* Linux-PAM prior to 0.74 */
#define PAM_BP_RCONTROL PAM_BP_CONTROL
#define PAM_BP_WDATA PAM_BP_DATA
#define PAM_BP_RDATA PAM_BP_DATA
#endif
#define USERPASS_AGENT_ID "userpass"
#define USERPASS_AGENT_ID_LENGTH 8
#define USERPASS_USER_MASK 0x03
#define USERPASS_USER_REQUIRED 1
#define USERPASS_USER_KNOWN 2
#define USERPASS_USER_FIXED 3
#endif
typedef struct {
char *user;
char *pass;
} pam_userpass_t;
static int pam_userpass_conv(int num_msg, lo_const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr)
{
pam_userpass_t *userpass = (pam_userpass_t *)appdata_ptr;
#if AUTH_PAM_USERPASS
pamc_bp_t prompt;
const char *input;
char *output;
char flags;
if (num_msg != 1 || msg[0]->msg_style != PAM_BINARY_PROMPT)
return PAM_CONV_ERR;
prompt = (pamc_bp_t)msg[0]->msg;
input = PAM_BP_RDATA(prompt);
if (PAM_BP_RCONTROL(prompt) != PAM_BPC_SELECT ||
strncmp(input, USERPASS_AGENT_ID "/", USERPASS_AGENT_ID_LENGTH + 1))
return PAM_CONV_ERR;
flags = input[USERPASS_AGENT_ID_LENGTH + 1];
input += USERPASS_AGENT_ID_LENGTH + 1 + 1;
if ((flags & USERPASS_USER_MASK) == USERPASS_USER_FIXED &&
strcmp(input, userpass->user))
return PAM_CONV_AGAIN;
if (!(*resp = malloc(sizeof(struct pam_response))))
return PAM_CONV_ERR;
prompt = NULL;
PAM_BP_RENEW(&prompt, PAM_BPC_DONE,
strlen(userpass->user) + 1 + strlen(userpass->pass));
output = PAM_BP_WDATA(prompt);
strcpy(output, userpass->user);
output += strlen(output) + 1;
memcpy(output, userpass->pass, strlen(userpass->pass));
(*resp)[0].resp_retcode = 0;
(*resp)[0].resp = (char *)prompt;
#else
char *string;
int i;
#if (defined(__sun) || defined(__hpux)) && \
!defined(LINUX_PAM) && !defined(_OPENPAM)
/*
* Insist on only one message per call because of differences in the
* layout of the "msg" parameter. It can be an array of pointers to
* struct pam_message (Linux-PAM, OpenPAM) or a pointer to an array of
* struct pam_message (Sun PAM). We only fully support the former.
*/
if (num_msg != 1)
return PAM_CONV_ERR;
#endif
if (!(*resp = malloc(num_msg * sizeof(struct pam_response))))
return PAM_CONV_ERR;
for (i = 0; i < num_msg; i++) {
string = NULL;
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_ON:
string = userpass->user;
case PAM_PROMPT_ECHO_OFF:
if (!string)
string = userpass->pass;
if (!(string = strdup(string)))
break;
case PAM_ERROR_MSG:
case PAM_TEXT_INFO:
(*resp)[i].resp_retcode = PAM_SUCCESS;
(*resp)[i].resp = string;
continue;
}
while (--i >= 0) {
if (!(*resp)[i].resp) continue;
memset((*resp)[i].resp, 0, strlen((*resp)[i].resp));
free((*resp)[i].resp);
(*resp)[i].resp = NULL;
}
free(*resp);
*resp = NULL;
return PAM_CONV_ERR;
}
#endif
return PAM_SUCCESS;
}
#endif /* USE_LIBPAM_USERPASS */
static int is_user_known(char *user)
{
struct passwd *pw;
if ((pw = getpwnam(user)))
memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
endpwent();
return pw != NULL;
}
struct passwd *auth_userpass(char *user, char *pass, int *known)
{
struct passwd *pw;
pam_handle_t *pamh;
pam_userpass_t userpass;
struct pam_conv conv = {pam_userpass_conv, &userpass};
pam_item_t item;
lo_const char *template;
int status;
*known = 0;
userpass.user = user;
userpass.pass = pass;
if (pam_start(AUTH_PAM_SERVICE, user, &conv, &pamh) != PAM_SUCCESS) {
*known = is_user_known(user);
return NULL;
}
if ((status = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
pam_end(pamh, status);
*known = is_user_known(user);
return NULL;
}
if ((status = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
pam_end(pamh, status);
*known = is_user_known(user);
return NULL;
}
status = pam_get_item(pamh, PAM_USER, &item);
if (status != PAM_SUCCESS) {
pam_end(pamh, status);
*known = is_user_known(user);
return NULL;
}
template = item;
template = strdup(template);
if (pam_end(pamh, PAM_SUCCESS) != PAM_SUCCESS || !template) {
*known = is_user_known(user);
return NULL;
}
if ((pw = getpwnam(template))) {
memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
*known = 1;
}
endpwent();
return pw;
}
#endif