Refactor of file main.c
[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\\\n  --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
200                 name);
201 }
202
203 // load config from disk and merge with CLI option
204 static void config_set_default(struct afb_config *config)
205 {
206         // default HTTP port
207         if (config->httpdPort == 0)
208                 config->httpdPort = 1234;
209
210         // default binding API timeout
211         if (config->apiTimeout == 0)
212                 config->apiTimeout = DEFLT_API_TIMEOUT;
213
214         // default AUTH_TOKEN
215         if (config->token == NULL)
216                 config->token = DEFLT_AUTH_TOKEN;
217
218         // cache timeout default one hour
219         if (config->cacheTimeout == 0)
220                 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
221
222         // cache timeout default one hour
223         if (config->cntxTimeout == 0)
224                 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
225
226         // max count of sessions
227         if (config->nbSessionMax == 0)
228                 config->nbSessionMax = CTX_NBCLIENTS;
229
230         if (config->rootdir == NULL) {
231                 config->rootdir = getenv("AFBDIR");
232                 if (config->rootdir == NULL) {
233                         config->rootdir = malloc(512);
234                         strncpy(config->rootdir, getenv("HOME"), 512);
235                         strncat(config->rootdir, "/.AFB", 512);
236                 }
237         }
238         // if no Angular/HTML5 rootbase let's try '/' as default
239         if (config->rootbase == NULL)
240                 config->rootbase = "/opa";
241
242         if (config->rootapi == NULL)
243                 config->rootapi = "/api";
244
245         if (config->ldpaths == NULL)
246                 config->ldpaths = BINDING_INSTALL_DIR;
247
248         // if no session dir create a default path from rootdir
249         if (config->sessiondir == NULL) {
250                 config->sessiondir = malloc(512);
251                 strncpy(config->sessiondir, config->rootdir, 512);
252                 strncat(config->sessiondir, "/sessions", 512);
253         }
254         // if no config dir create a default path from sessiondir
255         if (config->console == NULL) {
256                 config->console = malloc(512);
257                 strncpy(config->console, config->sessiondir, 512);
258                 strncat(config->console, "/AFB-console.out", 512);
259         }
260 }
261
262 /*---------------------------------------------------------
263  | main
264  |   Parse option and launch action
265  +--------------------------------------------------------- */
266
267 static void list_add(struct afb_config_list **head, char *value)
268 {
269         struct afb_config_list *item;
270
271         /*
272          * search tail 
273          */
274         item = *head;
275         while (item != NULL) {
276                 head = &item->next;
277                 item = item->next;
278         }
279
280         /*
281          * alloc the item 
282          */
283         item = malloc(sizeof *item);
284         if (item == NULL) {
285                 ERROR("out of memory");
286                 exit(1);
287         }
288
289         /*
290          * init the item 
291          */
292         *head = item;
293         item->value = value;
294         item->next = NULL;
295 }
296
297 static char *argvalstr(int index)
298 {
299         if (optarg == 0) {
300                 ERROR("option [--%s] needs a value i.e. --%s=xxx",
301                       cliOptions[index].name, cliOptions[index].name);
302                 exit(1);
303         }
304         return optarg;
305 }
306
307 static int argvalenum(int index, struct enumdesc *desc)
308 {
309         int i;
310         size_t len;
311         char *list, *name = argvalstr(index);
312
313         i = 0;
314         while(desc[i].name && strcmp(desc[i].name, name))
315                 i++;
316         if (!desc[i].name) {
317                 len = 0;
318                 i = 0;
319                 while(desc[i].name)
320                         len += strlen(desc[i++].name);
321                 list = malloc(len + i + i);
322                 if (!i || !list)
323                         ERROR("option [--%s] bad value (found %s)",
324                                 cliOptions[index].name, name);
325                 else {
326                         i = 0;
327                         strcpy(list, desc[i].name ? : "");
328                         while(desc[++i].name)
329                                 strcat(strcat(list, ", "), desc[i].name);
330                         ERROR("option [--%s] bad value, only accepts values %s (found %s)",
331                                 cliOptions[index].name, list, name);
332                 }
333                 free(list);
334                 exit(1);
335         }
336         return desc[i].value;
337 }
338
339 static int argvalint(int index, int mini, int maxi, int base)
340 {
341         char *beg, *end;
342         long int val;
343         beg = argvalstr(index);
344         val = strtol(beg, &end, base);
345         if (*end || end == beg) {
346                 ERROR("option [--%s] requires a valid integer (found %s)",
347                         cliOptions[index].name, beg);
348                 exit(1);
349         }
350         if (val < (long int)mini || val > (long int)maxi) {
351                 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
352                         cliOptions[index].name, mini, val, maxi);
353                 exit(1);
354         }
355         return (int)val;
356 }
357
358 static int argvalintdec(int index, int mini, int maxi)
359 {
360         return argvalint(index, mini, maxi, 10);
361 }
362
363 static void noarg(int index)
364 {
365         if (optarg != 0) {
366                 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
367                 exit(1);
368         }
369 }
370
371 static void parse_arguments(int argc, char **argv, struct afb_config *config)
372 {
373         char *programName = argv[0];
374         int optionIndex = 0;
375         int optc, ind;
376         int nbcmd;
377         struct option *gnuOptions;
378
379         // ------------------ Process Command Line -----------------------
380
381         // if no argument print help and return
382         if (argc < 2) {
383                 printHelp(stderr, programName);
384                 exit(1);
385         }
386         // build GNU getopt info from cliOptions
387         nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
388         gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
389         for (ind = 0; ind < nbcmd; ind++) {
390                 gnuOptions[ind].name = cliOptions[ind].name;
391                 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
392                 gnuOptions[ind].flag = 0;
393                 gnuOptions[ind].val = cliOptions[ind].val;
394         }
395
396         // get all options from command line
397         while ((optc =
398                 getopt_long(argc, argv, "vsp?", gnuOptions, &optionIndex))
399                != EOF) {
400                 switch (optc) {
401                 case SET_VERBOSE:
402                         verbosity++;
403                         break;
404
405                 case SET_TCP_PORT:
406                         config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
407                         break;
408
409                 case SET_APITIMEOUT:
410                         config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
411                         break;
412
413                 case SET_CNTXTIMEOUT:
414                         config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
415                         break;
416
417                 case SET_ROOT_DIR:
418                         config->rootdir = argvalstr(optionIndex);
419                         INFO("Forcing Rootdir=%s", config->rootdir);
420                         break;
421
422                 case SET_ROOT_HTTP:
423                         config->roothttp = argvalstr(optionIndex);
424                         INFO("Forcing Root HTTP=%s", config->roothttp);
425                         break;
426
427                 case SET_ROOT_BASE:
428                         config->rootbase = argvalstr(optionIndex);
429                         INFO("Forcing Rootbase=%s", config->rootbase);
430                         break;
431
432                 case SET_ROOT_API:
433                         config->rootapi = argvalstr(optionIndex);
434                         INFO("Forcing Rootapi=%s", config->rootapi);
435                         break;
436
437                 case SET_ALIAS:
438                         list_add(&config->aliases, argvalstr(optionIndex));
439                         break;
440
441                 case SET_AUTH_TOKEN:
442                         config->token = argvalstr(optionIndex);
443                         break;
444
445                 case SET_LDPATH:
446                         config->ldpaths = argvalstr(optionIndex);
447                         break;
448
449                 case SET_SESSION_DIR:
450                         config->sessiondir = argvalstr(optionIndex);
451                         break;
452
453                 case SET_CACHE_TIMEOUT:
454                         config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
455                         break;
456
457                 case SET_SESSIONMAX:
458                         config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
459                         break;
460
461                 case SET_FORGROUND:
462                         noarg(optionIndex);
463                         config->background = 0;
464                         break;
465
466                 case SET_BACKGROUND:
467                         noarg(optionIndex);
468                         config->background = 1;
469                         break;
470
471                 case SET_MODE:
472                         config->mode = argvalenum(optionIndex, mode_desc);
473                         break;
474
475                 case SET_READYFD:
476                         config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
477                         break;
478
479                 case DBUS_CLIENT:
480                         list_add(&config->dbus_clients, argvalstr(optionIndex));
481                         break;
482
483                 case DBUS_SERVICE:
484                         list_add(&config->dbus_servers, argvalstr(optionIndex));
485                         break;
486
487                 case WS_CLIENT:
488                         list_add(&config->ws_clients, argvalstr(optionIndex));
489                         break;
490
491                 case WS_SERVICE:
492                         list_add(&config->ws_servers, argvalstr(optionIndex));
493                         break;
494
495                 case SO_BINDING:
496                         list_add(&config->so_bindings, argvalstr(optionIndex));
497                         break;
498
499                 case SET_TRACEREQ:
500                         config->tracereq = argvalenum(optionIndex, tracereq_desc);
501                         break;
502
503                 case DISPLAY_VERSION:
504                         noarg(optionIndex);
505                         printVersion(stdout);
506                         break;
507
508                 case DISPLAY_HELP:
509                 default:
510                         printHelp(stdout, programName);
511                         exit(0);
512                 }
513         }
514         free(gnuOptions);
515
516         config_set_default(config);
517 }
518
519 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
520 {
521         struct afb_config *result;
522
523         result = calloc(1, sizeof *result);
524
525         parse_arguments(argc, argv, result);
526         return result;
527 }