-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrutils.c
More file actions
273 lines (232 loc) · 5.92 KB
/
rutils.c
File metadata and controls
273 lines (232 loc) · 5.92 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
/*
* Copyright (c) 2018 Eric Radman <ericshane@eradman.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/wait.h>
#include <err.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_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 += strlcpy(p + argc, *argv, max_length - argc);
argv++;
if (argv && *argv)
argc += strlcpy(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
*/
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);
}
/*
* 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);
}
}
/*
* 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 += strlcpy(buf + index, hostname, sizeof(buf) - index);
break;
case 'l':
index += strlcpy(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 += strlcpy(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);
}