1 /***************************************************************************
2 * Copyright (C) 2007 International Business Machines Corp. *
3 * All Rights Reserved. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 * Klaus Heinrich Kiwi <klausk@br.ibm.com> *
22 * based on code by Steve Grubb <sgrubb@redhat.com> *
23 ***************************************************************************/
25 #include "zos-remote-config.h"
35 #include "zos-remote-log.h"
37 /* Local prototypes */
48 int (*parser) (struct nv_pair *, int, plugin_conf_t *);
58 static char *get_line(FILE *, char *);
59 static int nv_split(char *, struct nv_pair *);
60 static const struct kw_pair *kw_lookup(const char *);
61 static int server_parser(struct nv_pair *, int, plugin_conf_t *);
62 static int port_parser(struct nv_pair *, int, plugin_conf_t *);
63 static int timeout_parser(struct nv_pair *, int, plugin_conf_t *);
64 static int user_parser(struct nv_pair *, int, plugin_conf_t *);
65 static int password_parser(struct nv_pair *, int, plugin_conf_t *);
66 static int q_depth_parser(struct nv_pair *, int, plugin_conf_t *);
67 static int sanity_check(plugin_conf_t *, const char *);
69 static const struct kw_pair keywords[] = {
70 {"server", server_parser, 0},
71 {"port", port_parser, 0},
72 {"timeout", timeout_parser, 0},
73 {"user", user_parser, 0},
74 {"password", password_parser, 0},
75 {"q_depth", q_depth_parser, 0},
82 * Set everything to its default value
84 void plugin_clear_config(plugin_conf_t * c)
92 /* not re-setting counter */
95 int plugin_load_config(plugin_conf_t * c, const char *file)
97 int fd, rc, mode, lineno = 1;
102 plugin_clear_config(c);
106 rc = open(file, mode);
108 if (errno != ENOENT) {
109 log_err("Error opening %s (%s)", file,
113 log_warn("Config file %s doesn't exist, skipping", file);
118 /* check the file's permissions: owned by root, not world anything,
121 if (fstat(fd, &st) < 0) {
122 log_err("Error fstat'ing config file (%s)",
127 if (st.st_uid != 0) {
128 log_err("Error - %s isn't owned by root", file);
132 if ((st.st_mode & (S_IRUSR | S_IWUSR | S_IRGRP)) !=
133 (S_IRUSR | S_IWUSR | S_IRGRP)) {
134 log_err("%s permissions should be 0640", file);
138 if (!S_ISREG(st.st_mode)) {
139 log_err("Error - %s is not a regular file", file);
144 /* it's ok, read line by line */
147 log_err("Error - fdopen failed (%s)", strerror(errno));
152 while (get_line(f, buf)) {
153 /* convert line into name-value pair */
154 const struct kw_pair *kw;
157 rc = nv_split(buf, &nv);
161 case 1: /* not the right number of tokens. */
162 log_err("Wrong number of arguments for line %d in %s", lineno, file);
164 case 2: /* no '=' sign */
165 log_err("Missing equal sign for line %d in %s",
168 default: /* something else went wrong... */
169 log_err("Unknown error for line %d in %s",
173 if (nv.name == NULL) {
177 if (nv.value == NULL) {
182 /* identify keyword or error */
183 kw = kw_lookup(nv.name);
184 if (kw->name == NULL) {
185 log_err("Unknown keyword \"%s\" in line %d of %s",
186 nv.name, lineno, file);
191 /* Check number of options */
192 if (kw->max_options == 0 && nv.option != NULL) {
193 log_err("Keyword \"%s\" has invalid option "
194 "\"%s\" in line %d of %s",
195 nv.name, nv.option, lineno, file);
200 /* dispatch to keyword's local parser */
201 rc = kw->parser(&nv, lineno, c);
204 return 1; /* local parser puts message out */
211 c->name = strdup(basename(file));
213 return sanity_check(c, file);
217 static char *get_line(FILE * f, char *buf)
219 if (fgets_unlocked(buf, 128, f)) {
221 char *ptr = strchr(buf, 0x0a);
230 static int nv_split(char *buf, struct nv_pair *nv)
232 /* Get the name part */
233 char *ptr, *saved = NULL;
238 ptr = strtok_r(buf, " ", &saved);
240 return 0; /* If there's nothing, go to next line */
242 return 0; /* If there's a comment, go to next line */
245 /* Check for a '=' */
246 ptr = strtok_r(NULL, " ", &saved);
249 if (strcmp(ptr, "=") != 0)
253 ptr = strtok_r(NULL, " ", &saved);
258 /* See if there's an option */
259 ptr = strtok_r(NULL, " ", &saved);
263 /* Make sure there's nothing else */
264 ptr = strtok_r(NULL, " ", &saved);
269 /* Everything is OK */
273 static const struct kw_pair *kw_lookup(const char *val)
277 while (keywords[i].name != NULL) {
278 if (strcasecmp(keywords[i].name, val) == 0)
286 static int server_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
289 if (nv->value == NULL)
292 c->server = strdup(nv->value);
297 static int port_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
299 const char *ptr = nv->value;
302 /* check that all chars are numbers */
303 for (i = 0; ptr[i]; i++) {
304 if (!isdigit(ptr[i])) {
305 log_err("Value %s should only be numbers - line %d", nv->value, line);
310 /* convert to unsigned long */
312 i = strtoul(nv->value, NULL, 10);
314 log_err("Error converting string to a number (%s) - line %d", strerror(errno), line);
323 static int timeout_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
325 const char *ptr = nv->value;
328 /* check that all chars are numbers */
329 for (i = 0; ptr[i]; i++) {
330 if (!isdigit(ptr[i])) {
331 log_err("Value %s should only be numbers - line %d", nv->value, line);
336 /* convert to unsigned long */
338 i = strtoul(nv->value, NULL, 10);
340 log_err("Error converting string to a number (%s) - line %d", strerror(errno), line);
350 static int user_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
353 if (nv->value == NULL)
356 c->user = strdup(nv->value);
361 static int password_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
364 if (nv->value == NULL)
367 c->password = strdup(nv->value);
372 static int q_depth_parser(struct nv_pair *nv, int line, plugin_conf_t * c)
374 const char *ptr = nv->value;
377 /* check that all chars are numbers */
378 for (i = 0; ptr[i]; i++) {
379 if (!isdigit(ptr[i])) {
380 log_err("Value %s should only be numbers - line %d", nv->value, line);
385 /* convert to unsigned long */
387 i = strtoul(nv->value, NULL, 10);
389 log_err("Error converting string to a number (%s) - line %d", strerror(errno), line);
393 if (i < 16 || i > 99999) {
394 log_err("q_depth must be between 16 and 99999");
405 * Check configuration.At this point, all fields have been read.
406 * Returns 0 if no problems and 1 if problems detected.
408 static int sanity_check(plugin_conf_t * c, const char *file)
412 log_err("Error - no server hostname given");
417 log_err("Error - no bind user given");
422 log_err("Error - no password given");
427 log_err("Error - timeout can't be zero");
433 void plugin_free_config(plugin_conf_t * c)
439 free((void *) c->server);
440 free((void *) c->user);
441 free((void *) c->password);
442 free((void *) c->name);