-
Notifications
You must be signed in to change notification settings - Fork 7
/
rutils.c
204 lines (175 loc) · 4.46 KB
/
rutils.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
/*
* Copyright (c) 2018 Eric Radman <[email protected]>
*
* 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 <libgen.h>
#include <limits.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"
unsigned session_id;
/* globals */
int dir_mode = 0700;
/*
* 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;
}
/*
* Mimic dirname(3) on OpenBSD which does not modify it's input
*/
char *
xdirname(const char *path) {
static char dname[PATH_MAX];
strlcpy(dname, path, sizeof(dname));
return dirname(dname);
}
/*
* Mimic basename(3) on OpenBSD which does not modify it's input
*/
char *
xbasename(const char *path) {
static char dname[PATH_MAX];
strlcpy(dname, path, sizeof(dname));
return basename(dname);
}
/*
* 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);
}