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