aboutsummaryrefslogtreecommitdiff
path: root/src/checkown.c
blob: 5326321a4309fc78be4d97a69c9da4de4f479847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
   checkown.c
   Checks for the existance of a directory and creates it
   if necessary. It can also correct its ownership.

   Copyright 2007 Gentoo Foundation
   */

#define APPLET "checkown"

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "builtins.h"
#include "einfo.h"

static char *applet = NULL;

static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode)
{
	struct stat dirstat;

	memset (&dirstat, 0, sizeof (dirstat));

	if (stat (path, &dirstat)) {
		einfo ("%s: creating directory", path);
		if (! mode)
			mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
		if (mkdir (path, mode)) {
			eerror ("%s: mkdir: %s", applet, strerror (errno));
			return (-1);
		}
	} else if (mode && (dirstat.st_mode & 0777) != mode) {
		einfo ("%s: correcting mode", applet);
		if (chmod (path, mode)) {
			eerror ("%s: chmod: %s", applet, strerror (errno));
			return (-1);
		}
	}

	if (dirstat.st_uid != uid || dirstat.st_gid != gid) {
		if (dirstat.st_dev || dirstat.st_ino)
			einfo ("%s: correcting owner", path);
		if (chown (path, uid, gid)) {
			eerror ("%s: chown: %s", applet, strerror (errno));
			return (-1);
		}
	}

	return (0);
}

/* Based on busybox */
static int parse_mode (mode_t *mode, char *text)
{
	/* Check for a numeric mode */
	if ((*mode - '0') < 8) {
		char *p;
		unsigned long l = strtoul (text, &p, 8);
		if (*p || l > 07777U) {
			errno = EINVAL;
			return (-1);
		}
		*mode = l;
		return (0);
	}

	/* We currently don't check g+w type stuff */
	errno = EINVAL;
	return (-1);
}

static struct passwd *get_user (char **name)
{
	struct passwd *pw;
	char *p = *name;
	char *token;
	int tid;

	token = strsep (&p, ":");
	if (sscanf (token, "%d", &tid) != 1)
		pw = getpwnam (token);
	else
		pw = getpwuid (tid);

	if (! pw)
		eerrorx ("%s: user `%s' not found", applet, token);

	*name = p;
	return (pw);
}

static struct group *get_group (const char *name)
{
	struct group *gr;
	int tid;

	if (sscanf (name, "%d", &tid) != 1)
		gr = getgrnam (name);
	else
		gr = getgrgid (tid);

	if (! gr)
		eerrorx ("%s: group `%s' not found", applet, name);

	return (gr);
}

#include "_usage.h"
#define getoptstring "m:g:u:" getoptstring_COMMON
static struct option longopts[] = {
	{ "mode",			1, NULL, 'm'},
	{ "user",           1, NULL, 'u'},
	{ "group",          1, NULL, 'g'},
	longopts_COMMON
	{ NULL,             0, NULL, 0}
};
#include "_usage.c"

int checkown (int argc, char **argv)
{
	int opt;
	uid_t uid = geteuid();
	gid_t gid = getgid();
	mode_t mode = 0;

	struct passwd *pw = NULL;
	struct group *gr = NULL;
	char *p;
	int retval = EXIT_SUCCESS;

	applet = argv[0];

	while ((opt = getopt_long (argc, argv, getoptstring,
							   longopts, (int *) 0)) != -1)
	{
		switch (opt) {
			case 'm':
				if (parse_mode (&mode, optarg))
					eerrorx ("%s: invalid mode `%s'", applet, optarg);
				break;
			case 'u':
				p = optarg;
				pw = get_user (&p);
				if (p && *p)
					optarg = p;
				else
					break;
			case 'g':
				gr = get_group (optarg);
				break;

				case_RC_COMMON_GETOPT
		}
	}

	if (pw) {
		uid = pw->pw_uid;
		gid = pw->pw_gid;
	}
	if (gr)
		gid = gr->gr_gid;

	while (optind < argc) {
		if (do_check (argv[optind], uid, gid, mode))
			retval = EXIT_FAILURE;
		optind++;
	}

	exit (retval);
}