10
10
* Released under the GPLv3 only.\n
11
11
* SPDX-License-Identifier: GPL-3.0
13
* @version _v1.0.4 ==== 18/05/2019_
13
* @version _v1.0.5 ==== 01/06/2019_
16
16
/* **********************************************************************
24
24
* Add SPDX license tag. *
25
25
* 07/03/2018 MG 1.0.3 Remove redundant global variable portno *
26
26
* 18/05/2019 MG 1.0.4 Merge sub-projects into one. *
27
* 01/06/2019 MG 1.0.5 Trivial type safety improvements. *
28
29
************************************************************************
33
37
#include <stdlib.h>
34
39
#include <sys/stat.h>
37
40
#include <syslog.h>
42
42
#include <configmake.h>
44
#include <mge-errno.h>
45
44
#include <configfile.h>
46
45
#include <libswoccommon.h>
46
#include <mge-errno.h>
49
48
static int validateconfigfileparams(const struct confsection *);
50
49
static int validatepollint(const struct confsection *);
54
53
static int validatesshportno(const struct confsection *);
55
54
static int validatesshuser(const struct confsection *ps);
58
int pollint; /**< Polling interval. */
59
int ssh; /**< Use SSH false == 0, true == 1 */
60
char server[_POSIX_HOST_NAME_MAX]; /**< Server name. */
61
int srvportno; /**< Server port number. */
62
int sshportno; /**< Local port to use if using SSH. */
63
char sshuser[_POSIX_LOGIN_NAME_MAX]; /**< Server username for SSH. */
56
int pollint; /**< Polling interval. */
57
int ssh; /**< Use SSH false == 0, true == 1 */
58
char server[_POSIX_HOST_NAME_MAX]; /**< Server name. */
59
int srvportno; /**< Server port number. */
60
int sshportno; /**< Local port to use if using SSH. */
61
char sshuser[_POSIX_LOGIN_NAME_MAX]; /**< Server username for SSH. */
67
64
* Parse and validate the config file.
73
70
int swscom_err = 0;
74
71
/* Expand config file full path. */
75
char *configfile = SYSCONFDIR"/swoc.conf";
72
char *configfile = SYSCONFDIR "/swoc.conf";
76
73
struct confsection *psections;
78
75
/* Set up config file parameters. */
80
psections = malloc((sizeof(struct confsection)) * nsections);
77
psections = malloc((sizeof(struct confsection)) * (size_t)nsections);
81
78
if (psections == NULL) {
83
80
mge_errno = MGE_ERRNO;
87
psections[0] = (struct confsection) {"General", 1, 0, {
88
{"pollint", 1, 0, ""},
92
psections[1] = (struct confsection) {"Server", 1, 0, {
94
{"srvportno", 1, 0, ""}
97
psections[2] = (struct confsection) {"SSH", 1, 0, {
98
{"sshportno", 1, 0, ""},
84
psections[0] = (struct confsection){ "General",
87
{ { "pollint", 1, 0, "" },
88
{ "ssh", 1, 0, "" } } };
90
psections[1] = (struct confsection){ "Server",
93
{ { "server", 1, 0, "" },
94
{ "srvportno", 1, 0, "" } } };
96
psections[2] = (struct confsection){ "SSH",
99
{ { "sshportno", 1, 0, "" },
100
{ "sshuser", 1, 0, "" } } };
102
102
/* Parse config file. */
103
103
swscom_err = parsefile(psections, nsections, configfile);
143
if ((strlen(ps->keys[0].value) < (size_t) 1) ||
144
(strlen(ps->keys[0].value) > (size_t) 5))
143
if ((strlen(ps->keys[0].value) < (size_t)1)
144
|| (strlen(ps->keys[0].value) > (size_t)5))
146
while ((isdigit(ps->keys[0].value[x])) &&
147
(x < strlen(ps->keys[0].value)))
146
while ((isdigit(ps->keys[0].value[x]))
147
&& (x < strlen(ps->keys[0].value)))
149
149
if (x != strlen(ps->keys[0].value))
157
157
mge_errno = MGE_CONFIG_PARAM;
158
syslog((int) (LOG_USER | LOG_NOTICE), "Config param pollint does not "
159
"contain a valid polling interval - %s",
158
syslog((int)(LOG_USER | LOG_NOTICE),
159
"Config param pollint does not "
160
"contain a valid polling interval - %s",
161
162
return mge_errno;
170
171
char tmpanswer[MAX_KEYVAL_LENGTH] = { '\0' };
172
while ((tmpanswer[x] = tolower(ps->keys[1].value[x]))
173
&& (x < strlen(ps->keys[1].value)))
173
while ((tmpanswer[x] = (char)tolower(ps->keys[1].value[x]))
174
&& (x < strlen(ps->keys[1].value)))
175
176
if (strcmp(tmpanswer, "yes") && strcmp(tmpanswer, "no"))
184
185
mge_errno = MGE_CONFIG_PARAM;
185
syslog((int) (LOG_USER | LOG_NOTICE), "Config param ssh does not "
186
"contain yes or no - %s", ps->keys[1].value);
186
syslog((int)(LOG_USER | LOG_NOTICE),
187
"Config param ssh does not "
188
"contain yes or no - %s",
187
190
return mge_errno;
193
196
static int validateserver(const struct confsection *ps)
195
if ((strlen((ps + 1)->keys[0].value) < (size_t) 1) ||
196
(strlen((ps + 1)->keys[0].value) > sizeof(server))) {
198
if ((strlen((ps + 1)->keys[0].value) < (size_t)1)
199
|| (strlen((ps + 1)->keys[0].value) > sizeof(server))) {
197
200
mge_errno = MGE_CONFIG_PARAM;
198
syslog((int) (LOG_USER | LOG_NOTICE), "Config param server "
199
"does not contain a valid server name.");
201
syslog((int)(LOG_USER | LOG_NOTICE),
202
"Config param server "
203
"does not contain a valid server name.");
200
204
return mge_errno;
202
206
strcpy(server, (ps + 1)->keys[0].value);
213
if (strlen((ps + 1)->keys[1].value) != (size_t) 5)
217
if (strlen((ps + 1)->keys[1].value) != (size_t)5)
215
while ((isdigit((ps + 1)->keys[1].value[x])) &&
216
(x < strlen((ps + 1)->keys[1].value)))
219
while ((isdigit((ps + 1)->keys[1].value[x]))
220
&& (x < strlen((ps + 1)->keys[1].value)))
218
222
if (x != strlen((ps + 1)->keys[1].value))
226
230
mge_errno = MGE_CONFIG_PARAM;
227
syslog((int) (LOG_USER | LOG_NOTICE), "Config param srvportno does not "
228
"contain a valid port number - %s", (ps + 1)->keys[1].value);
231
syslog((int)(LOG_USER | LOG_NOTICE),
232
"Config param srvportno does not "
233
"contain a valid port number - %s",
234
(ps + 1)->keys[1].value);
229
235
return mge_errno;
239
if (strlen((ps + 2)->keys[0].value) != (size_t) 5)
245
if (strlen((ps + 2)->keys[0].value) != (size_t)5)
241
while ((isdigit((ps + 2)->keys[0].value[x])) &&
242
(x < strlen((ps + 2)->keys[0].value)))
247
while ((isdigit((ps + 2)->keys[0].value[x]))
248
&& (x < strlen((ps + 2)->keys[0].value)))
244
250
if (x != strlen((ps + 2)->keys[0].value))
246
252
sshportno = atoi((ps + 2)->keys[0].value);
247
253
if ((sshportno < 49152) || (sshportno > 65535)
248
|| (sshportno == srvportno))
254
|| (sshportno == srvportno))
253
259
mge_errno = MGE_CONFIG_PARAM;
254
syslog((int) (LOG_USER | LOG_NOTICE), "Config param sshportno does not "
255
"contain a valid port number - %s", (ps + 2)->keys[0].value);
260
syslog((int)(LOG_USER | LOG_NOTICE),
261
"Config param sshportno does not "
262
"contain a valid port number - %s",
263
(ps + 2)->keys[0].value);
256
264
return mge_errno;
262
270
static int validatesshuser(const struct confsection *ps)
264
if ((strlen((ps + 2)->keys[1].value) < (size_t) 1) ||
265
(strlen((ps + 2)->keys[1].value) > sizeof(sshuser))) {
272
if ((strlen((ps + 2)->keys[1].value) < (size_t)1)
273
|| (strlen((ps + 2)->keys[1].value) > sizeof(sshuser))) {
266
274
mge_errno = MGE_CONFIG_PARAM;
267
syslog((int) (LOG_USER | LOG_NOTICE), "Config param sshuser "
268
"does not contain a valid user name.");
275
syslog((int)(LOG_USER | LOG_NOTICE),
276
"Config param sshuser "
277
"does not contain a valid user name.");
269
278
return mge_errno;
271
280
strcpy(sshuser, (ps + 2)->keys[1].value);