+struct itkacl_config {
+ char nszone[256];
+};
+
+#define CONFIG_FILENAME "/etc/itkacl.conf"
+
+static int itkacl_read_config(const char * const filename,
+ struct itkacl_config *config,
+ char *errmsg, size_t errmsg_size)
+{
+ FILE *fp;
+ int lineno = 0;
+
+ strcpy(config->nszone, "");
+
+ fp = fopen(CONFIG_FILENAME, "r");
+ if (fp == NULL) {
+ if (errmsg)
+ snprintf(errmsg, errmsg_size, "%s: %s",
+ CONFIG_FILENAME, strerror(errno));
+ return -1;
+ }
+
+ while (!feof(fp)) {
+ char line[256], arg[256], *ptr;
+
+ if (fgets(line, sizeof(line), fp) == NULL) {
+ break;
+ }
+ ++lineno;
+
+ /* Remove trailing newlines and then comments. */
+ ptr = strchr(line, '\n');
+ if (ptr != NULL)
+ *ptr = 0;
+
+ ptr = strchr(line, '\r');
+ if (ptr != NULL)
+ *ptr = 0;
+
+ ptr = strchr(line, '#');
+ if (ptr != NULL)
+ *ptr = 0;
+
+ /* Remove trailing whitespace, if any. */
+ ptr = line + strlen(line) - 1;
+ while (ptr >= line && isspace(*ptr))
+ *ptr-- = 0;
+
+ /* Skip lines that now ended up blank. */
+ if (line[0] == 0)
+ continue;
+
+ if (sscanf(line, "zone %255s", arg) == 1) {
+ strcpy(config->nszone, arg);
+ continue;
+ }
+
+ if (errmsg)
+ snprintf(errmsg, errmsg_size, "%s: Could not parse line %d",
+ CONFIG_FILENAME, lineno);
+ fclose(fp);
+ return -1;
+ }
+
+ if (strlen(config->nszone) == 0) {
+ if (errmsg)
+ snprintf(errmsg, errmsg_size, "%s: Missing 'zone' directive",
+ CONFIG_FILENAME);
+ fclose(fp);
+ return -1;
+ }
+
+ fclose(fp);
+ return 0;
+}