/[pdpsoft]/trunk/grid-mw-security/ees/src/app/main.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/app/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1754 - (show annotations) (download) (as text)
Thu Jun 10 16:35:55 2010 UTC (11 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 5305 byte(s)
Fixed most memleaks in localaccount/localgroup plugin
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 #include <unistd.h>
6 #include <netinet/in.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <string.h>
10 #include <netdb.h>
11 #include <arpa/inet.h>
12
13 #include "ees_eics.h"
14 #include "config.h"
15 #include "xacml.h"
16 #include "xacml_server.h"
17 #include "eef_library.h"
18 #include "cgul/log_to_file/log_to_file.h"
19 #include "cgul_patched/network/net_common.h"
20
21 #define _MULTI_THREADED
22 #define PORT 1234
23 #define BACKLOG 100
24
25 char* config_file_path; /* config file path */
26 xacml_server_t server; /* xacml server object */
27 int signal_is_being_handled = 0; /* signal handler state/lock */
28
29 void signal_handler(int sig); /* main signal handler */
30 EES_RC process_xacml(void); /* creates and starts new xacml server instance */
31 EES_RC ees_start(void); /* initializes EEF */
32 EES_RC ees_stop(void); /* terminates EEF */
33 EES_RC ees_loop(void); /* main loop - calls process_xacml and pauses */
34 EES_RC add_dummy_aos_obligation(void); /* adds some stuff to the AOS */
35
36
37
38 /**
39 * Adds some dummy uid/gid obligations in the AOS
40 */
41 EES_RC add_dummy_aos_obligation(void){
42 aos_context_t* _context;
43 aos_attribute_t *_uid, *_gid;
44
45 char* _uid_str = "1234";
46 char* _gid_str = "1234";
47
48 /*fill aos */
49 _uid = createAttribute();
50 _gid = createAttribute();
51
52 setAttributeId(_uid, "posix-uid");
53 setAttributeValue(_uid, _uid_str, strlen(_uid_str));
54 setAttributeId(_gid, "posix-gid");
55 setAttributeValue(_gid, _gid_str, strlen(_gid_str));
56
57 if((_context = createContext(OBLIGATION)) != NULL){
58 setContextObligationId(_context, "uidgid");
59
60 addAttribute(_context, _uid);
61 addAttribute(_context, _gid);
62
63 addContext(_context);
64 }
65 return EES_SUCCESS;
66 }
67
68 /**
69 * Prepares logging, initializes EEF and puts the EEF in threading mode
70 */
71 EES_RC ees_start(void){
72 /* set log options */
73 if(cgul_openlog("/tmp/ees_log") != 0){
74 return EES_FAILURE;
75 }
76 cgul_set_ident(BINARY_NAME);
77 cgul_set_log_options(CGUL_SYSLOG | CGUL_LOGFILE | CGUL_STDERR);
78
79 /* reuse this part when implementing SIGHUP */
80 cgul_log(LOG_INFO, "Parsing config file %s\n", config_file_path);
81 if(EEF_init(config_file_path, cgul_log) == EES_FAILURE){
82 /* at this point, EEF_log can/will be a reference to the second argument passed to EEF_init */
83 EEF_log(LOG_ERR, "Failed to parse config file (%s), exiting...", config_file_path);
84 return EES_FAILURE;
85 } else {
86 EEF_startThreading();
87 }
88 EEF_log(LOG_NOTICE, "EES Ready.\n");
89 return EES_SUCCESS;
90 }
91
92 /**
93 * Main EES loop. Starts xacml server and pauses.
94 */
95 EES_RC ees_loop(void){
96 while(1){ /* this is to prevent falling out of the loop when a signal gets handled */
97 signal_is_being_handled = 0;
98 if(process_xacml() == EES_SUCCESS){
99 printf("Paused.\n");
100 pause();
101 } else {
102 EEF_log(LOG_ERR, "Unable to start xacml server\n");
103 ees_stop();
104 exit(EXIT_FAILURE);
105 }
106 }
107 return EES_SUCCESS;
108 }
109
110 /**
111 * Terminates EES
112 */
113 EES_RC ees_stop(void){
114 xacml_server_destroy(server);
115 AOS_Clean();
116 if(EEF_term() == EES_SUCCESS){
117 EEF_log(LOG_NOTICE, "EEF terminated!\n");
118 } else {
119 EEF_log(LOG_NOTICE, "Error while terminating EEF!\n");
120 }
121 fflush(stdout);
122 fflush(stderr);
123 EEF_log(LOG_NOTICE, "EES Stopped.\n");
124 cgul_closelog();
125 return EES_SUCCESS;
126 }
127
128 /**
129 * Creates and starts xacml server.
130 */
131 EES_RC process_xacml(void){
132 int listener;
133 int fd;
134 /*shutdown(fd, 0);*/
135 if((fd = createAndSetUpATCPServerSocket(PORT, BACKLOG)) > -1){
136 xacml_init(); /* if you look at the code, you'll see this function is empty */
137 if(xacml_server_init(&server, ees_xacml_authorize, NULL) == XACML_RESULT_SUCCESS){
138 xacml_server_set_fd(server, fd);
139 xacml_server_start(server);
140 return EES_SUCCESS;
141 }
142 }
143 return EES_FAILURE;
144 }
145
146 /**
147 * Handles incoming signals
148 */
149 void signal_handler(int sig){
150 printf("Got signal\n");
151 if(signal_is_being_handled == 0){
152 signal_is_being_handled = 1;
153 switch(sig){
154 case SIGHUP:
155 EEF_log(LOG_NOTICE, "Got SIGHUP - restart \n");
156 ees_stop();
157 if(ees_start() == EES_FAILURE){
158 EEF_log(LOG_ERR, "Unable to restart EES. Exiting...\n");
159 exit(EXIT_FAILURE);
160 }
161 break;
162 case SIGTERM:
163 EEF_log(LOG_NOTICE, "Got SIGTERM - shutting down\n");
164 ees_stop();
165 exit(EXIT_SUCCESS);
166 break;
167 }
168 }
169 }
170
171 /**
172 * Main
173 */
174 int main (int argc, char* argv[]){
175 struct sigaction sa;
176
177 if(argc > 1){
178 config_file_path = argv[1];
179 } else {
180 config_file_path = "examples/example_1.pdl";
181 }
182
183 /* catch hangup and term signals */
184 sa.sa_handler = signal_handler;
185 sigemptyset(&sa.sa_mask);
186 sa.sa_flags = SA_RESTART;
187
188 if(sigaction(SIGHUP, &sa, NULL) != 0){
189 EEF_log(LOG_ERR, "Unable to add signal handler for SIGHUP\n");
190 }
191 if(sigaction(SIGTERM, &sa, NULL) != 0){
192 EEF_log(LOG_ERR, "Unable to add signal handler for SIGHUP\n");
193 }
194
195 #if ENABLE_DEBUG
196 cgul_set_log_options(cgul_get_log_options() | CGUL_STDDEBUG);
197 #endif
198 if(ees_start() == EES_SUCCESS){
199 #if !ENABLE_DEBUG
200 EEF_daemonize();
201 #endif
202
203 ees_loop();
204 return EXIT_SUCCESS;
205 }
206
207 return EXIT_FAILURE;
208
209 }
210

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28