ESAPI-C 1.0
The OWASP Enterprise Security API for C

properties.c

Go to the documentation of this file.
00001 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #include "crypto.h"
00013 #include "properties.h"
00014 #include "log.h"
00015 #include "user.h"
00016 
00017 #define LF      0x0A
00018 #define CR      0x0D
00019 
00020 char *getline(char *s, int n, FILE *f) {
00021         char *line = fgets(s, n, f);
00022 
00023         if (line != NULL) {
00024                 /* Strip any trailing newline */
00025                 int len = strlen(line);
00026                 if (len > 0 && line[len - 1] == '\n') {
00027                         line[len - 1] = '\0';
00028                 }
00029         }
00030 
00031         return line;
00032 }
00033 
00034 int putline(char *s, FILE *f) {
00035         return fputs(s, f);
00036 }
00037 
00038 char *get_property(map_t *map, struct esapi_ctx *ctx, const char *key) {
00039 
00040         char *value = NULL;
00041 
00042         map_t *entry = NULL;
00043         HASH_FIND(hh, map, key, strlen(key), entry);
00044         if (entry != NULL) {
00045                 const char *encrypted_value = entry->val;
00046                 if (encrypted_value != NULL) {
00047                         value = esapi_decrypt(ctx, encrypted_value);
00048                         if (value == NULL) {
00049                                 esapi_log_error(NULL, EVENT_FAILURE,
00050                                                 "Property retrieval failure - couldn't decrypt property");
00051                         }
00052                 }
00053         }
00054 
00055         return value;
00056 }
00057 
00058 char *get_property_or_default(map_t *map, struct esapi_ctx *ctx,
00059                 const char *key, const char *default_value) {
00060 
00061         char *value = get_property(map, ctx, key);
00062         if (value == NULL) {
00063                 value = (char *) default_value;
00064         }
00065 
00066         return value;
00067 }
00068 
00069 char *set_property(map_t **maph, struct esapi_ctx *ctx, const char *key,
00070                 const char *value) {
00071 
00072         char *encrypted_value;
00073 
00074         if (key == NULL) {
00075                 esapi_log_error(NULL, EVENT_FAILURE, "Property name may not be null.");
00076         }
00077         if (value == NULL) {
00078                 esapi_log_error(NULL, EVENT_FAILURE, "Property value may not be null.");
00079         }
00080 
00081         // Need error handling that logs "Property setting failure", "Couldn't encrypt property"
00082         encrypted_value = esapi_encrypt(ctx, value);
00083         if (encrypted_value == NULL) {
00084                 esapi_log_error(NULL, EVENT_FAILURE,
00085                                 "Property setting failure - couldn't encrypt property");
00086         }
00087 
00088         map_t *entry;
00089         if ((entry = (map_t *) malloc(sizeof(map_t))) == 0) {
00090                 (void) fprintf(stderr, "malloc failure in %s\n", __func__);
00091                 exit(EXIT_FAILURE);
00092         }
00093 
00094         strcpy(entry->key, key);
00095         if ((entry->val = strdup(encrypted_value)) == 0) {
00096                 (void) fprintf(stderr, "strdup failure in %s\n", __func__);
00097                 exit(EXIT_FAILURE);
00098         }
00099 
00100         HASH_ADD(hh, *maph, key, strlen(entry->key), entry);
00101 
00102         return entry->val;
00103 }
00104 
00105 char **keys(map_t *map) {
00106 
00107         char **keys;
00108         int keylen = -1;
00109 
00110         map_t *entry, *tmp_entry;
00111         HASH_ITER(hh, map, entry, tmp_entry) {
00112 
00113                 keylen = strlen(entry->key);
00114                 *keys = (char *) malloc(keylen);
00115                 // Error handling here
00116                 strncpy(*keys, entry->key, keylen);
00117                 keys[keylen] = '\0';
00118                 (*keys)++;
00119         }
00120 
00121         return keys;
00122 }
00123 
00124 void clear_properties(map_t *map) {
00125         map_t *entry, *tmp_entry;
00126 
00127         HASH_ITER(hh, map, entry, tmp_entry) {
00128                 HASH_DEL(map,entry); /* delete it */
00129                 free(entry->val); /* free the dynamically sized value first */
00130                 free(entry); /* free it */
00131         }
00132 }
00133 
00134 map_t *load_properties(const char *filename) {
00135 
00136         map_t *map = NULL;
00137         FILE *fh;
00138         char line[MAX_PROPERTY_LINE_LENGTH];
00139         //printf("Reading properties from file %s\n", filename);
00140 
00141         if ((fh = fopen(filename, "r")) != 0) {
00142                 while (getline(line, MAX_PROPERTY_LINE_LENGTH, fh) != NULL) {
00143                         //printf("Read properties file line %s\n", line);
00144 
00145                         char *colon;
00146 
00147                         if ((line[0] == '#') || (line[0] == '\0')) {
00148                                 continue;
00149                         }
00150                         colon = strchr(line, ':');
00151                         if (colon != NULL) {
00152                                 *colon = '\0';
00153                         }
00154 
00155                         map_t *entry;
00156                         if ((entry = (map_t *) malloc(sizeof(map_t))) == 0) {
00157                                 (void) fprintf(stderr, "malloc failure in %s\n", __func__);
00158                                 exit(EXIT_FAILURE);
00159                         } else {
00160                                 strcpy(entry->key, line);
00161                                 //strcpy(entry->val, colon + 1);
00162                                 if ((entry->val = strdup(colon + 1)) == 0) {
00163                                         (void) fprintf(stderr, "strdup failure in %s\n", __func__);
00164                                         exit(EXIT_FAILURE);
00165                                 }
00166 
00167                                 HASH_ADD(hh, map, key, strlen(entry->key), entry);
00168                         }
00169 
00170                 }
00171                 fclose(fh);
00172         }
00173         //printf("Finished reading properties from file\n");
00174 
00175         return map;
00176 }
00177 
00178 void store_properties(map_t *map, const char *filename) {
00179 
00180         FILE *fh;
00181         char line[MAX_PROPERTY_LINE_LENGTH];
00182         //printf("Writing properties to file %s\n", filename);
00183         //printf("Number of properties in map at %p is %d\n", map, HASH_CNT(hh, map));
00184 
00185         if ((fh = fopen(filename, "w+")) != 0) {
00186                 map_t *entry, *tmp_entry;
00187 
00188                 HASH_ITER(hh, map, entry, tmp_entry) {
00189                         if (snprintf(line, MAX_PROPERTY_LINE_LENGTH, "%s:%s\n", entry->key, entry->val)
00190                                         == -1) {
00191                                 (void) fprintf(stderr, "property line too long in %s\n",
00192                                                 __func__);
00193                         } else {
00194                                 //printf("Writing properties file line %s\n", line);
00195                                 putline(line, fh);
00196                         }
00197                 }
00198                 fflush(fh);
00199                 fclose(fh);
00200         }
00201         //printf("Finished writing properties to file\n");
00202 }
00203 
 All Data Structures Files Functions Variables Typedefs Defines