sys_config.c

Go to the documentation of this file.
00001 /*
00002  ** Copyright 2005 - INL
00003  ** Written by Eric Leblond <regit@inl.fr>
00004  **            Vincent Deffontaines <vincent@inl.fr>
00005  ** INL http://www.inl.fr/
00006  **
00007  ** $Id: checks.c 3968 2007-11-26 14:03:43Z lds $
00008  **
00009  **
00010  ** This program is free software; you can redistribute it and/or modify
00011  ** it under the terms of the GNU General Public License as published by
00012  ** the Free Software Foundation, version 3 of the License.
00013  **
00014  ** This program is distributed in the hope that it will be useful,
00015  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  ** GNU General Public License for more details.
00018  **
00019  ** You should have received a copy of the GNU General Public License
00020  ** along with this program; if not, write to the Free Software
00021  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00022  */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <unistd.h>
00032 
00033 #include <nubase.h>
00034 
00035 #include "sys_config.h"
00036 #include "getdelim.h"
00037 
00038 #define SYS_CONF_FILE CONFIG_DIR "/nuclient.conf"
00039 
00040 static int config_loaded = 0;
00041 static char* default_hostname = NULL;
00042 static char* default_port = NULL;
00043 static char* default_tls_ca = NULL;
00044 static char* default_tls_cert = NULL;
00045 static char* default_tls_key = NULL;
00046 static char* default_tls_crl = NULL;
00047 static int default_suppress_fqdn_verif = 0;
00048 
00049 #ifdef FREEBSD
00050 #include "getdelim.h"
00051 
00052 static char *strndup(const char* s, size_t n)
00053 {
00054         char *new;
00055         size_t len = strlen(s);
00056 
00057         if(len > n)
00058                 len = n;
00059 
00060         new = (char *) malloc (len + 1);
00061         if (new == NULL)
00062                 return NULL;
00063 
00064         new[len] = '\0';
00065         return (char *) memcpy (new, s, len);
00066 }
00067 
00068 static ssize_t getline(char **lineptr, size_t * n, FILE * stream)
00069 {
00070         return getdelim(lineptr, n, '\n', stream);
00071 }
00072 #endif /* #ifdef FREEBSD */
00073 
00074 static int str_to_bool(const char *val, int default_value)
00075 {
00076         if ( (!strcasecmp(val,"1")) ||
00077              (!strcasecmp(val,"true")) ||
00078              (!strcasecmp(val,"yes")) )
00079                 return 1;
00080 
00081         if ( (!strcasecmp(val,"0")) ||
00082              (!strcasecmp(val,"false")) ||
00083              (!strcasecmp(val,"no")) )
00084                 return 0;
00085 
00086         return default_value;
00087 }
00088 
00089 char *compute_user_config_path()
00090 {
00091         char path_dir[254];
00092         char *home = nu_get_home_dir();
00093         if (home == NULL)
00094                 return NULL;
00095         secure_snprintf(path_dir, sizeof(path_dir), "%s/.nufw", home);
00096         if (access(path_dir, R_OK) != 0) {
00097                 return NULL;
00098         }
00099         secure_snprintf(path_dir, sizeof(path_dir), "%s/.nufw/nuclient.conf", home);
00100         free(home);
00101         if (access(path_dir, R_OK) != 0) {
00102                 return NULL;
00103         }
00104         return strdup(path_dir);
00105 }
00106 
00107 static void replace_value(char ** initval, char *newval)
00108 {
00109         if (! initval) {
00110                 return;
00111         }
00112 
00113         if (*initval) {
00114                 free(*initval);
00115         }
00116         *initval = newval;
00117 }
00118 
00119 int parse_sys_config(const char *filename)
00120 {
00121         char *opt, *val, *line;
00122         size_t len;
00123         FILE * file;
00124         int line_nbr = 0;
00125         line = NULL;
00126 
00127         file = fopen(filename, "r");
00128         if(!file)
00129                 return 0;
00130 
00131         printf("Loading settings from %s\n", filename);
00132 
00133         while (getline(&line, &len, file) >= 0)
00134         {
00135                 char* equ_pos;
00136                 line_nbr++;
00137                 if(strlen(line) == 0 || *line == '#' || *line == '\n' )
00138                         continue;
00139 
00140                 equ_pos = strchr(line,'=');
00141                 if(equ_pos == NULL) {
00142                         fprintf(stderr, "Wrong format on line %i: %s\n",line_nbr, line);
00143                         continue;
00144                 }
00145 
00146                 opt = strndup(line, equ_pos - line);
00147                 val = strdup(equ_pos + 1);
00148 
00149                 if(strlen(val) >= 1)
00150                         val[strlen(val)-1] = '\0'; /* Strip '\n' */
00151 
00152                 if(!strcmp(opt, "nuauth_ip"))
00153                         replace_value(&default_hostname, val);
00154                 else
00155                 if(!strcmp(opt, "nuauth_port"))
00156                         replace_value(&default_port, val);
00157                 else
00158                 if(!strcmp(opt, "nuauth_tls_ca"))
00159                         replace_value(&default_tls_ca, val);
00160                 else
00161                 if(!strcmp(opt, "nuauth_tls_cert"))
00162                         replace_value(&default_tls_cert, val);
00163                 else
00164                 if(!strcmp(opt, "nuauth_tls_key"))
00165                         replace_value(&default_tls_key, val);
00166                 else
00167                 if(!strcmp(opt, "nuauth_tls_crl"))
00168                         replace_value(&default_tls_crl, val);
00169                 else
00170                 if(!strcmp(opt, "nuauth_suppress_fqdn_verif")) {
00171                         default_suppress_fqdn_verif = str_to_bool(val,1);
00172                         free(val);
00173                 }
00174                 else {
00175                         printf("warning: unknown option '%s' in config file\n", opt);
00176                         free(val);
00177                 }
00178                 free(opt);
00179         }
00180         if(line)
00181                 free(line);
00182         fclose(file);
00183         return 1;
00184 }
00185 
00186 void load_sys_config()
00187 {
00188         char* user_config;
00189 
00190         if(config_loaded)
00191                 return;
00192 
00193         config_loaded = 1;
00194 
00195         parse_sys_config(SYS_CONF_FILE);
00196         user_config = compute_user_config_path();
00197         if (user_config) {
00198                 if (!parse_sys_config(user_config)) {
00199                         fprintf(stderr,
00200                                 "Warning: unable to parse config file \"%s\"\n",
00201                                 user_config);
00202                         free(user_config);
00203                         return;
00204                 }
00205         }
00206         free(user_config);
00207 }
00208 
00209 const char* nu_client_default_hostname()
00210 {
00211         return default_hostname;
00212 }
00213 
00214 const char* nu_client_default_port()
00215 {
00216         return  default_port;
00217 }
00218 
00219 const char* nu_client_default_tls_ca()
00220 {
00221         return  default_tls_ca;
00222 }
00223 
00224 const char* nu_client_default_tls_cert()
00225 {
00226         return  default_tls_cert;
00227 }
00228 
00229 const char* nu_client_default_tls_key()
00230 {
00231         return  default_tls_key;
00232 }
00233 
00234 const char* nu_client_default_tls_crl()
00235 {
00236         return  default_tls_crl;
00237 }
00238 
00239 int nu_client_default_suppress_fqdn_verif()
00240 {
00241         return  default_suppress_fqdn_verif;
00242 }

Generated on Thu Nov 20 04:00:34 2008 for NuFW by  doxygen 1.4.7