OSDN Git Service

Ver.1.5.34 Fixed CLang warning and others
[opengate/opengate.git] / opengate / opengatesrv / utilities.c
index 804c631..100a325 100644 (file)
@@ -114,7 +114,6 @@ readln(int fd, void *vptr, size_t maxlen)
   }
   /* null terminate string */  
   *ptr++ = 0;
-
   return(n);
 }
 
@@ -197,12 +196,13 @@ int isNull(const char *pStr)
 
 /**************************************************/
 /* popen with argument list                       */
+/* rootPriv: if 1, run command as root user       */
 /* type : open type "r" or "w"                    */
 /* path : command path to fork/exec               */
 /* ... : command arguments. last must be (char*)0 */
 /*  DO NOT SET user entered string in args        */
 /**************************************************/
-FILE *Popenl(const char *type, const char *path, ...)
+FILE *Popenl(int rootPriv, const char *type, const char *path, ...)
 {
   char commandLine[BUFFMAXLN];
   va_list     ap;
@@ -210,34 +210,46 @@ FILE *Popenl(const char *type, const char *path, ...)
   FILE *file;
 
   /* insert command path */
-  strncpy(commandLine, path, BUFFMAXLN);
+  strlcpy(commandLine, path, BUFFMAXLN);
 
   /* insert command arguments */
   va_start(ap, path);
   
   while((pStr=va_arg(ap, char *))!=(char *)0){
     strcat(commandLine, " ");
-    strncat(commandLine, pStr, BUFFMAXLN);
+    strlcat(commandLine, pStr, BUFFMAXLN);
   }
 
   va_end(ap);
 
+  /* if desired, add root privilege */
+  if(rootPriv==1){
+    if(seteuid(0)!=0){
+      err_msg("ERR at %s#%d: cannot add root privilege ",
+             __FILE__,__LINE__);
+    } 
+  }
+
   /* open the pipe to the program  */
   if(debug>1) err_msg("DEBUG:=>popen(%s, %s)", commandLine, type);
   file=popen(commandLine, type);
   if(debug>1) err_msg("DEBUG:(%x)<=popen( )",file);  
 
+  /* remove root privilege */
+  seteuid(getuid()); 
+
   return file;
 }
 
 
 /**************************************************/
 /* system with argument list                      */
+/* rootPriv: if 1, run command as root user       */
 /* path : command path to fork/exec               */
 /* ... : command arguments. last must be (char*)0 */
 /*  DO NOT SET user entered string in args        */
 /**************************************************/
-int Systeml(const char *path, ...)
+int Systeml(int rootPriv, const char *path, ...)
 {
   char commandLine[BUFFMAXLN];
   va_list     ap;
@@ -245,23 +257,34 @@ int Systeml(const char *path, ...)
   int ret;
 
   /* insert command path */
-  strncpy(commandLine, path, BUFFMAXLN);
+  strlcpy(commandLine, path, BUFFMAXLN);
 
   /* insert command arguments */
   va_start(ap, path);
   
   while((pStr=va_arg(ap, char *))!=(char *)0){
     strcat(commandLine, " ");
-    strncat(commandLine, pStr, BUFFMAXLN);
+    strlcat(commandLine, pStr, BUFFMAXLN);
   }
 
   va_end(ap);
 
+  /* if desired, add root privilege */
+  if(rootPriv==1){
+    if(seteuid(0)!=0){
+      err_msg("ERR at %s#%d: cannot add root privilege ",
+             __FILE__,__LINE__);
+    } 
+  }
+
   /* execute shell  */
   if(debug>1) err_msg("DEBUG:=>system(%s)", commandLine);
   ret=system(commandLine);
   if(debug>1) err_msg("DEBUG:<=system()");
 
+  /* remove root privilege */
+  seteuid(getuid()); 
+
   return ret;
 }
 
@@ -296,8 +319,9 @@ char *getServicePortStr(char *servName)
 void createSessionId(char *sessionId)
 {
   srandom(getpid()+time(NULL));
-  snprintf(sessionId, BUFFMAXLN, "%ld", random() );
+  snprintf(sessionId, SIDMAXLN, "%ld", random() );
 }
+
 /*************************************************/
 /* calc MD5 in hex form                          */
 /*  str: plain text to convert                   */
@@ -318,7 +342,7 @@ char *md5hex(char *hexdigest, int len, char *str)
   }
 
   /* calc MD5 digest */
-  MD5(str, strlen(str), digest);
+  MD5((unsigned char*)str, strlen(str), digest);
 
   /* convert to HEX string */
   for(i=0;i<16;i++){
@@ -330,69 +354,147 @@ char *md5hex(char *hexdigest, int len, char *str)
   return hexdigest;
 }
 
