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
|
//
// Created by Anshul Gupta on 4/6/25.
//
#include "config.h"
#include <ctype.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
const char *config_locations[] = {
"/etc/github_mirror/config.ini",
"/usr/local/etc/github_mirror/config.ini",
"/usr/local/github_mirror/config.ini",
"config.ini",
NULL,
};
enum config_section {
section_none,
section_github,
section_git,
};
static char *file_read(const char *path, size_t *size_out)
{
char *contents = NULL;
// Open the file for reading
const int fd = open(path, O_RDONLY);
if (fd < 0) {
perror("Error reading config file");
return NULL;
}
// Stat the file to get its size
struct stat st;
if (fstat(fd, &st) < 0) {
perror("Error getting file size");
goto end;
}
// Get the size of the file
const size_t size = st.st_size;
if (size == 0) {
fprintf(stderr, "Error reading config file: file is empty\n");
goto end;
}
// Map the file into memory
contents = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (contents == MAP_FAILED) {
perror("Error reading config file");
goto end;
}
// Output file size
if (size_out)
*size_out = size;
end:
close(fd);
return contents;
}
static char *trim(char *start, char *end)
{
while (start < end && isspace(*start))
start++;
while (end > start && isspace(*(end - 1)))
end--;
*end = '\0';
return start;
}
static int parse_line_inner(struct config *cfg, enum config_section section,
char *key, char *value)
{
char *endptr;
struct passwd *pw;
struct group *gr;
switch (section) {
case section_none:
fprintf(stderr,
"Unexpected key-value pair outside of section: %s=%s\n",
key, value);
return -1;
case section_github:
if (!strcmp(key, "endpoint"))
cfg->endpoint = value;
else if (!strcmp(key, "token"))
cfg->token = value;
else if (!strcmp(key, "user_agent"))
cfg->user_agent = value;
else if (!strcmp(key, "owner"))
cfg->owner = value;
else {
fprintf(stderr,
"Error parsing config file: unknown key: %s\n",
key);
return -1;
}
break;
case section_git:
if (!strcmp(key, "base"))
cfg->git_base = value;
else if (!strcmp(key, "owner")) {
// If the value is a number, set the owner to that
// number
cfg->git_owner = strtol(value, &endptr, 10);
if (*endptr == '\0')
return 0;
// Otherwise, convert the string into a uid
if (!((pw = getpwnam(value)))) {
fprintf(stderr,
"Error parsing config file: unknown "
"user: %s\n",
value);
return -1;
}
cfg->git_owner = pw->pw_uid;
} else if (!strcmp(key, "group")) {
// If the value is a number, set the group to that
// number
cfg->git_group = strtol(value, &endptr, 10);
if (*endptr == '\0')
return 0;
// Otherwise, convert the string into a gid
if (!((gr = getgrnam(value)))) {
fprintf(stderr,
"Error parsing config file: unknown "
"group: %s\n",
value);
return -1;
}
cfg->git_group = gr->gr_gid;
} else {
fprintf(stderr,
"Error parsing config file: unknown key: %s\n",
key);
return -1;
}
break;
}
return 0;
}
static int parse_line(struct config *cfg, char *line,
enum config_section *section)
{
switch (*line) {
case ';':
case '#':
case '\0':
// Ignore comments and empty lines
return 0;
case '[': {
// Handle section headers
char *close = strchr(line, ']');
if (!close) {
fprintf(stderr,
"Error parsing config file: invalid section "
"header: %s\n",
line);
return -1;
}
*close = '\0';
char *section_name = trim(line + 1, close);
if (!strcmp(section_name, "github"))
*section = section_github;
else if (!strcmp(section_name, "git"))
*section = section_git;
else {
fprintf(stderr,
"Error parsing config file: unknown section: "
"%s\n",
section_name);
return -1;
}
return 0;
}
default: {
// Handle key-value pairs
char *line_end = line + strlen(line);
char *equals = strchr(line, '=');
if (!equals) {
fprintf(stderr,
"Error parsing config file: invalid line: %s\n",
line);
return -1;
}
*equals = '\0';
char *key = trim(line, equals);
char *value = trim(equals + 1, line_end);
return parse_line_inner(cfg, *section, key, value);
}
}
}
static int config_parse(struct config *cfg)
{
char *ptr = cfg->contents;
char *end = cfg->contents + cfg->contents_len;
enum config_section section = section_none;
while (ptr < end) {
// Find the end of the line
char *newline = memchr(ptr, '\n', end - ptr);
char *line_end = newline ? newline : end;
// Handle line endings
char *actual_end = line_end;
if (actual_end > ptr && *(actual_end - 1) == '\r')
actual_end--;
// Null-terminate the line
if (line_end < end)
*line_end = '\0';
// Trim whitespace
char *line = trim(ptr, actual_end);
// Parse the line
if (parse_line(cfg, line, §ion) < 0)
return -1;
// Move to the next line
ptr = newline ? newline + 1 : end;
}
return 0;
}
static void config_defaults(struct config *cfg)
{
cfg->endpoint = GH_DEFAULT_ENDPOINT;
cfg->user_agent = GH_DEFAULT_USER_AGENT;
cfg->git_base = "/srv/git";
cfg->git_owner = getuid();
cfg->git_group = getgid();
}
static int config_validate(const struct config *cfg)
{
if (!cfg->token) {
fprintf(stderr,
"Error: missing required field: github.token\n");
return -1;
}
if (!cfg->owner) {
fprintf(stderr,
"Error: missing required field: github.owner\n");
return -1;
}
return 0;
}
struct config *config_read(const char *path)
{
struct config *cfg = calloc(1, sizeof(*cfg));
if (!cfg) {
perror("error allocating config");
return NULL;
}
config_defaults(cfg);
// Read the config file
cfg->contents = file_read(path, &cfg->contents_len);
if (!cfg->contents)
goto fail;
// Parse the config file
if (config_parse(cfg) < 0)
goto fail2;
// Validate the config file
if (config_validate(cfg) < 0)
goto fail2;
return cfg;
fail2:
munmap(cfg->contents, cfg->contents_len);
fail:
free(cfg);
return NULL;
}
void config_free(struct config *config)
{
if (!config || !config->contents)
return;
munmap(config->contents, config->contents_len);
free(config);
}
|