aboutsummaryrefslogtreecommitdiffstats
path: root/main/ca-certificates/update-ca.c
blob: 4a2f8fc0bf7a7def8919c5dd9709d55f5370e2ca (plain)
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>

#include <sys/stat.h>
#include <sys/sendfile.h>

#define CERTSDIR "/usr/share/ca-certificates/"
#define LOCALCERTSDIR "/usr/local/share/ca-certificates/"
#define ETCCERTSDIR "/etc/ssl/certs/"
#define CERTBUNDLE "ca-certificates.crt"
#define CERTSCONF "/etc/ca-certificates.conf"

static bool str_begins(const char* str, const char* prefix)
{
	return !strncmp(str, prefix, strlen(prefix));
}

/* A string pair */
struct pair
{
	char** first;
	char** second;

	/* Total size */
	unsigned size;
	/* Fill-level */
	unsigned count;
};

static void pair_free(struct pair* data)
{
	int i = 0;
	for (i = 0; i < data->size; i++) {
		free(data->first[i]);
		free(data->second[i]);
	}
	free(data->first);
	free(data->second);
	free(data);
}

static struct pair* pair_alloc(int size)
{
       struct pair* d = (struct pair*) malloc(sizeof(struct pair));
       d->size = size;
       d->count = 0;
       d->first = calloc(size, sizeof(char** ));
       d->second = calloc(size, sizeof(char** ));
       
       return d;
}

static const char*
get_pair(struct pair* data, const char* key, int* pos)
{
	int i = 0;
	for (i = 0; i < data->size; i++) {
		*pos = i;
		if (data->second[i] && data->first[i]) {
			if (str_begins(key, data->second[i]))
				return data->first[i];
			else if (str_begins(key, data->first[i]))
				return data->second[i];
		}
	}
	
	return 0;
}

static bool
add_ca_from_pem(struct pair* data, const char* ca, const char* pem)
{
 	int count = data->count++;
	if (count >= data->size)
		return false;
        
	data->first[count] = strdup(ca);
	data->second[count] = strdup(pem);

	return true;
}

static bool
copyfile(const char* source, int output)
{
	int input;
	if ((input = open(source, O_RDONLY)) == -1)
		return -1;

	off_t bytes = 0;
	struct stat fileinfo = {0};
	fstat(input, &fileinfo);
	int result = sendfile(output, input, &bytes, fileinfo.st_size);

	close(input);

	return (fileinfo.st_size == result);
}

typedef void (*proc_path)(const char*, struct pair*, int);

static void proc_localglobaldir(const char* path, struct pair* d, int tmpfile_fd)
{
	/* basename() requires we duplicate the string */
	char* base = strdup(path);
	char* tmp_file = strdup(basename(base));
	int base_len = strlen(tmp_file);
	char* actual_file = 0;

	/* Snip off the .crt suffix*/
	if (base_len > 4 && strcmp(&tmp_file[base_len - 4], ".crt") == 0)
		tmp_file[base_len - 4] = 0;

	bool build_string = asprintf(&actual_file, "%s%s%s", "ca-cert-",
				     tmp_file, ".pem") != -1;

	if (base_len > 0 && build_string) {
		char* s;
		for (s = actual_file; *s != 0; s++) {
			switch(*s) {
			case ',':
			case ' ':
				*s = '_';
				break;
			case ')':
			case '(':
				*s = '=';
				break;
			default:
				break;
			}
		}

		if (add_ca_from_pem(d, path, actual_file)) {
			if (copyfile(path, tmpfile_fd) == -1)
				printf("Cant copy %s\n", path);
		} else {
			printf("Warn! Cannot add: %s\n", path);
		}
	} else {
		printf("Can't open path: %s\n", path);
	}

	free(base);
	free(actual_file);
	free(tmp_file);
}

static void proc_etccertsdir(const char* path, struct pair* d, int tmpfile_fd)
{
	struct stat statbuf;

	if (lstat(path, &statbuf) == -1)
		return;

	char* fullpath = (char*) malloc(sizeof(char*) *  statbuf.st_size + 1);
	if (readlink(path, fullpath, statbuf.st_size + 1) == -1)
		return;

	char* base = strdup(path);
	const char* actual_file = basename(base);
	int pos = -1;
	const char* target = get_pair(d, actual_file, &pos);

	if (!target) {
		/* Symlink exists but is not wanted
		 * Delete it if it points to 'our' directory
		 */
		if (str_begins(fullpath, CERTSDIR) || str_begins(fullpath, LOCALCERTSDIR))
			remove(fullpath);
	} else if (strncmp(fullpath, target, strlen(fullpath)) != 0) {
		/* Symlink exists but points wrong */
		if (symlink(target, path) == -1)
			printf("Warning! Can't link %s -> %s\n", target, path);
	} else {
		/* Symlink exists and is ok */
		memset(d->first[pos], 0, strlen(d->first[pos]));
	}

	free(base);
	free(fullpath);
}

