1 /*
2 * Copyright (c) 2019 Balabit
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * As an additional exemption you are allowed to compile & link against the
19 * OpenSSL libraries as published by the OpenSSL project. See the file
20 * COPYING for details.
21 *
22 */
23
24 #include "commands.h"
25 #include "control-client.h"
26
27 #include <string.h>
28 #include <stdio.h>
29
30 static ControlClient *control_client;
31
32 GOptionEntry no_options[] =
33 {
34 { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL }
35 };
36
37 gboolean
is_syslog_ng_running(void)38 is_syslog_ng_running(void)
39 {
40 return control_client_connect(control_client);
41 }
42
43 gint
process_response_status(GString * response)44 process_response_status(GString *response)
45 {
46 if (strncmp(response->str, "FAIL ", 5) == 0)
47 {
48 g_string_erase(response, 0, 5);
49 return 1;
50 }
51 else if (strncmp(response->str, "OK ", 3) == 0)
52 {
53 g_string_erase(response, 0, 3);
54 return 0;
55 }
56 return 0;
57 }
58
59 static gboolean
slng_send_cmd(const gchar * cmd)60 slng_send_cmd(const gchar *cmd)
61 {
62 if (!control_client_connect(control_client))
63 {
64 return FALSE;
65 }
66
67 if (control_client_send_command(control_client, cmd) < 0)
68 {
69 return FALSE;
70 }
71
72 return TRUE;
73 }
74
75 GString *
slng_run_command(const gchar * command)76 slng_run_command(const gchar *command)
77 {
78 if (!slng_send_cmd(command))
79 return NULL;
80
81 return control_client_read_reply(control_client);
82 }
83
84 static gboolean
_is_response_empty(GString * response)85 _is_response_empty(GString *response)
86 {
87 return (response == NULL || g_str_equal(response->str, ""));
88 }
89
90 static void
clear_and_free(GString * str)91 clear_and_free(GString *str)
92 {
93 if (str)
94 {
95 memset(str->str, 0, str->len);
96 g_string_free(str, TRUE);
97 }
98 }
99
100 gint
dispatch_command(const gchar * cmd)101 dispatch_command(const gchar *cmd)
102 {
103 gint retval = 0;
104 gchar *dispatchable_command = g_strdup_printf("%s\n", cmd);
105 GString *rsp = slng_run_command(dispatchable_command);
106
107 if (_is_response_empty(rsp))
108 {
109 retval = 1;
110 }
111 else
112 {
113 retval = process_response_status(rsp);
114 printf("%s\n", rsp->str);
115 }
116
117 clear_and_free(rsp);
118
119 secret_storage_wipe(dispatchable_command, strlen(dispatchable_command));
120 g_free(dispatchable_command);
121
122 return retval;
123 }
124
125 gint
run(const gchar * control_name,gint argc,gchar ** argv,CommandDescriptor * mode,GOptionContext * ctx)126 run(const gchar *control_name, gint argc, gchar **argv, CommandDescriptor *mode, GOptionContext *ctx)
127 {
128 control_client = control_client_new(control_name);
129 gint result = mode->main(argc, argv, mode->mode, ctx);
130 control_client_free(control_client);
131 return result;
132 }
133