OSDN Git Service

modified comments
[opengatem/opengatem.git] / mdsrc / util.c
1 /**************************************************
2 OpengateM - a MAC address authentication system
3
4  module for misc routines
5
6 Copyright (C) 2011 Opengate Project Team
7 Written by Yoshiaki Watanabe
8
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.
13
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.
18
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.
22
23 Email: watanaby@is.saga-u.ac.jp
24 **************************************************/
25 #include "opengatemd.h"
26
27 /******************************/
28 /* lock functions using fcntl */
29 /******************************/
30 int lock(int fd){
31   struct flock lck;
32   
33   lck.l_type=F_WRLCK;
34   lck.l_whence=SEEK_SET;
35   lck.l_start=0;
36   lck.l_len=0;
37   return fcntl(fd, F_SETLKW, &lck);
38 }
39   
40 /********************************/
41 /* unlock functions using fcntl */
42 /********************************/
43 int unlock(int fd)
44 {
45   struct flock lck;
46
47   lck.l_type=F_UNLCK;
48   lck.l_whence=SEEK_SET;
49   lck.l_start=0;
50   lck.l_len=0;
51   return fcntl(fd, F_SETLK, &lck);
52 }
53
54 /*****************************************************************/
55 /* return true, if pStr is null-pointer or points to null-string */
56 /*****************************************************************/
57 int isNull(const char *pStr)
58 {
59   if(pStr==NULL) return TRUE;
60   if(*pStr=='\0') return TRUE;
61   return FALSE;
62 }
63
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, ...)
74 {
75   char  commandLine[BUFFMAXLN];
76   va_list     ap;
77   char *pStr;
78   FILE *file;
79
80   /* insert command path */
81   strlcpy(commandLine, path, BUFFMAXLN);
82
83   /* insert command arguments */
84   va_start(ap, path);
85   
86   while((pStr=va_arg(ap, char *))!=(char *)0){
87     strcat(commandLine, " ");
88     strlcat(commandLine, pStr, BUFFMAXLN);
89   }
90   free(pStr);
91   va_end(ap);
92
93   /* if desired, add root privilege */
94   if(rootPriv==1){
95     if(seteuid(0)!=0){
96       err_msg("ERR at %s#%d: cannot add root privilege ",
97               __FILE__,__LINE__);
98     } 
99   }
100
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);  
105
106   /* remove root privilege */
107   seteuid(getuid()); 
108
109   return file;
110 }
111
112
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, ...)
122 {
123   char  commandLine[BUFFMAXLN];
124   va_list     ap;
125   char *pStr;
126   int ret;
127
128   /* insert command path */
129   strlcpy(commandLine, path, BUFFMAXLN);
130
131   /* insert command arguments */
132   va_start(ap, path);
133   
134   while((pStr=va_arg(ap, char *))!=(char *)0){
135     strcat(commandLine, " ");
136     strlcat(commandLine, pStr, BUFFMAXLN);
137   }
138   free(pStr);
139   va_end(ap);
140
141   /* if desired, add root privilege */
142   if(rootPriv==1){
143     if(seteuid(0)!=0){
144       err_msg("ERR at %s#%d: cannot add root privilege ",
145               __FILE__,__LINE__);
146     } 
147   }
148
149   /* execute shell  */
150   if(debug>1) err_msg("DEBUG:=>system(%s)", commandLine);
151   ret=system(commandLine);
152   if(debug>1) err_msg("DEBUG:<=system()");
153
154   /* remove root privilege */
155   seteuid(getuid()); 
156   return ret;
157 }
158
159 /****************************************/
160 /****************************************/
161 int Pclose(FILE *stream)
162 {
163   int ret;
164
165   if(debug>1) err_msg("DEBUG:=>pclose( )");
166   ret = pclose(stream);
167   if(debug>1) err_msg("DEBUG:<=pclose( )");  
168
169   return ret;
170 }
171
172
173 int Lock(int fd)
174 {
175   int ret;
176
177   if(debug>1) err_msg("DEBUG:=>lock( )");
178   ret=lock(fd);
179   if(debug>1) err_msg("DEBUG:(%d)<=lock( )",ret);
180
181   return ret;
182 }
183
184
185 int Unlock(int fd)
186 {
187   int ret;
188
189   if(debug>1) err_msg("DEBUG:=>unlock( )");
190   ret=unlock(fd);
191   if(debug>1) err_msg("DEBUG:(%d)<=unlock( )",ret);
192
193   return ret;
194 }
195
196
197 int
198 Open(const char *pathname, int oflag, mode_t mode)
199 {
200         int             fd;
201
202         if ( (fd = open(pathname, oflag, mode)) == -1)
203                 err_msg("open error for %s", pathname);
204         return(fd);
205 }
206
207 int
208 Close(int fd)
209 {
210   int ret;
211
212   /*if( (ret=close(fd)) == -1)
213    *  err_msg("close error");
214    */
215
216   ret=close(fd);
217
218   return ret;
219 }
220
221 pid_t
222 Fork(void)
223 {
224         pid_t   pid;
225
226         if ( (pid = fork()) == -1)
227                 err_msg("fork error");
228         return(pid);
229 }
230
231 int
232 Pipe(int *fds)
233 {
234   int ret;
235         if ((ret=pipe(fds)) < 0)
236                 err_msg("pipe error");
237
238         return ret;
239 }
240
241 void *
242 Malloc(size_t size)
243 {
244         void    *ptr;
245
246         if ( (ptr = malloc(size)) == NULL)
247                 err_msg("malloc error");
248         return(ptr);
249 }
250