/
misc.c
300 lines (265 loc) · 6.06 KB
/
misc.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
/* Copyright 2003-2008 Wang, Chun-Pin All rights reserved. */
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/param.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <ctype.h>
#include "smbftpd.h"
#include "auth.h"
#ifndef LINE_MAX
#define LINE_MAX 2048
#endif
const char *set_get_value(struct opt_set *set, const char *user)
{
struct opt_set *p = set;
char *key;
int match = 0;
if (!user || !set) {
return NULL;
}
while (p) {
key = p->key;
if (*key != '@') { //user
if (0 == strcmp(key, user)) {
match = 1;
}
} else if (*(key+1) == '\0') { /* "@" match all */
match = 1;
} else { /* Group */
if (is_user_in_group(user, key + 1)) {
match = 1;
}
}
if (match) {
return p->value;
}
p = p->next;
}
return NULL;
}
/**
* Allocate another string and copy string "s" into new buffer.
* When there is double quote in s, put add another double.
*
* For example,
* This is "s"
* will become
* This is ""s""
*
* @param s
*
* @return Success: Returen a new string, call should free the string
* Malloc failed: Return NULL
*/
char *doublequote(const char *s)
{
int n;
const char *p1;
char *p, *s2;
for (p1 = s, n = 0; *p1; p1++)
if (*p1 == '"')
n++;
if ((s2 = malloc(p1 - s + n + 1)) == NULL)
return(NULL);
for (p = s2; *s; s++, p++) {
if ((*p = *s) == '"')
*(++p) = '"';
}
*p = '\0';
return(s2);
}
/**
* This function will remove the spaces ' ' and tab '\t' and
* newline characters '\r', '\n' in the front and tail of the
* given "str". So the "str" buffer will be modified.
*
* if remove_quote is 1, we will also remove the in pairs double
* quote '"' or single quote '\''.
*
* For example, if the string is " this is a string ", it
* will become "this is a string".
*
* If the string is " \"this is a string\" ", when remove_quote
* is 1, it will become "this is a string".
*
* @param str
* @param remove_quote
*
* @return The pointer of str
*/
static char *str_trim(char *str, int remove_quote)
{
char *head, *tail;
if (!str) {
return NULL;
}
for (head = str; isspace(*head) && *head != 0; head++);
for (tail = head + strlen(head) - 1; tail>head && isspace(*tail); tail--);
*(tail+1) = 0;
if (remove_quote) {
if ((tail > head) && ((*head == '"' && *tail == '"') ||
(*head == '\'' && *tail == '\''))) {
head++;
*tail = 0;
tail--;
}
}
if (str != head) {
memmove(str, head, (tail - head + 2));
}
return str;
}
char *str_trim_space(char *str)
{
return str_trim(str, 0);
}
char *str_trim_space_quote(char *str)
{
return str_trim(str, 1);
}
/**
* Check whether user is in list. The list is a string of
* users/groups that separate by ','. The group name has
* prefix @.
*
* For example szList = "user1, user2, user3, @group1, @group2..."
*
* We will get each user in the list and check when the user name
* is the same. When we get group, we will check whether user
* belongs to the group.
*
* @param user User name to check
* @param list The string that contains users and groups separated by ",".
*
* @return 1: Yes, the user is in the list
* 0: No, the user is not in the list
*/
int is_user_in_list(const char *user, const char *list)
{
char *tmplist = NULL, *token;
int err = 0;
if (!user || !list) {
return err;
}
tmplist = (char *)malloc(strlen(list) + 1);
if (tmplist == NULL) {
syslog(LOG_ERR, "%s (%d) failed to allocate memory, errno:%d(%s)",
__FILE__, __LINE__, errno, strerror(errno));
return err;
}
strcpy(tmplist, list);
for (token = strtok(tmplist, ","); token; token = strtok(NULL, ",")) {
str_trim_space(token);
if (*token != '@') { //user
if (0 == strcmp(user, token)) {
err = 1;
break;
}
} else {
if (1 == smbftpd_auth_is_user_in_group(user, token+1)) {
err = 1;
goto Error;
}
}
}
Error:
if (tmplist != NULL) {
free(tmplist);
}
return err;
}
/**
* Check whether user belongs to group. We will check group id and
* its members.
*
* @param user The user name to check
* @param group The group name to search
*
* @return 1: Yes, the user belongs to the group.
* 0: No, not belongs to the group.
*/
int is_user_in_group(const char *user, const char *group)
{
return smbftpd_auth_is_user_in_group(user, group);
}
/**
* Config file parser function. This function is used to parse config
* file that is the following format:
*
* Option1 Value
* Option2 Value
*
* We will read each line of config file and pass the option/value to
* the opt_handler function.
*
* The opt_handler function will then assign/convert the value to
* proper format.
*
* @param file The path of the config file.
* @param opt_handler
* The option/value handler function.
*
* @return 0: Success
* -1: Failed
*/
int smbftpd_config_parser(const char *file, int (*opt_handler)(char *option, char *opt_arg))
{
FILE *pf = NULL;
char line[LINE_MAX];
char *option, *opt_arg;
int error = -1, len = 0;
if (NULL == file || !opt_handler) {
return -1;
}
pf = fopen(file, "r");
if (!pf) {
syslog(LOG_ERR, "%s (%d) Failed to open [%s] (%s)", __FILE__, __LINE__,
file, strerror(errno));
return -1;
}
while (NULL != (fgets(line, sizeof(line), pf))) {
len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
}
option = line;
// Trim space
while ((*option == ' ') || (*option == '\t')) {
option++;
}
if (*option == '\0' || *option == '#') {
continue;
}
opt_arg = option;
while ((*opt_arg != ' ') && (*opt_arg != '\t') && (*opt_arg != '\0')) {
opt_arg++;
}
if (opt_arg == option) {
// Empty line
continue;
}
*opt_arg = '\0';
opt_arg++;
str_trim_space_quote(opt_arg);
if (*opt_arg == '\0') {
syslog(LOG_ERR, "%s (%d) bad syntax of config option %s",
__FILE__, __LINE__, option);
goto Error;
}
if (0 != opt_handler(option, opt_arg)) {
goto Error;
}
}
error = 0;
Error:
if (pf) {
fclose(pf);
}
return error;
}