1 /**************************************************
2 OpengateM - a MAC address authentication system
4 module for misc routines
6 Copyright (C) 2011 Opengate Project Team
7 Written by Yoshiaki Watanabe
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 **************************************************/
25 #include "opengatemd.h"
27 /******************************/
28 /* lock functions using fcntl */
29 /******************************/
34 lck.l_whence=SEEK_SET;
37 return fcntl(fd, F_SETLKW, &lck);
40 /********************************/
41 /* unlock functions using fcntl */
42 /********************************/
48 lck.l_whence=SEEK_SET;
51 return fcntl(fd, F_SETLK, &lck);
54 /*****************************************************************/
55 /* return true, if pStr is null-pointer or points to null-string */
56 /*****************************************************************/
57 int isNull(const char *pStr)
59 if(pStr==NULL) return TRUE;
60 if(*pStr=='\0') return TRUE;
64 /**************************************************/
65 /* popen with argument list */
66 /* rootPriv: if 1, run command as root user */
67 /* type : open type "r" or "w" */
68 /* path : command path to fork/exec */
69 /* ... : command arguments. last must be (char*)0 */
70 /* DO NOT SET [user entered string] in args */
71 /* to prevent hacking */
72 /**************************************************/
73 FILE *Popenl(int rootPriv, const char *type, const char *path, ...)
75 char commandLine[BUFFMAXLN];
80 /* insert command path */
81 strlcpy(commandLine, path, BUFFMAXLN);
83 /* insert command arguments */
86 while((pStr=va_arg(ap, char *))!=(char *)0){
87 strcat(commandLine, " ");
88 strlcat(commandLine, pStr, BUFFMAXLN);
93 /* if desired, add root privilege */
96 err_msg("ERR at %s#%d: cannot add root privilege ",
101 /* open the pipe to the program */
102 if(debug>1) err_msg("DEBUG:=>popen(%s, %s)", commandLine, type);
103 file=popen(commandLine, type);
104 if(debug>1) err_msg("DEBUG:(%x)<=popen( )",file);
106 /* remove root privilege */
113 /**************************************************/
114 /* system with argument list */
115 /* rootPriv: if 1, run command as root user */
116 /* path : command path to fork/exec */
117 /* ... : command arguments. last must be (char*)0 */
118 /* DO NOT SET [user entered string] in args */
119 /* to prevent hacking */
120 /**************************************************/
121 int Systeml(int rootPriv, const char *path, ...)
123 char commandLine[BUFFMAXLN];
128 /* insert command path */
129 strlcpy(commandLine, path, BUFFMAXLN);
131 /* insert command arguments */
134 while((pStr=va_arg(ap, char *))!=(char *)0){
135 strcat(commandLine, " ");
136 strlcat(commandLine, pStr, BUFFMAXLN);
141 /* if desired, add root privilege */
144 err_msg("ERR at %s#%d: cannot add root privilege ",
150 if(debug>1) err_msg("DEBUG:=>system(%s)", commandLine);
151 ret=system(commandLine);
152 if(debug>1) err_msg("DEBUG:<=system()");
154 /* remove root privilege */
159 /****************************************/
160 /****************************************/
161 int Pclose(FILE *stream)
165 if(debug>1) err_msg("DEBUG:=>pclose( )");
166 ret = pclose(stream);
167 if(debug>1) err_msg("DEBUG:<=pclose( )");
177 if(debug>1) err_msg("DEBUG:=>lock( )");
179 if(debug>1) err_msg("DEBUG:(%d)<=lock( )",ret);
189 if(debug>1) err_msg("DEBUG:=>unlock( )");
191 if(debug>1) err_msg("DEBUG:(%d)<=unlock( )",ret);
198 Open(const char *pathname, int oflag, mode_t mode)
202 if ( (fd = open(pathname, oflag, mode)) == -1)
203 err_msg("open error for %s", pathname);
212 /*if( (ret=close(fd)) == -1)
213 * err_msg("close error");
226 if ( (pid = fork()) == -1)
227 err_msg("fork error");
235 if ((ret=pipe(fds)) < 0)
236 err_msg("pipe error");
246 if ( (ptr = malloc(size)) == NULL)
247 err_msg("malloc error");