aboutsummaryrefslogtreecommitdiffstats
path: root/cifs.idmap.c
blob: cbc01c5414566767a14851e293f04b797568f875 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* CIFS idmap helper.
* Copyright (C) Shirish Pargaonkar (shirishp@us.ibm.com) 2011
*
* Used by /sbin/request-key.conf for handling
* cifs upcall for SID to uig/gid and uid/gid to SID mapping.
* You should have keyutils installed and add
* this lines to /etc/request-key.conf file:

    create cifs.idmap * * /usr/local/sbin/cifs.idmap %k

* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <keyutils.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

#include "cifsacl.h"
#include "idmap_plugin.h"

static void *plugin_handle;

static const char *prog = "cifs.idmap";

static const struct option long_options[] = {
	{"help", 0, NULL, 'h'},
	{"timeout", 1, NULL, 't'},
	{"version", 0, NULL, 'v'},
	{NULL, 0, NULL, 0}
};

static void usage(void)
{
	fprintf(stderr, "Usage: %s [-h] [-v] [-t timeout] key_serial\n", prog);
}

static char *
strget(const char *str, const char *substr)
{
	int sublen;
	char *substrptr;

	/* find the prefix */
	substrptr = strstr(str, substr);
	if (!substrptr)
		return substrptr;

	/* skip over it */
	sublen = strlen(substr);
	substrptr += sublen;

	/* if there's nothing after the prefix, return NULL */
	if (*substrptr == '\0')
		return NULL;

	return substrptr;
}

/*
 * Convert a string representation of unsigned int into a numeric one. Also
 * check for incomplete string conversion and overflow.
 */
static int
str_to_uint(const char *src, unsigned int *dst)
{
	unsigned long tmp;
	char *end;

	errno = 0;
	tmp = strtoul(src, &end, 0);

	if (*end != '\0')
		return EINVAL;
	if (tmp > UINT_MAX)
		return EOVERFLOW;

	*dst = (unsigned int)tmp;
	return 0;
}

static int
cifs_idmap(const key_serial_t key, const char *key_descr)
{
	int rc = 1;
	char *sidstr = NULL;
	struct cifs_sid sid;
	struct cifs_uxid cuxid;

	/*
	 * Use winbind to convert received string to a SID and lookup
	 * name and map that SID to an uid.  If either of these
	 * function calls return with an error, return an error the
	 * upcall caller.  Otherwise instanticate a key using that uid.
	 *
	 * The same applies to SID and gid mapping.
	 */
	sidstr = strget(key_descr, "os:");
	if (sidstr) {
		rc = str_to_sid(plugin_handle, sidstr, &sid);
		if (rc) {
			syslog(LOG_DEBUG, "Unable to convert owner string %s "
				"to SID: %s", key_descr, plugin_errmsg);
			goto cifs_idmap_ret;
		}

		rc = sids_to_ids(plugin_handle, &sid, 1, &cuxid);
		if (rc || (cuxid.type != CIFS_UXID_TYPE_UID &&
			   cuxid.type != CIFS_UXID_TYPE_BOTH)) {
			syslog(LOG_DEBUG, "Unable to convert %s to "
				"UID: %s", key_descr, plugin_errmsg);
			rc = rc ? rc : -EINVAL;
			goto cifs_idmap_ret;
		}
		rc = keyctl_instantiate(key, &cuxid.id.uid, sizeof(uid_t), 0);
		if (rc)
			syslog(LOG_ERR, "%s: key inst: %s", __func__,
					strerror(errno));

		goto cifs_idmap_ret;
	}

	sidstr = strget(key_descr, "gs:");
	if (sidstr) {
		rc = str_to_sid(plugin_handle, sidstr, &sid);
		if (rc) {
			syslog(LOG_DEBUG, "Unable to convert group string %s "
				"to SID: %s", key_descr, plugin_errmsg);
			goto cifs_idmap_ret;
		}

		rc = sids_to_ids(plugin_handle, &sid, 1, &cuxid);
		if (rc || (cuxid.type != CIFS_UXID_TYPE_GID &&
			   cuxid.type != CIFS_UXID_TYPE_BOTH)) {
			syslog(LOG_DEBUG, "Unable to convert %s to "
				"GID: %s", key_descr, plugin_errmsg);
			rc = rc ? rc : -EINVAL;
			goto cifs_idmap_ret;
		}
		rc = keyctl_instantiate(key, &cuxid.id.gid, sizeof(gid_t), 0);
		if (rc)
			syslog(LOG_ERR, "%s: key inst: %s", __func__,
					strerror(errno));

		goto cifs_idmap_ret;
	}

	sidstr = strget(key_descr, "oi:");
	if (sidstr) {
		rc = str_to_uint(sidstr, (unsigned int *)&cuxid.id.uid);
		if (rc) {
			syslog(LOG_ERR, "Unable to convert %s to uid: %s",
				sidstr, strerror(rc));
			goto cifs_idmap_ret;
		}
		cuxid.type = CIFS_UXID_TYPE_UID;

		syslog(LOG_DEBUG, "SID: %s, uid: %u", sidstr, cuxid.id.uid);
		rc = ids_to_sids(plugin_handle, &cuxid, 1, &sid);
		if (rc || sid.revision == 0) {
			syslog(LOG_DEBUG, "uid %u to SID error: %s",
				cuxid.id.uid, plugin_errmsg);
			goto cifs_idmap_ret;
		}

		rc = keyctl_instantiate(key, &sid, sizeof(struct cifs_sid), 0);
		if (rc)
			syslog(LOG_ERR, "%s: key inst: %s", __func__,
				strerror(errno));

		goto cifs_idmap_ret;
	}

	sidstr = strget(key_descr, "gi:");
	if (sidstr) {
		rc = str_to_uint(sidstr, (unsigned int *)&cuxid.id.gid);
		if (rc) {
			syslog(LOG_ERR, "Unable to convert %s to gid: %s",
				sidstr, strerror(rc));
			goto cifs_idmap_ret;
		}
		cuxid.type = CIFS_UXID_TYPE_GID;

		syslog(LOG_DEBUG, "SID: %s, gid: %u", sidstr, cuxid.id.gid);
		rc = ids_to_sids(plugin_handle, &cuxid, 1, &sid);
		if (rc || sid.revision == 0) {
			syslog(LOG_DEBUG, "gid %u to SID error: %s",
				cuxid.id.gid, plugin_errmsg);
			goto cifs_idmap_ret;
		}

		rc = keyctl_instantiate(key, &sid, sizeof(struct cifs_sid), 0);
		if (rc)
			syslog(LOG_ERR, "%s: key inst: %s", __func__,
				strerror(errno));

		goto cifs_idmap_ret;
	}

	syslog(LOG_DEBUG, "Invalid key: %s", key_descr);

cifs_idmap_ret:
	return rc;
}

