libnfc  1.7.0-rc7
conf.c
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * Additional contributors of this file:
11  *
12  * This program is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation, either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20  * more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif // HAVE_CONFIG_H
29 
30 #include "conf.h"
31 
32 #ifdef CONFFILES
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <dirent.h>
36 #include <string.h>
37 #include <regex.h>
38 #include <sys/stat.h>
39 
40 #include <nfc/nfc.h>
41 #include "nfc-internal.h"
42 #include "log.h"
43 
44 #define LOG_CATEGORY "libnfc.config"
45 #define LOG_GROUP NFC_LOG_GROUP_CONFIG
46 
47 #ifndef LIBNFC_SYSCONFDIR
48 // If this define does not already exists, we build it using SYSCONFDIR
49 #ifndef SYSCONFDIR
50 #error "SYSCONFDIR is not defined but required."
51 #endif // SYSCONFDIR
52 #define LIBNFC_SYSCONFDIR SYSCONFDIR"/nfc"
53 #endif // LIBNFC_SYSCONFDIR
54 
55 #define LIBNFC_CONFFILE LIBNFC_SYSCONFDIR"/libnfc.conf"
56 #define LIBNFC_DEVICECONFDIR LIBNFC_SYSCONFDIR"/devices.d"
57 
58 static bool
59 conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data, const char *key, const char *value), void *data)
60 {
61  FILE *f = fopen(filename, "r");
62  if (!f) {
63  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unable to open file: %s", filename);
64  return false;
65  }
66  char line[BUFSIZ];
67  const char *str_regex = "^[[:space:]]*([[:alnum:]_.]+)[[:space:]]*=[[:space:]]*(\"(.+)\"|([^[:space:]]+))[[:space:]]*$";
68  regex_t preg;
69  if (regcomp(&preg, str_regex, REG_EXTENDED | REG_NOTEOL) != 0) {
70  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Regular expression used for configuration file parsing is not valid.");
71  fclose(f);
72  return false;
73  }
74  size_t nmatch = preg.re_nsub + 1;
75  regmatch_t *pmatch = malloc(sizeof(*pmatch) * nmatch);
76  if (!pmatch) {
77  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Not enough memory: malloc failed.");
78  regfree(&preg);
79  fclose(f);
80  return false;
81  }
82 
83  int lineno = 0;
84  while (fgets(line, BUFSIZ, f) != NULL) {
85  lineno++;
86  switch (line[0]) {
87  case '#':
88  case '\n':
89  break;
90  default: {
91  int match;
92  if ((match = regexec(&preg, line, nmatch, pmatch, 0)) == 0) {
93  const size_t key_size = pmatch[1].rm_eo - pmatch[1].rm_so;
94  const off_t value_pmatch = pmatch[3].rm_eo != -1 ? 3 : 4;
95  const size_t value_size = pmatch[value_pmatch].rm_eo - pmatch[value_pmatch].rm_so;
96  char key[key_size + 1];
97  char value[value_size + 1];
98  strncpy(key, line + (pmatch[1].rm_so), key_size);
99  key[key_size] = '\0';
100  strncpy(value, line + (pmatch[value_pmatch].rm_so), value_size);
101  value[value_size] = '\0';
102  conf_keyvalue(data, key, value);
103  } else {
104  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Parse error on line #%d: %s", lineno, line);
105  }
106  }
107  break;
108  }
109  }
110 
111  free(pmatch);
112  regfree(&preg);
113  fclose(f);
114  return false;
115 }
116 
117 static void
118 conf_keyvalue_context(void *data, const char *key, const char *value)
119 {
120  nfc_context *context = (nfc_context *)data;
121  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "key: [%s], value: [%s]", key, value);
122  if (strcmp(key, "allow_autoscan") == 0) {
123  string_as_boolean(value, &(context->allow_autoscan));
124  } else if (strcmp(key, "allow_intrusive_scan") == 0) {
125  string_as_boolean(value, &(context->allow_intrusive_scan));
126  } else if (strcmp(key, "log_level") == 0) {
127  context->log_level = atoi(value);
128  } else if (strcmp(key, "device.name") == 0) {
129  if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].name, "") != 0) {
130  if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
131  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
132  return;
133  }
134  context->user_defined_device_count++;
135  }
136  strcpy(context->user_defined_devices[context->user_defined_device_count - 1].name, value);
137  } else if (strcmp(key, "device.connstring") == 0) {
138  if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].connstring, "") != 0) {
139  if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
140  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
141  return;
142  }
143  context->user_defined_device_count++;
144  }
145  strcpy(context->user_defined_devices[context->user_defined_device_count - 1].connstring, value);
146  } else if (strcmp(key, "device.optional") == 0) {
147  if ((context->user_defined_device_count == 0) || context->user_defined_devices[context->user_defined_device_count - 1].optional) {
148  if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
149  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
150  return;
151  }
152  context->user_defined_device_count++;
153  }
154  if ((strcmp(value, "true") == 0) || (strcmp(value, "True") == 0) || (strcmp(value, "1") == 0)) //optional
155  context->user_defined_devices[context->user_defined_device_count - 1].optional = true;
156  } else {
157  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unknown key in config line: %s = %s", key, value);
158  }
159 }
160 
161 static void
162 conf_keyvalue_device(void *data, const char *key, const char *value)
163 {
164  char newkey[BUFSIZ];
165  sprintf(newkey, "device.%s", key);
166  conf_keyvalue_context(data, newkey, value);
167 }
168 
169 static void
170 conf_devices_load(const char *dirname, nfc_context *context)
171 {
172  DIR *d = opendir(dirname);
173  if (!d) {
174  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open directory: %s", dirname);
175  } else {
176  struct dirent *de;
177  struct dirent entry;
178  struct dirent *result;
179  while ((readdir_r(d, &entry, &result) == 0) && (result != NULL)) {
180  de = &entry;
181  if (de->d_name[0] != '.') {
182  const size_t filename_len = strlen(de->d_name);
183  const size_t extension_len = strlen(".conf");
184  if ((filename_len > extension_len) &&
185  (strncmp(".conf", de->d_name + (filename_len - extension_len), extension_len) == 0)) {
186  char filename[BUFSIZ] = LIBNFC_DEVICECONFDIR"/";
187  strcat(filename, de->d_name);
188  struct stat s;
189  if (stat(filename, &s) == -1) {
190  perror("stat");
191  continue;
192  }
193  if (S_ISREG(s.st_mode)) {
194  conf_parse_file(filename, conf_keyvalue_device, context);
195  }
196  }
197  }
198  }
199  closedir(d);
200  }
201 }
202 
203 void
204 conf_load(nfc_context *context)
205 {
206  conf_parse_file(LIBNFC_CONFFILE, conf_keyvalue_context, context);
207  conf_devices_load(LIBNFC_DEVICECONFDIR, context);
208 }
209 
210 #endif // CONFFILES
211