aboutsummaryrefslogtreecommitdiffstats
path: root/src/afm-launch.c
blob: 46bc4e02e0489c58cea126921a3fe7a319984674 (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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
 Copyright 2015 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.
*/

#define _GNU_SOURCE

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>

extern char **environ;

#include "verbose.h"
#include "afm-launch-mode.h"
#include "afm-launch.h"
#include "secmgr-wrap.h"

#define DEFAULT_TYPE "text/html"

struct type_list {
	struct type_list *next;
	char type[1];
};

struct desc_list {
	struct desc_list *next;
	enum afm_launch_mode mode;
	struct type_list *types;
	char **execs[2];
};

struct launchparam {
	int port;
	char **uri;
	const char *secret;
	const char *datadir;
	const char **master;
	const char **slave;
};

struct confread {
	const char *filepath;
	FILE *file;
	int lineno;
	int index;
	int length;
	char buffer[4096];
};

struct desc_list *launchers = NULL;

static gid_t groupid = 0;

const char separators[] = " \t\n";

static void dump_launchers()
{
	int j, k;
	struct desc_list *desc;
	struct type_list *type;

	for (desc = launchers ; desc != NULL ; desc = desc->next) {
		printf("mode %s\n", name_of_launch_mode(desc->mode));
		for (type = desc->types ; type != NULL ; type = type->next)
			printf("%s\n", type->type);
		for ( j = 0 ; j < 2 ; j++)
			if (desc->execs[j] != NULL) {
				for (k = 0 ; desc->execs[j][k] != NULL ; k++)
					printf("  %s", desc->execs[j][k]);
				printf("\n");
			}
		printf("\n");
	}
}

static int next_token(struct confread *cread)
{
	int idx = cread->index + cread->length;
	cread->index = idx + strspn(&cread->buffer[idx], separators);
	cread->length = strcspn(&cread->buffer[cread->index], separators);
	return cread->length;
}

static int read_line(struct confread *cread)
{
	while (fgets(cread->buffer, sizeof cread->buffer, cread->file) != NULL) {
		cread->lineno++;
		cread->index = strspn(cread->buffer, separators);
		if (cread->buffer[cread->index] && cread->buffer[cread->index] != '#') {
			cread->length = strcspn(&cread->buffer[cread->index], separators);
			assert(cread->length > 0);
			return cread->length;
		}
	}
	if (ferror(cread->file)) {
		ERROR("%s:%d: error while reading, %m", cread->filepath, cread->lineno);
		return -1;
	}
	return 0;
}

static char **read_vector(struct confread *cread)
{
	int index0, length0;
	char **vector, *args;
	int count, length;

	/* record origin */
	index0 = cread->index;
	length0 = cread->length;

	/* count */
	count = 0;
	length = 0;
	while(cread->length) {
		count++;
		length += cread->length;
		next_token(cread);
	}

	/* allocates */
	cread->index = index0;
	cread->length = length0;
	vector = malloc(length + count + (count + 1) * sizeof(char*));
	if (vector == NULL)
		return NULL;

	/* copies */
	args = (char*)(vector + count + 1);
	count = 0;
	while(cread->length) {
		vector[count++] = args;
		memcpy(args, &cread->buffer[cread->index], cread->length);
		args += cread->length;
		*args++ = 0;
		next_token(cread);
	}
	vector[count] = NULL;
	cread->index = index0;
	cread->length = length0;
	return vector;
}

static struct type_list *read_type(struct confread *cread)
{
	int index, length;
	struct type_list *result;

	/* record index and length */
	index = cread->index;
	length = cread->length;

	/* check no extra characters */
	if (next_token(cread)) {
		ERROR("%s:%d: extra characters found after type %.*s",
			cread->filepath, cread->lineno, length, &cread->buffer[index]);
		errno = EINVAL;
		return NULL;
	}

	/* allocate structure */
	result = malloc(sizeof(struct type_list) + length);
	if (result == NULL) {
		ERROR("%s:%d: out of memory", cread->filepath, cread->lineno);
		errno = ENOMEM;
		return NULL;
	}

	/* fill the structure */
	memcpy(result->type, &cread->buffer[index], length);
	result->type[length] = 0;
	return result;
}

static enum afm_launch_mode read_mode(struct confread *cread)
{
	int index, length;
	enum afm_launch_mode result;

	assert(cread->index == 0);
	assert(!strncmp(&cread->buffer[cread->index], "mode", 4));

	/* get the next token: the mode string */
	if (!next_token(cread)) {
		ERROR("%s:%d: no mode value set", cread->filepath, cread->lineno);
		errno = EINVAL;
		return invalid_launch_mode;
	}

	/* record index and length */
	index = cread->index;
	length = cread->length;

	/* check no extra characters */
	if (next_token(cread)) {
		ERROR("%s:%d: extra characters found after mode %.*s",
			cread->filepath, cread->lineno, length, &cread->buffer[index]);
		errno = EINVAL;
		return invalid_launch_mode;
	}

	/* get the mode */
	cread->buffer[index + length] = 0;
	result = launch_mode_of_string(&cread->buffer[index]);
	if (result == invalid_launch_mode) {
		ERROR("%s:%d: invalid mode value %s",
			cread->filepath, cread->lineno, &cread->buffer[index]);
		errno = EINVAL;
	}
	return result;
}

static void free_type_list(struct type_list *types)
{
	while (types != NULL) {
		struct type_list *next = types->next;
		free(types);
		types = next;
	}
}

static int read_launchers(struct confread *cread)
{
	int rc;
	struct type_list *types, *lt;
	struct desc_list *desc;
	enum afm_launch_mode mode;
	char **vector;

	/* reads the file */
	lt = NULL;
	types = NULL;
	desc = NULL;
	mode = invalid_launch_mode;
	rc = read_line(cread);
	while (rc > 0) {
		if (cread->index == 0) {
			if (cread->length == 4
			&& !memcmp(&cread->buffer[cread->index], "mode", 4)) {
				/* check if allowed */
				if (types != NULL) {
					ERROR("%s:%d: mode found before launch vector",
						cread->filepath, cread->lineno);
					errno = EINVAL;
					free_type_list(types);
					return -1;
				}

				/* read the mode */
				mode = read_mode(cread);
				if (mode == invalid_launch_mode)
					return -1;
			} else {
				if (mode == invalid_launch_mode) {
					ERROR("%s:%d: mode not found before type",
							cread->filepath, cread->lineno);
					errno = EINVAL;
					assert(types == NULL);
					return -1;
				}
				/* read a type */
				lt = read_type(cread);
				if (lt == NULL) {
					free_type_list(types);
					return -1;
				}
				lt->next = types;
				types = lt;
			}
			desc = NULL;
		} else if (types == NULL && desc == NULL) {
			if (lt == NULL)
				ERROR("%s:%d: untyped launch vector found",
					cread->filepath, cread->lineno);
			else
				ERROR("%s:%d: extra launch vector found (2 max)",
					cread->filepath, cread->lineno);
			errno = EINVAL;
			return -1;
		} else {
			vector = read_vector(cread);
			if (vector == NULL) {
				ERROR("%s:%d: out of memory",
					cread->filepath, cread->lineno);
				free_type_list(types);
				errno = ENOMEM;
				return -1;
			}
			if (types) {
				assert(desc == NULL);
				desc = malloc(sizeof * desc);
				if (desc == NULL) {
					ERROR("%s:%d: out of memory",
						cread->filepath, cread->lineno);
					free_type_list(types);
					errno = ENOMEM;
					return -1;
				}
				desc->next = launchers;
				desc->mode = mode;
				desc->types = types;
				desc->execs[0] = vector;
				desc->execs[1] = NULL;
				types = NULL;
				launchers = desc;
			} else {
				desc->execs[1] = vector;
				desc = NULL;
			}
		}
		rc = read_line(cread);
	}
	if (types != NULL) {
		ERROR("%s:%d: end of file found before launch vector",
			cread->filepath, cread->lineno);
		free_type_list(types);
		errno = EINVAL;
		return -1;
	}
	return rc;
}

static int read_configuration_file(const char *filepath)
{
	int rc;
	struct confread cread;

	/* opens the configuration file */
	cread.file = fopen(filepath, "r");
	if (cread.file == NULL) {
		/* error */
		ERROR("can't read file %s: %m", filepath);
		rc = -1;
	} else {
		/* reads it */
		cread.filepath = filepath;
		cread.lineno = 0;
		rc = read_launchers(&cread);
		fclose(cread.file);
	}
	return rc;
}

/*
%I icondir			FWK_ICON_DIR
%P port				params->port
%S secret			params->secret
%D datadir			params->datadir
%r rootdir			desc->path
%h homedir			desc->home
%t tag (smack label)		desc->tag
%a appid			desc->appid
%c content			desc->content
%m mime-type			desc->type
%n name				desc->name
%p plugins			desc->plugins
%W width			desc->width
%H height			desc->height
%% %
*/

union arguments {
	char *scalar;
	char **vector;
};

static union arguments instantiate_arguments(
	const char            **args,
	struct afm_launch_desc *desc,
	struct launchparam     *params,
	int                     wants_vector
)
{
	const char **iter, *p, *v;
	char *data, port[20], width[20], height[20], mini[3], c, sep;
	int n, s;
	union arguments result;

	/* init */
	sep = wants_vector ? 0 : ' ';
	mini[0] = '%';
	mini[2] = 0;

	/* loop that either compute the size and build the result */
	data = NULL;
	n = s = 0;
	for (;;) {
		iter = args;
		n = 0;
		while (*iter) {
			p = *iter++;
			if (data && !sep)
				result.vector[n] = data;
			n++;
			while((c = *p++) != 0) {
				if (c != '%') {
					if (data)
						*data++ = c;
					else
						s++;
				} else {
					c = *p++;
					switch (c) {
					case 'I': v = FWK_ICON_DIR; break;
					case 'S': v = params->secret; break;
					case 'D': v = params->datadir; break;
					case 'r': v = desc->path; break;
					case 'h': v = desc->home; break;
					case 't': v = desc->tag; break;
					case 'a': v = desc->appid; break;
					case 'c': v = desc->content; break;
					case 'm': v = desc->type; break;
					case 'n': v = desc->name; break;
					case 'p': v = "" /*desc->plugins*/; break;
					case 'P':
						if(!data)
							sprintf(port, "%d", params->port);
						v = port;
						break;
					case 'W':
						if(!data)
							sprintf(width, "%d", desc->width);
						v = width;
						break;
					case 'H':
						if(!data)
							sprintf(height, "%d", desc->height);
						v = height;
						break;
					case '%':
						c = 0;
					default:
						mini[1] = c;
						v = mini;
						break;
					}
					if (data)
						data = stpcpy(data, v);
					else
						s += strlen(v);
				}
			}
			if (data)
				*data++ = sep;
			else
				s++;
		}
		if (sep) {
			assert(!wants_vector);
			if (data) {
				*--data = 0;
				return result;
			}
			/* allocation */
			result.scalar = malloc(s);
			if (result.scalar == NULL) {
				errno = ENOMEM;
				return result;
			}
			data = result.scalar;
		} else {
			assert(wants_vector);
			if (data) {
				result.vector[n] = NULL;
				return result;
			}
			/* allocation */
			result.vector = malloc((n+1)*sizeof(char*) + s);
			if (result.vector == NULL) {
				errno = ENOMEM;
				return result;
			}
			data = (char*)(&result.vector[n + 1]);
		}
	}
}

static void mksecret(char buffer[9])
{
	snprintf(buffer, 9, "%08lX", (0xffffffff & random()));
}

static int mkport()
{
	static int port_ring = 12345;
	int port = port_ring;
	if (port < 12345 || port > 15432)
		port = 12345;
	port_ring = port + 1;
	return port;
}

static int launch_local_1(
	struct afm_launch_desc *desc,
	pid_t                   children[2],
	struct launchparam     *params
)
{
	int rc;
	char **args;

	/* fork the master child */
	children[0] = fork();
	if (children[0] < 0) {
		ERROR("master fork failed: %m");
		return -1;
	}
	if (children[0]) {
		/********* in the parent process ************/
		return 0;
	}

	/********* in the master child ************/

	/* avoid set-gid effect */
	setresgid(groupid, groupid, groupid);

	/* enter the process group */
	rc = setpgid(0, 0);
	if (rc) {
		ERROR("setpgid failed");
		_exit(1);
	}

	/* enter security mode */
	rc = secmgr_prepare_exec(desc->tag);
	if (rc < 0) {
		ERROR("call to secmgr_prepare_exec failed: %m");
		_exit(1);
	}

	/* enter the datadirectory */
	rc = mkdir(params->datadir, 0755);
	if (rc && errno != EEXIST) {
		ERROR("creation of datadir %s failed: %m", params->datadir);
		_exit(1);
	}
	rc = chdir(params->datadir);
	if (rc) {
		ERROR("can't enter the datadir %s: %m", params->datadir);
		_exit(1);
	}

	args = instantiate_arguments(params->master, desc, params, 1).vector;
	if (args == NULL) {
		ERROR("out of memory in master");
	}
	else {
		rc = execve(args[0], args, environ);
		ERROR("failed to exec master %s: %m", args[0]);
	}
	_exit(1);
}

static int launch_local_2(
	struct afm_launch_desc *desc,
	pid_t                   children[2],
	struct launchparam     *params
)
{
	int rc;
	char message[10];
	int mpipe[2];
	int spipe[2];
	char **args;

	/* prepare the pipes */
	rc = pipe2(mpipe, O_CLOEXEC);
	if (rc < 0) {
		ERROR("error while calling pipe2: %m");
		return -1;
	}
	rc = pipe2(spipe, O_CLOEXEC);
	if (rc < 0) {
		ERROR("error while calling pipe2: %m");
		close(spipe[0]);
		close(spipe[1]);
		return -1;
	}

	/* fork the master child */
	children[0] = fork();
	if (children[0] < 0) {
		ERROR("master fork failed: %m");
		close(mpipe[0]);
		close(mpipe[1]);
		close(spipe[0]);
		close(spipe[1]);
		return -1;
	}
	if (children[0]) {
		/********* in the parent process ************/
		close(mpipe[1]);
		close(spipe[0]);
		/* wait the ready signal (that transmit the slave pid) */
		rc = read(mpipe[0], &children[1], sizeof children[1]);
		close(mpipe[0]);
		if (rc  <= 0) {
			ERROR("reading master pipe failed: %m");
			close(spipe[1]);
			return -1;
		}
		assert(rc == sizeof children[1]);
		/* start the child */
		rc = write(spipe[1], "start", 5);
		if (rc < 0) {
			ERROR("writing slave pipe failed: %m");
			close(spipe[1]);
			return -1;
		}
		assert(rc == 5);
		close(spipe[1]);
		return 0;
	}

	/********* in the master child ************/
	close(mpipe[0]);
	close(spipe[1]);

	/* avoid set-gid effect */
	setresgid(groupid, groupid, groupid);

	/* enter the process group */
	rc = setpgid(0, 0);
	if (rc) {
		ERROR("setpgid failed");
		_exit(1);
	}

	/* enter security mode */
	rc = secmgr_prepare_exec(desc->tag);
	if (rc < 0) {
		ERROR("call to secmgr_prepare_exec failed: %m");
		_exit(1);
	}

	/* enter the datadirectory */
	rc = mkdir(params->datadir, 0755);
	if (rc && errno != EEXIST) {
		ERROR("creation of datadir %s failed: %m", params->datadir);
		_exit(1);
	}
	rc = chdir(params->datadir);
	if (rc) {
		ERROR("can't enter the datadir %s: %m", params->datadir);
		_exit(1);
	}

	/* fork the slave child */
	children[1] = fork();
	if (children[1] < 0) {
		ERROR("slave fork failed: %m");
		_exit(1);
	}
	if (children[1] == 0) {
		/********* in the slave child ************/
		close(mpipe[0]);
		rc = read(spipe[0], message, sizeof message);
		if (rc <= 0) {
			ERROR("reading slave pipe failed: %m");
			_exit(1);
		}

		args = instantiate_arguments(params->slave, desc, params, 1).vector;
		if (args == NULL) {
			ERROR("out of memory in slave");
		}
		else {
			rc = execve(args[0], args, environ);
			ERROR("failed to exec slave %s: %m", args[0]);
		}
		_exit(1);
	}

	/********* still in the master child ************/
	close(spipe[1]);
	args = instantiate_arguments(params->master, desc, params, 1).vector;
	if (args == NULL) {
		ERROR("out of memory in master");
	}
	else {
		rc = write(mpipe[1], &children[1], sizeof children[1]);
		if (rc <= 0) {
			ERROR("can't write master pipe: %m");
		}
		else {
			close(mpipe[1]);
			rc = execve(args[0], args, environ);
			ERROR("failed to exec master %s: %m", args[0]);
		}
	}
	_exit(1);
}

static int launch_local(
	struct afm_launch_desc *desc,
	pid_t                   children[2],
	struct launchparam     *params
)
{
	if (params->slave == NULL)
		return launch_local_1(desc, children, params);
	return launch_local_2(desc, children, params);
}

static int launch_remote(
	struct afm_launch_desc *desc,
	pid_t                   children[2],
	struct launchparam     *params
)
{
	int rc;
	char *uri;

	/* instanciate the uri */
	if (params->slave == NULL)
		uri = strdup("");
	else
		uri = instantiate_arguments(params->slave, desc, params, 0).scalar;
	if (uri == NULL) {
		ERROR("out of memory for remote uri");
		errno = ENOMEM;
		return -1;
	}

	/* launch the command */
	rc = launch_local_1(desc, children, params);
	if (rc)
		free(uri);
	else
		*params->uri = uri;
	return rc;
}

int afm_launch_initialize()
{
	int rc;
	gid_t r, e, s;

	getresgid(&r, &e, &s);
	if (s && s != e)
		groupid = s;
	else
		groupid = -1;

	rc = read_configuration_file(FWK_LAUNCH_CONF);
	dump_launchers();
	return rc;
}

static struct desc_list *search_launcher(const char *type, enum afm_launch_mode mode)
{
	struct desc_list *dl;
	struct type_list *tl;

	for (dl = launchers ; dl ; dl = dl->next)
		if (dl->mode == mode)
			for (tl = dl->types ; tl != NULL ; tl = tl->next)
				if (!strcmp(tl->type, type))
					return dl;
	return NULL;
}

int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri)
{
	int rc;
	char datadir[PATH_MAX];
	char secret[9];
	struct launchparam params;
	const char *type;
	struct desc_list *dl;

	/* should be init */
	assert(groupid != 0);
	assert(launch_mode_is_valid(desc->mode));
	assert(desc->mode == mode_local || uri != NULL);
	assert(uri == NULL || *uri == NULL);

	/* init */
	children[0] = 0;
	children[1] = 0;

	/* what launcher ? */
	type = desc->type != NULL && *desc->type ? desc->type : DEFAULT_TYPE;
	dl = search_launcher(type, desc->mode);
	if (dl == NULL) {
		ERROR("type %s not found for mode %s!", type, name_of_launch_mode(desc->mode));
		errno = ENOENT;
		return -1;
	}

	/* prepare paths */
	rc = snprintf(datadir, sizeof datadir, "%s/%s", desc->home, desc->tag);
	if (rc < 0 || rc >= sizeof datadir) {
		ERROR("overflow for datadir");
		errno = EINVAL;
		return -1;
	}

	/* make the secret and port */
	mksecret(secret);
	params.uri = uri;
	params.port = mkport();
	params.secret = secret;
	params.datadir = datadir;
	params.master = (const char **)dl->execs[0];
	params.slave = (const char **)dl->execs[1];

	switch (desc->mode) {
	case mode_local:
		return launch_local(desc, children, &params);
	case mode_remote:
		return launch_remote(desc, children, &params);
	default:
		assert(0);
		return -1;
	}
}