-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathauth.c
423 lines (374 loc) · 10.1 KB
/
auth.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001=2020 Corey Minyard <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only
*
* In addition, as a special exception, the copyright holders of
* ser2net give you permission to combine ser2net with free software
* programs or libraries that are released under the GNU LGPL and
* with code included in the standard release of OpenSSL under the
* OpenSSL license (or modified versions of such code, with unchanged
* license). You may copy and distribute such a system following the
* terms of the GNU GPL for ser2net and the licenses of the other code
* concerned, provided that you include the source code of that
* other code when and as the GNU GPL requires distribution of source
* code.
*
* Note that people who make modified versions of ser2net are not
* obligated to grant this special exception for their modified
* versions; it is their choice whether to do so. The GNU General
* Public License gives permission to release a modified version
* without this exception; this exception also makes it possible to
* release a modified version which carries forward this exception.
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <dirent.h>
#include <gensio/gensio.h>
#include <gensio/gensio_list.h>
#include "ser2net.h"
#if defined(USE_PAM)
#include <pwd.h>
#include <security/pam_appl.h>
#endif
/*
* Ambiguity in spec: is it an array of pointers or a pointer to an array?
* Stolen from openssh.
*/
#ifdef PAM_SUN_CODEBASE
# define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
#else
# define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
#endif
struct user {
struct gensio_link link;
char *name;
};
int
add_allowed_users(struct gensio_list **users, const char *istr,
struct absout *eout)
{
char *name, *strtok_data, *str = NULL;
if (!*users) {
*users = malloc(sizeof(**users));
if (!*users)
goto out_nomem;
gensio_list_init(*users);
}
str = strdup(istr);
if (!str)
goto out_nomem;
name = strtok_r(str, " \t", &strtok_data);
while (name) {
struct user *user = malloc(sizeof(*user));
if (!user)
goto out_nomem;
memset(user, 0, sizeof(*user));
user->name = strdup(name);
if (!user->name) {
free(user);
goto out_nomem;
}
gensio_list_add_tail(*users, &user->link);
name = strtok_r(NULL, " \t", &strtok_data);
}
free(str);
return 0;
out_nomem:
if (str)
free(str);
eout->out(eout, "Out of memory allocating allowed user list");
return GE_NOMEM;
}
static bool
is_user_present(const struct gensio_list *users, char *name)
{
struct gensio_link *u;
if (!users)
return true;
gensio_list_for_each(users, u) {
struct user *user = gensio_container_of(u, struct user, link);
if (strcmp(user->name, name) == 0)
return true;
}
return false;
}
void
free_user_list(struct gensio_list *users)
{
struct gensio_link *u, *u2;
if (!users)
return;
gensio_list_for_each_safe(users, u, u2) {
struct user *user = gensio_container_of(u, struct user, link);
gensio_list_rm(users, u);
free(user->name);
free(user);
}
free(users);
}
/*
* The next few functions are for authentication handling.
*/
static int
handle_auth_begin(struct gensio *net, const char *authdir, const char *pamauth,
const struct gensio_list *allowed_users)
{
gensiods len;
char username[100];
int err;
len = sizeof(username);
err = gensio_control(net, 0, true, GENSIO_CONTROL_USERNAME, username,
&len);
if (err) {
seout.out(&seout, "No username provided by remote: %s",
gensio_err_to_str(err));
return GE_AUTHREJECT;
}
if (!is_user_present(allowed_users, username)) {
seout.out(&seout,
"Username not allowed for this connection: %s",
username);
return GE_AUTHREJECT;
}
#if defined(USE_PAM)
/* set user-specific authdir if it exists. */
if (pamauth) {
char userauthdir[1000];
DIR *dir;
struct passwd *pw;
pw = getpwnam(username);
if (pw) {
len = snprintf(userauthdir, sizeof(userauthdir),
"%s/.gtlssh/allowed_certs/", pw->pw_dir);
dir = opendir(userauthdir);
if (dir) {
closedir(dir);
err = gensio_control(net, 0, GENSIO_CONTROL_SET,
GENSIO_CONTROL_CERT_AUTH, userauthdir, &len);
if (err) {
seout.out(&seout,
"Could not set authdir %s: %s",
userauthdir, gensio_err_to_str(err));
return GE_NOTSUP;
}
}
}
}
#endif
return GE_NOTSUP;
}
static int
handle_precert(struct gensio *net, const char *authdir)
{
gensiods len;
char username[100];
char filename[PATH_MAX];
int err;
char *s = username;
len = sizeof(username);
err = gensio_control(net, 0, true, GENSIO_CONTROL_USERNAME, username,
&len);
if (err) {
/* Try to get the username from the cert common name. */
snprintf(username, sizeof(username), "-1,CN");
len = sizeof(username);
err = gensio_control(net, 0, true, GENSIO_CONTROL_GET_PEER_CERT_NAME,
username, &len);
if (err) {
seout.out(&seout,
"No username provided by remote or cert: %s",
gensio_err_to_str(err));
return GE_AUTHREJECT;
}
/* Skip over the <n>,CN, in the username output. */
s = strchr(username, ',');
if (s)
s = strchr(s + 1, ',');
if (!s) {
seout.out(&seout, "Got invalid username: %s",
username);
return GE_AUTHREJECT;
}
s++;
/* Set the username so it's available later. */
err = gensio_control(net, 0, false, GENSIO_CONTROL_USERNAME, s,
NULL);
if (err) {
seout.out(&seout, "Unable to set username to %s: %s", s,
gensio_err_to_str(err));
return GE_AUTHREJECT;
}
}
snprintf(filename, sizeof(filename), "%s%c%s%callowed_certs%c",
authdir, DIRSEP, s, DIRSEP, DIRSEP);
err = gensio_control(net, 0, false, GENSIO_CONTROL_CERT_AUTH,
filename, &len);
if (err && err != GE_CERTNOTFOUND) {
seout.out(&seout,
"Unable to set authdir to %s: %s", filename,
gensio_err_to_str(err));
}
return GE_NOTSUP;
}
static int
handle_password(struct gensio *net, const char *authdir, const char *password)
{
gensiods len;
char username[100];
char filename[PATH_MAX];
FILE *pwfile;
char readpw[100], *s;
int err;
len = sizeof(username);
err = gensio_control(net, 0, true, GENSIO_CONTROL_USERNAME, username,
&len);
if (err) {
seout.out(&seout, "No username provided by remote: %s",
gensio_err_to_str(err));
return GE_AUTHREJECT;
}
snprintf(filename, sizeof(filename), "%s/%s/password",
authdir, username);
pwfile = fopen(filename, "r");
if (!pwfile) {
seout.out(&seout, "Can't open password file %s", filename);
return GE_AUTHREJECT;
}
s = fgets(readpw, sizeof(readpw), pwfile);
fclose(pwfile);
if (!s) {
seout.out(&seout, "Can't read password file %s", filename);
return GE_AUTHREJECT;
}
s = strchr(readpw, '\n');
if (s)
*s = '\0';
if (strcmp(readpw, password) == 0)
return 0;
return GE_NOTSUP;
}
#if defined(USE_PAM)
static int
pam_conversation_cb(int num_msg, const struct pam_message **msg,
struct pam_response **pam_response, void *appdata_ptr)
{
int i;
const char *password = appdata_ptr;
struct pam_response *resp = NULL;
if (password == NULL) {
return PAM_CONV_ERR;
}
resp = calloc(num_msg, sizeof(struct pam_response));
if (resp == NULL) {
return PAM_BUF_ERR;
}
for (i = 0; i < num_msg; i++) {
resp[i].resp_retcode = 0;
switch(PAM_MSG_MEMBER(msg, i, msg_style)) {
case PAM_PROMPT_ECHO_ON:
case PAM_PROMPT_ECHO_OFF:
resp[i].resp = strdup(password);
if(resp[i].resp == NULL) {
goto error;
}
}
}
*pam_response = resp;
return PAM_SUCCESS;
error:
if (resp) {
for (i = 0; i < num_msg; i++) {
free(resp[i].resp);
}
free(resp);
}
return PAM_BUF_ERR;
}
static int
handle_password_pam(struct gensio *net, const char *pamauth, const char *password)
{
int ret = GE_AUTHREJECT;
char username[100];
gensiods len;
int err, pam_err = 0;
pam_handle_t *pamh = NULL;
struct pam_conv pam_conv;
len = sizeof(username);
err = gensio_control(net, 0, true, GENSIO_CONTROL_USERNAME, username,
&len);
if (err) {
seout.out(&seout, "No username provided by remote: %s",
gensio_err_to_str(err));
goto exit;
}
pam_conv.conv = pam_conversation_cb;
pam_conv.appdata_ptr = (char *)password;
pam_err = pam_start(pamauth, username, &pam_conv, &pamh);
if (pam_err != PAM_SUCCESS) {
seout.out(&seout, "Unable to start PAM transaction: %s",
pam_strerror(pamh, pam_err));
goto exit;
}
pam_err = pam_authenticate(pamh, PAM_SILENT);
if (pam_err != PAM_SUCCESS) {
seout.out(&seout, "PAM authentication failed: %s",
pam_strerror(pamh, pam_err)
);
goto exit;
} else {
seout.out(&seout, "Accepted password for %s\n", username);
}
pam_err = pam_acct_mgmt(pamh, 0);
if (pam_err == PAM_NEW_AUTHTOK_REQD) {
seout.out(&seout, "user %s password expired", username);
goto exit;
}
if (pam_err != PAM_SUCCESS) {
seout.out(&seout, "pam_acct_mgmt failed for %s: %s",
username, pam_strerror(pamh, pam_err));
goto exit;
}
ret = 0;
exit:
if (pamh) {
pam_end(pamh, pam_err);
}
return ret;
}
#endif
int
handle_acc_auth_event(const char *authdir, const char *pamauth,
const struct gensio_list *allowed_users,
int event, void *data)
{
switch (event) {
case GENSIO_ACC_EVENT_AUTH_BEGIN:
if (!authdir)
return 0;
return handle_auth_begin(data, authdir, pamauth, allowed_users);
case GENSIO_ACC_EVENT_PRECERT_VERIFY:
if (!authdir)
return 0;
return handle_precert(data, authdir);
case GENSIO_ACC_EVENT_PASSWORD_VERIFY: {
struct gensio_acc_password_verify_data *pwdata;
pwdata = (struct gensio_acc_password_verify_data *) data;
#if defined(USE_PAM)
if (pamauth) {
return handle_password_pam(pwdata->io, pamauth, pwdata->password);
} else
#endif
if (authdir) {
return handle_password(pwdata->io, authdir, pwdata->password);
} else {
return 0;
}
}
default:
return GE_NOTSUP;
}
}