Determine snapshot date from commit date, simplify
[usmb.git] / conffile.c
blob10b0c6b78dd373b3cbe82fc22b6bfbee3b7de0a1
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2010 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "config.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "utils.h"
28 #include "xml.h"
29 #include "conffile.h"
30 #include "config.rng.h"
33 static bool check_conf_perms (const char *conffile, int fd)
35 struct stat buf;
37 if (0 == fstat (fd, &buf))
39 if (getuid() != buf.st_uid)
41 fprintf (stderr, "You do not own the configuration file %s.\n", conffile);
42 return false;
45 if (buf.st_mode & (S_IRWXG | S_IRWXO))
47 fprintf (stderr, "Configuration file %s is accessible to non-owner.\n",
48 conffile);
49 return false;
53 else
55 fprintf (stderr, "Cannot stat configuration file %s: %s.\n",
56 conffile, strerror (errno));
57 return false;
60 return true;
64 static bool conffile_read (const char *filename,
65 xmlDocPtr *doc,
66 xmlXPathContextPtr *ctx)
68 int fd = open (filename, O_RDONLY);
69 if (-1 == fd)
71 fprintf (stderr, "Cannot open %s: %s.\n", filename, strerror (errno));
72 return false;
75 if (!check_conf_perms (filename, fd))
77 (void)close (fd);
78 return false;
81 *doc = xmlReadFd (fd, NULL, NULL, XML_PARSE_NONET);
82 (void)close (fd);
84 if (NULL == *doc)
86 fprintf (stderr, "Cannot parse %s.\n", filename);
87 return false;
90 if (!xml_validate_relaxng (*doc, rng_config))
92 fprintf (stderr, "%s isn't a valid USMB configuration.\n", filename);
93 xmlFreeDoc (*doc);
94 return false;
97 *ctx = xmlXPathNewContext (*doc);
98 if (NULL == *ctx)
100 fputs ("Cannot create XPath context.\n", stderr);
101 xmlFreeDoc (*doc);
102 return false;
105 return true;
109 static bool do_xpath_text (xmlXPathContextPtr ctx,
110 const char *parent, const char *id,
111 const char *child, char **out)
113 char xpath[2048];
114 snprintf (xpath, sizeof (xpath),
115 "/usmbconfig/%s[@id='%s']/%s/text()", parent, id, child);
116 return xml_xpath_text (ctx, xpath, (void *)out);
120 bool conffile_get_mount (const char *filename, const char *key,
121 char **server, char **share,
122 char **mountpoint, char **options,
123 char **domain, char **username,
124 char **password)
126 xmlDocPtr doc;
127 xmlXPathContextPtr ctx;
128 char xp[2048];
129 char *creds = NULL;
131 *server = *share = *mountpoint = *options = NULL;
132 *domain = *username = *password = NULL;
134 if (strchr (key, '\''))
136 fprintf (stderr, "Invalid share name: %s.\n", key);
137 return false;
140 if (!conffile_read (filename, &doc, &ctx))
141 return false;
143 do {
144 if (!do_xpath_text (ctx, "mount", key, "server", server)) break;
145 if (!do_xpath_text (ctx, "mount", key, "share", share)) break;
146 if (!do_xpath_text (ctx, "mount", key, "mountpoint", mountpoint)) break;
147 (void)do_xpath_text (ctx, "mount", key, "options", options);
149 snprintf (xp, sizeof (xp), "/usmbconfig/mount[@id='%s']", key);
150 if (!xml_xpath_attr_value (ctx, xp, "credentials", &creds)) break;
152 (void)do_xpath_text (ctx, "credentials", creds, "domain", domain);
153 if (!do_xpath_text (ctx, "credentials", creds, "username", username)) break;
155 if (!do_xpath_text (ctx, "credentials", creds, "password", password))
156 *password = NULL;
158 xmlXPathFreeContext (ctx);
159 xmlFreeDoc (doc);
161 return true;
162 /*NOTREACHED*/
163 } while (false /*CONSTCOND*/);
165 fputs ("Invalid configuration.\n", stderr);
167 xfree (*username);
168 clear_and_free (*password);
169 xfree (*domain);
170 xfree (creds);
171 xfree (*options);
172 xfree (*mountpoint);
173 xfree (*share);
174 xfree (*server);
176 xmlXPathFreeContext (ctx);
177 xmlFreeDoc (doc);
179 return false;