-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrutils.c
More file actions
366 lines (313 loc) · 7.54 KB
/
Copy pathrutils.c
File metadata and controls
366 lines (313 loc) · 7.54 KB
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
/*
* rutils.c
* Utility functions for rset
*/
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <err.h>
#include <regex.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "missing/compat.h"
#include "config.h"
#include "rutils.h"
#include "xlibc.h"
unsigned session_id;
/* globals */
int dir_mode = 0700;
/*
* str_cpy - copy string, format error and abort on overflow
*/
size_t
str_cpy(char *dst, const char *src, size_t dsize) {
char err_str[20];
int i;
size_t len;
if (!src) {
*dst = '\0';
return 0;
}
len = strlen(src);
if (len + 1 > dsize) {
for (i = 0; i < len && i < sizeof(err_str) - 1; i++) {
err_str[i] = *(src + i);
/* erase control characters */
if ((err_str[i] > '\0') && (err_str[i] < ' '))
err_str[i] = '+';
}
err_str[i] = '\0';
errx(1, "input too long: %s.. > %zu bytes", err_str, dsize);
}
memcpy(dst, src, len + 1);
return len;
}
/*
* str_to_array - split a string using the input string as the buffer
* array_to_str - format an array using the output string as the buffer
* array_append - add a list of arguments to an array
*/
int
str_to_array(char *argv[], const char *inputstring, int max_elements, const char *delim) {
int argc;
char *input;
char **ap;
input = xstrdup(inputstring, "input");
argc = 0;
for (ap = argv; ap < &argv[max_elements] && (*ap = strsep(&input, delim)) != NULL;) {
argc++;
if (**ap != '\0')
ap++;
}
*ap = NULL;
return argc;
}
int
array_to_str(char *argv[], char *outputstring, int max_length, const char *delim) {
int argc = 0;
char *p = outputstring;
while (argv && *argv) {
argc += str_cpy(p + argc, *argv, max_length - argc);
argv++;
if (argv && *argv)
argc += str_cpy(p + argc, delim, max_length - argc);
}
outputstring[argc] = '\0';
return argc;
}
int
array_append(char *argv[], int argc, char *arg1, ...) {
char *s;
va_list ap;
va_start(ap, arg1);
for (s = arg1; s != NULL; s = va_arg(ap, char *))
argv[argc++] = s;
va_end(ap);
argv[argc] = NULL;
return argc;
}
/*
* Update global session ID before starting a new SSH session
*/
unsigned
generate_session_id() {
while ((session_id = arc4random()) == 0)
;
return session_id;
}
unsigned
current_session_id() {
return session_id;
}
/*
* check_permissions - verify and memorize top-level directory permissions
*/
void
check_permissions(const char *dir) {
struct stat stat_buf;
stat(dir, &stat_buf);
if (stat_buf.st_mode & (S_IWGRP | S_IRWXO))
errx(1, "invalid permissions for %s: mode must be u=rwx (0700) or u=rwx,g=rx (0750)", dir);
dir_mode = stat_buf.st_mode;
}
/*
* create_dir - ensure a directory exists
* install_if_new - ensure a file is up to date
* assert_not_exists - abort if a file already exists
* list_dir_match - return first directory matching prefix
*/
int
create_dir(const char *dir) {
struct stat dst_sb;
if (stat(dir, &dst_sb) == -1) {
printf("rset: initialized directory '%s'\n", dir);
(void) mkdir(dir, dir_mode);
return 1;
}
return 0;
}
void
install_if_new(const char *src, const char *dst) {
int pid;
int status;
struct stat src_sb;
struct stat dst_sb;
if (stat(src, &src_sb) == -1)
err(1, "%s", src);
if (create_dir(xdirname(dst)) == 0) {
if ((stat(dst, &dst_sb) == -1) || (src_sb.st_mtime > dst_sb.st_mtime))
printf("rset: updating '%s'\n", dst);
else
return; /* directory exists and no update required */
}
pid = fork();
if (pid == 0) {
if (execl("/usr/bin/install", "/usr/bin/install", src, dst, NULL) != -1)
err(1, "%s", dst);
}
waitpid(pid, &status, 0);
if (status != 0)
warnx("copy failed %s -> %s", src, dst);
}
void
assert_not_exists(const char *filename) {
struct stat sb;
if (stat(filename, &sb) != -1)
errx(1, "%s already exists", filename);
}
char *
list_dir_match(const char *base_path, const char *file_prefix) {
struct dirent *dp;
DIR *dfd = opendir(base_path);
size_t len;
static char match[PATH_MAX];
match[0] = '\0';
len = strlen(file_prefix);
if (dfd == NULL)
errx(1, "unable to open directory: '%s'", base_path);
while ((dp = readdir(dfd)) != NULL) {
if ((strlen(dp->d_name) >= len) && strncmp(dp->d_name, file_prefix, len) == 0) {
snprintf(match, PATH_MAX, "%s/%s", base_path, dp->d_name);
return match;
}
}
closedir(dfd);
return NULL;
}
/*
* hl_range - colorize a line, reversing parts that match a range
*/
void
hl_range(const char *s, const char *color, unsigned so, unsigned eo) {
char *start, *match;
if (so == 0 && eo == 0)
printf("%s%s" HL_RESET, color, s);
else {
start = strndup(s, so);
match = strndup(s + so, eo - so);
printf("%s%s" HL_REVERSE "%s" HL_RESET "%s%s" HL_RESET, color, start, match, color, s + eo);
free(start);
free(match);
}
}
/*
* pattern_match - match exact string or regular expression
*/
const char *
pattern_match(const char *pattern, const char *string) {
char c;
int n, len;
bool is_char, is_digit, is_dash, is_dot, is_colon;
bool try_regex = false;
const char *match = NULL;
regex_t label_reg;
regmatch_t regmatch;
/* test for input consistant with a valid hostname or address */
len = strlen(pattern);
for (n = 0; n < len; n++) {
c = pattern[n];
is_char = (c >= 'a') && (c <= 'z');
is_digit = (c >= '0') && (c <= '9');
is_dash = (c == '-') && (n > 0) && (n + 1 < len);
is_dot = (c == '.') && (n > 0) && (n + 1 < len);
is_colon = (c == ':') && (n + 1 < len);
if (is_char | is_digit | is_dash | is_dot | is_colon)
continue;
else
try_regex = true;
}
if (try_regex) {
xregcomp(&label_reg, pattern, REG_EXTENDED);
if (xregexec(&label_reg, string, 1, ®match) == 0) {
match = string;
}
} else {
if (strcmp(pattern, string) == 0)
match = string;
}
return match;
}
/*
* log_msg - write log message and interpolate variables
*/
void
log_msg(char *template, char *hostname, char *label_name, int exit_code) {
char *p;
char buf[_POSIX2_LINE_MAX];
char tmstr[64];
int index = 0;
time_t tv;
struct tm *tm;
p = template;
tv = time(NULL);
tm = localtime(&tv);
if (!template)
return;
while (p[0] != '\0') {
if (p[0] == '%') {
p++;
switch (p[0]) {
case 'e':
index += snprintf(buf + index, sizeof(buf) - index, "%d", exit_code);
break;
case 'h':
index += str_cpy(buf + index, hostname, sizeof(buf) - index);
break;
case 'l':
index += str_cpy(buf + index, label_name, sizeof(buf) - index);
break;
case 's':
index += snprintf(buf + index, sizeof(buf) - index, "%08" PRIx32, session_id);
break;
case 'T':
strftime(tmstr, sizeof(tmstr), LOG_TIMESTAMP_FORMAT, tm);
index += str_cpy(buf + index, tmstr, sizeof(buf) - index);
break;
case '%':
buf[index++] = p[0];
break;
default:
break;
}
} else
buf[index++] = p[0];
p++;
}
buf[index++] = '\n';
write(STDOUT_FILENO, buf, index);
}
/*
* trace_shell - log ssh commands using system(3)
* trace_exec - log ssh commands using execvp(3)
* trace_http - format log messages emitted by miniquark(1)
*/
void
trace_shell(char *cmd) {
if (!getenv("SSH_TRACE"))
return;
printf("+ " HL_TRACE "sh -c \"%s\"" HL_RESET "\n", cmd);
}
void
trace_exec(char *cmd[]) {
char argv_repr[PATH_MAX];
if (!getenv("SSH_TRACE"))
return;
array_to_str(cmd, argv_repr, sizeof(argv_repr), " ");
printf("+ " HL_TRACE "%s" HL_RESET "\n", argv_repr);
}
void
trace_http(const char *http_log) {
char *ap, *bp, *input;
if (!getenv("HTTP_TRACE"))
return;
bp = input = xstrdup(http_log, "http_log");
while ((ap = strsep(&input, "\n")) != NULL) {
if (*ap != '\0')
printf("+ " HL_TRACE "%s" HL_RESET "\n", ap);
}
free(bp);
}