+/*******************************************/
+/* create random session cookie            */
+/*******************************************/
+void createCookie(char *cookie)
+{
+  char str[BUFFMAXLN];
+
+  /* make Http-cookie from pid&time */
+  snprintf(str, BUFFMAXLN, "%d%ld", getpid(),time(NULL));
+  md5hex(cookie, SIDMAXLN, str);
+}
+
+/****************************************************/
+/* getenv for multiple variables                    */
+/*             after replacing '-' with '_'         */
+/*                                                  */
+/* if env = "ab cd-ef-gh ijk"                       */
+/*  repeat getenv until getting non-null value      */
+/*  getnev("ab"),getenv("cd_ef_gh"),getenv("ijk")   */
+/* if pre=TRUE convert '-' to '_' in env-var name   */
+/* if post=TRUE convert ' '|'@' to '_' in get-str   */
+/****************************************************/
+char* getenvEx(char* env, int pre, int post){
+  char work[BUFFMAXLN];
+  char* envValue="";
+  char* p=NULL;
+  char* thisVar=NULL;
+  char* nextVar=NULL;
+  int found=FALSE;
+
+  /* copy string not to destroy it */
+  strlcpy(work, env, BUFFMAXLN);
+
+  /* repeat for variables */
+  thisVar=nextVar=work;
+
+  while(!isNull(thisVar)){
+
+    /* skip preceeding space in string */
+    for(p=thisVar; *p==' '; p++);
+    thisVar=p;
+
+    /* search space (end of this variable) */
+    for(p=thisVar; (*p!=' ' && *p!='\0'); p++);
+
+    /* prepare next variable */
+    if(*p=='\0') nextVar=p; /* end of env string */
+    else{                   /* some variales follows */
+      *p='\0';                /* set end of this variable */
+      nextVar=p+1;            /* and start of next variable */
+    }
+
+    /* replace '-' in string with '_' */
+    if(pre){
+      for(p=thisVar; *p!='\0'; p++){
+       if(*p=='-') *p='_';
+      }
+    }
+
+    /* exeute getenv. if success, exit loop */
+    envValue = getenv(thisVar);
+    if(!isNull(envValue)){
+      found=TRUE;
+      break;
+    }
+
+    /* try next variable */
+    thisVar=nextVar;
+  }
+
+  /* getting no data */
+  if(!found) return NULL;
+
+  /* convert ' ' and '@' to '_' */
+  if(post){
+    for(p=envValue; *p!='\0'; p++){
+      if(*p==' ') *p='_';
+      if(*p=='@') *p='_';
+    }
+  }
+
+  return envValue;
+}
+
 /****************************************/
+/* routine for debug output             */
 /****************************************/
 int Pclose(FILE *stream)
 {
   int ret;
-
   if(debug>1) err_msg("DEBUG:=>pclose( )");
   ret = pclose(stream);
   if(debug>1) err_msg("DEBUG:<=pclose( )");  
-
   return ret;
 }
 
 char *GetServicePortStr(char *servName)
 {
   char *ret;
-
   if(debug>1) err_msg("DEBUG:=>getServicePortStr(%s)", servName);
   ret = getServicePortStr(servName);
   if(debug>1) err_msg("DEBUG:(%s)<=getServicePortStr( )", ret);  
-
   return ret;
 }
 
 ssize_t Readln(int fd, void *ptr, size_t maxlen)
 {
   ssize_t              n;
-
   if(debug>1) err_msg("DEBUG:=>readln( )");
   if ( (n = readln(fd, ptr, maxlen)) < 0){
     err_msg("ERR at %s#%d: readln error",__FILE__,__LINE__);
   }
   if(debug>1) err_msg("DEBUG:(%d)<=readln( )",n);
-
   return(n);
 }
 
 int Lock(int fd)
 {
   int ret;
-
   if(debug>1) err_msg("DEBUG:=>lock( )");
   ret=lock(fd);
   if(debug>1) err_msg("DEBUG:(%d)<=lock( )",ret);
-
   return ret;
 }
 
-
 int Unlock(int fd)
 {
   int ret;
-
   if(debug>1) err_msg("DEBUG:=>unlock( )");
   ret=unlock(fd);
   if(debug>1) err_msg("DEBUG:(%d)<=unlock( )",ret);
-
   return ret;
 }
 
-
 void CreateSessionId(char *sessionId){
   if(debug>1) err_msg("DEBUG:=>createSessionId( )");
   createSessionId(sessionId);
   if(debug>1) err_msg("DEBUG:<=createSessionId(%s)",sessionId);
 }
+void CreateCookie(char *cookie){
+  if(debug>1) err_msg("DEBUG:=>createCookie( )");
+  createCookie(cookie);
+  if(debug>1) err_msg("DEBUG:<=createCookie(%s)",cookie);
+}