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