Hack --plugins=path
[src/app-framework-binder.git] / src / main.c
index 040fc95..dddc3e1 100644 (file)
@@ -29,6 +29,7 @@
 #include <setjmp.h>
 #include <signal.h>
 #include <getopt.h>
+#include <pwd.h>
 
 static sigjmp_buf exitPoint; // context save for set/longjmp
 
@@ -63,7 +64,7 @@ static sigjmp_buf exitPoint; // context save for set/longjmp
  #define SET_ROOT_ALIAS     124
 
  #define SET_CACHE_TO       130
- #define SET_cardid         131
+ #define SET_USERID         131
  #define SET_PID_FILE       132
  #define SET_SESSION_DIR    133
  #define SET_CONFIG_FILE    134
@@ -71,8 +72,10 @@ static sigjmp_buf exitPoint; // context save for set/longjmp
  #define SET_CONFIG_EXIT    138
 
  #define SET_SMACK          140
- #define SET_PLUGINS        141
- #define SET_APITIMEOUT     142
+ #define SET_AUTH_TOKEN     141
+ #define SET_PLUGINS        142
+ #define SET_APITIMEOUT     143
+ #define SET_CNTXTIMEOUT    144
 
  #define DISPLAY_VERSION    150
  #define DISPLAY_HELP       151
@@ -88,22 +91,25 @@ static  AFB_options cliOptions [] = {
   {KILL_PREV_REST   ,0,"restart"         , "Kill active process if any and restart"},
 
   {SET_TCP_PORT     ,1,"port"            , "HTTP listening TCP port  [default 1234]"},
-  {SET_ROOT_DIR     ,1,"rootdir"         , "HTTP Root Directory [default $HOME/.AFB"},
-  {SET_ROOT_BASE    ,1,"rootbase"        , "Angular Base Root URL [default /opa"},
-  {SET_ROOT_API     ,1,"rootapi"         , "HTML Root API URL [default /api"},
+  {SET_ROOT_DIR     ,1,"rootdir"         , "HTTP Root Directory [default $HOME/.AFB]"},
+  {SET_ROOT_BASE    ,1,"rootbase"        , "Angular Base Root URL [default /opa]"},
+  {SET_ROOT_API     ,1,"rootapi"         , "HTML Root API URL [default /api]"},
   {SET_ROOT_ALIAS   ,1,"alias"           , "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
+  
   {SET_APITIMEOUT   ,1,"apitimeout"      , "Plugin API timeout in seconds [default 10]"},
-
+  {SET_CNTXTIMEOUT  ,1,"cntxtimeout"     , "Client Session Context Timeout [default 900]"},
   {SET_CACHE_TO     ,1,"cache-eol"       , "Client cache end of live [default 3600s]"},
-  {SET_cardid       ,1,"setuid"          , "Change user id [default don't change]"},
+  
+  {SET_USERID       ,1,"setuid"          , "Change user id [default don't change]"},
   {SET_PID_FILE     ,1,"pidfile"         , "PID file path [default none]"},
   {SET_SESSION_DIR  ,1,"sessiondir"      , "Sessions file path [default rootdir/sessions]"},
   {SET_CONFIG_FILE  ,1,"config"          , "Config Filename [default rootdir/sessions/configs/default.AFB]"},
   {SET_CONFIG_SAVE  ,0,"save"            , "Save config on disk [default no]"},
   {SET_CONFIG_EXIT  ,0,"saveonly"        , "Save config on disk and then exit"},
 
-  {SET_SMACK        ,1,"smack"           , "Set Smack Label [default=demo"},
-  {SET_PLUGINS      ,1,"mods"            , "Enable module [default=all"},
+  {SET_SMACK        ,1,"smack"           , "Set Smack Label [default demo]"},
+  {SET_PLUGINS      ,1,"plugins"         , "Load Plugins from dir [default = PLUGIN_INSTALL_DIR"},
+  {SET_AUTH_TOKEN   ,1,"token"           , "Initial Secret [default=no-session, --token="" for session without authentication]"},
   
   {DISPLAY_VERSION  ,0,"version"         , "Display version and copyright"},
   {DISPLAY_HELP     ,0,"help"            , "Display this help"},
@@ -153,7 +159,7 @@ void signalQuit (int signum) {
          fprintf (stderr,"  --%-15s %s\n", command, cliOptions[ind].help);
       }
     }
-    fprintf (stderr,"Example:\n  %s\\\n  --verbose --port=1234 --smack=xxxx --mods=alsa:dbus\n", name);
+    fprintf (stderr,"Example:\n  %s\\\n  --verbose --port=1234 --smack=xxxx --token='azerty' --plugins=build/plugins\n", name);
 } // end printHelp
 
 /*----------------------------------------------------------
@@ -240,8 +246,7 @@ static void listenLoop (AFB_session *session) {
         fprintf (stderr, "hoops returned from infinite loop [report bug]\n");
   }
 }
-
-
+  
 /*---------------------------------------------------------
  | main
  |   Parse option and launch action
@@ -303,6 +308,11 @@ int main(int argc, char *argv[])  {
        if (!sscanf (optarg, "%d", &cliconfig.apiTimeout)) goto notAnInteger;
        break;
 
+    case SET_CNTXTIMEOUT:
+       if (optarg == 0) goto needValueForOption;
+       if (!sscanf (optarg, "%d", &cliconfig.cntxTimeout)) goto notAnInteger;
+       break;
+
     case SET_ROOT_DIR:
        if (optarg == 0) goto needValueForOption;
        cliconfig.rootdir   = optarg;
@@ -340,9 +350,13 @@ int main(int argc, char *argv[])  {
        cliconfig.smack   = optarg;
        break;
 
+    case SET_AUTH_TOKEN:
+       if (optarg == 0) goto needValueForOption;
+       cliconfig.token   = optarg;
+       break;
+
     case SET_PLUGINS:
        if (optarg == 0) goto needValueForOption;
-       fprintf (stderr, "Not Implemented yet\n");
        cliconfig.plugins = optarg;
        break;
 
@@ -377,9 +391,9 @@ int main(int argc, char *argv[])  {
        session->configsave  = 1;
        break;
 
-    case SET_cardid:
+    case SET_USERID:
        if (optarg == 0) goto needValueForOption;
-       if (!sscanf (optarg, "%d", &cliconfig.setuid)) goto notAnInteger;
+       if (!sscanf (optarg, "%s", &cliconfig.setuid)) goto notAnInteger;
        break;
 
     case SET_FAKE_MOD:
@@ -419,11 +433,10 @@ int main(int argc, char *argv[])  {
 
   }
   }
-  // Create session config
-  configInit (/* session & config are initialized globally */);
-
   // if exist merge config file with CLI arguments
   configLoadFile  (session, &cliconfig);
