2007-09-19 21:57:37 +05:30
|
|
|
/*
|
2009-04-24 03:01:22 +05:30
|
|
|
checkpath.c
|
|
|
|
Checks for the existance of a file or directory and creates it
|
|
|
|
if necessary. It can also correct its ownership.
|
|
|
|
*/
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2008-01-14 10:35:22 +05:30
|
|
|
/*
|
2009-05-01 19:41:40 +05:30
|
|
|
* Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
|
2007-11-14 20:52:04 +05:30
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-09-19 21:57:37 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-03-16 22:30:56 +05:30
|
|
|
|
2007-09-19 21:57:37 +05:30
|
|
|
#include <errno.h>
|
2007-09-19 22:38:25 +05:30
|
|
|
#include <fcntl.h>
|
2007-09-19 21:57:37 +05:30
|
|
|
#include <getopt.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "builtins.h"
|
2008-01-06 19:17:39 +05:30
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc-misc.h"
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2008-02-12 01:44:09 +05:30
|
|
|
extern const char *applet;
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static int
|
|
|
|
do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
|
2007-09-19 21:57:37 +05:30
|
|
|
{
|
2007-09-19 22:48:43 +05:30
|
|
|
struct stat st;
|
2008-03-16 22:30:56 +05:30
|
|
|
int fd;
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
if (stat(path, &st)) {
|
2007-09-19 22:38:25 +05:30
|
|
|
if (file) {
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("%s: creating file", path);
|
2009-04-24 03:01:22 +05:30
|
|
|
if (!mode)
|
|
|
|
mode = S_IRUSR | S_IWUSR | S_IRGRP |
|
|
|
|
S_IWGRP | S_IROTH;
|
2008-06-17 20:44:33 +05:30
|
|
|
if ((fd = open(path, O_CREAT, mode)) == -1) {
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: open: %s", applet, strerror(errno));
|
|
|
|
return -1;
|
2007-09-19 22:38:25 +05:30
|
|
|
}
|
2008-01-14 10:35:22 +05:30
|
|
|
close (fd);
|
2007-09-19 22:38:25 +05:30
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("%s: creating directory", path);
|
2009-04-24 03:01:22 +05:30
|
|
|
if (!mode)
|
2007-09-19 22:38:25 +05:30
|
|
|
mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
|
2008-03-16 22:30:56 +05:30
|
|
|
if (mkdir(path, mode)) {
|
2009-04-24 03:01:22 +05:30
|
|
|
eerror("%s: mkdir: %s", applet,
|
|
|
|
strerror (errno));
|
2008-03-16 22:30:56 +05:30
|
|
|
return -1;
|
2007-09-19 22:38:25 +05:30
|
|
|
}
|
|
|
|
mode = 0;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
2007-09-19 22:48:43 +05:30
|
|
|
} else {
|
2008-03-16 22:30:56 +05:30
|
|
|
if ((file && S_ISDIR(st.st_mode)) ||
|
2009-04-24 03:01:22 +05:30
|
|
|
(!file && !S_ISDIR(st.st_mode)))
|
2007-09-19 22:48:43 +05:30
|
|
|
{
|
|
|
|
if (file)
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: is a directory", path);
|
2007-09-19 22:48:43 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
eerror("%s: is a file", path);
|
|
|
|
return -1;
|
2007-09-19 22:48:43 +05:30
|
|
|
}
|
2007-09-19 22:38:25 +05:30
|
|
|
}
|
2008-01-11 21:21:40 +05:30
|
|
|
|
2007-09-19 22:48:43 +05:30
|
|
|
if (mode && (st.st_mode & 0777) != mode) {
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("%s: correcting mode", applet);
|
|
|
|
if (chmod(path, mode)) {
|
|
|
|
eerror("%s: chmod: %s", applet, strerror(errno));
|
|
|
|
return -1;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-19 22:48:43 +05:30
|
|
|
if (st.st_uid != uid || st.st_gid != gid) {
|
|
|
|
if (st.st_dev || st.st_ino)
|
2008-03-16 22:30:56 +05:30
|
|
|
einfo("%s: correcting owner", path);
|
|
|
|
if (chown(path, uid, gid)) {
|
|
|
|
eerror("%s: chown: %s", applet, strerror(errno));
|
|
|
|
return -1;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
return 0;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
static int
|
|
|
|
parse_owner(struct passwd **user, struct group **group, const char *owner)
|
2007-09-19 21:57:37 +05:30
|
|
|
{
|
2007-12-19 16:19:03 +05:30
|
|
|
char *u = xstrdup (owner);
|
|
|
|
char *g = strchr (u, ':');
|
|
|
|
int id = 0;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (g)
|
|
|
|
*g++ = '\0';
|
|
|
|
|
|
|
|
if (user && *u) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(u, "%d", &id) == 1)
|
|
|
|
*user = getpwuid((uid_t) id);
|
2008-01-14 10:35:22 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
*user = getpwnam(u);
|
2009-04-24 03:01:22 +05:30
|
|
|
if (*user == NULL)
|
2007-12-19 16:19:03 +05:30
|
|
|
retval = -1;
|
|
|
|
}
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2007-12-19 16:19:03 +05:30
|
|
|
if (group && g && *g) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (sscanf(g, "%d", &id) == 1)
|
|
|
|
*group = getgrgid((gid_t) id);
|
2007-12-19 16:19:03 +05:30
|
|
|
else
|
2008-03-16 22:30:56 +05:30
|
|
|
*group = getgrnam(g);
|
2009-04-24 03:01:22 +05:30
|
|
|
if (*group == NULL)
|
2007-12-19 16:19:03 +05:30
|
|
|
retval = -1;
|
|
|
|
}
|
2007-09-19 21:57:37 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
free(u);
|
|
|
|
return retval;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#include "_usage.h"
|
2008-01-14 11:18:31 +05:30
|
|
|
#define extraopts "path1 path2 ..."
|
2008-01-14 11:02:47 +05:30
|
|
|
#define getoptstring "dfm:o:" getoptstring_COMMON
|
2008-02-02 01:24:46 +05:30
|
|
|
static const struct option longopts[] = {
|
2007-09-19 22:38:25 +05:30
|
|
|
{ "directory", 0, NULL, 'd'},
|
|
|
|
{ "file", 0, NULL, 'f'},
|
2008-01-11 21:21:40 +05:30
|
|
|
{ "mode", 1, NULL, 'm'},
|
2007-12-19 16:19:03 +05:30
|
|
|
{ "owner", 1, NULL, 'o'},
|
2007-09-19 21:57:37 +05:30
|
|
|
longopts_COMMON
|
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
2007-10-09 23:11:53 +05:30
|
|
|
"Check if a directory",
|
|
|
|
"Check if a file",
|
|
|
|
"Mode to check",
|
2007-12-19 16:19:03 +05:30
|
|
|
"Owner to check (user:group)",
|
2007-09-25 21:51:38 +05:30
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-09-19 21:57:37 +05:30
|
|
|
#include "_usage.c"
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
int
|
|
|
|
checkpath(int argc, char **argv)
|
2007-09-19 21:57:37 +05:30
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
uid_t uid = geteuid();
|
|
|
|
gid_t gid = getgid();
|
|
|
|
mode_t mode = 0;
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
struct group *gr = NULL;
|
2007-09-19 22:38:25 +05:30
|
|
|
bool file = 0;
|
2007-12-21 00:19:44 +05:30
|
|
|
int retval = EXIT_SUCCESS;
|
2007-09-19 22:38:25 +05:30
|
|
|
|
2008-03-16 22:30:56 +05:30
|
|
|
while ((opt = getopt_long(argc, argv, getoptstring,
|
2009-04-24 03:01:22 +05:30
|
|
|
longopts, (int *) 0)) != -1)
|
2007-09-19 21:57:37 +05:30
|
|
|
{
|
|
|
|
switch (opt) {
|
2008-03-16 22:30:56 +05:30
|
|
|
case 'd':
|
|
|
|
file = 0;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
file = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (parse_mode(&mode, optarg) != 0)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: invalid mode `%s'",
|
|
|
|
applet, optarg);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if (parse_owner(&pw, &gr, optarg) != 0)
|
2009-04-24 03:01:22 +05:30
|
|
|
eerrorx("%s: owner `%s' not found",
|
|
|
|
applet, optarg);
|
2008-03-16 22:30:56 +05:30
|
|
|
break;
|
|
|
|
|
2009-04-24 03:01:22 +05:30
|
|
|
case_RC_COMMON_GETOPT;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-21 17:22:37 +05:30
|
|
|
if (optind >= argc)
|
2008-03-16 22:30:56 +05:30
|
|
|
usage(EXIT_FAILURE);
|
2007-09-21 17:22:37 +05:30
|
|
|
|
2007-09-19 21:57:37 +05:30
|
|
|
if (pw) {
|
|
|
|
uid = pw->pw_uid;
|
|
|
|
gid = pw->pw_gid;
|
|
|
|
}
|
|
|
|
if (gr)
|
|
|
|
gid = gr->gr_gid;
|
|
|
|
|
|
|
|
while (optind < argc) {
|
2008-03-16 22:30:56 +05:30
|
|
|
if (do_check(argv[optind], uid, gid, mode, file))
|
2007-09-19 21:57:37 +05:30
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2008-09-18 18:39:51 +05:30
|
|
|
return retval;
|
2007-09-19 21:57:37 +05:30
|
|
|
}
|