aboutsummaryrefslogtreecommitdiffstats
path: root/src/main-cynagorad.c
blob: 0af145cd4b323fbbda56b2fa4466a7dbe1bab31b (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/******************************************************************************/
/******************************************************************************/
/* IMPLEMENTATION OF CYNAGORA SERVER                                          */
/******************************************************************************/
/******************************************************************************/

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/capability.h>

#if defined(WITH_SYSTEMD_ACTIVATION)
#include <systemd/sd-daemon.h>
#endif

#include "data.h"
#include "db.h"
#include "cyn.h"
#include "cyn-server.h"
#include "cyn-protocol.h"
#include "dbinit.h"
#include "agent-at.h"

#if !defined(DEFAULT_DB_DIR)
#    define  DEFAULT_DB_DIR      "/var/lib/cynagora"
#endif
#if !defined(DEFAULT_INIT_FILE)
#    define  DEFAULT_INIT_FILE   "/etc/security/cynagora.initial"
#endif
#if !defined(DEFAULT_CYNAGORA_USER)
#    define  DEFAULT_CYNAGORA_USER   NULL
#endif
#if !defined(DEFAULT_CYNAGORA_GROUP)
#    define  DEFAULT_CYNAGORA_GROUP  NULL
#endif
#if !defined(DEFAULT_LOCKFILE)
#    define  DEFAULT_LOCKFILE      ".cynagora-lock"
#endif

#define _DBDIR_       'd'
#define _GROUP_       'g'
#define _HELP_        'h'
#define _INIT_        'i'
#define _LOG_         'l'
#define _MAKEDBDIR_   'm'
#define _MAKESOCKDIR_ 'M'
#define _OWNSOCKDIR_  'O'
#define _OWNDBDIR_    'o'
#define _SOCKETDIR_   'S'
#define _SYSTEMD_     's'
#define _USER_        'u'
#define _VERSION_     'v'

static
const char
shortopts[] = "d:g:hi:lmMOoS:u:v"
#if defined(WITH_SYSTEMD_ACTIVATION)
	"s"
#endif
;

static
const struct option
longopts[] = {
	{ "dbdir", 1, NULL, _DBDIR_ },
	{ "group", 1, NULL, _GROUP_ },
	{ "help", 0, NULL, _HELP_ },
	{ "init", 1, NULL, _INIT_ },
	{ "log", 0, NULL, _LOG_ },
	{ "make-db-dir", 0, NULL, _MAKEDBDIR_ },
	{ "make-socket-dir", 0, NULL, _MAKESOCKDIR_ },
	{ "own-db-dir", 0, NULL, _OWNDBDIR_ },
	{ "own-socket-dir", 0, NULL, _OWNSOCKDIR_ },
	{ "socketdir", 1, NULL, _SOCKETDIR_ },
#if defined(WITH_SYSTEMD_ACTIVATION)
	{ "systemd", 0, NULL, _SYSTEMD_ },
#endif
	{ "user", 1, NULL, _USER_ },
	{ "version", 0, NULL, _VERSION_ },
	{ NULL, 0, NULL, 0 }
};

static
const char
helptxt[] =
	"\n"
	"usage: cynagorad [options]...\n"
	"\n"
	"otpions:\n"
#if defined(WITH_SYSTEMD_ACTIVATION)
	"	-s, --systemd         socket activation by systemd\n"
#endif
	"	-u, --user xxx        set the user\n"
	"	-g, --group xxx       set the group\n"
	"	-i, --init xxx        initialize if needed the database with file xxx\n"
	"	                        (default: "DEFAULT_INIT_FILE"\n"
	"	-l, --log             activate log of transactions\n"
	"	-d, --dbdir xxx       set the directory of database\n"
	"	                        (default: "DEFAULT_DB_DIR")\n"
	"	-m, --make-db-dir     make the database directory\n"
	"	-o, --own-db-dir      set user and group on database directory\n"
	"\n"
	"	-S, --socketdir xxx   set the base directory xxx for sockets\n"
	"	                        (default: %s)\n"
	"	-M, --make-socket-dir make the socket directory\n"
	"	-O, --own-socket-dir  set user and group on socket directory\n"
	"\n"
	"	-h, --help            print this help and exit\n"
	"	-v, --version         print the version and exit\n"
	"\n"
;

static
const char
versiontxt[] =
	"cynagorad version 1.99.99\n"
;

static int isid(const char *text);
static void ensure_directory(const char *path, int uid, int gid);
static int lockdir(const char *dir);

int main(int ac, char **av)
{
	int opt;
	int rc;
	int makesockdir = 0;
	int makedbdir = 0;
	int owndbdir = 0;
	int ownsockdir = 0;
	int flog = 0;
	int help = 0;
	int version = 0;
	int error = 0;
	int systemd = 0;
	int uid = -1;
	int gid = -1;
	const char *init = NULL;
	const char *dbdir = NULL;
	const char *socketdir = NULL;
	const char *user = NULL;
	const char *group = NULL;
	struct passwd *pw;
	struct group *gr;
	cap_t caps = { 0 };
	cyn_server_t *server;
	char *spec_socket_admin, *spec_socket_check, *spec_socket_agent;

	setlinebuf(stdout);
	setlinebuf(stderr);

	/* scan arguments */
	for (;;) {
		opt = getopt_long(ac, av, shortopts, longopts, NULL);
		if (opt == -1)
			break;

		switch(opt) {
		case _DBDIR_:
			dbdir = optarg;
			break;
		case _GROUP_:
			group = optarg;
			break;
		case _HELP_:
			help = 1;
			break;
		case _INIT_:
			init = optarg;
			break;
		case _LOG_:
			flog = 1;
			break;
		case _MAKEDBDIR_:
			makedbdir = 1;
			break;
		case _MAKESOCKDIR_:
			makesockdir = 1;
			break;
		case _OWNSOCKDIR_:
			ownsockdir = 1;
			break;
		case _OWNDBDIR_:
			owndbdir = 1;
			break;
		case _SOCKETDIR_:
			socketdir = optarg;
			break;
#if defined(WITH_SYSTEMD_ACTIVATION)
		case _SYSTEMD_:
			systemd = 1;
			break;
#endif
		case _USER_:
			user = optarg;
			break;
		case _VERSION_:
			version = 1;
			break;
		default:
			error = 1;
			break;
		}
	}

	/* handles help, version, error */
	if (help) {
		fprintf(stdout, helptxt, cyn_default_socket_dir);
		return 0;
	}
	if (version) {
		fprintf(stdout, versiontxt);
		return 0;
	}
	if (error)
		return 1;
	if (systemd && (socketdir || makesockdir)) {
		fprintf(stderr, "can't set options --systemd and --%s together\n",
			socketdir ? "socketdir" : "make-socket-dir");
		return 1;
	}

	/* set the defaults */
	dbdir = dbdir ?: DEFAULT_DB_DIR;
	socketdir = socketdir ?: cyn_default_socket_dir;
	user = user ?: DEFAULT_CYNAGORA_USER;
	group = group ?: DEFAULT_CYNAGORA_GROUP;
	init = init ?: DEFAULT_INIT_FILE;

	/* activate the agents */
	agent_at_activate();

	/* compute socket specs */
	spec_socket_admin = spec_socket_check = spec_socket_agent = 0;
	if (systemd) {
		spec_socket_admin = strdup("sd:admin");
		spec_socket_check = strdup("sd:check");
		spec_socket_agent = strdup("sd:agent");
	} else {
		rc = asprintf(&spec_socket_admin, "%s:%s/%s", cyn_default_socket_scheme, socketdir, cyn_default_admin_socket_base);
		rc = asprintf(&spec_socket_check, "%s:%s/%s", cyn_default_socket_scheme, socketdir, cyn_default_check_socket_base);
		rc = asprintf(&spec_socket_agent, "%s:%s/%s", cyn_default_socket_scheme, socketdir, cyn_default_agent_socket_base);
	}
	if (!spec_socket_admin || !spec_socket_check || !spec_socket_agent) {
		fprintf(stderr, "can't make socket paths\n");
		return 1;
	}

	/* compute user and group */
	if (user) {
		uid = isid(user);
		if (uid < 0) {
			pw = getpwnam(user);
			if (pw == NULL) {
				fprintf(stderr, "can not find user '%s'\n", user);
				return -1;
			}
			uid = (int)pw->pw_uid;
			gid = (int)pw->pw_gid;
		}
	}
	if (group) {
		gid = isid(group);
		if (gid < 0) {
			gr = getgrnam(group);
			if (gr == NULL) {
				fprintf(stderr, "can not find group '%s'\n", group);
				return -1;
			}
			gid = (int)gr->gr_gid;
		}
	}

	/* handle directories */
	if (makedbdir)
		ensure_directory(dbdir, owndbdir ? uid : -1, owndbdir ? gid : -1);
	if (makesockdir && socketdir[0] != '@')
		ensure_directory(socketdir, ownsockdir ? uid : -1, ownsockdir ? gid : -1);

	/* drop privileges */
	if (gid >= 0) {
		rc = setgid((gid_t)gid);
		if (rc < 0) {
			fprintf(stderr, "can not change group: %m\n");
			return -1;
		}
	}
	if (uid >= 0) {
		rc = setuid((uid_t)uid);
		if (rc < 0) {
			fprintf(stderr, "can not change user: %m\n");
			return -1;
		}
	}
	cap_clear(caps);
	rc = cap_set_proc(caps);

	/* get lock for the database */
	rc = lockdir(dbdir);
	if (rc < 0) {
		fprintf(stderr, "can not lock database of directory %s: %m\n", dbdir);
		return 1;
	}

	/* connection to the database */
	rc = db_open(dbdir);
	if (rc < 0) {
		fprintf(stderr, "can not open database of directory %s: %m\n", dbdir);
		return 1;
	}

	/* initialisation of the database */
	if (db_is_empty()) {
		rc = dbinit_add_file(init);
		if (rc < 0) {
			fprintf(stderr, "can't initialize database: %m\n");
			return 1;
		}
	}

	/* reset the change ids */
	cyn_changeid_reset();

	/* initialize server */
	setvbuf(stderr, NULL, _IOLBF, 1000);
	cyn_server_log = (bool)flog;
	signal(SIGPIPE, SIG_IGN); /* avoid SIGPIPE! */
	rc = cyn_server_create(&server, spec_socket_admin, spec_socket_check, spec_socket_agent);
	if (rc < 0) {
		fprintf(stderr, "can't initialize server: %m\n");
		return 1;
	}

	/* ready ! */
#if defined(WITH_SYSTEMD_ACTIVATION)
	if (systemd)
		sd_notify(0, "READY=1");
#endif

	/* serve */
	rc = cyn_server_serve(server);
	return rc ? 3 : 0;
}

/** returns the value of the id for 'text' (positive) or a negative value (-1) */
static int isid(const char *text)
{
	long long int value = 0;
	while(*text && value < INT_MAX)
		if (*text < '0' || *text > '9' || value >= INT_MAX)
			return -1;
		else
			value = 10 * value + (*text++ - '0');
	return value <= INT_MAX ? (int)value : -1;
}

/** returns a pointer to the first last / of the path if it is meaningful */
static char *enddir(char *path)
{
	/*
	 * /       -> NULL
	 * /xxx    -> NULL
	 * /xxx/   -> NULL
	 * /xxx/y  -> /y
	 * /xxx//y -> //y
	 */
	char *c = NULL, *r = NULL, *i = path;
	for(;;) {
		while(*i == '/')
			i++;
		if (*i)
			r = c;
		while(*i != '/')
			if (!*i++)
				return r;
		c = i;
	}
}

/** ensure that 'path' is a directory for the user and group */
static void ensuredir(char *path, int length, int uid, int gid)
{
	struct stat st;
	int rc, n;
	char *e;

	n = length;
	for(;;) {
		path[n] = 0;
		rc = mkdir(path, 0755);
		if (rc == 0 || errno == EEXIST) {
			/* exists */
			if (n == length) {
				rc = stat(path, &st);
				if (rc < 0) {
					fprintf(stderr, "can not check %s: %m\n", path);
					exit(1);
				} else if ((st.st_mode & S_IFMT) != S_IFDIR) {
					fprintf(stderr, "not a directory %s: %m\n", path);
					exit(1);
				}
				/* set ownership */
				if (((uid_t)uid != st.st_uid && uid >= 0) || ((gid_t)gid != st.st_gid && gid >= 0)) {
					rc = chown(path, (uid_t)uid, (gid_t)gid);
					if (rc < 0) {
						fprintf(stderr, "can not own directory %s for uid=%d & gid=%d: %m\n", path, uid, gid);
						exit(1);
					}
				}
				return;
			}
			path[n] = '/';
			n = (int)strlen(path);
		} else if (errno == ENOENT) {
			/* a part of the path doesn't exist, try to create it */
			e = enddir(path);
			if (!e) {
				/* can't create it because at root */
				fprintf(stderr, "can not ensure directory %s\n", path);
				exit(1);
			}
			n = (int)(e - path);
		} else {
			fprintf(stderr, "can not ensure directory %s: %m\n", path);
			exit(1);
		}
	}
}

/** ensure that 'path' is a directory for the user and group */
static void ensure_directory(const char *path, int uid, int gid)
{
	size_t l;
	char *p;

	l = strlen(path);
	if (l > INT_MAX) {
		/* ?!?!?!? *#@! */
		fprintf(stderr, "path toooooo long (%s)\n", path);
		exit(1);
	}
	p = strndupa(path, l);
	ensuredir(p, (int)l, uid, gid);
}

static int lockdir(const char *dir)
{
	char path[PATH_MAX];
	int fd, rc;

	rc = snprintf(path, sizeof path, "%s/%s", dir, DEFAULT_LOCKFILE);
	if (rc >= (int)sizeof path) {
		rc = -1;
		errno = ENAMETOOLONG;
	} else if (rc >= 0) {
		rc = open(path, O_RDWR|O_CREAT, 0600);
		if (rc >= 0) {
			fd = rc;
			rc = flock(fd, LOCK_EX|LOCK_NB);
		}
	}
	return rc;
}