-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoreplay2asciinema.c
244 lines (219 loc) · 7.02 KB
/
sudoreplay2asciinema.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
static unsigned char *escape(unsigned char *data, int *data_size)
{
int esc_pos, data_pos;
ssize_t esc_size = 4096;
unsigned char *esc = malloc(esc_size);
u_int32_t unicode;
esc[0] = 0;
for(esc_pos = data_pos = 0; data_pos < *data_size; data_pos++) {
if((esc_pos + 16) > esc_size) {
esc_size += 4096;
esc = realloc(esc, esc_size);
}
if(data[data_pos] == '\r') {
sprintf((char *)&esc[esc_pos], "\\r");
esc_pos += 2;
} else if(data[data_pos] == '\n') {
sprintf((char *)&esc[esc_pos], "\\n");
esc_pos += 2;
} else if(data[data_pos] == '\"') {
sprintf((char *)&esc[esc_pos], "\\\"");
esc_pos += 2;
} else if((data[data_pos] < 32) || (data[data_pos] == '\\')) {
sprintf((char *)&esc[esc_pos], "\\u%04x", (unsigned char)data[data_pos]);
esc_pos += 6;
} else if((data[data_pos] & 248 == 240) && ((*data_size - data_pos) > 4)) {
// Unicode 4 bytes
unicode = ((data[data_pos] & 0x07) << 18) + ((data[data_pos + 2] & 0x3F) << 12) + ((data[data_pos + 2] & 0x3F) << 6) + (data[data_pos + 3] & 0x3F);
sprintf((char *)&esc[esc_pos], "\\u%04x", unicode);
data_pos += 3;
esc_pos += 6;
} else if((data[data_pos] & 248 == 224) && ((*data_size - data_pos) > 3)) {
// Unicode 3 bytes
unicode = ((data[data_pos] & 0x0F) << 12) + ((data[data_pos + 1] & 0x3F) << 6) + (data[data_pos + 2] & 0x3F);
sprintf((char *)&esc[esc_pos], "\\u%04x", unicode);
data_pos += 2;
esc_pos += 6;
} else if((data[data_pos] & 248 == 192) && ((*data_size - data_pos) > 2)) {
// Unicode 2 bytes
unicode = ((data[data_pos] & 0x1F) << 6) + (data[data_pos + 1] & 0x3F);
sprintf((char *)&esc[esc_pos], "\\u%04x", unicode);
data_pos += 1;
esc_pos += 6;
} else {
esc[esc_pos] = data[data_pos];
esc_pos++;
}
}
*data_size = esc_pos;
return esc;
}
static FILE *open_file(const char *dir, const char *file)
{
FILE *fd;
char *fullname;
asprintf(&fullname, "%s/%s", dir, file);
if((fd = fopen(fullname, "r")) == NULL) {
perror(file);
free(fullname);
exit(EXIT_FAILURE);
}
free(fullname);
return fd;
}
static gzFile gz_open_file(const char *dir, const char *file)
{
gzFile fd;
char *fullname;
asprintf(&fullname, "%s/%s", dir, file);
if((fd = gzopen(fullname, "r")) == NULL) {
perror(file);
free(fullname);
exit(EXIT_FAILURE);
}
free(fullname);
return fd;
}
int main(int argc, char *argv[])
{
FILE *output;
int gz = 1;
if(argc < 2) {
fprintf(stderr, "Syntax error\n\n");
fprintf(stderr, "sudo2asciinema <dir> [output]\n");
exit(EXIT_FAILURE);
}
if(argc < 3) {
output = stdout;
} else {
if((output = fopen(argv[2], "w+")) == NULL) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
char *basedir = argv[1];
FILE *fdlog = open_file(basedir, "log");
FILE *fdstderr = open_file(basedir, "stderr");
FILE *fdstdout = open_file(basedir, "stdout");
FILE *fdtiming;
FILE *fdttyout;
gzFile fdgzttyout;
gzFile fdgztiming;
if(gz) {
fdgzttyout = gz_open_file(basedir, "ttyout");
fdgztiming = gz_open_file(basedir, "timing");
} else {
fdttyout = open_file(basedir, "ttyout");
fdtiming = open_file(basedir, "timing");
}
char timestamp[64], user[64], group[64], terminal[64];
char home[64];
char command[64];
char *line;
size_t size;
// get timestamp, user, group and terminal from log
line = NULL, size = 0;
getline(&line, &size, fdlog);
sscanf(line, "%[^:]:%[^:]:%[^:]::%[^\n]\n", (char *)×tamp, (char *)&user, (char *)&group, (char *)&terminal);
free(line);
// get home from log
line = NULL, size = 0;
getline(&line, &size, fdlog);
sscanf(line, "%[^\n]", (char *)&home);
free(line);
// get command from log
line = NULL, size = 0;
getline(&line, &size, fdlog);
sscanf(line, "%[^\n]", (char *)&command);
free(line);
// print header
fprintf(output, "{\n");
fprintf(output, " \"version\": %u,\n", 1);
fprintf(output, " \"width\": %u,\n", 89);
fprintf(output, " \"height\": %u,\n", 26);
fprintf(output, " \"duration\": %f,\n", 27.221634);
fprintf(output, " \"command\": null,\n");
fprintf(output, " \"title\": null,\n");
fprintf(output, " \"start\": %s,\n", timestamp);
fprintf(output, " \"user\": \"%s\",\n", user);
fprintf(output, " \"group\": \"%s\",\n", group);
fprintf(output, " \"terminal\": \"%s\",\n", terminal);
fprintf(output, " \"home\": \"%s\",\n", home);
fprintf(output, " \"command\": \"%s\",\n", command);
fprintf(output, " \"env\": {\n");
fprintf(output, " \"TERM\": \"xterm-256color\",\n");
fprintf(output, " \"SHELL\": \"/bin/bash\"\n");
fprintf(output, " },\n");
fprintf(output, " \"stdout\": [\n");
// get stream chunk
int op;
float duration;
int bytes;
unsigned char *escape_data;
unsigned char *data;
int first = 1;
for(;;) {
line = NULL, size = 0;
if(gz) {
line = malloc(1024);
if(gzgets(fdgztiming, line, 1024) == NULL) {
free(line);
break;
}
} else {
if(getline(&line, &size, fdtiming) == -1) {
free(line);
break;
}
}
sscanf(line, "%u %f %u", &op, &duration, &bytes);
free(line);
if(first) first = 0;
else fprintf(output, " ],\n");
fprintf(output, " [\n");
fprintf(output, " %f,\n", duration);
// Read data from ttyout
data = malloc(bytes);
if(gz) {
if(gzfread(data, bytes, 1, fdgzttyout) != 1) {
fprintf(stderr, "Missing data in fdttyout\n");
exit(EXIT_FAILURE);
}
} else {
if(fread(data, bytes, 1, fdttyout) != 1) {
fprintf(stderr, "Missing data in fdttyout\n");
exit(EXIT_FAILURE);
}
}
escape_data = escape(data, &bytes);
free(data);
fprintf(output, " \"");
fwrite(escape_data, bytes, 1, output);
fprintf(output, "\"\n");
free(escape_data);
}
// Last item
fprintf(output, " ]\n");
fprintf(output, " ]\n");
fprintf(output, "}\n");
fclose(fdlog);
fclose(fdstderr);
fclose(fdstdout);
if(gz) {
gzclose(fdgztiming);
gzclose(fdgzttyout);
} else {
fclose(fdtiming);
fclose(fdttyout);
}
fclose(output);
}