allows to repeat option --ldpaths
[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        1
54 #define SET_BACKGROUND     2
55 #define SET_FORGROUND      3
56
57 #define SET_TCP_PORT       5
58 #define SET_ROOT_DIR       6
59 #define SET_ROOT_BASE      7
60 #define SET_ROOT_API       8
61 #define SET_ALIAS          9
62
63 #define SET_CACHE_TIMEOUT  10
64 #define SET_SESSION_DIR    11
65
66 #define SET_AUTH_TOKEN     12
67 #define SET_LDPATH         13
68 #define SET_APITIMEOUT     14
69 #define SET_CNTXTIMEOUT    15
70
71 #define DISPLAY_VERSION    16
72 #define DISPLAY_HELP       17
73
74 #define SET_MODE           18
75 #define SET_READYFD        19
76
77 #define DBUS_CLIENT        20
78 #define DBUS_SERVICE       21
79 #define SO_BINDING         22
80
81 #define SET_SESSIONMAX     23
82
83 #define WS_CLIENT          24
84 #define WS_SERVICE         25
85
86 #define SET_ROOT_HTTP      26
87
88 #define SET_TRACEREQ       27
89
90 // Command line structure hold cli --command + help text
91 typedef struct {
92         int val;                // command number within application
93         int has_arg;            // command number within application
94         char *name;             // command as used in --xxxx cli
95         char *help;             // help text
96 } AFB_options;
97
98 // Supported option
99 static AFB_options cliOptions[] = {
100 /* *INDENT-OFF* */
101         {SET_VERBOSE,       0, "verbose",     "Verbose Mode, repeat to increase verbosity"},
102
103         {SET_FORGROUND,     0, "foreground",  "Get all in foreground mode"},
104         {SET_BACKGROUND,    0, "daemon",      "Get all in background mode"},
105
106         {SET_TCP_PORT,      1, "port",        "HTTP listening TCP port  [default 1234]"},
107         {SET_ROOT_DIR,      1, "rootdir",     "Root Directory [default $HOME/.AFB]"},
108         {SET_ROOT_HTTP,     1, "roothttp",    "HTTP Root Directory [default rootdir]"},
109         {SET_ROOT_BASE,     1, "rootbase",    "Angular Base Root URL [default /opa]"},
110         {SET_ROOT_API,      1, "rootapi",     "HTML Root API URL [default /api]"},
111         {SET_ALIAS,         1, "alias",       "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
112
113         {SET_APITIMEOUT,    1, "apitimeout",  "Binding API timeout in seconds [default 10]"},
114         {SET_CNTXTIMEOUT,   1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
115         {SET_CACHE_TIMEOUT, 1, "cache-eol",   "Client cache end of live [default 3600]"},
116
117         {SET_SESSION_DIR,   1, "sessiondir",  "Sessions file path [default rootdir/sessions]"},
118
119         {SET_LDPATH,        1, "ldpaths",     "Load bindingss from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
120         {SET_AUTH_TOKEN,    1, "token",       "Initial Secret [default=no-session, --token= for session without authentication]"},
121
122         {DISPLAY_VERSION,   0, "version",     "Display version and copyright"},
123         {DISPLAY_HELP,      0, "help",        "Display this help"},
124
125         {SET_MODE,          1, "mode",        "Set the mode: either local, remote or global"},
126         {SET_READYFD,       1, "readyfd",     "Set the #fd to signal when ready"},
127
128         {DBUS_CLIENT,       1, "dbus-client", "Bind to an afb service through dbus"},
129         {DBUS_SERVICE,      1, "dbus-server", "Provides an afb service through dbus"},
130         {WS_CLIENT,         1, "ws-client",   "Bind to an afb service through websocket"},
131         {WS_SERVICE,        1, "ws-server",   "Provides an afb service through websockets"},
132         {SO_BINDING,        1, "binding",     "Load the binding of path"},
133
134         {SET_SESSIONMAX,    1, "session-max", "Max count of session simultaneously [default 10]"},
135
136         {SET_TRACEREQ,      1, "tracereq",    "Log the requests: no, common, extra, all"},
137
138         {0, 0, NULL, NULL}
139 /* *INDENT-ON* */
140 };
141
142
143 struct enumdesc
144 {
145         const char *name;
146         int value;
147 };
148
149 static struct enumdesc tracereq_desc[] = {
150         { "no",     0 },
151         { "common", afb_hook_flags_req_common },
152         { "extra",  afb_hook_flags_req_extra },
153         { "all",    afb_hook_flags_req_all },
154         { NULL, 0 }
155 };
156
157 static struct enumdesc mode_desc[] = {
158         { "local",  AFB_MODE_LOCAL },
159         { "remote", AFB_MODE_REMOTE },
160         { "global", AFB_MODE_GLOBAL },
161         { NULL, 0 }
162 };
163
164 /*----------------------------------------------------------
165  | printversion
166  |   print version and copyright
167  +--------------------------------------------------------- */
168 static void printVersion(FILE * file)
169 {
170         fprintf(file, "\n----------------------------------------- \n");
171         fprintf(file, "  AFB [Application Framework Binder] version=%s |\n",
172                 AFB_VERSION);
173         fprintf(file, " \n");
174         fprintf(file,
175                 "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
176         fprintf(file, "  AFB comes with ABSOLUTELY NO WARRANTY.\n");
177         fprintf(file, "  Licence Apache 2\n\n");
178         exit(0);
179 }
180
181 /*----------------------------------------------------------
182  | printHelp
183  |   print information from long option array
184  +--------------------------------------------------------- */
185
186 static void printHelp(FILE * file, const char *name)
187 {
188         int ind;
189         char command[50];
190
191         fprintf(file, "%s:\nallowed options\n", name);
192         for (ind = 0; cliOptions[ind].name != NULL; ind++) {
193                 strcpy(command, cliOptions[ind].name);
194                 if (cliOptions[ind].has_arg)
195                         strcat(command, "=xxxx");
196                 fprintf(file, "  --%-15s %s\n", command, cliOptions[ind].help);
197         }
198         fprintf(file,
199                 "Example:\n  %s  --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
200                 name);
201 }
202
203 /*----------------------------------------------------------
204  |   adds a string to the list
205  +--------------------------------------------------------- */
206 static void list_add(struct afb_config_list **head, char *value)
207 {
208         struct afb_config_list *item;
209
210         /*
211          * search tail 
212          */
213         item = *head;
214         while (item != NULL) {
215                 head = &item->next;
216                 item = item->next;
217         }
218
219         /*
220          * alloc the item 
221          */
222         item = malloc(sizeof *item);
223         if (item == NULL) {
224                 ERROR("out of memory");
225                 exit(1);
226         }
227
228         /*
229          * init the item 
230          */
231         *head = item;
232         item->value = value;
233         item->next = NULL;
234 }
235
236 /*---------------------------------------------------------
237  |   helpers for argument scanning
238  +--------------------------------------------------------- */
239
240 static char *argvalstr(int index)
241 {
242         if (optarg == 0) {
243                 ERROR("option [--%s] needs a value i.e. --%s=xxx",
244                       cliOptions[index].name, cliOptions[index].name);
245                 exit(1);
246         }
247         return optarg;
248 }
249
250 static int argvalenum(int index, struct enumdesc *desc)
251 {
252         int i;
253         size_t len;
254         char *list, *name = argvalstr(index);
255
256         i = 0;
257         while(desc[i].name && strcmp(desc[i].name, name))
258                 i++;
259         if (!desc[i].name) {
260                 len = 0;
261                 i = 0;
262                 while(desc[i].name)
263                         len += strlen(desc[i++].name);
264                 list = malloc(len + i + i);
265                 if (!i || !list)
266                         ERROR("option [--%s] bad value (found %s)",
267                                 cliOptions[index].name, name);
268                 else {
269                         i = 0;
270                         strcpy(list, desc[i].name ? : "");
271                         while(desc[++i].name)
272                                 strcat(strcat(list, ", "), desc[i].name);
273                         ERROR("option [--%s] bad value, only accepts values %s (found %s)",
274                                 cliOptions[index].name, list, name);
275                 }
276                 free(list);
277                 exit(1);
278         }
279         return desc[i].value;
280 }
281
282 static int argvalint(int index, int mini, int maxi, int base)
283 {
284         char *beg, *end;
285         long int val;
286         beg = argvalstr(index);
287         val = strtol(beg, &end, base);
288         if (*end || end == beg) {
289                 ERROR("option [--%s] requires a valid integer (found %s)",
290                         cliOptions[index].name, beg);
291                 exit(1);
292         }
293         if (val < (long int)mini || val > (long int)maxi) {
294                 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
295                         cliOptions[index].name, mini, val, maxi);
296                 exit(1);
297         }
298         return (int)val;
299 }
300
301 static int argvalintdec(int index, int mini, int maxi)
302 {
303         return argvalint(index, mini, maxi, 10);
304 }
305
306 static void noarg(int index)
307 {
308         if (optarg != 0) {
309                 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
310                 exit(1);
311         }
312 }
313
314 /*---------------------------------------------------------
315  |   Parse option and launch action
316  +--------------------------------------------------------- */
317
318 static void parse_arguments(int argc, char **argv, struct afb_config *config)
319 {
320         char *programName = argv[0];
321         int optionIndex = 0;
322         int optc, ind;
323         int nbcmd;
324         struct option *gnuOptions;
325
326         // ------------------ Process Command Line -----------------------
327
328         // if no argument print help and return
329         if (argc < 2) {
330                 printHelp(stderr, programName);
331                 exit(1);
332         }
333         // build GNU getopt info from cliOptions
334         nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
335         gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
336         for (ind = 0; ind < nbcmd; ind++) {
337                 gnuOptions[ind].name = cliOptions[ind].name;
338                 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
339                 gnuOptions[ind].flag = 0;
340                 gnuOptions[ind].val = cliOptions[ind].val;
341         }
342
343         // get all options from command line
344         while ((optc = getopt_long(argc, argv, "", gnuOptions, &optionIndex)) != EOF) {
345                 switch (optc) {
346                 case SET_VERBOSE:
347                         verbosity++;
348                         break;
349
350                 case SET_TCP_PORT:
351                         config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
352                         break;
353
354                 case SET_APITIMEOUT:
355                         config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
356                         break;
357
358                 case SET_CNTXTIMEOUT:
359                         config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
360                         break;
361
362                 case SET_ROOT_DIR:
363                         config->rootdir = argvalstr(optionIndex);
364                         INFO("Forcing Rootdir=%s", config->rootdir);
365                         break;
366
367                 case SET_ROOT_HTTP:
368                         config->roothttp = argvalstr(optionIndex);
369                         INFO("Forcing Root HTTP=%s", config->roothttp);
370                         break;
371
372                 case SET_ROOT_BASE:
373                         config->rootbase = argvalstr(optionIndex);
374                         INFO("Forcing Rootbase=%s", config->rootbase);
375                         break;
376
377                 case SET_ROOT_API:
378                         config->rootapi = argvalstr(optionIndex);
379                         INFO("Forcing Rootapi=%s", config->rootapi);
380                         break;
381
382                 case SET_ALIAS:
383                         list_add(&config->aliases, argvalstr(optionIndex));
384                         break;
385
386                 case SET_AUTH_TOKEN:
387                         config->token = argvalstr(optionIndex);
388                         break;
389
390                 case SET_LDPATH:
391                         list_add(&config->ldpaths, argvalstr(optionIndex));
392                         break;
393
394                 case SET_SESSION_DIR:
395                         config->sessiondir = argvalstr(optionIndex);
396                         break;
397
398                 case SET_CACHE_TIMEOUT:
399                         config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
400                         break;
401
402                 case SET_SESSIONMAX:
403                         config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
404                         break;
405
406                 case SET_FORGROUND:
407                         noarg(optionIndex);
408                         config->background = 0;
409                         break;
410
411                 case SET_BACKGROUND:
412                         noarg(optionIndex);
413                         config->background = 1;
414                         break;
415
416                 case SET_MODE:
417                         config->mode = argvalenum(optionIndex, mode_desc);
418                         break;
419
420                 case SET_READYFD:
421                         config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
422                         break;
423
424                 case DBUS_CLIENT:
425                         list_add(&config->dbus_clients, argvalstr(optionIndex));
426                         break;
427
428                 case DBUS_SERVICE:
429                         list_add(&config->dbus_servers, argvalstr(optionIndex));
430                         break;
431
432                 case WS_CLIENT:
433                         list_add(&config->ws_clients, argvalstr(optionIndex));
434                         break;
435
436                 case WS_SERVICE:
437                         list_add(&config->ws_servers, argvalstr(optionIndex));
438                         break;
439
440                 case SO_BINDING:
441                         list_add(&config->so_bindings, argvalstr(optionIndex));
442                         break;
443
444                 case SET_TRACEREQ:
445                         config->tracereq = argvalenum(optionIndex, tracereq_desc);
446                         break;
447
448                 case DISPLAY_VERSION:
449                         noarg(optionIndex);
450                         printVersion(stdout);
451                         break;
452
453                 case DISPLAY_HELP:
454                         printHelp(stdout, programName);
455                         exit(0);
456
457                 default:
458                         exit(1);
459                 }
460         }
461         free(gnuOptions);
462 }
463
464 // load config from disk and merge with CLI option
465 static void config_set_default(struct afb_config *config)
466 {
467         // default HTTP port
468         if (config->httpdPort == 0)
469                 config->httpdPort = 1234;
470
471         // default binding API timeout
472         if (config->apiTimeout == 0)
473                 config->apiTimeout = DEFLT_API_TIMEOUT;
474
475         // default AUTH_TOKEN
476         if (config->token == NULL)
477                 config->token = DEFLT_AUTH_TOKEN;
478
479         // cache timeout default one hour
480         if (config->cacheTimeout == 0)
481                 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
482
483         // cache timeout default one hour
484         if (config->cntxTimeout == 0)
485                 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
486
487         // max count of sessions
488         if (config->nbSessionMax == 0)
489                 config->nbSessionMax = CTX_NBCLIENTS;
490
491         if (config->rootdir == NULL) {
492                 config->rootdir = getenv("AFBDIR");
493                 if (config->rootdir == NULL) {
494                         config->rootdir = malloc(512);
495                         strncpy(config->rootdir, getenv("HOME"), 512);
496                         strncat(config->rootdir, "/.AFB", 512);
497                 }
498         }
499         // if no Angular/HTML5 rootbase let's try '/' as default
500         if (config->rootbase == NULL)
501                 config->rootbase = "/opa";
502
503         if (config->rootapi == NULL)
504                 config->rootapi = "/api";
505
506         if (config->ldpaths == NULL)
507                 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
508
509         // if no session dir create a default path from rootdir
510         if (config->sessiondir == NULL) {
511                 config->sessiondir = malloc(512);
512                 strncpy(config->sessiondir, config->rootdir, 512);
513                 strncat(config->sessiondir, "/sessions", 512);
514         }
515         // if no config dir create a default path from sessiondir
516         if (config->console == NULL) {
517                 config->console = malloc(512);
518                 strncpy(config->console, config->sessiondir, 512);
519                 strncat(config->console, "/AFB-console.out", 512);
520         }
521 }
522
523 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
524 {
525         struct afb_config *result;
526
527         result = calloc(1, sizeof *result);
528
529         parse_arguments(argc, argv, result);
530         config_set_default(result);
531         return result;
532 }