use $(LIBENSCVECTOR) instead of libensc_vector.a
[util-vserver.git] / util-vserver / src / vserver-info.c
index a5ac32e..3c73d93 100644 (file)
 #include <errno.h>
 #include <sys/utsname.h>
 #include <dirent.h>
+#include <strings.h>
 
 #define ENSC_WRAPPERS_FCNTL    1
 #define ENSC_WRAPPERS_IO       1
 #define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_VSERVER  1
 #include <wrappers.h>
 
-typedef enum { tgNONE,tgCONTEXT, tgRUNNING,
+#undef _POSIX_SOURCE
+#include "capability-compat.h"
+
+typedef enum { tgNONE,tgCONTEXT, tgID, tgRUNNING,
               tgVDIR, tgNAME, tgCFGDIR, tgAPPDIR,
-              tgAPIVER,
+              tgAPIVER, tgPXID,
               tgINITPID, tgINITPID_PID,
               tgXID, tgUTS, tgSYSINFO,
-              tgFEATURE,
+              tgFEATURE, tgCANONIFY,
+              tgVERIFYCAP, tgXIDTYPE,
 }      VserverTag;
 
 static struct {
@@ -56,6 +62,7 @@ static struct {
 }  const TAGS[] = {
   { "CONTEXT", tgCONTEXT, ("the current and/or assigned context; when an optinal argument "
                           "evaluates to false,only the current context will be printed") },
+  { "ID",      tgID,      "gives out the vserver-id for the context-xid" },
   { "RUNNING", tgRUNNING, "gives out '1' when vserver is running; else, it fails without output" },
   { "VDIR",    tgVDIR,    "gives out the root-directory of the vserver" },
   { "NAME",    tgNAME,    "gives out the name of the vserver" },
@@ -70,6 +77,10 @@ static struct {
                                   "machine and domainname") },
   { "SYSINFO",     tgSYSINFO,     "gives out information about the systen" },
   { "FEATURE",     tgFEATURE,     "returns 0 iff the queried feature is supported" },
+  { "PXID",        tgPXID,        "returns the xid of the parent context" },
+  { "CANONIFY",    tgCANONIFY,    "canonifies the vserver-name and removes dangerous characters" },
+  { "VERIFYCAP",   tgVERIFYCAP,   "test if the kernel supports linux capabilities" },
+  { "XIDTYPE",     tgXIDTYPE,     "returns the type of the given XID" },
 };
 
 int wrapper_exit_code = 1;
@@ -147,6 +158,41 @@ utsText2Tag(char const *str)
   }
 }
 
+static bool
+verifyCap()
+{
+  struct __user_cap_header_struct header;
+  struct __user_cap_data_struct user;
+  header.version = _LINUX_CAPABILITY_VERSION;
+  header.pid     = 0;
+
+  if (getuid()!=0) {
+    WRITE_MSG(2, "'VERIFYCAP' can be executed as root only\n");
+    return false;
+  }
+
+//  if( prctl( PR_SET_KEEPCAPS, 1,0,0,0 ) < 0 ) {
+//    perror( "prctl:" );
+//    return false;
+//  }
+  
+  if (capget(&header, &user)==-1) {
+    perror("capget()");
+    return false;
+  }
+
+  user.effective   = 0;
+  user.permitted   = 0;
+  user.inheritable = 0;
+
+  if (capset(&header, &user)==-1) {
+    perror("capset()");
+    return false;
+  }
+
+  return chroot("/")==-1;
+}
+
 static char *
 getAPIVer(char *buf)
 {
@@ -164,9 +210,9 @@ getAPIVer(char *buf)
 }
 
 static char *
-getXid(char *buf, char const *vserver)
+getXid(char *buf, char const *pid_str)
 {
-  pid_t                pid = atoi(vserver);
+  pid_t                pid = atoi(pid_str);
   xid_t                xid = vc_get_task_xid(pid);
 
   if (xid==VC_NOCTX) perror("vc_get_task_xid()");
@@ -179,6 +225,13 @@ getXid(char *buf, char const *vserver)
 }
 
 static char *
