This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.c
347 lines (285 loc) · 7.03 KB
/
io.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
/*
* Copyright 2015 Horváth Henrich
*
* Sudo security plugin is free software
* released under GNU Lesser General Public License.
*
*/
#include "io.h"
int copy_file(char * from, int target_fd)
{
int source_fd;
struct stat s;
off_t offset = 0;
/* Source file does not exist, do not need to copy */
if ((source_fd = open(from, O_RDONLY)) == -1)
{
return -1;
}
if (fstat(source_fd, &s) < 0)
{
close(source_fd);
return 0;
}
int result = (s.st_size > 0) ? (sendfile(target_fd, source_fd, &offset, s.st_size) == s.st_size) : 1;
close(source_fd);
return result;
}
bool cmp_files(char * oldFile, char * newFile)
{
int ch1, ch2;
bool result = true;
FILE * fp1 = fopen(oldFile, "r");
if (!fp1)
{
return false;
}
FILE * fp2 = fopen(newFile, "r");
if (!fp2)
{
fclose(fp2);
return false;
}
do
{
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
if (ch1 != ch2)
{
result = false;
break;
}
} while (ch1 != EOF);
fclose(fp1);
fclose(fp2);
return result;
}
bool save_int(size_t value, int bytes, int fd)
{
unsigned char val1, val2, val3, val4;
switch (bytes)
{
case 1:
return (write(fd, &value, 1) == 1);
case 2:
val1 = value & 0xFF;
val2 = (value >> 8) & 0xFF;
return (write(fd, &val1, 1) == 1) && (write(fd, &val2, 1) == 1);
case 4:
val1 = value & 0xFF;
val2 = (value >> 8) & 0xFF;
val3 = (value >> 16) & 0xFF;
val4 = (value >> 24) & 0xFF;
return (write(fd, &val1, 1) == 1) && (write(fd, &val2, 1) == 1) && (write(fd, &val3, 1) == 1) && (write(fd, &val4, 1) == 1);
default:
return false;
}
}
bool save_string(char * str, int fd)
{
if (str)
{
// <length:4bytes><string>
size_t length = strlen(str) + 1;
return (length <= UINT32_MAX &&
save_int(length, 4, fd) &&
write(fd, str, length) == (ssize_t)length);
}
else
{
// 0000 (4 bytes)
return save_int(0, 4, fd);
}
}
bool save_string_array(char ** str_array, int fd)
{
size_t len;
char ** str;
/* Array length */
if ((len = str_array_len(str_array)) > UINT16_MAX)
{
return false;
}
int result = save_int(len, 2, fd);
/* Items */
str = str_array;
if (str)
while (*str)
{
result &= save_string(*str, fd);
str++;
}
return result;
}
bool load_string(int fd, char ** str)
{
unsigned char int_buffer[4];
char * string;
if (read(fd, int_buffer, 4) != 4)
{
return false;
}
size_t len = convert_from_bytes(int_buffer, 4);
/* Empty string */
if (len == 0)
{
*str = NULL;
return true;
}
/* Load string <length:4bytes><string> */
if ((string = malloc(sizeof(char) * len)) == NULL)
{
return false;
}
if (len > SSIZE_MAX || read(fd, string, sizeof(char) * len) != (ssize_t)len)
{
free(string);
return false;
}
/* Checking length */
if (string[len-1] != '\0' || strlen(string)+1 != len)
{
free(string);
return false;
}
*str = string;
return true;
}
bool load_string_array(int fd, char *** str_array)
{
unsigned char int_buffer[2];
char ** array;
/* Arguments count */
if (read(fd, int_buffer, 2) != 2)
{
return false;
}
size_t len = convert_from_bytes(int_buffer, 2);
if (len == 0)
{
*str_array = NULL;
return true;
}
if ( (array = calloc(len+1, sizeof(char*))) == NULL )
{
return false;
}
/* Load strings */
for (size_t i = 0; i < len; i++)
{
char * str = NULL;
if (!load_string(fd, &str))
{
free_2d_null(array);
return false;
}
array[i] = str;
}
*str_array = array;
return true;
}
command_data ** load(int fd)
{
command_data ** cmds;
unsigned char int_buffer[4];
if (fd == -1)
{
return NULL;
}
/* Commands count */
if (read(fd, int_buffer, 4) != 4)
{
return NULL;
}
size_t count = convert_from_bytes(int_buffer, 4);
if (count > (SIZE_MAX / sizeof(command_data*) - 1) || (cmds = calloc((count+1), sizeof(command_data*))) == NULL)
{
//sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
return NULL;
}
/* Load each command */
for (size_t i = 0; i < count; i++)
{
if ((cmds[i] = load_command(fd)) == NULL)
{
free_commands_null(cmds);
//sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
return NULL;
}
}
return cmds;
}
command_data * load_command(int fd)
{
unsigned char sudoedit[1];
command_data * command;
if ((command = make_command()) == NULL)
{
return NULL;
}
bool result = load_string(fd, &command->file) &&
load_string_array(fd, &command->argv) &&
load_string_array(fd, &command->envp) &&
(read(fd, sudoedit, 1) == 1) &&
load_string(fd, &command->runas_user) &&
load_string(fd, &command->runas_group) &&
load_string_array(fd, &command->exec_by_users) &&
load_string_array(fd, &command->rem_by_users);
if (!result)
{
free_command(command);
return NULL;
}
command->sudoedit = sudoedit[0];
return command;
}
bool save(command_data ** commands)
{
int tmp_fd;
char fileName[] = PLUGIN_COMMANDS_TEMP_FILE;
if ((tmp_fd = mkstemp(fileName)) == -1)
{
return false;
}
/* Commands count */
size_t count = commands_array_len(commands);
if (count > UINT32_MAX || !save_int(count, 4, tmp_fd))
{
close(tmp_fd);
unlink(fileName);
return false;
}
/* Save each command */
command_data ** command = commands;
while (*command)
{
if (!save_command(*command, tmp_fd))
{
close(tmp_fd);
unlink(fileName);
return false;
}
command++;
}
/* Rename temp file to original file */
if (rename(fileName, PLUGIN_COMMANDS_FILE) == -1)
{
close(tmp_fd);
unlink(fileName);
return false;
}
close(tmp_fd);
return true;
}
bool save_command(command_data * command, int fd)
{
bool result = save_string(command->file, fd) &&
save_string_array(command->argv, fd) &&
save_string_array(command->envp, fd) &&
save_int(command->sudoedit, 1, fd) &&
save_string(command->runas_user, fd) &&
save_string(command->runas_group, fd) &&
save_string_array(command->exec_by_users, fd) &&
save_string_array(command->rem_by_users, fd);
return result;
}