-
Notifications
You must be signed in to change notification settings - Fork 2
/
pam_trimspaces.c
51 lines (40 loc) · 1.07 KB
/
pam_trimspaces.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
#define PAM_SM_AUTH
#include <security/pam_modules.h>
#include <string.h>
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
const char *user;
int ret;
ret = pam_get_item(pamh, PAM_USER, (const void **) &user);
if (ret != PAM_SUCCESS) {
return PAM_AUTH_ERR;
}
if (user == NULL) {
return PAM_SUCCESS;
}
size_t len = strlen(user);
char copy[len + 1];
char *new_user = copy;
memcpy(copy, user, len + 1);
// strip trailing spaces
for (int i = len - 1; i >= 0; i--) {
if (copy[i] != ' ') {
break;
}
copy[i] = '\0';
}
// strip leading spaces
for (int i = 0; i < len; i++) {
if (copy[i] != ' ') {
break;
}
new_user = ©[i + 1];
}
ret = pam_set_item(pamh, PAM_USER, new_user);
if (ret != PAM_SUCCESS) {
return PAM_AUTH_ERR;
}
return PAM_SUCCESS;
}
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return PAM_SUCCESS;
}