1    | /***************************************
2    |   $Revision: 1.18 $
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_AUTHENTICATE    "0"
70   | #define DEFLT_WHOIS_SUSPENDED "0"
71   | #define DEFLT_DO_SERVER       "1"
72   | #define DEFLT_WELCOME         "Welcome to the whois R.I.P. server.\n"
73   | #define DEFLT_PROMPT          "whois R.I.P. config> "
74   | #define DEFLT_CLEAR_SCREEN    "0"
75   | #define DEFLT_ACCOUNTING      "0"
76   | #define DEFLT_CONFIG_FILE     "rip.config"
77   | 
78   | /*+ Each constant has a +*/
79   | struct _constant {
80   |   const char *token;              /*+ Token to be found in properties file. +*/
81   |   const char *deflt;                    /*+ Default value for the constant. +*/
82   |   int (*set_func)(void *, char *);        /*+ Function to set the constant. +*/
83   |   void *constant_ptr;                     /*+ Pointer to the constant value +*/
84   |   char *(*show_func)(void *);            /*+ Function to show the constant. +*/
85   | };
86   | 
87   | 
88   | /*+ The Constants array has a +*/
89   | typedef struct _Constants {
90   |   int   authenticate[1];                         /*+ Authenticate users. +*/
91   |   int   whois_suspended[1];                /*+ Suspend the whois server. +*/
92   |   char  welcome[1024];                  /*+ Welcome for config protocol. +*/
93   |   char  prompt[1024];                    /*+ Prompt for config protocol. +*/
94   |   int   clear_screen[1];         /*+ Clear screen after config commands. +*/
95   |   int   accounting[1];          /*+ Conduct accounting on whois queries. +*/
96   | 
97   |   int   do_server[1]; /*+ turns off execution of the all servers(threads) +*/
98   |   int   do_update[1]; /*+ switches on and off the updates +*/
99   | 
100  | } *Constants;
101  | 
102  | /*
103  |  * Global Variables
104  |  */
105  | /*+ The array of Global Constants. +*/
106  | static Constants  Global_constants=NULL;
107  | 
108  | /* 
109  |  * Set Functions
110  |  */
111  | static int set_string(void *constant, char *value) {
112  | 
113  |   strcpy((char *)constant, value);
114  | 
115  |   return 0;
116  | } /* set_string() */
117  | 
118  | static int set_int(void *constant, char *value) {
119  |   int i;
120  |   
121  |   i = atol(value);
122  |   ((int *)constant)[0] = i;
123  | 
124  |   return 0;
125  | } /* set_int() */
126  | 
127  | static int set_boolean(void *constant, char *value) {
128  |   int result=1;
129  |   int i;
130  |   
131  |   i = atol(value);
132  | 
133  |   /* If a valid boolean */
134  |   if ( (i == 0) || (i == 1)) {
135  |     ((int *)constant)[0] = i;
136  |     result = 0;
137  |   }
138  | 
139  |   return result;
140  | } /* set_boolean() */
141  | 
142  | 
143  | /* 
144  |  * Show Functions
145  |  */
146  | /* AR. changed for unification with oter show funcs */
147  | static char *show_string(void *constant) {
148  |   char *tmp;
149  |   
150  |   /*  tmp = calloc(1, strlen((char *)constant)+1); */
151  |   dieif( wr_malloc((void **)&tmp, strlen((char *)constant)+1) != UT_OK);  
152  |   
153  |   strcpy(tmp, (char *)constant);
154  | /*  return((char *)constant); */
155  |   return tmp;
156  | } /* show_string() */
157  | 
158  | static char *show_int(void *constant) {
159  |   char *tmp;
160  | 
161  |   /* tmp = calloc(1, 64); */
162  |   dieif( wr_malloc((void **)&tmp, 64) != UT_OK); 
163  | 
164  |   sprintf(tmp, "%d", ((int *)constant)[0]);
165  |   return tmp;
166  | } /* show_int() */
167  | 
168  | static char *show_boolean(void *constant) {
169  |   char *tmp;
170  | 
171  |   /*  tmp = calloc(1, 64); */
172  |   dieif( wr_malloc((void **)&tmp, 64) != UT_OK); 
173  | 
174  |   sprintf(tmp, "%d", ((int *)constant)[0]);
175  |   return tmp;
176  | } /* show_boolean() */
177  | 
178  | 
179  | /* 
180  |  * Get Functions
181  |  */
182  | 
183  | int CO_get_authenticate() {
184  |   return Global_constants->authenticate[0];
185  | }
186  | 
187  | int CO_get_whois_suspended() {
188  |   return Global_constants->whois_suspended[0];
189  | }
190  | 
191  | char *CO_get_welcome() {
192  |   return Global_constants->welcome;
193  | }
194  | 
195  | char *CO_get_prompt() {
196  |   return Global_constants->prompt;
197  | }
198  | 
199  | int CO_get_clear_screen() {
200  |   return Global_constants->clear_screen[0];
201  | }
202  | 
203  | int CO_get_accounting() {
204  |   return Global_constants->accounting[0];
205  | }
206  | 
207  | int CO_get_do_server() {
208  |   return Global_constants->do_server[0];
209  | }
210  |   
211  | int CO_get_do_update() {
212  |   return Global_constants->do_update[0];
213  | }
214  | 
215  | /*+
216  |  * Contains the constant definitions for the Token, set_function, show_function.
217  |  * (See: _constant)
218  | +*/
219  | static struct _constant constant[MAX_CONSTS];
220  | 
221  | /* init_constants() */
222  | /*++++++++++++++++++++++++++++++++++++++
223  |   Initialize all the constants.
224  | 
225  |   More:
226  |   +html+ <PRE>
227  |   Authors:
228  |         ottrey
229  | 
230  |   +html+ </PRE><DL COMPACT>
231  |   +html+ <DT>Online References:
232  |   +html+ <DD><UL>
233  |   +html+ </UL></DL>
234  | 
235  |   ++++++++++++++++++++++++++++++++++++++*/
236  | static void init_constants(void) {
237  |   int n=0;
238  | 
239  |   constant[n].token="SV.authenticate";
240  |   constant[n].deflt=DEFLT_AUTHENTICATE;
241  |   constant[n].set_func=set_boolean;
242  |   constant[n].constant_ptr=Global_constants->authenticate;
243  |   constant[n].show_func=show_boolean;
244  |   n++;
245  | 
246  |   constant[n].token="SV.whois_suspended";
247  |   constant[n].deflt=DEFLT_WHOIS_SUSPENDED;
248  |   constant[n].set_func=set_boolean;
249  |   constant[n].constant_ptr=Global_constants->whois_suspended;
250  |   constant[n].show_func=show_boolean;
251  |   n++;
252  |   
253  |   constant[n].token="SV.do_server";
254  |   constant[n].deflt=DEFLT_DO_SERVER;
255  |   constant[n].set_func=set_boolean;
256  |   constant[n].constant_ptr=Global_constants->do_server;
257  |   constant[n].show_func=show_boolean;
258  |   n++;
259  |   
260  |   constant[n].token="UD.do_update";
261  |   constant[n].deflt="1";
262  |   constant[n].set_func=set_int;
263  |   constant[n].constant_ptr=Global_constants->do_update;
264  |   constant[n].show_func=show_int;
265  |   n++;
266  | 
267  |   constant[n].token="PC.prompt";
268  |   constant[n].deflt=DEFLT_PROMPT;
269  |   constant[n].set_func=set_string;
270  |   constant[n].constant_ptr=Global_constants->prompt;
271  |   constant[n].show_func=show_string;
272  |   n++;
273  | 
274  |   constant[n].token="PC.clear_screen";
275  |   constant[n].deflt=DEFLT_CLEAR_SCREEN;
276  |   constant[n].set_func=set_boolean;
277  |   constant[n].constant_ptr=Global_constants->clear_screen;
278  |   constant[n].show_func=show_boolean;
279  |   n++;
280  | 
281  |   constant[n].token=NULL;
282  | 
283  | } /* init_constants() */
284  | 
285  | 
286  | /* CO_to_string() */
287  | /*++++++++++++++++++++++++++++++++++++++
288  |   Returns the constants as a string.
289  | 
290  |   More:
291  |   +html+ <PRE>
292  |   Authors:
293  |         ottrey
294  | 
295  |   +html+ </PRE><DL COMPACT>
296  |   +html+ <DT>Online References:
297  |   +html+ <DD><UL>
298  |   +html+ </UL></DL>
299  | 
300  |   ++++++++++++++++++++++++++++++++++++++*/
301  | char *CO_to_string(void) {
302  |   char *consts;
303  |   const char *token;
304  |   char *value;
305  |   char tmp_consts[2048];
306  |   char tmp_const[1024];
307  |   int i=0;
308  | 
309  |   sprintf(tmp_consts, "Constants = { ");
310  |   while(constant[i].token != NULL) {
311  |     token = constant[i].token;
312  |     value = constant[i].show_func(constant[i].constant_ptr);
313  |     sprintf(tmp_const, "\n[%s]=\"%s\"", token, value);
314  |     wr_free(value); /* Otherwise we have memory leaks */
315  |     strcat(tmp_consts, tmp_const);
316  |     i++;
317  |   }
318  |   strcat(tmp_consts, "}");
319  | 
320  |   /* consts = calloc(1, strlen(tmp_consts)+1); */
321  |   dieif(  wr_malloc((void **)&consts, strlen(tmp_consts)+1) != UT_OK);
322  | 
323  |   strcpy(consts, tmp_consts);
324  | 
325  |   return consts;
326  | } /* CO_to_string() */
327  | 
328  | 
329  | char *CO_const_to_string(char *name) {
330  |   char *result=NULL;
331  |   int i;
332  |   
333  |   for (i=0; constant[i].token != NULL; i++) {
334  |     if (strcmp(constant[i].token, name) == 0) {
335  |       result = constant[i].show_func(constant[i].constant_ptr);
336  |       break;
337  |     }
338  |   }
339  | 
340  |   return result;
341  | } /* CO_const_to_string() */
342  | 
343  |  /* CO_set_const() */
344  | /*++++++++++++++++++++++++++++++++++++++
345  |   Sets the value of one constant.  Returns 0 if no error.
346  | 
347  |   More:
348  |   +html+ <PRE>
349  |   Authors:
350  |         ottrey
351  | 
352  |   +html+ </PRE><DL COMPACT>
353  |   +html+ <DT>Online References:
354  |   +html+ <DD><UL>
355  |   +html+ </UL></DL>
356  | 
357  |   ++++++++++++++++++++++++++++++++++++++*/
358  | int CO_set_const(char *name, char *value) {
359  |   int result=1;
360  |   int i;
361  |   
362  |   for (i=0; constant[i].token != NULL; i++) {
363  |     if (strcmp(constant[i].token, name) == 0) {
364  |       result = constant[i].set_func((void *)constant[i].constant_ptr, value);
365  |       break;
366  |     }
367  |   }
368  | 
369  |   return result;
370  | } /* CO_set_const() */
371  | 
372  | 
373  | /* CO_set() */
374  | /*++++++++++++++++++++++++++++++++++++++
375  |   Sets the constants from the properties module.
376  |   Returns the number of constants set.
377  | 
378  |   More:
379  |   +html+ <PRE>
380  |   Authors:
381  |         ottrey
382  |   +html+ </PRE><DL COMPACT>
383  |   +html+ <DT>Online References:
384  |   +html+ <DD><UL>
385  |   +html+   <LI><A HREF="../src/.properties">.properties</A>
386  |   +html+ </UL></DL>
387  | 
388  |   ++++++++++++++++++++++++++++++++++++++*/
389  | char *CO_set(void) {
390  |   int i;
391  |   int set_count=0;
392  |   int set;
393  |   char result_buff[256];
394  |   char *result;
395  |   char *property;
396  | 
397  |   /* Initialize if necessary */
398  |   if (Global_constants == NULL) {
399  |     /*  Global_constants = (Constants)calloc(1, sizeof(struct _Constants)); */
400  |     dieif( wr_calloc((void **)&Global_constants, 1, 
401  | 		     sizeof(struct _Constants)) != UT_OK);  
402  |     
403  |     init_constants();
404  |   }
405  | 
406  |   for (i=0; constant[i].token != NULL; i++) {
407  |     property = PR_get_property(constant[i].token, constant[i].deflt);
408  |     set = constant[i].set_func((void *)constant[i].constant_ptr, property);
409  |     wr_free(property);
410  |     if (set == 0) {
411  |       set_count++;
412  |     }
413  |   }
414  | 
415  |   sprintf(result_buff, "%d out of %d constant(s) set.", set_count, i);
416  | 
417  |   /* result = (char *)calloc(1, strlen(result_buff)+1); */
418  |   dieif( wr_malloc((void **)&result, strlen(result_buff)+1) != UT_OK);  
419  |   strcpy(result, result_buff);
420  | 
421  |   return result;
422  | } /* CO_set() */
423  |