+getPXid(char UNUSED *buf, char const UNUSED *vserver)
+{
+  // TODO: implement me when available
+  return 0;
+}
+
+static char *
 getInitPid_native(char *buf, xid_t xid)
 {
   struct vc_vx_info            info;
@@ -218,7 +271,7 @@ getInitPid_internal(pid_t pid, xid_t xid, pid_t *res)
     pos          = strstr(buf, "\ninitpid: ");
     
     if (pos!=0) {
-      pos         += sizeof("\ninitpid: ")-1;
+      pos       += sizeof("\ninitpid: ")-1;
       if (strncmp(pos, "none", 4)==0) *res = -1;
       else                            *res = atoi(pos);
     }
@@ -232,8 +285,8 @@ getInitPid_emulated(char *buf, xid_t xid)
 {
   struct dirent **namelist;
   int          n;
-  
-  vc_new_s_context(1,0,0);     // ignore errors silently...
+
+  switchToWatchXid(0); // ignore errors silently...
   n = scandir("/proc", &namelist, selectPid, alphasort);
   if (n<0) perror("scandir()");
   else while (n--) {
@@ -360,6 +413,31 @@ getContext(char *buf, char const *vserver, bool allow_only_static)
   return buf;
 }
 
+static char const *
+getXIDType(xid_t xid, int argc, char *argv[])
+{
+  char const *         tp;
+  
+  switch (vc_getXIDType(xid)) {
+    case vcTYPE_INVALID                :  tp = "invalid"; break;
+    case vcTYPE_MAIN           :  tp = "main";    break;
+    case vcTYPE_WATCH          :  tp = "watch";   break;
+    case vcTYPE_STATIC         :  tp = "static";  break;
+    case vcTYPE_DYNAMIC                :  tp = "dynamic"; break;
+    default                    :  tp = 0;         break;
+  }
+
+  if (argc==0 || tp==0)
+    return tp;
+
+  while (argc>0) {
+    --argc;
+    if (strcasecmp(argv[argc], tp)==0) return tp;
+  }
+
+  return 0;
+}
+
 static int
 testFeature(int argc, char *argv[])
 {
@@ -369,15 +447,14 @@ testFeature(int argc, char *argv[])
 static bool
 str2bool(char const *str)
 {
-  return atoi(str)!=0 || strchr("yYtY", str[0])!=0;
+  return atoi(str)!=0 || strchr("yYtY", str[0])!=0 || strcasecmp("true", str)==0;
 }
 
 static int
 execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
 {
   char const *         res = 0;
-  char                         buf[sizeof(xid_t)*4 + 1024];
-  xid_t                        xid = *vserver!='\0' ? (xid_t)(atoi(vserver)) : VC_SAMECTX;
+  char                         buf[sizeof(xid_t)*4 + 1024 + strlen(vserver)];
 
   memset(buf, 0, sizeof buf);
   switch (tag) {
@@ -394,17 +471,34 @@ execQuery(char const *vserver, VserverTag tag, int argc, char *argv[])
       res = (vc_getVserverCtx(vserver, vcCFG_AUTO, false, 0)==VC_NOCTX) ? 0 : "1";
       break;
 
+    case tgCANONIFY    :
+      strcpy(buf, vserver);
+      if (canonifyVserverName(buf)>0) res = buf;
+      break;
+      
     case tgCONTEXT     :  res = getContext(buf, vserver,
                                            argc==0 || str2bool(argv[0])); break;
-    case tgXID         :  res = getXid(buf, vserver);         break;
-    case tgINITPID     :  res = getInitPid(buf, xid);         break;
     case tgINITPID_PID :  res = getInitPidPid(buf, vserver);  break;
     case tgAPIVER      :  res = getAPIVer(buf);               break;
-    case tgUTS         :  res = getUTS(buf, xid, argc, argv); break;
+    case tgXID         :  res = getXid(buf, vserver);         break;
+    case tgPXID                :  res = getPXid(buf, vserver);        break;
     case tgSYSINFO     :  return printSysInfo(buf);           break;
     case tgFEATURE     :  return testFeature(argc,argv);      break;
+    case tgVERIFYCAP   :  return verifyCap() ? 0 : 1;         break;
+
+
+    default            : {
+      xid_t            xid = *vserver!='\0' ? vc_xidopt2xid(vserver,true,0) : VC_SAMECTX;
+
+      switch (tag) {
+       case tgID       :  res = vc_getVserverByCtx(xid,0,0);  break;
+       case tgINITPID  :  res = getInitPid(buf, xid);         break;
+       case tgUTS      :  res = getUTS(buf, xid, argc, argv); break;
+       case tgXIDTYPE  :  res = getXIDType(xid, argc, argv);  break;
     
-    default            :  assert(false); abort();  // TODO
+       default         :  assert(false); abort();  // TODO
+      }
+    }
   }
 
   if (res==0) return EXIT_FAILURE;
@@ -438,8 +532,9 @@ int main(int argc, char *argv[])
   }
 
   if (optind+2>argc) {
-    WRITE_MSG(2, "No vserver or tag give; please try '--help' for more information.\n");
-    exit(1);
+    execQuery("-", tgSYSINFO, 0, 0);
+    WRITE_MSG(2, "\nAssumed 'SYSINFO' as no other option given; try '--help' for more information.\n");
+    exit(0);
   }
 
   vserver = argv[optind];