~m-grant-prg/swoc/stretch

« back to all changes in this revision

Viewing changes to src/prg/c/src/com-lib/libswoccommon/validateconfig.c

  • Committer: Mark Grant
  • Date: 2019-11-11 10:07:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: m.grant.prg@gmail.com-20191111100741-jox2t0boj7bsvd8k
Merge new upstream development release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 * Released under the GPLv3 only.\n
11
11
 * SPDX-License-Identifier: GPL-3.0
12
12
 *
13
 
 * @version _v1.0.4 ==== 18/05/2019_
 
13
 * @version _v1.0.5 ==== 01/06/2019_
14
14
 */
15
15
 
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.       *
27
28
 *                                                                      *
28
29
 ************************************************************************
29
30
 */
30
31
 
31
 
 
 
32
#include <ctype.h>
 
33
#include <errno.h>
 
34
#include <limits.h>
 
35
#include <stdarg.h>
32
36
#include <stdio.h>
33
37
#include <stdlib.h>
 
38
#include <string.h>
34
39
#include <sys/stat.h>
35
 
#include <stdarg.h>
36
 
#include <string.h>
37
40
#include <syslog.h>
38
 
#include <ctype.h>
39
 
#include <limits.h>
40
 
#include <errno.h>
41
41
 
42
42
#include <configmake.h>
43
43
 
44
 
#include <mge-errno.h>
45
44
#include <configfile.h>
46
45
#include <libswoccommon.h>
47
 
 
 
46
#include <mge-errno.h>
48
47
 
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);
56
55
 
57
 
 
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. */
64
 
 
 
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. */
65
62
 
66
63
/**
67
64
 * Parse and validate the config file.
72
69
{
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;
77
74
 
78
75
        /* Set up config file parameters. */
79
76
        int nsections = 3;
80
 
        psections = malloc((sizeof(struct confsection)) * nsections);
 
77
        psections = malloc((sizeof(struct confsection)) * (size_t)nsections);
81
78
        if (psections == NULL) {
82
79
                sav_errno = errno;
83
80
                mge_errno = MGE_ERRNO;
84
81
                return mge_errno;
85
82
        }
86
83
 
87
 
        psections[0] = (struct confsection) {"General", 1, 0, {
88
 
                        {"pollint", 1, 0, ""},
89
 
                        {"ssh", 1, 0, ""}
90
 
                        }};
91
 
 
92
 
        psections[1] = (struct confsection) {"Server", 1, 0, {
93
 
                        {"server", 1, 0, ""},
94
 
                        {"srvportno", 1, 0, ""}
95
 
                        }};
96
 
 
97
 
        psections[2] = (struct confsection) {"SSH", 1, 0, {
98
 
                        {"sshportno", 1, 0, ""},
99
 
                        {"sshuser", 1, 0, ""}
100
 
                        }};
 
84
        psections[0] = (struct confsection){ "General",
 
85
                                             1,
 
86
                                             0,
 
87
                                             { { "pollint", 1, 0, "" },
 
88
                                               { "ssh", 1, 0, "" } } };
 
89
 
 
90
        psections[1] = (struct confsection){ "Server",
 
91
                                             1,
 
92
                                             0,
 
93
                                             { { "server", 1, 0, "" },
 
94
                                               { "srvportno", 1, 0, "" } } };
 
95
 
 
96
        psections[2] = (struct confsection){ "SSH",
 
97
                                             1,
 
98
                                             0,
 
99
                                             { { "sshportno", 1, 0, "" },
 
100
                                               { "sshuser", 1, 0, "" } } };
101
101
 
102
102
        /* Parse config file. */
103
103
        swscom_err = parsefile(psections, nsections, configfile);
140
140
{
141
141
        size_t x = 0;
142
142
 
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))
145
145
                goto poll_error;
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)))
148
148
                x++;
149
149
        if (x != strlen(ps->keys[0].value))
150
150
                goto poll_error;
155
155
 
156
156
poll_error:
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",
160
 
                ps->keys[0].value);
 
158
        syslog((int)(LOG_USER | LOG_NOTICE),
 
159
               "Config param pollint does not "
 
160
               "contain a valid polling interval - %s",
 
161
               ps->keys[0].value);
161
162
        return mge_errno;
162
163
}
163
164
 
169
170
        size_t x = 0;
170
171
        char tmpanswer[MAX_KEYVAL_LENGTH] = { '\0' };
171
172
 
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)))
174
175
                x++;
175
176
        if (strcmp(tmpanswer, "yes") && strcmp(tmpanswer, "no"))
176
177
                goto ssh_error;
182
183
 
183
184
ssh_error:
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",
 
189
               ps->keys[1].value);
187
190
        return mge_errno;
188
191
}
189
192
 
192
195
 */
193
196
static int validateserver(const struct confsection *ps)
194
197
{
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;
201
205
        }
202
206
        strcpy(server, (ps + 1)->keys[0].value);
210
214
{
211
215
        size_t x = 0;
212
216
 
213
 
        if (strlen((ps + 1)->keys[1].value) != (size_t) 5)
 
217
        if (strlen((ps + 1)->keys[1].value) != (size_t)5)
214
218
                goto port_error;
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)))
217
221
                x++;
218
222
        if (x != strlen((ps + 1)->keys[1].value))
219
223
                goto port_error;
224
228
 
225
229
port_error:
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;
230
236
}
231
237
 
236
242
{
237
243
        size_t x = 0;
238
244
 
239
 
        if (strlen((ps + 2)->keys[0].value) != (size_t) 5)
 
245
        if (strlen((ps + 2)->keys[0].value) != (size_t)5)
240
246
                goto port_error;
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)))
243
249
                x++;
244
250
        if (x != strlen((ps + 2)->keys[0].value))
245
251
                goto port_error;
246
252
        sshportno = atoi((ps + 2)->keys[0].value);
247
253
        if ((sshportno < 49152) || (sshportno > 65535)
248
 
                || (sshportno == srvportno))
 
254
            || (sshportno == srvportno))
249
255
                goto port_error;
250
256
        return 0;
251
257
 
252
258
port_error:
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;
257
265
}
258
266
 
261
269
 */
262
270
static int validatesshuser(const struct confsection *ps)
263
271
{
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;
270
279
        }
271
280
        strcpy(sshuser, (ps + 2)->keys[1].value);