1 | /***************************************
2 | $Revision: 1.17 $
3 |
4 | Constants module (co) - this _should_ eventually get merged in with the
5 | config module.
6 |
7 | Status: NOT REVUED, NOT TESTED
8 |
9 | +html+ <DL COMPACT>
10 | +html+ <DT>Online References:
11 | +html+ <DD><UL>
12 | +html+ </UL>
13 | +html+ </DL>
14 | +html+ <PRE>
15 | Instructions for use:
16 |
17 | To add a constant:
18 | 0. Add a default value for the constant. (string)
19 | 1. Add the constant declaration to the _Constants struct.
20 | 2. Add a CO_get_function()
21 | 3. Add initializing code to init_constants()
22 |
23 | To access the constant:
24 | use the CO_get<Constant>() function from your other code.
25 | +html+ </PRE>
26 |
27 | ******************/ /******************
28 | Filename : constants.c
29 | Author : ottrey@ripe.net
30 | OSs Tested : Solaris
31 | Related Modules : Used in conjunction with the properties module.
32 | Problems :
33 | To Do : Merge into a "config module"
34 | Comments :
35 | ******************/ /******************
36 | Copyright (c) 1999 RIPE NCC
37 |
38 | All Rights Reserved
39 |
40 | Permission to use, copy, modify, and distribute this software and its
41 | documentation for any purpose and without fee is hereby granted,
42 | provided that the above copyright notice appear in all copies and that
43 | both that copyright notice and this permission notice appear in
44 | supporting documentation, and that the name of the author not be
45 | used in advertising or publicity pertaining to distribution of the
46 | software without specific, written prior permission.
47 |
48 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
49 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
50 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
51 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
52 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54 | ***************************************/
55 | #include <stdio.h>
56 | #include <stdlib.h>
57 | #include <string.h>
58 |
59 | #include "memwrap.h"
60 | #include "properties.h"
61 |
62 | #define STR_XL 4095
63 |
64 | /*+ Maximum number of constants. +*/
65 | #define MAX_CONSTS 100
66 |
67 | /*+ Default values for constants. +*/
68 |
69 | #define DEFLT_WHOIS_PORT "0"
70 | #define DEFLT_CONFIG_PORT "0"
71 | #define DEFLT_MIRROR_PORT "0"
72 | #define DEFLT_UPDATE_PORT "0"
73 | #define DEFLT_AUTHENTICATE "0"
74 | #define DEFLT_WHOIS_SUSPENDED "0"
75 | #define DEFLT_DO_SERVER "1"
76 | #define DEFLT_WELCOME "Welcome to the whois R.I.P. server.\n"
77 | #define DEFLT_PROMPT "whois R.I.P. config> "
78 | #define DEFLT_CLEAR_SCREEN "0"
79 | #define DEFLT_ACCOUNTING "0"
80 | #define DEFLT_CONFIG_FILE "rip.config"
81 |
82 | /*+ Each constant has a +*/
83 | struct _constant {
84 | const char *token; /*+ Token to be found in properties file. +*/
85 | const char *deflt; /*+ Default value for the constant. +*/
86 | int (*set_func)(void *, char *); /*+ Function to set the constant. +*/
87 | void *constant_ptr; /*+ Pointer to the constant value +*/
88 | char *(*show_func)(void *); /*+ Function to show the constant. +*/
89 | };
90 |
91 |
92 | /*+ The Constants array has a +*/
93 | typedef struct _Constants {
94 | char whois_port[64]; /*+ Port for whois clients to rendezvous with. +*/
95 | char config_port[64]; /*+ Port for config clients to rendezvous with. +*/
96 | char mirror_port[64]; /*+ Port for mirror clients to rendezvous with. +*/
97 | char update_port[64]; /*+ Port for DBupdate clients to rendezvous with. +*/
98 | int authenticate[1]; /*+ Authenticate users. +*/
99 | int whois_suspended[1]; /*+ Suspend the whois server. +*/
100 | char welcome[1024]; /*+ Welcome for config protocol. +*/
101 | char prompt[1024]; /*+ Prompt for config protocol. +*/
102 | int clear_screen[1]; /*+ Clear screen after config commands. +*/
103 | int accounting[1]; /*+ Conduct accounting on whois queries. +*/
104 | char config_file[1024]; /*+ File for the config. +*/
105 |
106 | int do_server[1]; /*+ turns off execution of the all servers(threads) +*/
107 | int do_update[1]; /*+ switches on and off the updates +*/
108 |
109 | } *Constants;
110 |
111 | /*
112 | * Global Variables
113 | */
114 | /*+ The array of Global Constants. +*/
115 | static Constants Global_constants=NULL;
116 |
117 | /*
118 | * Set Functions
119 | */
120 | static int set_string(void *constant, char *value) {
121 |
122 | strcpy((char *)constant, value);
123 |
124 | return 0;
125 | } /* set_string() */
126 |
127 | static int set_int(void *constant, char *value) {
128 | int i;
129 |
130 | i = atol(value);
131 | ((int *)constant)[0] = i;
132 |
133 | return 0;
134 | } /* set_int() */
135 |
136 | static int set_boolean(void *constant, char *value) {
137 | int result=1;
138 | int i;
139 |
140 | i = atol(value);
141 |
142 | /* If a valid boolean */
143 | if ( (i == 0) || (i == 1)) {
144 | ((int *)constant)[0] = i;
145 | result = 0;
146 | }
147 |
148 | return result;
149 | } /* set_boolean() */
150 |
151 |
152 | /*
153 | * Show Functions
154 | */
155 | /* AR. changed for unification with oter show funcs */
156 | static char *show_string(void *constant) {
157 | char *tmp;
158 |
159 | /* tmp = calloc(1, strlen((char *)constant)+1); */
160 | dieif( wr_malloc((void **)&tmp, strlen((char *)constant)+1) != UT_OK);
161 |
162 | strcpy(tmp, (char *)constant);
163 | /* return((char *)constant); */
164 | return tmp;
165 | } /* show_string() */
166 |
167 | static char *show_int(void *constant) {
168 | char *tmp;
169 |
170 | /* tmp = calloc(1, 64); */
171 | dieif( wr_malloc((void **)&tmp, 64) != UT_OK);
172 |
173 | sprintf(tmp, "%d", ((int *)constant)[0]);
174 | return tmp;
175 | } /* show_int() */
176 |
177 | static char *show_boolean(void *constant) {
178 | char *tmp;
179 |
180 | /* tmp = calloc(1, 64); */
181 | dieif( wr_malloc((void **)&tmp, 64) != UT_OK);
182 |
183 | sprintf(tmp, "%d", ((int *)constant)[0]);
184 | return tmp;
185 | } /* show_boolean() */
186 |
187 |
188 | /*
189 | * Get Functions
190 | */
191 | char *CO_get_whois_port() {
192 | return Global_constants->whois_port;
193 | }
194 |
195 | char *CO_get_config_port() {
196 | return Global_constants->config_port;
197 | }
198 |
199 | char *CO_get_mirror_port() {
200 | return Global_constants->mirror_port;
201 | }
202 |
203 | char *CO_get_update_port() {
204 | return Global_constants->update_port;
205 | }
206 |
207 | int CO_get_authenticate() {
208 | return Global_constants->authenticate[0];
209 | }
210 |
211 | int CO_get_whois_suspended() {
212 | return Global_constants->whois_suspended[0];
213 | }
214 |
215 | char *CO_get_welcome() {
216 | return Global_constants->welcome;
217 | }
218 |
219 | char *CO_get_prompt() {
220 | return Global_constants->prompt;
221 | }
222 |
223 | int CO_get_clear_screen() {
224 | return Global_constants->clear_screen[0];
225 | }
226 |
227 | int CO_get_accounting() {
228 | return Global_constants->accounting[0];
229 | }
230 |
231 | char *CO_get_config_file() {
232 | return Global_constants->config_file;
233 | }
234 |
235 |
236 | int CO_get_do_server() {
237 | return Global_constants->do_server[0];
238 | }
239 |
240 | int CO_get_do_update() {
241 | return Global_constants->do_update[0];
242 | }
243 |
244 | /*+
245 | * Contains the constant definitions for the Token, set_function, show_function.
246 | * (See: _constant)
247 | +*/
248 | static struct _constant constant[MAX_CONSTS];
249 |
250 | /* init_constants() */
251 | /*++++++++++++++++++++++++++++++++++++++
252 | Initialize all the constants.
253 |
254 | More:
255 | +html+ <PRE>
256 | Authors:
257 | ottrey
258 |
259 | +html+ </PRE><DL COMPACT>
260 | +html+ <DT>Online References:
261 | +html+ <DD><UL>
262 | +html+ </UL></DL>
263 |
264 | ++++++++++++++++++++++++++++++++++++++*/
265 | static void init_constants(void) {
266 | int n=0;
267 |
268 | constant[n].token="SV.whois_port";
269 | constant[n].deflt=DEFLT_WHOIS_PORT;
270 | constant[n].set_func=set_string;
271 | constant[n].constant_ptr=Global_constants->whois_port;
272 | constant[n].show_func=show_string;
273 | n++;
274 |
275 | constant[n].token="SV.config_port";
276 | constant[n].deflt=DEFLT_CONFIG_PORT;
277 | constant[n].set_func=set_string;
278 | constant[n].constant_ptr=Global_constants->config_port;
279 | constant[n].show_func=show_string;
280 | n++;
281 |
282 | constant[n].token="SV.mirror_port";
283 | constant[n].deflt=DEFLT_MIRROR_PORT;
284 | constant[n].set_func=set_string;
285 | constant[n].constant_ptr=Global_constants->mirror_port;
286 | constant[n].show_func=show_string;
287 | n++;
288 |
289 | constant[n].token="SV.update_port";
290 | constant[n].deflt=DEFLT_UPDATE_PORT;
291 | constant[n].set_func=set_string;
292 | constant[n].constant_ptr=Global_constants->update_port;
293 | constant[n].show_func=show_string;
294 | n++;
295 | constant[n].token="SV.authenticate";
296 | constant[n].deflt=DEFLT_AUTHENTICATE;
297 | constant[n].set_func=set_boolean;
298 | constant[n].constant_ptr=Global_constants->authenticate;
299 | constant[n].show_func=show_boolean;
300 | n++;
301 |
302 | constant[n].token="SV.whois_suspended";
303 | constant[n].deflt=DEFLT_WHOIS_SUSPENDED;
304 | constant[n].set_func=set_boolean;
305 | constant[n].constant_ptr=Global_constants->whois_suspended;
306 | constant[n].show_func=show_boolean;
307 | n++;
308 |
309 | constant[n].token="SV.do_server";
310 | constant[n].deflt=DEFLT_DO_SERVER;
311 | constant[n].set_func=set_boolean;
312 | constant[n].constant_ptr=Global_constants->do_server;
313 | constant[n].show_func=show_boolean;
314 | n++;
315 |
316 | constant[n].token="UD.do_update";
317 | constant[n].deflt="1";
318 | constant[n].set_func=set_int;
319 | constant[n].constant_ptr=Global_constants->do_update;
320 | constant[n].show_func=show_int;
321 | n++;
322 |
323 | constant[n].token="PC.welcome";
324 | constant[n].deflt=DEFLT_WELCOME;
325 | constant[n].set_func=set_string;
326 | constant[n].constant_ptr=Global_constants->welcome;
327 | constant[n].show_func=show_string;
328 | n++;
329 |
330 | constant[n].token="PC.prompt";
331 | constant[n].deflt=DEFLT_PROMPT;
332 | constant[n].set_func=set_string;
333 | constant[n].constant_ptr=Global_constants->prompt;
334 | constant[n].show_func=show_string;
335 | n++;
336 |
337 | constant[n].token="PC.clear_screen";
338 | constant[n].deflt=DEFLT_CLEAR_SCREEN;
339 | constant[n].set_func=set_boolean;
340 | constant[n].constant_ptr=Global_constants->clear_screen;
341 | constant[n].show_func=show_boolean;
342 | n++;
343 |
344 | constant[n].token="WQ.accounting";
345 | constant[n].deflt=DEFLT_ACCOUNTING;
346 | constant[n].set_func=set_boolean;
347 | constant[n].constant_ptr=Global_constants->accounting;
348 | constant[n].show_func=show_boolean;
349 | n++;
350 |
351 |
352 |
353 | constant[n].token="CO.config_file";
354 | constant[n].deflt=DEFLT_CONFIG_FILE;
355 | constant[n].set_func=set_string;
356 | constant[n].constant_ptr=Global_constants->config_file;
357 | constant[n].show_func=show_string;
358 | n++;
359 |
360 | constant[n].token=NULL;
361 |
362 | } /* init_constants() */
363 |
364 |
365 | /* CO_to_string() */
366 | /*++++++++++++++++++++++++++++++++++++++
367 | Returns the constants as a string.
368 |
369 | More:
370 | +html+ <PRE>
371 | Authors:
372 | ottrey
373 |
374 | +html+ </PRE><DL COMPACT>
375 | +html+ <DT>Online References:
376 | +html+ <DD><UL>
377 | +html+ </UL></DL>
378 |
379 | ++++++++++++++++++++++++++++++++++++++*/
380 | char *CO_to_string(void) {
381 | char *consts;
382 | const char *token;
383 | char *value;
384 | char tmp_consts[2048];
385 | char tmp_const[1024];
386 | int i=0;
387 |
388 | sprintf(tmp_consts, "Constants = { ");
389 | while(constant[i].token != NULL) {
390 | token = constant[i].token;
391 | value = constant[i].show_func(constant[i].constant_ptr);
392 | sprintf(tmp_const, "\n[%s]=\"%s\"", token, value);
393 | wr_free(value); /* Otherwise we have memory leaks */
394 | strcat(tmp_consts, tmp_const);
395 | i++;
396 | }
397 | strcat(tmp_consts, "}");
398 |
399 | /* consts = calloc(1, strlen(tmp_consts)+1); */
400 | dieif( wr_malloc((void **)&consts, strlen(tmp_consts)+1) != UT_OK);
401 |
402 | strcpy(consts, tmp_consts);
403 |
404 | return consts;
405 | } /* CO_to_string() */
406 |
407 |
408 | char *CO_const_to_string(char *name) {
409 | char *result=NULL;
410 | int i;
411 |
412 | for (i=0; constant[i].token != NULL; i++) {
413 | if (strcmp(constant[i].token, name) == 0) {
414 | result = constant[i].show_func(constant[i].constant_ptr);
415 | break;
416 | }
417 | }
418 |
419 | return result;
420 | } /* CO_const_to_string() */
421 |
422 | /* CO_set_const() */
423 | /*++++++++++++++++++++++++++++++++++++++
424 | Sets the value of one constant. Returns 0 if no error.
425 |
426 | More:
427 | +html+ <PRE>
428 | Authors:
429 | ottrey
430 |
431 | +html+ </PRE><DL COMPACT>
432 | +html+ <DT>Online References:
433 | +html+ <DD><UL>
434 | +html+ </UL></DL>
435 |
436 | ++++++++++++++++++++++++++++++++++++++*/
437 | int CO_set_const(char *name, char *value) {
438 | int result=1;
439 | int i;
440 |
441 | for (i=0; constant[i].token != NULL; i++) {
442 | if (strcmp(constant[i].token, name) == 0) {
443 | result = constant[i].set_func((void *)constant[i].constant_ptr, value);
444 | break;
445 | }
446 | }
447 |
448 | return result;
449 | } /* CO_set_const() */
450 |
451 |
452 | /* CO_set() */
453 | /*++++++++++++++++++++++++++++++++++++++
454 | Sets the constants from the properties module.
455 | Returns the number of constants set.
456 |
457 | More:
458 | +html+ <PRE>
459 | Authors:
460 | ottrey
461 | +html+ </PRE><DL COMPACT>
462 | +html+ <DT>Online References:
463 | +html+ <DD><UL>
464 | +html+ <LI><A HREF="../src/.properties">.properties</A>
465 | +html+ </UL></DL>
466 |
467 | ++++++++++++++++++++++++++++++++++++++*/
468 | char *CO_set(void) {
469 | int i;
470 | int set_count=0;
471 | int set;
472 | char result_buff[256];
473 | char *result;
474 | char *property;
475 |
476 | /* Initialize if necessary */
477 | if (Global_constants == NULL) {
478 | /* Global_constants = (Constants)calloc(1, sizeof(struct _Constants)); */
479 | dieif( wr_calloc((void **)&Global_constants, 1,
480 | sizeof(struct _Constants)) != UT_OK);
481 |
482 | init_constants();
483 | }
484 |
485 | for (i=0; constant[i].token != NULL; i++) {
486 | property = PR_get_property(constant[i].token, constant[i].deflt);
487 | set = constant[i].set_func((void *)constant[i].constant_ptr, property);
488 | wr_free(property);
489 | if (set == 0) {
490 | set_count++;
491 | }
492 | }
493 |
494 | sprintf(result_buff, "%d out of %d constant(s) set.", set_count, i);
495 |
496 | /* result = (char *)calloc(1, strlen(result_buff)+1); */
497 | dieif( wr_malloc((void **)&result, strlen(result_buff)+1) != UT_OK);
498 | strcpy(result, result_buff);
499 |
500 | return result;
501 | } /* CO_set() */
502 |