OSDN Git Service

8d955f9508c48ac9ccc5e7ca70a52534139f30aa
[opengatem/opengatem.git] / mngsrc / proc.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for child process control 
4
5  this module opens the network for the candidate of the user device.
6  (this is used in opengatemchk).
7  a child process opens net, sleeps while, and closes net automatically.
8  independent process is employed to perform a series of processing surely.
9
10 Copyright (C) 2011 Opengate Project Team
11 Written by Yoshiaki Watanabe
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2
16 of the License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
27 Email: watanaby@is.saga-u.ac.jp
28 **************************************************/
29
30 #include        "opengatemmng.h"
31
32 int rule4=0;
33 int rule6=0;
34 char loadPath[BUFFMAXLN];
35
36 /**********************************************************/
37 /* start child process that open-net, sleep and close-net */
38 /**********************************************************/
39 int startChildProc(char* macAddress){
40
41   int dummyfd[2];
42   char ipv4[ADDRMAXLN];
43   char ipv6[ADDRMAXLN];
44   int pid;
45
46   /* fork child process */
47   if((pid=fork())==-1){
48     err_msg("ERR at %s#%d: fork error",__FILE__,__LINE__);
49     return -1;
50   }
51
52   /* parent process */
53   else if(pid!=0){
54     return pid;
55   }
56
57   /* child process */
58   else{
59
60     /* detach from server */
61     Close(0);Close(1);Close(2);
62     Pipe(dummyfd);      /* connect dummy pipe for stdin and out */
63
64     /* get ipv4/ipv6 corresponding to the mac from db */
65     GetIpFromMacCheckTable(macAddress, ipv4, ipv6);
66
67     /* open firewall */
68     if(!isNull(ipv4)) rule4=OpenClientGate(ipv4,FALSE,"temp","","");
69     if(!isNull(ipv6)) rule6=OpenClientGate(ipv6,FALSE,"temp","","");
70     
71     /* save pid/ruleNumber to db */
72     SavePidToMacCheckTable(macAddress, getpid(), rule4, rule6);
73
74     /* at TERM signal is received, call closeExit */
75     signal(SIGTERM, closeExit);
76
77     /* sleep a while */
78     sleep(atoi(GetConfValue("OpenTimeout")));
79
80     /* close firewall */
81     if(rule4!=0) CloseClientGate(rule4);
82     if(rule6!=0) CloseClientGate(rule6);    
83
84     return 0;
85   }
86 }
87
88 /***************************/
89 /* signal function         */
90 /***************************/
91 void closeExit(int signo){
92
93     /* close firewall */
94     if(rule4!=0) CloseClientGate(rule4);
95     if(rule6!=0) CloseClientGate(rule6);    
96     exit(0);
97 }
98
99 /*********************************/
100 /* stop child process            */
101 /*********************************/
102 int stopChildProc(char* macAddress){
103   
104   int pid;
105
106   /* get pid/ruleNumber from db */
107   GetPidFromMacCheckTable(macAddress, &pid, &rule4, &rule6);
108
109   /* close firewall */
110   if(rule4!=0) CloseClientGate(rule4);
111   if(rule6!=0) CloseClientGate(rule6);    
112
113   /* kill child process */
114   kill(pid, SIGKILL);
115
116   return TRUE;
117 }
118
119 /******************************/
120 /* save the program load path */
121 /******************************/
122 void saveLoadPath(char* argv0){
123   strlcpy(loadPath, argv0, BUFFMAXLN);
124 }
125
126 /***********************************************/
127 /* get the program name extracted from argv[0] */
128 /***********************************************/
129 char* getProgramName(void){
130   char* pCh;
131   pCh = strrchr(loadPath, '/');
132   if(isNull(pCh)) return loadPath;
133   else return pCh+1;
134 }
135
136 /*********************************************
137 **********************************************/
138 int StartChildProc(char* macAddress){
139   int ret;
140   if(debug>1) err_msg("DEBUG:=>startChildProc(%s)",macAddress);
141   ret=startChildProc(macAddress);
142   if(debug>1) err_msg("DEBUG:(%d)<=startChildProc( )",ret);
143   return ret;
144 }
145
146 int StopChildProc(char* macAddress){
147   int ret;
148   if(debug>1) err_msg("DEBUG:=>stopChildProc(%s)",macAddress);
149   ret=stopChildProc(macAddress);
150   if(debug>1) err_msg("DEBUG:(%d)<=stopChildProc( )",ret);
151   return ret;
152 }
153