1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <platform/cbassert.h>
5 
6 #include <libconflate/conflate.h>
7 #include "conflate_internal.h"
8 
9 bool commands_initialized = false;
10 
process_serverlist(void *opaque, conflate_handle_t *handle, const char *cmd, bool direct, kvpair_t *conf, conflate_form_result *r)11 static enum conflate_mgmt_cb_result process_serverlist(void *opaque,
12                                                        conflate_handle_t *handle,
13                                                        const char *cmd,
14                                                        bool direct,
15                                                        kvpair_t *conf,
16                                                        conflate_form_result *r)
17 {
18    (void)opaque;
19    (void)cmd;
20    (void)r;
21 
22     /* If we have "config_is_private" set to "yes" we should only
23        process this if it's direct (i.e. ignore pubsub) */
24     if (!direct) {
25         char *priv = conflate_get_private(handle, "config_is_private",
26                                           handle->conf->save_path);
27 
28         if (priv && strcmp(priv, "yes") == 0) {
29             handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
30                               "Currently using a private config, ignoring update.");
31             return RV_OK;
32         }
33         free(priv);
34     }
35 
36     handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
37                       "Processing a serverlist");
38 
39     /* Persist the config lists */
40     if (!save_kvpairs(handle, conf, handle->conf->save_path)) {
41         handle->conf->log(handle->conf->userdata, LOG_LVL_ERROR,
42                           "Can not save config to %s",
43                           handle->conf->save_path);
44     }
45 
46     /* Send the config to the callback */
47     handle->conf->new_config(handle->conf->userdata, conf);
48 
49     return RV_OK;
50 }
51 
process_set_private(void *opaque, conflate_handle_t *handle, const char *cmd, bool direct, kvpair_t *form, conflate_form_result *r)52 static enum conflate_mgmt_cb_result process_set_private(void *opaque,
53                                                         conflate_handle_t *handle,
54                                                         const char *cmd,
55                                                         bool direct,
56                                                         kvpair_t *form,
57                                                         conflate_form_result *r)
58 {
59     enum conflate_mgmt_cb_result rv = RV_ERROR;
60 
61     char *key = get_simple_kvpair_val(form, "key");
62     char *value = get_simple_kvpair_val(form, "value");
63 
64     (void)opaque;
65     (void)cmd;
66     (void)r;
67 
68     /* Only direct stat requests are handled. */
69     cb_assert(direct);
70 
71     if (key && value) {
72         if (conflate_save_private(handle, key, value,
73                                   handle->conf->save_path)) {
74             rv = RV_OK;
75         }
76     } else {
77         rv = RV_BADARG;
78     }
79 
80     return rv;
81 }
82 
process_get_private(void *opaque, conflate_handle_t *handle, const char *cmd, bool direct, kvpair_t *form, conflate_form_result *r)83 static enum conflate_mgmt_cb_result process_get_private(void *opaque,
84                                                         conflate_handle_t *handle,
85                                                         const char *cmd,
86                                                         bool direct,
87                                                         kvpair_t *form,
88                                                         conflate_form_result *r)
89 {
90     enum conflate_mgmt_cb_result rv = RV_ERROR;
91     char *key = get_simple_kvpair_val(form, "key");
92     (void)opaque;
93     (void)cmd;
94 
95     /* Only direct stat requests are handled. */
96     cb_assert(direct);
97 
98     if (key) {
99         /* Initialize the form so there's always one there */
100         char *value;
101         conflate_init_form(r);
102         value = conflate_get_private(handle, key, handle->conf->save_path);
103         if (value) {
104             conflate_add_field(r, key, value);
105             free(value);
106         }
107 
108         rv = RV_OK;
109     } else {
110         rv = RV_BADARG;
111     }
112 
113     return rv;
114 }
115 
process_delete_private(void *opaque, conflate_handle_t *handle, const char *cmd, bool direct, kvpair_t *form, conflate_form_result *r)116 static enum conflate_mgmt_cb_result process_delete_private(void *opaque,
117                                                            conflate_handle_t *handle,
118                                                            const char *cmd,
119                                                            bool direct,
120                                                            kvpair_t *form,
121                                                            conflate_form_result *r)
122 {
123     enum conflate_mgmt_cb_result rv = RV_ERROR;
124     char *key = get_simple_kvpair_val(form, "key");
125     (void)opaque;
126     (void)cmd;
127     (void)r;
128 
129     /* Only direct stat requests are handled. */
130     cb_assert(direct);
131 
132     if (key) {
133         if (conflate_delete_private(handle, key, handle->conf->save_path)) {
134             rv = RV_OK;
135         }
136     } else {
137         rv = RV_BADARG;
138     }
139 
140     return rv;
141 }
142 
conflate_init_commands(void)143 void conflate_init_commands(void)
144 {
145     if (commands_initialized) {
146         return;
147     }
148 
149     conflate_register_mgmt_cb("set_private",
150                               "Set a private value on the agent.",
151                               process_set_private);
152     conflate_register_mgmt_cb("get_private",
153                               "Get a private value from the agent.",
154                               process_get_private);
155     conflate_register_mgmt_cb("rm_private",
156                               "Delete a private value from the agent.",
157                               process_delete_private);
158 
159     conflate_register_mgmt_cb("serverlist", "Configure a server list.",
160                               process_serverlist);
161 
162     commands_initialized = true;
163 }
164