Adds options --no-httpd and --exec
[src/app-framework-binder.git] / src / afb-config.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <limits.h>
26
27 #include "verbose.h"
28 #include "afb-config.h"
29 #include "afb-hook.h"
30
31 #include <afb/afb-binding.h>
32
33 #if !defined(BINDING_INSTALL_DIR)
34 #error "you should define BINDING_INSTALL_DIR"
35 #endif
36
37 #define AFB_VERSION    "0.5"
38
39 // default
40 #define DEFLT_CNTX_TIMEOUT  3600        // default Client Connection
41                                         // Timeout
42 #define DEFLT_API_TIMEOUT   20          // default Plugin API Timeout [0=NoLimit
43                                         // for Debug Only]
44 #define DEFLT_CACHE_TIMEOUT 100000      // default Static File Chache
45                                         // [Client Side Cache
46                                         // 100000~=1day]
47 #define DEFLT_AUTH_TOKEN    NULL        // expect for debug should == NULL
48 #define CTX_NBCLIENTS       10          // allow a default of 10 authenticated
49                                         // clients
50
51
52 // Define command line option
53 #define SET_VERBOSE        'v'
54 #define SET_BACKGROUND     2
55 #define SET_FORGROUND      3
56 #define SET_QUIET          'q'
57
58 #define SET_TCP_PORT       5
59 #define SET_ROOT_DIR       6
60 #define SET_ROOT_BASE      7
61 #define SET_ROOT_API       8
62 #define SET_ALIAS          9
63
64 #define SET_CACHE_TIMEOUT  10
65 #define SET_SESSION_DIR    11
66
67 #define SET_AUTH_TOKEN     12
68 #define SET_LDPATH         13
69 #define SET_APITIMEOUT     14
70 #define SET_CNTXTIMEOUT    15
71
72 #define DISPLAY_VERSION    'V'
73 #define DISPLAY_HELP       'h'
74
75 #define SET_MODE           18
76 #define SET_READYFD        19
77
78 #define DBUS_CLIENT        20
79 #define DBUS_SERVICE       21
80 #define SO_BINDING         22
81
82 #define SET_SESSIONMAX     23
83
84 #define WS_CLIENT          24
85 #define WS_SERVICE         25
86
87 #define SET_ROOT_HTTP      26
88
89 #define SET_TRACEREQ       27
90
91 #define SET_NO_HTTPD       28
92
93 #define SET_EXEC           'e'
94
95 #define SHORTOPTS       "vqhVe"
96
97 // Command line structure hold cli --command + help text
98 typedef struct {
99         int val;                // command number within application
100         int has_arg;            // command number within application
101         char *name;             // command as used in --xxxx cli
102         char *help;             // help text
103 } AFB_options;
104
105 // Supported option
106 static AFB_options cliOptions[] = {
107 /* *INDENT-OFF* */
108         {SET_VERBOSE,       0, "verbose",     "Verbose Mode, repeat to increase verbosity"},
109         {SET_QUIET,         0, "quiet",       "Quiet Mode, repeat to decrease verbosity"},
110
111         {SET_FORGROUND,     0, "foreground",  "Get all in foreground mode"},
112         {SET_BACKGROUND,    0, "daemon",      "Get all in background mode"},
113
114         {SET_TCP_PORT,      1, "port",        "HTTP listening TCP port  [default 1234]"},
115         {SET_ROOT_DIR,      1, "rootdir",     "Root Directory [default $HOME/.AFB]"},
116         {SET_ROOT_HTTP,     1, "roothttp",    "HTTP Root Directory [default rootdir]"},
117         {SET_ROOT_BASE,     1, "rootbase",    "Angular Base Root URL [default /opa]"},
118         {SET_ROOT_API,      1, "rootapi",     "HTML Root API URL [default /api]"},
119         {SET_ALIAS,         1, "alias",       "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
120
121         {SET_APITIMEOUT,    1, "apitimeout",  "Binding API timeout in seconds [default 10]"},
122         {SET_CNTXTIMEOUT,   1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
123         {SET_CACHE_TIMEOUT, 1, "cache-eol",   "Client cache end of live [default 3600]"},
124
125         {SET_SESSION_DIR,   1, "sessiondir",  "Sessions file path [default rootdir/sessions]"},
126
127         {SET_LDPATH,        1, "ldpaths",     "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
128         {SET_AUTH_TOKEN,    1, "token",       "Initial Secret [default=no-session, --token= for session without authentication]"},
129
130         {DISPLAY_VERSION,   0, "version",     "Display version and copyright"},
131         {DISPLAY_HELP,      0, "help",        "Display this help"},
132
133         {SET_MODE,          1, "mode",        "Set the mode: either local, remote or global"},
134         {SET_READYFD,       1, "readyfd",     "Set the #fd to signal when ready"},
135
136         {DBUS_CLIENT,       1, "dbus-client", "Bind to an afb service through dbus"},
137         {DBUS_SERVICE,      1, "dbus-server", "Provides an afb service through dbus"},
138         {WS_CLIENT,         1, "ws-client",   "Bind to an afb service through websocket"},
139         {WS_SERVICE,        1, "ws-server",   "Provides an afb service through websockets"},
140         {SO_BINDING,        1, "binding",     "Load the binding of path"},
141
142         {SET_SESSIONMAX,    1, "session-max", "Max count of session simultaneously [default 10]"},
143
144         {SET_TRACEREQ,      1, "tracereq",    "Log the requests: no, common, extra, all"},
145
146         {SET_NO_HTTPD,      0, "no-httpd",    "Forbids HTTP service"},
147         {SET_EXEC,          0, "exec",        "Execute the remaining arguments"},
148
149         {0, 0, NULL, NULL}
150 /* *INDENT-ON* */
151 };
152
153
154 struct enumdesc
155 {
156         const char *name;
157         int value;
158 };
159
160 static struct enumdesc tracereq_desc[] = {
161         { "no",     0 },
162         { "common", afb_hook_flags_req_common },
163         { "extra",  afb_hook_flags_req_extra },
164         { "all",    afb_hook_flags_req_all },
165         { NULL, 0 }
166 };
167
168 static struct enumdesc mode_desc[] = {
169         { "local",  AFB_MODE_LOCAL },
170         { "remote", AFB_MODE_REMOTE },
171         { "global", AFB_MODE_GLOBAL },
172         { NULL, 0 }
173 };
174
175 /*----------------------------------------------------------
176  | printversion
177  |   print version and copyright
178  +--------------------------------------------------------- */
179 static void printVersion(FILE * file)
180 {
181         fprintf(file, "\n----------------------------------------- \n");
182         fprintf(file, "  AFB [Application Framework Binder] version=%s |\n",
183                 AFB_VERSION);
184         fprintf(file, " \n");
185         fprintf(file,
186                 "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
187         fprintf(file, "  AFB comes with ABSOLUTELY NO WARRANTY.\n");
188         fprintf(file, "  Licence Apache 2\n\n");
189 }
190
191 /*----------------------------------------------------------
192  | printHelp
193  |   print information from long option array
194  +--------------------------------------------------------- */
195
196 static void printHelp(FILE * file, const char *name)
197 {
198         int ind;
199         char command[50];
200
201         fprintf(file, "%s:\nallowed options\n", name);
202         for (ind = 0; cliOptions[ind].name != NULL; ind++) {
203                 strcpy(command, cliOptions[ind].name);
204                 if (cliOptions[ind].has_arg)
205                         strcat(command, "=xxxx");
206                 fprintf(file, "  --%-15s %s\n", command, cliOptions[ind].help);
207         }
208         fprintf(file,
209                 "Example:\n  %s  --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
210                 name);
211 }
212
213 /*----------------------------------------------------------
214  |   adds a string to the list
215  +--------------------------------------------------------- */
216 static void list_add(struct afb_config_list **head, char *value)
217 {
218         struct afb_config_list *item;
219
220         /*
221          * search tail 
222          */
223         item = *head;
224         while (item != NULL) {
225                 head = &item->next;
226                 item = item->next;
227         }
228
229         /*
230          * alloc the item 
231          */
232         item = malloc(sizeof *item);
233         if (item == NULL) {
234                 ERROR("out of memory");
235                 exit(1);
236         }
237
238         /*
239          * init the item 
240          */
241         *head = item;
242         item->value = value;
243         item->next = NULL;
244 }
245
246 /*---------------------------------------------------------
247  |   helpers for argument scanning
248  +--------------------------------------------------------- */
249
250 static char *argvalstr(int index)
251 {
252         if (optarg == 0) {
253                 ERROR("option [--%s] needs a value i.e. --%s=xxx",
254                       cliOptions[index].name, cliOptions[index].name);
255                 exit(1);
256         }
257         return optarg;
258 }
259
260 static int argvalenum(int index, struct enumdesc *desc)
261 {
262         int i;
263         size_t len;
264         char *list, *name = argvalstr(index);
265
266         i = 0;
267         while(desc[i].name && strcmp(desc[i].name, name))
268                 i++;
269         if (!desc[i].name) {
270                 len = 0;
271                 i = 0;
272                 while(desc[i].name)
273                         len += strlen(desc[i++].name);
274                 list = malloc(len + i + i);
275                 if (!i || !list)
276                         ERROR("option [--%s] bad value (found %s)",
277                                 cliOptions[index].name, name);
278                 else {
279                         i = 0;
280                         strcpy(list, desc[i].name ? : "");
281                         while(desc[++i].name)
282                                 strcat(strcat(list, ", "), desc[i].name);
283                         ERROR("option [--%s] bad value, only accepts values %s (found %s)",
284                                 cliOptions[index].name, list, name);
285                 }
286                 free(list);
287                 exit(1);
288         }
289         return desc[i].value;
290 }
291
292 static int argvalint(int index, int mini, int maxi, int base)
293 {
294         char *beg, *end;
295         long int val;
296         beg = argvalstr(index);
297         val = strtol(beg, &end, base);
298         if (*end || end == beg) {
299                 ERROR("option [--%s] requires a valid integer (found %s)",
300                         cliOptions[index].name, beg);
301                 exit(1);
302         }
303         if (val < (long int)mini || val > (long int)maxi) {
304                 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
305                         cliOptions[index].name, mini, val, maxi);
306                 exit(1);
307         }
308         return (int)val;
309 }
310
311 static int argvalintdec(int index, int mini, int maxi)
312 {
313         return argvalint(index, mini, maxi, 10);
314 }
315
316 static void noarg(int index)
317 {
318         if (optarg != 0) {
319                 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
320                 exit(1);
321         }
322 }
323
324 /*---------------------------------------------------------
325  |   Parse option and launch action
326  +--------------------------------------------------------- */
327
328 static void parse_arguments(int argc, char **argv, struct afb_config *config)
329 {
330         char *programName = argv[0];
331         int optionIndex = 0;
332         int optc, ind;
333         int nbcmd;
334         struct option *gnuOptions;
335
336         // ------------------ Process Command Line -----------------------
337
338         // build GNU getopt info from cliOptions
339         nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
340         gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
341         for (ind = 0; ind < nbcmd; ind++) {
342                 gnuOptions[ind].name = cliOptions[ind].name;
343                 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
344                 gnuOptions[ind].flag = 0;
345                 gnuOptions[ind].val = cliOptions[ind].val;
346         }
347
348         // get all options from command line
349         while ((optc = getopt_long(argc, argv, SHORTOPTS, gnuOptions, &optionIndex)) != EOF) {
350                 switch (optc) {
351                 case SET_VERBOSE:
352                         verbosity++;
353                         break;
354
355                 case SET_QUIET:
356                         verbosity--;
357                         break;
358
359                 case SET_TCP_PORT:
360                         config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
361                         break;
362
363                 case SET_APITIMEOUT:
364                         config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
365                         break;
366
367                 case SET_CNTXTIMEOUT:
368                         config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
369                         break;
370
371                 case SET_ROOT_DIR:
372                         config->rootdir = argvalstr(optionIndex);
373                         INFO("Forcing Rootdir=%s", config->rootdir);
374                         break;
375
376                 case SET_ROOT_HTTP:
377                         config->roothttp = argvalstr(optionIndex);
378                         INFO("Forcing Root HTTP=%s", config->roothttp);
379                         break;
380
381                 case SET_ROOT_BASE:
382                         config->rootbase = argvalstr(optionIndex);
383                         INFO("Forcing Rootbase=%s", config->rootbase);
384                         break;
385
386                 case SET_ROOT_API:
387                         config->rootapi = argvalstr(optionIndex);
388                         INFO("Forcing Rootapi=%s", config->rootapi);
389                         break;
390
391                 case SET_ALIAS:
392                         list_add(&config->aliases, argvalstr(optionIndex));
393                         break;
394
395                 case SET_AUTH_TOKEN:
396                         config->token = argvalstr(optionIndex);
397                         break;
398
399                 case SET_LDPATH:
400                         list_add(&config->ldpaths, argvalstr(optionIndex));
401                         break;
402
403                 case SET_SESSION_DIR:
404                         config->sessiondir = argvalstr(optionIndex);
405                         break;
406
407                 case SET_CACHE_TIMEOUT:
408                         config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
409                         break;
410
411                 case SET_SESSIONMAX:
412                         config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
413                         break;
414
415                 case SET_FORGROUND:
416                         noarg(optionIndex);
417                         config->background = 0;
418                         break;
419
420                 case SET_BACKGROUND:
421                         noarg(optionIndex);
422                         config->background = 1;
423                         break;
424
425                 case SET_MODE:
426                         config->mode = argvalenum(optionIndex, mode_desc);
427                         break;
428
429                 case SET_READYFD:
430                         config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
431                         break;
432
433                 case DBUS_CLIENT:
434                         list_add(&config->dbus_clients, argvalstr(optionIndex));
435                         break;
436
437                 case DBUS_SERVICE:
438                         list_add(&config->dbus_servers, argvalstr(optionIndex));
439                         break;
440
441                 case WS_CLIENT:
442                         list_add(&config->ws_clients, argvalstr(optionIndex));
443                         break;
444
445                 case WS_SERVICE:
446                         list_add(&config->ws_servers, argvalstr(optionIndex));
447                         break;
448
449                 case SO_BINDING:
450                         list_add(&config->so_bindings, argvalstr(optionIndex));
451                         break;
452
453                 case SET_TRACEREQ:
454                         config->tracereq = argvalenum(optionIndex, tracereq_desc);
455                         break;
456
457                 case SET_NO_HTTPD:
458                         noarg(optionIndex);
459                         config->noHttpd = 1;
460                         break;
461
462                 case SET_EXEC:
463                         config->exec = &argv[optind];
464                         optind = argc;
465                         break;
466
467                 case DISPLAY_VERSION:
468                         noarg(optionIndex);
469                         printVersion(stdout);
470                         exit(0);
471
472                 case DISPLAY_HELP:
473                         printHelp(stdout, programName);
474                         exit(0);
475
476                 default:
477                         exit(1);
478                 }
479         }
480         free(gnuOptions);
481 }
482
483 // load config from disk and merge with CLI option
484 static void config_set_default(struct afb_config *config)
485 {
486         // default HTTP port
487         if (config->httpdPort == 0)
488                 config->httpdPort = 1234;
489
490         // default binding API timeout
491         if (config->apiTimeout == 0)
492                 config->apiTimeout = DEFLT_API_TIMEOUT;
493
494         // default AUTH_TOKEN
495         if (config->token == NULL)
496                 config->token = DEFLT_AUTH_TOKEN;
497
498         // cache timeout default one hour
499         if (config->cacheTimeout == 0)
500                 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
501
502         // cache timeout default one hour
503         if (config->cntxTimeout == 0)
504                 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
505
506         // max count of sessions
507         if (config->nbSessionMax == 0)
508                 config->nbSessionMax = CTX_NBCLIENTS;
509
510         if (config->rootdir == NULL) {
511                 config->rootdir = getenv("AFBDIR");
512                 if (config->rootdir == NULL) {
513                         config->rootdir = malloc(512);
514                         strncpy(config->rootdir, getenv("HOME"), 512);
515                         strncat(config->rootdir, "/.AFB", 512);
516                 }
517         }
518         // if no Angular/HTML5 rootbase let's try '/' as default
519         if (config->rootbase == NULL)
520                 config->rootbase = "/opa";
521
522         if (config->rootapi == NULL)
523                 config->rootapi = "/api";
524
525         if (config->ldpaths == NULL)
526                 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
527
528         // if no session dir create a default path from rootdir
529         if (config->sessiondir == NULL) {
530                 config->sessiondir = malloc(512);
531                 strncpy(config->sessiondir, config->rootdir, 512);
532                 strncat(config->sessiondir, "/sessions", 512);
533         }
534         // if no config dir create a default path from sessiondir
535         if (config->console == NULL) {
536                 config->console = malloc(512);
537                 strncpy(config->console, config->sessiondir, 512);
538                 strncat(config->console, "/AFB-console.out", 512);
539         }
540 }
541
542 void afb_config_dump(struct afb_config *config)
543 {
544         struct afb_config_list *l;
545         struct enumdesc *e;
546         char **v;
547
548 #define NN(x)   (x)?:""
549 #define P(...)  fprintf(stderr, __VA_ARGS__)
550 #define PF(x)   P("-- %15s: ", #x)
551 #define PE      P("\n")
552 #define S(x)    PF(x);P("%s",NN(config->x));PE;
553 #define D(x)    PF(x);P("%d",config->x);PE;
554 #define H(x)    PF(x);P("%x",config->x);PE;
555 #define B(x)    PF(x);P("%s",config->x?"yes":"no");PE;
556 #define L(x)    PF(x);l=config->x;if(l){P("%s\n",NN(l->value));for(l=l->next;l;l=l->next)P("-- %15s  %s\n","",NN(l->value));}else PE;
557 #define E(x,d)  for(e=d;e->name&&e->value!=config->x;e++);if(e->name){PF(x);P("%s",e->name);PE;}else{D(x);}
558 #define V(x)    P("-- %15s:", #x);for(v=config->x;v&&*v;v++)P(" %s",*v); PE;
559
560         P("---BEGIN-OF-CONFIG---\n");
561         S(console)
562         S(rootdir)
563         S(roothttp)
564         S(rootbase)
565         S(rootapi)
566         S(sessiondir)
567         S(token)
568
569         L(aliases)
570         L(dbus_clients)
571         L(dbus_servers)
572         L(ws_clients)
573         L(ws_servers)
574         L(so_bindings)
575         L(ldpaths)
576
577         V(exec)
578
579         D(httpdPort)
580         B(background)
581         D(readyfd)
582         D(cacheTimeout)
583         D(apiTimeout)
584         D(cntxTimeout)
585         D(nbSessionMax)
586         E(mode,mode_desc)
587         E(tracereq,tracereq_desc)
588         B(noHttpd)
589         P("---END-OF-CONFIG---\n");
590
591 #undef V
592 #undef E
593 #undef L
594 #undef B
595 #undef H
596 #undef D
597 #undef S
598 #undef PE
599 #undef PF
600 #undef P
601 #undef NN
602 }
603
604 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
605 {
606         struct afb_config *result;
607
608         result = calloc(1, sizeof *result);
609
610         parse_arguments(argc, argv, result);
611         config_set_default(result);
612         if (verbosity >= 3)
613                 afb_config_dump(result);
614         return result;
615 }
616