int main(const int argc, char *const argv[])
{
	int c;
	long rc;
	key_serial_t key = 0;
	char *buf;
	unsigned int timeout = 600; /* default idmap cache timeout */

	openlog(prog, 0, LOG_DAEMON);

	while ((c = getopt_long(argc, argv, "ht:v",
					long_options, NULL)) != -1) {
		switch (c) {
		case 'h':
			rc = 0;
			usage();
			goto out;
		case 't':
			rc = str_to_uint(optarg, &timeout);
			if (rc) {
				syslog(LOG_ERR, "bad timeout value %s: %s",
					optarg, strerror(rc));
				goto out;
			}
			break;
		case 'v':
			rc = 0;
			printf("version: %s\n", VERSION);
			goto out;
		default:
			rc = EINVAL;
			syslog(LOG_ERR, "unknown option: %c", c);
			goto out;
		}
	}

	rc = 1;
	/* is there a key? */
	if (argc <= optind) {
		usage();
		goto out;
	}

	/* get key and keyring values */
	errno = 0;
	key = strtol(argv[optind], NULL, 10);
	if (errno != 0) {
		key = 0;
		syslog(LOG_ERR, "Invalid key format: %s", strerror(errno));
		goto out;
	}

	if (init_plugin(&plugin_handle)) {
		plugin_handle = NULL;
		syslog(LOG_ERR, "Unable to initialize ID mapping plugin: %s",
			plugin_errmsg);
		goto out;
	}

	/* set timeout on key */
	rc = keyctl_set_timeout(key, timeout);
	if (rc == -1) {
		syslog(LOG_ERR, "unable to set key timeout: %s",
			strerror(errno));
		goto out_exit_plugin;
	}

	rc = keyctl_describe_alloc(key, &buf);
	if (rc == -1) {
		syslog(LOG_ERR, "keyctl_describe_alloc failed: %s",
		       strerror(errno));
		goto out_exit_plugin;
	}

	syslog(LOG_DEBUG, "key description: %s", buf);

	rc = cifs_idmap(key, buf);
out_exit_plugin:
	exit_plugin(plugin_handle);
out:
	return rc;
}