Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

configuration.c

Go to the documentation of this file.
00001 /*! \file configuration.c
00002     This file implements the configuration loader.
00003  */
00004 
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include "server_config.h"
00009 #include "strings_utils.h"
00010 
00011 /*! \brief Maximum number of characters for the error message associated with the error message.
00012  */
00013 
00014 #define CONFIGURATION_ERROR_SIZE 4096
00015 
00016 /*! \brief Global DHCP configuration handler.
00017  */
00018 
00019 struct global_config configuration_globale;
00020 
00021 /*! \brief Buffer used to store the last error message.
00022  */
00023 
00024 static char last_error[CONFIGURATION_ERROR_SIZE];
00025 
00026 /*! \brief return a pointer to a zero terminated string of characters that represents the last
00027            error message.
00028     \author Denis BEURIVE
00029     \warning The string is returned in a statically allocated buffer, which subsequent calls
00030              will overwrite.
00031  */
00032 
00033 char *configuration_last_error() { return last_error; }
00034 
00035 /*! \author Denis BEURIVE
00036     \brief Load the DHCP server configuration from a given file.
00037     \param path Pointer to a zero terminated string of characters that represents the path to the
00038                 configuration file to load.
00039     \param error pointer to a pointer that will point to the error message if an error occured.
00040     \return Upon successful completion, the function returns the value 0.
00041             Otherwize, the function returns the value 1.
00042  */
00043 
00044 int configuration_load (char *path, char **error)
00045 {
00046   int           res, e, rc;
00047   char          *value;
00048 
00049   /* -------------------------------------------------- */
00050   /* Initialize variables                               */
00051   /* -------------------------------------------------- */
00052 
00053   memset ((void*)last_error, 0, CONFIGURATION_ERROR_SIZE);
00054   *error = last_error;
00055 
00056   /* -------------------------------------------------- */
00057   /* Load the configuration file into memory            */
00058   /* -------------------------------------------------- */
00059 
00060   rc = parse_configuration_file (path, &e);
00061   if (rc != PARSE_CONFIGURATION_OK)
00062   {
00063     switch (rc)
00064     {
00065       case PARSE_CONFIGURATION_NO_MEM:
00066            {
00067               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00068                         "Could not load the configuration file <%s> - Could not allocate memory!",
00069                         path); 
00070            }; break;
00071       case PARSE_CONFIGURATION_FILE_NOT_FOUND:
00072            {
00073               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00074                         "Could not load the configuration file <%s> - %s",
00075                         path, strerror(e));
00076            }; break;
00077       case PARSE_CONFIGURATION_SYNTAX_ERROR:
00078            {
00079               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00080                         "Could not load the configuration file <%s> - Found syntax error.",
00081                         path);
00082            }; break;
00083       case PARSE_CONFIGURATION_DUPLICATED_TAG:
00084            {
00085               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00086                         "Could not load the configuration file <%s> - Duplicated tag found.",
00087                         path);
00088            }; break;
00089       case PARSE_CONFIGURATION_DLL_OPEN_ERROR:
00090            {
00091               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00092                         "Could not load the configuration file <%s> - Could not open DLL. %s.",
00093                         path, conf_get_last_error());
00094            }; break;
00095       case PARSE_CONFIGURATION_DLL_INVALID:
00096            {
00097               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00098                         "Could not load the configuration file <%s> - DLL is not valid. %s.",
00099                         path, conf_get_last_error());
00100            }; break;
00101       case PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID:
00102            {
00103               snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00104                         "Could not load the configuration file <%s> - Modem's configuration is not valid.",
00105                         path);
00106            }; break;
00107     } 
00108 
00109     return 1;   
00110   }
00111 
00112   /* -------------------------------------------------- */
00113   /* Check configuration                                */
00114   /* -------------------------------------------------- */
00115 
00116     /* --- */
00117 
00118   res = get_value ("sql-expiration-security-delay", &value);
00119 
00120   if (res == 0)
00121   {
00122     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00123               "<%s>: tag 'debug-level' is missing",
00124               path);
00125 
00126     return 1;
00127   }
00128 
00129   if (res == -1)
00130   {
00131     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00132              "<%s>: Unexpected error while checking tag 'debug-level'",
00133              path);
00134 
00135     return 1;
00136   }
00137 
00138   configuration_globale.sql_expiration_security_delay = atoi(value);
00139 
00140     /* --- */
00141 
00142   res = get_value ("log-file", &value);
00143 
00144   if (res == 0)
00145   {
00146     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00147               "<%s>: tag 'log-file' is missing",
00148               path);
00149 
00150     return 1;
00151   }
00152 
00153   if (res == -1)
00154   {
00155     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00156              "<%s>: Unexpected error while checking tag 'log-file'",
00157              path);
00158 
00159     return 1;
00160   }
00161 
00162   if (strlen(value) >= FILE_NAME_SIZE)
00163   {
00164     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00165              "<%s>: Tag 'log-file' is too long (buffer overflow avoided!)",
00166              path);
00167 
00168     return 1;
00169   }
00170 
00171   strcpy (configuration_globale.log_file, value);
00172 
00173     /* --- */
00174 
00175   res = get_value ("csv-file", &value);
00176 
00177   if (res == 0)
00178   {
00179     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00180               "<%s>: tag 'csv-file' is missing",
00181               path);
00182 
00183     return 1;
00184   }
00185 
00186   if (res == -1)
00187   {
00188     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00189              "<%s>: Unexpected error while checking tag 'csv-file'",
00190              path);
00191 
00192     return 1;
00193   }
00194 
00195   if (strlen(value) >= FILE_NAME_SIZE)
00196   {
00197     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00198              "<%s>: Tag 'csv-file' is too long (buffer overflow avoided!)",
00199              path);
00200 
00201     return 1;
00202   }
00203 
00204   strcpy (configuration_globale.csv_file, value);
00205 
00206     /* --- */
00207 
00208   res = get_value ("dumper-file", &value);
00209 
00210   if (res == 0)
00211   {
00212     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00213               "<%s>: tag 'dumper-file' is missing",
00214               path);
00215 
00216     return 1;
00217   }
00218 
00219   if (res == -1)
00220   {
00221     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00222              "<%s>: Unexpected error while checking tag 'dumper-file'",
00223              path);
00224 
00225     return 1;
00226   }
00227 
00228   if (strlen(value) >= FILE_NAME_SIZE)
00229   {
00230     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00231              "<%s>: Tag 'dumper-file' is too long (buffer overflow avoided!)",
00232              path);
00233 
00234     return 1;
00235   }
00236 
00237   strcpy (configuration_globale.debug_file, value);
00238 
00239     /* --- */
00240 
00241   res = get_value ("deamon-working-directory", &value);
00242 
00243   if (res == 0)
00244   {
00245     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00246               "<%s>: tag 'deamon-working-directory' is missing",
00247               path);
00248 
00249     return 1;
00250   }
00251 
00252   if (res == -1)
00253   {
00254     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00255              "<%s>: Unexpected error while checking tag 'deamon-working-directory'",
00256              path);
00257 
00258     return 1;
00259   }
00260 
00261   if (strlen(value) >= FILE_NAME_SIZE)
00262   {
00263     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00264              "<%s>: Tag 'deamon-working-directory' is too long (buffer overflow avoided!)",
00265              path);
00266 
00267     return 1;
00268   }
00269 
00270   strcpy (configuration_globale.working_dir, value);
00271 
00272     /* --- */
00273 
00274   res = get_value ("bind-ip", &value);
00275 
00276   if (res == 0)
00277   {
00278     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00279               "<%s>: tag 'bind-ip' is missing",
00280               path);
00281 
00282     return 1;
00283   }
00284 
00285   if (res == -1)
00286   {
00287     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00288              "<%s>: Unexpected error while checking tag 'bind-ip'",
00289              path);
00290 
00291     return 1;
00292   }
00293 
00294   if (strlen(value) >= HOST_NAME_MAX_SIZE)
00295   {
00296     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00297              "<%s>: Tag 'bind-ip' is too long (buffer overflow avoided!)",
00298              path);
00299 
00300     return 1;
00301   }
00302 
00303   strcpy (configuration_globale.dhcp_bind_ip, value);
00304 
00305     /* --- */
00306 
00307   res = get_value ("debug-level", &value);
00308 
00309   if (res == 0)
00310   {
00311     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00312               "<%s>: tag 'debug-level' is missing",
00313               path);
00314 
00315     return 1;
00316   }
00317 
00318   if (res == -1)
00319   {
00320     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00321              "<%s>: Unexpected error while checking tag 'debug-level'",
00322              path);
00323 
00324     return 1;
00325   }
00326 
00327   configuration_globale.debug = atoi(value);
00328 
00329     /* --- */
00330 
00331   res = get_value ("activate-logfile-size-support", &value);
00332 
00333   if (res == 0)
00334   {
00335     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00336               "<%s>: tag 'activate-logfile-size-support' is missing",
00337               path);
00338 
00339     return 1;
00340   }
00341 
00342   if (res == -1)
00343   {
00344     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00345              "<%s>: Unexpected error while checking tag 'activate-logfile-size-support'",
00346              path);
00347 
00348     return 1;
00349   }
00350 
00351   if (strcmp(value, "yes") == 0) { configuration_globale.log_size_management = 1; }
00352   else                           { configuration_globale.log_size_management = 0; } 
00353 
00354     /* --- */
00355 
00356   res = get_value ("pid-file", &value);
00357 
00358   if (res == 0)
00359   {
00360     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00361               "<%s>: tag 'pid-file' is missing",
00362               path);
00363 
00364     return 1;
00365   }
00366 
00367   if (res == -1)
00368   {
00369     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00370              "<%s>: Unexpected error while checking tag 'pid-file'",
00371              path);
00372 
00373     return 1;
00374   }
00375 
00376   if (strlen(value) >= HOST_NAME_MAX_SIZE)
00377   {
00378     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00379              "<%s>: Tag 'pid-file' is too long (buffer overflow avoided!)",
00380              path);
00381 
00382     return 1;
00383   }
00384 
00385   strcpy (configuration_globale.pid_file, value);
00386 
00387     /* --- */
00388 
00389   res = get_value ("dumper-mode", &value);
00390 
00391   if (res == 0)
00392   {
00393     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00394               "<%s>: tag 'dumper-mode' is missing",
00395               path);
00396 
00397     return 1;
00398   }
00399 
00400   if (res == -1)
00401   {
00402     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00403              "<%s>: Unexpected error while checking tag 'dumper-mode'",
00404              path);
00405 
00406     return 1;
00407   }
00408 
00409   configuration_globale.dumper_mode = -1;
00410 
00411   if (strcasecmp(value, "no")     == 0) { configuration_globale.dumper_mode = 0; }
00412   if (strcasecmp(value, "normal") == 0) { configuration_globale.dumper_mode = 1; }
00413   if (strcasecmp(value, "total")  == 0) { configuration_globale.dumper_mode = 2; } 
00414 
00415   if (configuration_globale.dumper_mode == -1)
00416   {
00417     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00418               "<%s>: Invalid value for tag 'dumper-mode' (%s) - possible values are 'no', 'normal' or 'total'",
00419               path,
00420               value);
00421   }
00422 
00423     /* --- */
00424 
00425   res = get_value ("server-ip-address-as-seen-by-client", &value);
00426 
00427   if (res == 0)
00428   {
00429     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00430               "<%s>: tag 'server-ip-address-as-seen-by-client' is missing",
00431               path);
00432 
00433     return 1;
00434   }
00435 
00436   if (res == -1)
00437   {
00438     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00439              "<%s>: Unexpected error while checking tag 'server-ip-address-as-seen-by-client'",
00440              path);
00441 
00442     return 1;
00443   }
00444 
00445   if (strlen(value) >= MAX_IP_ADDRESS_STR_SIZE)
00446   {
00447     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00448              "<%s>: Tag 'server-ip-address-as-seen-by-client' is too long (buffer overflow avoided!)",
00449              path);
00450 
00451     return 1;
00452   }
00453 
00454   strcpy (configuration_globale.server_ip_seen_by_client, value);
00455 
00456     /* --- */
00457 
00458   res = get_value ("enable-broadcast-responses", &value);
00459 
00460   if (res == 0)
00461   {
00462     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00463               "<%s>: tag 'enable-broadcast-responses' is missing",
00464               path);
00465 
00466     return 1;
00467   }
00468 
00469   if (res == -1)
00470   {
00471     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00472              "<%s>: Unexpected error while checking tag 'enable-broadcast-responses'",
00473              path);
00474 
00475     return 1;
00476   }
00477 
00478 
00479   if (strcasecmp(value, "yes") == 0)
00480   { configuration_globale.broadcast_mode = 1; }
00481   else {
00482          if (strcasecmp(value, "no") == 0)
00483          { configuration_globale.broadcast_mode = 0; }
00484          else {
00485                 snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00486                           "<%s>: Invalid value for tag 'broadcast_mode' (%s) - possible values are ('yes' or 'no' - no matter the case)",
00487                           path,
00488                           value);
00489               }
00490        }
00491 
00492     /* --- */
00493 
00494   res = get_value ("deamon-user-id", &value);
00495 
00496   if (res == 0)
00497   {
00498     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00499               "<%s>: tag 'deamon-user-id' is missing",
00500               path);
00501 
00502     return 1;
00503   }
00504 
00505   if (res == -1)
00506   {
00507     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00508              "<%s>: Unexpected error while checking tag 'deamon-user-id'",
00509              path);
00510 
00511     return 1;
00512   }
00513 
00514   if (strlen(value) >= MAX_UID_SIZE)
00515   {
00516     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00517              "<%s>: Tag 'deamon-user-id' is too long (buffer overflow avoided!)",
00518              path);
00519 
00520     return 1;
00521   }
00522 
00523   strcpy (configuration_globale.deamon_user_id, value);
00524 
00525     /* --- */
00526 
00527   res = get_value ("deamon-group-id", &value);
00528 
00529   if (res == 0)
00530   {
00531     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00532               "<%s>: tag 'deamon-group-id' is missing",
00533               path);
00534 
00535     return 1;
00536   }
00537 
00538   if (res == -1)
00539   {
00540     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00541              "<%s>: Unexpected error while checking tag 'deamon-group-id'",
00542              path);
00543 
00544     return 1;
00545   }
00546 
00547   if (strlen(value) >= MAX_GID_SIZE)
00548   {
00549     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00550              "<%s>: Tag 'deamon-group-id' is too long (buffer overflow avoided!)",
00551              path);
00552 
00553     return 1;
00554   }
00555 
00556   strcpy (configuration_globale.deamon_group_id, value);
00557 
00558     /* --- */
00559 
00560   res = get_value ("dummy-ip-address", &value);
00561 
00562   if (res == 0)
00563   {
00564     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00565               "<%s>: tag 'dummy-ip-address' is missing",
00566               path);
00567 
00568     return 1;
00569   }
00570 
00571   if (res == -1)
00572   {
00573     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00574              "<%s>: Unexpected error while checking tag 'dummy-ip-address'",
00575              path);
00576 
00577     return 1;
00578   }
00579 
00580   if (strlen(value) >= MAX_IP_ADDRESS_STR_SIZE)
00581   {
00582     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00583              "<%s>: Tag 'dummy-ip-address' is too long (buffer overflow avoided!)",
00584              path);
00585 
00586     return 1;
00587   }
00588 
00589   strcpy (configuration_globale.dummy_ip, value);
00590 
00591     /* --- */
00592 
00593   res = get_value ("sync-dump-file", &value);
00594 
00595   if (res == 0)
00596   {
00597     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00598               "<%s>: tag 'sync-dump-file' is missing",
00599               path);
00600 
00601     return 1;
00602   }
00603 
00604   if (res == -1)
00605   {
00606     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00607              "<%s>: Unexpected error while checking tag 'sync-dump-file'",
00608              path);
00609 
00610     return 1;
00611   }
00612 
00613   if (strlen(value) >= FILE_NAME_SIZE)
00614   {
00615     snprintf (last_error, CONFIGURATION_ERROR_SIZE-1,
00616              "<%s>: Tag 'sync-dump-file' is too long (buffer overflow avoided!)",
00617              path);
00618 
00619     return 1;
00620   }
00621 
00622   strcpy (configuration_globale.sync_dump, value);
00623 
00624 
00625 
00626   return 0;
00627 }
00628 
00629 
00630 
00631 
00632 
00633 

Generated on Mon Jun 19 12:31:05 2006 for MyDhcp_V2 by doxygen1.2.15