]> pilppa.org Git - libplp.git/blob - src/config.c
memory leak fixes
[libplp.git] / src / config.c
1 #include <string.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <uci.h>
6
7 #include "log.h"
8 #include "config.h"
9
10 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
11 {
12         struct uci_ptr          ptr;
13         int                     ret_val;
14         char                    *cmd_data;
15         int                     len;
16
17         ret_val = -1;
18         if ((ctx != NULL) &&
19             (conf_file_name != NULL) &&
20             (section_type != NULL) &&
21             (section_name != NULL)) {
22                 len             = strlen(conf_file_name);
23                 len             = len + 1;
24                 len             = len + strlen(section_type);
25                 len             = len + 1;
26                 len             = len + strlen(section_name);
27                 len             = len + 1;
28                 cmd_data        = malloc(len);
29                 if (cmd_data != NULL) {
30                         snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
31                         if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
32                                 ret_val = uci_set(ctx, &ptr);
33                                 if (ret_val == UCI_OK) {
34                                         //ret_val       = uci_save(ctx, ptr.p);
35                                 }
36                         }
37                         free(cmd_data);
38                 }
39         }
40         return ret_val;
41 }
42
43 bool set_config_value(const char *conf_dir_name,
44                         const char *conf_file_name,
45                         const char *section_type,
46                         const char *section_name,
47                         const char *key,
48                         const char *value) {
49         struct uci_context      *ctx;
50         struct uci_package      *pkg;
51         struct uci_section      *sct;
52         struct uci_section      *tmp_sct;
53         struct uci_option       *opt;
54         int                     err_flg;
55         char                    *fname;
56         int                     b_count;
57         bool                    save;
58         struct uci_element      *elem;
59         struct uci_ptr          ptr;
60         FILE                    *fp;
61         bool                    ret_val;
62
63         ret_val = false;
64         save    = false;
65         if ((conf_dir_name != NULL) &&
66             (conf_file_name != NULL) &&
67             (section_type != NULL) &&
68             (section_name != NULL) &&
69             (key != NULL) &&
70             (value != NULL)) {
71                 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
72                 fname   = (char *)calloc(1, b_count);
73                 if (fname != NULL) {
74                         strncpy(fname, conf_dir_name, b_count);
75                         strncat(fname, "/", 1);
76                         strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
77                         ctx     = uci_alloc_context();
78                         if (ctx != NULL) {
79                                 sct     = NULL;
80                                 uci_set_confdir(ctx, conf_dir_name);
81                                 if (access(fname, W_OK) != 0) {
82                                         if (access(fname, F_OK) != 0) {
83                                                 fp      = fopen(fname, "w+");
84                                                 fclose(fp);
85                                         }
86                                 }
87                                 if (access(fname, W_OK) == 0) {
88                                         err_flg = uci_load(ctx, fname, &pkg);
89                                         uci_foreach_element(&pkg->sections, elem) {
90                                                 tmp_sct = uci_to_section(elem);
91                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
92                                                         sct     = tmp_sct;
93                                                         break;
94                                                 }
95                                         }
96                                         if (sct == NULL) {
97                                                 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
98                                                 //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
99                                                 //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
100                                                 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
101                                                 if (err_flg == UCI_OK) {
102                                                         uci_foreach_element(&pkg->sections, elem) {
103                                                                 tmp_sct = uci_to_section(elem);
104                                                                 if (strcmp(tmp_sct->type, section_type) == 0) {
105                                                                         sct     = tmp_sct;
106                                                                         break;
107                                                                 }
108                                                         }
109                                                 }
110                                         }
111                                         if (err_flg == 0) {
112                                                 opt     = uci_lookup_option(ctx, sct, key);
113                                                 if (opt != NULL) {
114                                                         memset(&ptr, 0, sizeof(ptr));
115                                                         ptr.package     = pkg->e.name;
116                                                         ptr.section     = sct->e.name;
117                                                         ptr.option      = key;
118
119                                                         if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
120                                                                 ptr.value       = value;
121                                                                 uci_set(ctx, &ptr);
122                                                                 save    = true;
123                                                         }
124                                                 }
125                                                 else {
126                                                         opt     = uci_alloc_option(sct, key, value);
127                                                         save    = true;
128                                                 }
129                                                 if (save == true) {
130                                                         uci_save(ctx, pkg);
131                                                 }
132                                                 uci_free_context(ctx);
133                                                 ret_val = true;
134                                         }
135                                         else {
136                                                 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
137                                         }
138                                 }
139                                 else {
140                                         log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
141                                 }
142                         }
143                         free(fname);
144                 }
145                 else {
146                         log_error("Could not change config value, out of memory");
147                 }
148         }
149         else {
150                 log_error("Could not change config value, invalid parameters");
151         }
152         return ret_val;
153 }