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/>.
23 #include <sys/types.h>
30 #include "config.rng.h"
33 static bool check_conf_perms (const char *conffile
, int fd
)
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
);
45 if (buf
.st_mode
& (S_IRWXG
| S_IRWXO
))
47 fprintf (stderr
, "Configuration file %s is accessible to non-owner.\n",
55 fprintf (stderr
, "Cannot stat configuration file %s: %s.\n",
56 conffile
, strerror (errno
));
64 static bool conffile_read (const char *filename
,
66 xmlXPathContextPtr
*ctx
)
68 int fd
= open (filename
, O_RDONLY
);
71 fprintf (stderr
, "Cannot open %s: %s.\n", filename
, strerror (errno
));
75 if (!check_conf_perms (filename
, fd
))
81 *doc
= xmlReadFd (fd
, NULL
, NULL
, XML_PARSE_NONET
);
86 fprintf (stderr
, "Cannot parse %s.\n", filename
);
90 if (!xml_validate_relaxng (*doc
, rng_config
))
92 fprintf (stderr
, "%s isn't a valid USMB configuration.\n", filename
);
97 *ctx
= xmlXPathNewContext (*doc
);
100 fputs ("Cannot create XPath context.\n", stderr
);
109 static bool do_xpath_text (xmlXPathContextPtr ctx
,
110 const char *parent
, const char *id
,
111 const char *child
, char **out
)
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
,
127 xmlXPathContextPtr ctx
;
131 *server
= *share
= *mountpoint
= *options
= NULL
;
132 *domain
= *username
= *password
= NULL
;
134 if (strchr (key
, '\''))
136 fprintf (stderr
, "Invalid share name: %s.\n", key
);
140 if (!conffile_read (filename
, &doc
, &ctx
))
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
))
158 xmlXPathFreeContext (ctx
);
163 } while (false /*CONSTCOND*/);
165 fputs ("Invalid configuration.\n", stderr
);
168 clear_and_free (*password
);
176 xmlXPathFreeContext (ctx
);