forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuserfile.c
166 lines (144 loc) · 4.1 KB
/
userfile.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include "env_settings.h" // for GetStringSetting
#include "logging.h" // for Log, LogErrno
#include "userfile.h"
const char *userfile_match_string[] = {
"NO_MATCH",
"USERNAME_MATCH",
"GROUP_MATCH"
};
int IsMemberOfGroup(const char *username, const char *grname, int *match) {
int i, ngroups;
gid_t *groups;
struct passwd *pw;
struct group *gr;
*match = 0;
pw = getpwnam(username);
if (pw == NULL) {
return USERFILE_ERROR;
}
ngroups = 0;
getgrouplist(username, pw->pw_gid, NULL, &ngroups);
groups = malloc(ngroups * sizeof (gid_t));
if (groups == NULL) {
Log("Unable to allocate memory");
return USERFILE_ERROR;
}
if (getgrouplist(username, pw->pw_gid, groups, &ngroups) == -1) {
Log("getgrouplist returned -1");
return USERFILE_ERROR;
}
for (i = 0; i < ngroups; i++) {
gr = getgrgid(groups[i]);
if (gr != NULL) {
if (!strcmp(grname, gr->gr_name)) {
*match = 1;
break;
}
}
}
free(groups);
return USERFILE_SUCCESS;
}
int UserInAuthListFile(const char * filename, const char* username, int *match) {
int rtn = USERFILE_SUCCESS;
*match = 0;
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
Log("Failed to open user file %s", filename);
return USERFILE_ERROR;
}
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, fp)) != -1) {
// Remove newline at end
line[strcspn(line, "\r\n")] = 0;
// Tokenize
const char *tokens = "\t :";
char *_line;
char *token = strtok_r(line, tokens, &_line);
if (token[0] != '#') {
if(token[0] == '@') {
// Process a group
token++; // Remove @
if (IsMemberOfGroup(username, token, match) != USERFILE_SUCCESS) {
*match = USERFILE_NO_MATCH;
rtn = USERFILE_ERROR;
break;
} else {
if (*match) {
*match = USERFILE_GROUP_MATCH;
rtn = USERFILE_SUCCESS;
}
}
} else {
if (strcmp(username, token) == 0) {
// We have a match
*match = USERFILE_USER_MATCH;
rtn = USERFILE_SUCCESS;
}
}
if (*match) {
while ((token = strtok_r(NULL, tokens, &_line)) != NULL) {
// Proces tokens
}
break;
}
}
}
free(line);
fclose(fp);
return rtn;
}
int UserInAuthListPriv(const char* username, int *match) {
int rtn;
#ifdef SECURE
const char *filename = USERFILE_PRIV;
#else
const char *filename =
GetStringSetting("XSECURELOCK_USERFILE_PRIV", USERFILE_PRIV);
#endif
rtn = UserInAuthListFile(filename, username, match);
return rtn;
}
int UserInAuthListBlock(const char* username, int *match) {
int rtn;
#ifdef SECURE
const char *filename = USERFILE_BLOCK;
#else
const char *filename =
GetStringSetting("XSECURELOCK_USERFILE_BLOCK", USERFILE_BLOCK);
#endif
rtn = UserInAuthListFile(filename, username, match);
return rtn;
}
int UserInAuthListAny(const char* username, int *match) {
int rtn;
#ifdef SECURE
const char *filename = USERFILE_ANY;
#else
const char *filename =
GetStringSetting("XSECURELOCK_USERFILE_ANY", USERFILE_ANY);
#endif
rtn = UserInAuthListFile(filename, username, match);
return rtn;
}
int UserInNoBlankList(const char* username, int *match) {
int rtn;
#ifdef SECURE
const char *filename = USERFILE_NOBLANK;
#else
const char *filename =
GetStringSetting("XSECURELOCK_USERFILE_BLANK", USERFILE_NOBLANK);
#endif
rtn = UserInAuthListFile(filename, username, match);
return rtn;
}