summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@sun.com>2009-09-22 13:42:31 -0700
committerAlan Coopersmith <alan.coopersmith@sun.com>2009-09-22 13:42:31 -0700
commit748cfcc771c7f599d8087c7aa044bc5ff770da1d (patch)
tree01a106822252cbde2a4ddf2c170008706116c1b4
parent619af82effb9125c904d34c9fbc4bf93385d7b9e (diff)
Make parent authdir if needed at startup
Allows setting authdir to /var/run/xdm Also refactor directory creation code for less duplication Signed-off-by: Alan Coopersmith <alan.coopersmith@sun.com>
-rw-r--r--auth.c68
1 files changed, 47 insertions, 21 deletions
diff --git a/auth.c b/auth.c
index a2cd9d6..d7cb30b 100644
--- a/auth.c
+++ b/auth.c
@@ -270,6 +270,38 @@ CleanUpFileName (char *src, char *dst, int len)
*dst = '\0';
}
+/* Checks to see if specified directory exists, makes it if not
+ * Returns: 0 if already exists, 1 if created, < 0 if error occured
+ */
+static int
+CheckServerAuthDir (const char *path, struct stat *statb, int mode)
+{
+ int r = stat(path, statb);
+
+ if (r != 0) {
+ if (errno == ENOENT) {
+ r = mkdir(path, mode);
+ if (r < 0) {
+ LogError ("cannot make authentication directory %s: %s\n",
+ path, _SysErrorMsg (errno));
+ } else {
+ r = 1;
+ }
+ } else {
+ LogError ("cannot access authentication directory %s: %s\n",
+ path, _SysErrorMsg (errno));
+ }
+ } else { /* Directory already exists */
+ if (!S_ISDIR(statb->st_mode)) {
+ LogError ("cannot make authentication directory %s: %s\n",
+ path, "file with that name already exists");
+ return -1;
+ }
+ }
+
+ return r;
+}
+
static char authdir1[] = "authdir";
static char authdir2[] = "authfiles";
@@ -298,6 +330,13 @@ MakeServerAuthFile (struct display *d, FILE ** file)
return FALSE;
} else {
CleanUpFileName (d->name, cleanname, NAMELEN - 8);
+
+ /* Make authDir if it doesn't already exist */
+ r = CheckServerAuthDir(authDir, &statb, 0755);
+ if (r < 0) {
+ return FALSE;
+ }
+
len = strlen (authDir) + strlen (authdir1) + strlen (authdir2)
+ strlen (cleanname) + 14;
d->authFile = malloc (len);
@@ -305,35 +344,22 @@ MakeServerAuthFile (struct display *d, FILE ** file)
return FALSE;
snprintf (d->authFile, len, "%s/%s", authDir, authdir1);
- r = stat(d->authFile, &statb);
+ r = CheckServerAuthDir(d->authFile, &statb, 0700);
if (r == 0) {
if (statb.st_uid != 0)
(void) chown(d->authFile, 0, statb.st_gid);
if ((statb.st_mode & 0077) != 0)
(void) chmod(d->authFile, statb.st_mode & 0700);
- } else {
- if (errno == ENOENT) {
- r = mkdir(d->authFile, 0700);
- if (r < 0) {
- LogError ("cannot make authentication directory %s: "
- "%s\n", d->authFile, _SysErrorMsg (errno));
- }
- } else {
- LogError ("cannot access authentication directory %s: "
- "%s\n", d->authFile, _SysErrorMsg (errno));
- }
- if (r < 0) {
- free (d->authFile);
- d->authFile = NULL;
- return FALSE;
- }
+ } else if (r < 0) {
+ free (d->authFile);
+ d->authFile = NULL;
+ return FALSE;
}
+
snprintf (d->authFile, len, "%s/%s/%s",
authDir, authdir1, authdir2);
- r = mkdir(d->authFile, 0700);
- if (r < 0 && errno != EEXIST) {
- LogError ("cannot make authentication directory %s: %s\n",
- d->authFile, _SysErrorMsg (errno));
+ r = CheckServerAuthDir(d->authFile, &statb, 0700);
+ if (r < 0) {
free (d->authFile);
d->authFile = NULL;
return FALSE;