1 /**************************************************
5 Copyright (C) 1999 Opengate Project Team
6 Written by Yoshiaki Watanabe
7 Modified Katsuhiko Eguchi, 2005
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Email: watanaby@is.saga-u.ac.jp
24 **************************************************/
26 #include "opengatesrv.h"
28 /*************************************************/
30 /* fd : file descriptor */
31 /* fmt : format to write */
32 /* ... : terms to write */
33 /*************************************************/
34 void Writefmt(int fd, const char *fmt, ...)
41 vsnprintf(buff, BUFFMAXLN, fmt, ap);
45 nwrt=write(fd, buff, nchar);
51 void WritefmtSSL(SSL *fd, const char *fmt, ...)
58 vsnprintf(buff, BUFFMAXLN, fmt, ap);
62 nwrt=SSL_write(fd, buff, nchar);
68 /******************************************************/
70 /* fd: file descriptor */
71 /* vptr: input buffer pointer */
72 /* maxlen: buffer length */
74 /* the chars terminated with EOL or EOF is read in */
75 /* ## this function assumes two EOL chars [CR LF] */
76 /* CRLF is not read in and skipped */
77 /* [abcdCRLFefghCRLF] => read[abcd],left[efghCRLF] */
80 /* plus value means the count of chars to read */
81 /* value 0 means NULL line (no-chars & CRLF) */
82 /* value -1 means error (errno is set) */
83 /* value -2 means EOF (no-chars & EOF) */
84 /******************************************************/
86 readln(int fd, void *vptr, size_t maxlen)
95 if(rc==0) return(-2); /* EOF */
96 if(rc<0) return(-1); /* ERR */
100 while(n < maxlen-1) {
101 if ( rc == 1) { /* get some char */
102 if (iscntrl(c)){ /* get control char (means EOL) */
103 rc = read(fd, &c, 1); /* skip second EOL char */
108 }else if (rc == 0) { /* EOF (but some chars are read already) */
113 rc = read(fd, &c, 1);
115 /* null terminate string */
122 readlnSSL(SSL *fd, void *vptr, size_t maxlen)
130 rc = SSL_read(fd, &c, 1);
131 if(rc==0) return(-2); /* EOF */
132 if(rc<0) return(-1); /* ERR */
136 while(n < maxlen-1) {
137 if ( rc == 1) { /* get some char */
138 if (iscntrl(c)){ /* get control char (means EOL) */
139 rc = SSL_read(fd, &c, 1); /* skip second EOL char */
144 }else if (rc == 0) { /* EOF (but some char are read already */
149 rc = SSL_read(fd, &c, 1);
151 /* null terminate string */
157 /******************************/
158 /* lock functions using fcntl */
159 /******************************/
165 lck.l_whence=SEEK_SET;
168 return fcntl(fd, F_SETLKW, &lck);
171 /********************************/
172 /* unlock functions using fcntl */
173 /********************************/
179 lck.l_whence=SEEK_SET;
182 return fcntl(fd, F_SETLK, &lck);
185 /**************************************/
186 /* check NULL or point to null string */
187 /**************************************/
188 int isNull(const char *pStr)
190 if(pStr==NULL) return TRUE;
191 if(*pStr=='\0') return TRUE;
197 /**************************************************/
198 /* popen with argument list */
199 /* rootPriv: if 1, run command as root user */
200 /* type : open type "r" or "w" */
201 /* path : command path to fork/exec */
202 /* ... : command arguments. last must be (char*)0 */
203 /* DO NOT SET user entered string in args */
204 /**************************************************/
205 FILE *Popenl(int rootPriv, const char *type, const char *path, ...)
207 char commandLine[BUFFMAXLN];
212 /* insert command path */
213 strncpy(commandLine, path, BUFFMAXLN);
215 /* insert command arguments */
218 while((pStr=va_arg(ap, char *))!=(char *)0){
219 strcat(commandLine, " ");
220 strncat(commandLine, pStr, BUFFMAXLN);
225 /* if desired, add root privilege */
228 err_msg("ERR at %s#%d: cannot add root privilege ",
233 /* open the pipe to the program */
234 if(debug>1) err_msg("DEBUG:=>popen(%s, %s)", commandLine, type);
235 file=popen(commandLine, type);
236 if(debug>1) err_msg("DEBUG:(%x)<=popen( )",file);
238 /* remove root privilege */
245 /**************************************************/
246 /* system with argument list */
247 /* rootPriv: if 1, run command as root user */
248 /* path : command path to fork/exec */
249 /* ... : command arguments. last must be (char*)0 */
250 /* DO NOT SET user entered string in args */
251 /**************************************************/
252 int Systeml(int rootPriv, const char *path, ...)
254 char commandLine[BUFFMAXLN];
259 /* insert command path */
260 strncpy(commandLine, path, BUFFMAXLN);
262 /* insert command arguments */
265 while((pStr=va_arg(ap, char *))!=(char *)0){
266 strcat(commandLine, " ");
267 strncat(commandLine, pStr, BUFFMAXLN);
272 /* if desired, add root privilege */
275 err_msg("ERR at %s#%d: cannot add root privilege ",
281 if(debug>1) err_msg("DEBUG:=>system(%s)", commandLine);
282 ret=system(commandLine);
283 if(debug>1) err_msg("DEBUG:<=system()");
285 /* remove root privilege */
291 /*******************************************/
292 /* get port number string in /etc/services */
293 /*******************************************/
294 char *getServicePortStr(char *servName)
296 struct servent *pServEnt;
297 static char portStr[WORDMAXLN];
299 /* get service info from service name */
300 pServEnt = getservbyname(servName, NULL);
303 err_msg("ERR at %s#%d: cannot find /etc/services entry for %s",
304 __FILE__,__LINE__,servName);
308 /* convert service port number to string form */
309 snprintf(portStr, sizeof(portStr),"%d",ntohs(pServEnt->s_port));
314 /*******************************************/
315 /* create random session id */
316 /* simple but might be overlapped */
317 /* change logic, if you need identical id */
318 /*******************************************/
319 void createSessionId(char *sessionId)
321 srandom(getpid()+time(NULL));
322 snprintf(sessionId, SIDMAXLN, "%ld", random() );
325 /*************************************************/
326 /* calc MD5 in hex form */
327 /* str: plain text to convert */
328 /* hexdigest: converted hex string */
329 /* prepare buff more or equal to 33chars */
330 /* len: length of hexdigest buffer */
331 /*************************************************/
332 char *md5hex(char *hexdigest, int len, char *str)
334 char unsigned digest[16];
335 char hexcode[16]="0123456789abcdef";
338 /* if not enough buffer, exit */
344 /* calc MD5 digest */
345 MD5((unsigned char*)str, strlen(str), digest);
347 /* convert to HEX string */
349 hexdigest[2*i]=hexcode[digest[i]/16];
350 hexdigest[2*i+1]=hexcode[digest[i]%16];
352 hexdigest[2*16]='\0';
357 /*******************************************/
358 /* create random session cookie */
359 /*******************************************/
360 void createCookie(char *cookie)
364 /* make Http-cookie from pid&time */
365 snprintf(str, BUFFMAXLN, "%d%d", getpid(),time(NULL));
366 md5hex(cookie, SIDMAXLN, str);
369 /****************************************************/
370 /* getenv for multiple variables */
371 /* after replacing '-' with '_' */
373 /* if env = "ab cd-ef-gh ijk" */
374 /* repeat getenv until getting non-null value */
375 /* getnev("ab"),getenv("cd_ef_gh"),getenv("ijk") */
376 /* if pre=TRUE convert '-' to '_' in env-var name */
377 /* if post=TRUE convert ' '|'@' to '_' in get-str */
378 /****************************************************/
379 char* getenvEx(char* env, int pre, int post){
380 char work[BUFFMAXLN];
387 /* copy string not to destroy it */
388 strncpy(work, env, BUFFMAXLN);
390 /* repeat for variables */
391 thisVar=nextVar=work;
393 while(!isNull(thisVar)){
395 /* skip preceeding space in string */
396 for(p=thisVar; *p==' '; p++);
399 /* search space (end of this variable) */
400 for(p=thisVar; (*p!=' ' && *p!='\0'); p++);
402 /* prepare next variable */
403 if(*p=='\0') nextVar=p; /* end of env string */
404 else{ /* some variales follows */
405 *p='\0'; /* set end of this variable */
406 nextVar=p+1; /* and start of next variable */
409 /* replace '-' in string with '_' */
411 for(p=thisVar; *p!='\0'; p++){
416 /* exeute getenv. if success, exit loop */
417 envValue = getenv(thisVar);
418 if(!isNull(envValue)){
423 /* try next variable */
427 /* getting no data */
428 if(!found) return NULL;
430 /* convert ' ' and '@' to '_' */
432 for(p=envValue; *p!='\0'; p++){
441 /****************************************/
442 /* routine for debug output */
443 /****************************************/
444 int Pclose(FILE *stream)
447 if(debug>1) err_msg("DEBUG:=>pclose( )");
448 ret = pclose(stream);
449 if(debug>1) err_msg("DEBUG:<=pclose( )");
453 char *GetServicePortStr(char *servName)
456 if(debug>1) err_msg("DEBUG:=>getServicePortStr(%s)", servName);
457 ret = getServicePortStr(servName);
458 if(debug>1) err_msg("DEBUG:(%s)<=getServicePortStr( )", ret);
462 ssize_t Readln(int fd, void *ptr, size_t maxlen)
465 if(debug>1) err_msg("DEBUG:=>readln( )");
466 if ( (n = readln(fd, ptr, maxlen)) < 0){
467 err_msg("ERR at %s#%d: readln error",__FILE__,__LINE__);
469 if(debug>1) err_msg("DEBUG:(%d)<=readln( )",n);
476 if(debug>1) err_msg("DEBUG:=>lock( )");
478 if(debug>1) err_msg("DEBUG:(%d)<=lock( )",ret);
485 if(debug>1) err_msg("DEBUG:=>unlock( )");
487 if(debug>1) err_msg("DEBUG:(%d)<=unlock( )",ret);
491 void CreateSessionId(char *sessionId){
492 if(debug>1) err_msg("DEBUG:=>createSessionId( )");
493 createSessionId(sessionId);
494 if(debug>1) err_msg("DEBUG:<=createSessionId(%s)",sessionId);
496 void CreateCookie(char *cookie){
497 if(debug>1) err_msg("DEBUG:=>createCookie( )");
498 createCookie(cookie);
499 if(debug>1) err_msg("DEBUG:<=createCookie(%s)",cookie);