static bool file_readline(const char* file, struct pair* d, int tmpfile_fd)
{
	FILE * fp = fopen(file, "r");
	if (fp == NULL)
		return false;

	char * line = NULL;
	size_t len = 0;
	ssize_t read;

	while ((read = getline(&line, &len, fp)) != -1) {
		if (str_begins(line, "#") || str_begins(line, "!"))
			continue;

		char* newline = strstr(line, "\n");
		if (newline) {
			line[newline - line] = '\0';
		}

		char* fullpath = 0;
		if (asprintf(&fullpath,"%s%s", CERTSDIR, line) != -1) {
			proc_localglobaldir(fullpath, d, tmpfile_fd);
			free(fullpath);
		}
	}

	fclose(fp);
	if (line)
		free(line);

	return true;
}

typedef enum {
	FILE_LINK,
	FILE_REGULAR
} filetype;

static bool is_filetype(const char* path, filetype file_check)
{
	struct stat statbuf;

	if (lstat(path, &statbuf) < 0)
		return false;
	switch(file_check) {
	case FILE_LINK: return S_ISLNK(statbuf.st_mode);
	case FILE_REGULAR: return S_ISREG(statbuf.st_mode);
	default: break;
	}

	return false;
}

static bool dir_readfiles(struct pair* d, const char* path,
			  filetype allowed_file_type,
			  proc_path path_processor,
			  int tmpfile_fd)
{
	DIR *dp = opendir(path);
	if (!dp)
		return false;
 
	struct dirent *dirp;
	while ((dirp = readdir(dp)) != NULL) {
		if (str_begins(dirp->d_name, "."))
			continue;

		int size = strlen(path) + strlen(dirp->d_name);
		char* fullpath = 0;
		if (asprintf(&fullpath, "%s%s", path, dirp->d_name) != -1) {
			if (is_filetype(fullpath, allowed_file_type))
				path_processor(fullpath, d, tmpfile_fd);

			free(fullpath);
		}
	}

	return closedir(dp) == 0;
}

int main(int a, char **v)
{
	struct pair* calinks = pair_alloc(256);

	const char* bundle = "bundleXXXXXX";
	int etccertslen = strlen(ETCCERTSDIR);
	char* tmpfile = 0;
	if (asprintf(&tmpfile, "%s%s", ETCCERTSDIR, bundle) == -1)
		return 0;

	int fd = mkstemp(tmpfile);
	if (fd == -1) {
		printf("Failed to open temporary file %s for ca bundle\n", tmpfile);
		exit(0);
	}

	/* Handle global CA certs from config file */
	file_readline(CERTSCONF, calinks, fd);

	/* Handle local CA certificates */
	dir_readfiles(calinks, LOCALCERTSDIR, FILE_REGULAR, &proc_localglobaldir, fd);

	/* Update etc cert dir for additions and deletions*/
	dir_readfiles(calinks, ETCCERTSDIR, FILE_LINK, &proc_etccertsdir, fd);

	int i = 0;
	for (i = 0; i < calinks->count; i++) {
		if (!strlen(calinks->first[i]))
			continue;
		int file_len = strlen(calinks->second[i]);
		char* newpath = 0;
		bool build_str = asprintf(&newpath, "%s%s", ETCCERTSDIR,
					 calinks->second[i]) != -1;
		if (!build_str || symlink(calinks->first[i], newpath) == -1)
			printf("Warning! Can't link %s -> %s\n",
			       calinks->first[i], newpath);
		free(newpath);
	}

	/* Update hashes and the bundle */
	if (fd != -1) {
		close(fd);
		char* newcertname = 0;
		if (asprintf(&newcertname, "%s%s", ETCCERTSDIR, CERTBUNDLE) != -1) {
			rename(tmpfile, newcertname);
			free(newcertname);
		}
	}

	pair_free(calinks);
	free(tmpfile);

	/* Execute c_rehash */
	int nullfd = open("/dev/null", O_WRONLY);
	if (nullfd == -1)
		return 0;

	if (dup2(nullfd, STDOUT_FILENO) == -1)
		return 0;

	char* c_rehash_args[] = { "/usr/bin/c_rehash", ETCCERTSDIR, ">", "/dev/null", 0 };
	execve(c_rehash_args[0], c_rehash_args, NULL);

	return 0;
}