+  initPlugins(session);
 
   // ------------------ sanity check ----------------------------------------
   if  ((session->background) && (session->foreground)) {
@@ -486,13 +499,17 @@ int main(int argc, char *argv[])  {
 
     if (session->config->setuid) {
         int err;
-
-        err = setuid(session->config->setuid);
-        if (err) fprintf (stderr, "Fail to change program cardid error=%s", strerror(err));
+        struct passwd *passwd;
+        passwd=getpwnam(session->config->setuid);
+        
+        if (passwd == NULL) goto errorSetuid;
+        
+        err = setuid(passwd->pw_uid);
+        if (err) goto errorSetuid;
     }
 
     // let's not take the risk to run as ROOT
-    if (getuid() == 0)  status=setuid(65534);  // run as nobody
+    //if (getuid() == 0)  goto errorNoRoot;
 
     // check session dir and create if it does not exist
     if (sessionCheckdir (session) != AFB_SUCCESS) goto errSessiondir;
@@ -575,44 +592,52 @@ normalExit:
   exit (0);
 
 // ------------- Fatal ERROR display error and quit  -------------
+errorSetuid:
+  fprintf (stderr,"\nERR:AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid);
+  exit (-1);
+  
+//errorNoRoot:
+//  fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n");
+//  exit (-1);
+
 errorPidFile:
-  fprintf (stderr,"\nERR:main Failled to write pid file [%s]\n\n", session->config->pidfile);
+  fprintf (stderr,"\nERR:AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile);
   exit (-1);
 
 errorFork:
-  fprintf (stderr,"\nERR:main Failled to fork son process\n\n");
+  fprintf (stderr,"\nERR:AFB-daemon Failed to fork son process\n\n");
   exit (-1);
 
 needValueForOption:
-  fprintf (stderr,"\nERR:main option [--%s] need a value i.e. --%s=xxx\n\n"
+  fprintf (stderr,"\nERR:AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n"
           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
   exit (-1);
 
 noValueForOption:
-  fprintf (stderr,"\nERR:main option [--%s] don't take value\n\n"
+  fprintf (stderr,"\nERR:AFB-daemon option [--%s] don't take value\n\n"
           ,gnuOptions[optionIndex].name);
   exit (-1);
 
 notAnInteger:
-  fprintf (stderr,"\nERR:main option [--%s] requirer an interger i.e. --%s=9\n\n"
+  fprintf (stderr,"\nERR:AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n"
           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
   exit (-1);
 
 exitOnSignal:
-  fprintf (stderr,"\n%s INF:main pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
+  fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
                  ,configTime(), getpid());
   exit (-1);
 
 errConsole:
-  fprintf (stderr,"\nERR:cannot open /dev/console (use --foreground)\n\n");
+  fprintf (stderr,"\nERR:AFB-daemon cannot open /dev/console (use --foreground)\n\n");
   exit (-1);
 
 errSessiondir:
-  fprintf (stderr,"\nERR:cannot read/write session dir\n\n");
+  fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n");
   exit (-1);
 
 errSoundCard:
-  fprintf (stderr,"\nERR:fail to probe sound cards\n\n");
+  fprintf (stderr,"\nERR:AFB-daemon fail to probe sound cards\n\n");
   exit (-1);
 
 exitInitLoop:
@@ -620,5 +645,5 @@ exitInitLoop:
   if (session->background && session->config->pidfile != NULL)  unlink (session->config->pidfile);
   exit (-1);
 
-}; /* END main() */
+}; /* END AFB-daemon() */