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