return;
}
-/****************************************************/
-/* Read one line */
-/* fd: file descriptor */
-/* vptr: input buffer pointer */
-/* maxlen: buffer length */
-/* return value is the count of readin chars */
-/* if error, return -1 */
-/* ## this function assumes two EOL chars [CR LF] */
-/****************************************************/
+
+/******************************************************/
+/* Read one line */
+/* fd: file descriptor */
+/* vptr: input buffer pointer */
+/* maxlen: buffer length */
+/* */
+/* the chars terminated with EOL or EOF is read in */
+/* ## this function assumes two EOL chars [CR LF] */
+/* CRLF is not read in and skipped */
+/* [abcdCRLFefghCRLF] => read[abcd],left[efghCRLF] */
+/* */
+/* return value */
+/* plus value means the count of chars to read */
+/* value 0 means NULL line (no-chars & CRLF) */
+/* value -1 means error (errno is set) */
+/* value -2 means EOF (no-chars & EOF) */
+/******************************************************/
ssize_t
readln(int fd, void *vptr, size_t maxlen)
{
/* pre read */
rc = read(fd, &c, 1);
- if(rc<=0) return(-1);
+ if(rc==0) return(-2); /* EOF */
+ if(rc<0) return(-1); /* ERR */
/* get char loop */
n=0;
}
*ptr++ = c;
n++;
- }else if (rc == 0) { /* EOF */
+ }else if (rc == 0) { /* EOF (but some chars are read already) */
break;
} else { /* ERR */
return(-1);
return(n);
}
+
ssize_t
readlnSSL(SSL *fd, void *vptr, size_t maxlen)
{
/* pre read */
rc = SSL_read(fd, &c, 1);
- if(rc<=0) return(-1);
+ if(rc==0) return(-2); /* EOF */
+ if(rc<0) return(-1); /* ERR */
/* get char loop */
n=0;
}
*ptr++ = c;
n++;
- }else if (rc == 0) { /* EOF */
+ }else if (rc == 0) { /* EOF (but some char are read already */
break;
} else { /* ERR */
return(-1);
return(n);
}
+
/******************************/
/* lock functions using fcntl */
/******************************/
/**************************************************/
/* 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;
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;
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;
}
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 */
}
/* 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++){
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);
+}