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 */
123 readlnSSL(SSL *fd, void *vptr, size_t maxlen)
131 rc = SSL_read(fd, &c, 1);
132 if(rc==0) return(-2); /* EOF */
133 if(rc<0) return(-1); /* ERR */
137 while(n < maxlen-1) {
138 if ( rc == 1) { /* get some char */
139 if (iscntrl(c)){ /* get control char (means EOL) */
140 rc = SSL_read(fd, &c, 1); /* skip second EOL char */
145 }else if (rc == 0) { /* EOF (but some char are read already */
150 rc = SSL_read(fd, &c, 1);
152 /* null terminate string */
158 /******************************/
159 /* lock functions using fcntl */
160 /******************************/
166 lck.l_whence=SEEK_SET;
169 return fcntl(fd, F_SETLKW, &lck);
172 /********************************/
173 /* unlock functions using fcntl */
174 /********************************/
180 lck.l_whence=SEEK_SET;
183 return fcntl(fd, F_SETLK, &lck);
186 /**************************************/
187 /* check NULL or point to null string */
188 /**************************************/
189 int isNull(const char *pStr)
191 if(pStr==NULL) return TRUE;
192 if(*pStr=='\0') return TRUE;
198 /**************************************************/
199 /* popen with argument list */
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(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 /* open the pipe to the program */
226 if(debug>1) err_msg("DEBUG:=>popen(%s, %s)", commandLine, type);
227 file=popen(commandLine, type);
228 if(debug>1) err_msg("DEBUG:(%x)<=popen( )",file);
234 /**************************************************/
235 /* system with argument list */
236 /* path : command path to fork/exec */
237 /* ... : command arguments. last must be (char*)0 */
238 /* DO NOT SET user entered string in args */
239 /**************************************************/
240 int Systeml(const char *path, ...)
242 char commandLine[BUFFMAXLN];
247 /* insert command path */
248 strncpy(commandLine, path, BUFFMAXLN);
250 /* insert command arguments */
253 while((pStr=va_arg(ap, char *))!=(char *)0){
254 strcat(commandLine, " ");
255 strncat(commandLine, pStr, BUFFMAXLN);
261 if(debug>1) err_msg("DEBUG:=>system(%s)", commandLine);
262 ret=system(commandLine);
263 if(debug>1) err_msg("DEBUG:<=system()");
268 /*******************************************/
269 /* get port number string in /etc/services */
270 /*******************************************/
271 char *getServicePortStr(char *servName)
273 struct servent *pServEnt;
274 static char portStr[WORDMAXLN];
276 /* get service info from service name */
277 pServEnt = getservbyname(servName, NULL);
280 err_msg("ERR at %s#%d: cannot find /etc/services entry for %s",
281 __FILE__,__LINE__,servName);
285 /* convert service port number to string form */
286 snprintf(portStr, sizeof(portStr),"%d",ntohs(pServEnt->s_port));
291 /*******************************************/
292 /* create random session id */
293 /* simple but might be overlapped */
294 /* change logic, if you need identical id */
295 /*******************************************/
296 void createSessionId(char *sessionId)
298 srandom(getpid()+time(NULL));
299 snprintf(sessionId, BUFFMAXLN, "%ld", random() );
301 /*************************************************/
302 /* calc MD5 in hex form */
303 /* str: plain text to convert */
304 /* hexdigest: converted hex string */
305 /* prepare buff more or equal to 33chars */
306 /* len: length of hexdigest buffer */
307 /*************************************************/
308 char *md5hex(char *hexdigest, int len, char *str)
310 char unsigned digest[16];
311 char hexcode[16]="0123456789abcdef";
314 /* if not enough buffer, exit */
320 /* calc MD5 digest */
321 MD5(str, strlen(str), digest);
323 /* convert to HEX string */
325 hexdigest[2*i]=hexcode[digest[i]/16];
326 hexdigest[2*i+1]=hexcode[digest[i]%16];
328 hexdigest[2*16]='\0';
333 /****************************************/
334 /****************************************/
335 int Pclose(FILE *stream)
339 if(debug>1) err_msg("DEBUG:=>pclose( )");
340 ret = pclose(stream);
341 if(debug>1) err_msg("DEBUG:<=pclose( )");
346 char *GetServicePortStr(char *servName)
350 if(debug>1) err_msg("DEBUG:=>getServicePortStr(%s)", servName);
351 ret = getServicePortStr(servName);
352 if(debug>1) err_msg("DEBUG:(%s)<=getServicePortStr( )", ret);
357 ssize_t Readln(int fd, void *ptr, size_t maxlen)
361 if(debug>1) err_msg("DEBUG:=>readln( )");
362 if ( (n = readln(fd, ptr, maxlen)) < 0){
363 err_msg("ERR at %s#%d: readln error",__FILE__,__LINE__);
365 if(debug>1) err_msg("DEBUG:(%d)<=readln( )",n);
374 if(debug>1) err_msg("DEBUG:=>lock( )");
376 if(debug>1) err_msg("DEBUG:(%d)<=lock( )",ret);
386 if(debug>1) err_msg("DEBUG:=>unlock( )");
388 if(debug>1) err_msg("DEBUG:(%d)<=unlock( )",ret);
394 void CreateSessionId(char *sessionId){
395 if(debug>1) err_msg("DEBUG:=>createSessionId( )");
396 createSessionId(sessionId);
397 if(debug>1) err_msg("DEBUG:<=createSessionId(%s)",sessionId);