summaryrefslogtreecommitdiff
path: root/source3/utils/net_idmap.c
blob: 47e1f93b69f20cad4813c1db1e49d7d8ac036a3a (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* 
   Samba Unix/Linux SMB client library 
   Distributed SMB/CIFS Server Management Utility 
   Copyright (C) 2003 Andrew Bartlett (abartlet@samba.org)

   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., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "includes.h"
#include "utils/net.h"


/***********************************************************
 Helper function for net_idmap_dump. Dump one entry.
 **********************************************************/
static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
				    TDB_DATA key,
				    TDB_DATA data,
				    void *unused)
{
	if (strcmp(key.dptr, "USER HWM") == 0) {
		printf("USER HWM %d\n", IVAL(data.dptr,0));
		return 0;
	}

	if (strcmp(key.dptr, "GROUP HWM") == 0) {
		printf("GROUP HWM %d\n", IVAL(data.dptr,0));
		return 0;
	}

	if (strncmp(key.dptr, "S-", 2) != 0)
		return 0;

	printf("%s %s\n", data.dptr, key.dptr);
	return 0;
}

/***********************************************************
 Dump the current idmap
 **********************************************************/
static int net_idmap_dump(int argc, const char **argv)
{
	TDB_CONTEXT *idmap_tdb;

	if ( argc != 1 )
		return net_help_idmap( argc, argv );

	idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);

	if (idmap_tdb == NULL) {
		d_fprintf(stderr, "Could not open idmap: %s\n", argv[0]);
		return -1;
	}

	tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);

	tdb_close(idmap_tdb);

	return 0;
}

/***********************************************************
 Fix up the HWMs after a idmap restore.
 **********************************************************/

struct hwms {
	BOOL ok;
	uid_t user_hwm;
	gid_t group_hwm;
};

static int net_idmap_find_max_id(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data,
				 void *handle)
{
	struct hwms *hwms = (struct hwms *)handle;
	void *idptr = NULL;
	BOOL isgid = False;
	int id;

	if (strncmp(key.dptr, "S-", 2) != 0)
		return 0;

	if (sscanf(data.dptr, "GID %d", &id) == 1) {
		idptr = (void *)&hwms->group_hwm;
		isgid = True;
	}

	if (sscanf(data.dptr, "UID %d", &id) == 1) {
		idptr = (void *)&hwms->user_hwm;
		isgid = False;
	}

	if (idptr == NULL) {
		d_fprintf(stderr, "Illegal idmap entry: [%s]->[%s]\n",
			 key.dptr, data.dptr);
		hwms->ok = False;
		return -1;
	}

	if (isgid) {
		if (hwms->group_hwm <= (gid_t)id) {
			hwms->group_hwm = (gid_t)(id+1);
		}
	} else {
		if (hwms->user_hwm <= (uid_t)id) {
			hwms->user_hwm = (uid_t)(id+1);
		}
	}

	return 0;
}

static NTSTATUS net_idmap_fixup_hwm(void)
{
	NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
	TDB_CONTEXT *idmap_tdb;
	char *tdbfile = NULL;

	struct hwms hwms;
	struct hwms highest;

	if (!lp_idmap_uid(&hwms.user_hwm, &highest.user_hwm) ||
	    !lp_idmap_gid(&hwms.group_hwm, &highest.group_hwm)) {
		d_fprintf(stderr, "idmap range missing\n");
		return NT_STATUS_UNSUCCESSFUL;
	}

	tdbfile = SMB_STRDUP(lock_path("winbindd_idmap.tdb"));
	if (!tdbfile) {
		DEBUG(0, ("idmap_init: out of memory!\n"));
		return NT_STATUS_NO_MEMORY;
	}

	idmap_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0);

	if (idmap_tdb == NULL) {
		d_fprintf(stderr, "Could not open idmap: %s\n", tdbfile);
		return NT_STATUS_NO_SUCH_FILE;
	}

	hwms.ok = True;

	tdb_traverse(idmap_tdb, net_idmap_find_max_id, &hwms);

	if (!hwms.ok) {
		goto done;
	}

	d_printf("USER HWM: %d  GROUP HWM: %d\n",
		 hwms.user_hwm, hwms.group_hwm);

	if (hwms.user_hwm >= highest.user_hwm) {
		d_fprintf(stderr, "Highest UID out of uid range\n");
		goto done;
	}

	if (hwms.group_hwm >= highest.group_hwm) {
		d_fprintf(stderr, "Highest GID out of gid range\n");
		goto done;
	}

	if ((tdb_store_int32(idmap_tdb, "USER HWM", (int32)hwms.user_hwm) != 0) ||
	    (tdb_store_int32(idmap_tdb, "GROUP HWM", (int32)hwms.group_hwm) != 0)) {
		d_fprintf(stderr, "Could not store HWMs\n");
		goto done;
	}

	result = NT_STATUS_OK;
 done:
	tdb_close(idmap_tdb);
	return result;
}

/***********************************************************
 Write entries from stdin to current local idmap
 **********************************************************/
static int net_idmap_restore(int argc, const char **argv)
{
	if (!idmap_init(lp_idmap_backend())) {
		d_fprintf(stderr, "Could not init idmap\n");
		return -1;
	}

	while (!feof(stdin)) {
		fstring line, sid_string, fmt_string;
		int len;
		unid_t id;
		int type = ID_EMPTY;
		DOM_SID sid;

		if (fgets(line, sizeof(line)-1, stdin) == NULL)
			break;

		len = strlen(line);

		if ( (len > 0) && (line[len-1] == '\n') )
			line[len-1] = '\0';

		/* Yuck - this is broken for sizeof(gid_t) != sizeof(int) */
		snprintf(fmt_string, sizeof(fmt_string), "GID %%d %%%us", FSTRING_LEN);
		if (sscanf(line, fmt_string, &id.gid, sid_string) == 2) {
			type = ID_GROUPID;
		}

		/* Yuck - this is broken for sizeof(uid_t) != sizeof(int) */

		snprintf(fmt_string, sizeof(fmt_string), "UID %%d %%%us", FSTRING_LEN);
		if (sscanf(line, fmt_string, &id.uid, sid_string) == 2) {
			type = ID_USERID;
		}

		if (type == ID_EMPTY) {
			d_printf("ignoring invalid line [%s]\n", line);
			continue;
		}

		if (!string_to_sid(&sid, sid_string)) {
			d_printf("ignoring invalid sid [%s]\n", sid_string);
			continue;
		}

		if (!NT_STATUS_IS_OK(idmap_set_mapping(&sid, id, type))) {
			d_fprintf(stderr, "Could not set mapping of %s %lu to sid %s\n",
				 (type == ID_GROUPID) ? "GID" : "UID",
				 (type == ID_GROUPID) ? (unsigned long)id.gid:
				 (unsigned long)id.uid, 
				 sid_string_static(&sid));
			continue;
		}
				 
	}

	idmap_close();

	return NT_STATUS_IS_OK(net_idmap_fixup_hwm()) ? 0 : -1;
}

/***********************************************************
 Delete a SID mapping from a winbindd_idmap.tdb
 **********************************************************/
static int net_idmap_delete(int argc, const char **argv)
{
	TDB_CONTEXT *idmap_tdb;
	TDB_DATA key, data;
	fstring sid;

	if (argc != 2)
		return net_help_idmap(argc, argv);

	idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDWR, 0);

	if (idmap_tdb == NULL) {
		d_fprintf(stderr, "Could not open idmap: %s\n", argv[0]);
		return -1;
	}

	fstrcpy(sid, argv[1]);

	if (strncmp(sid, "S-1-5-", strlen("S-1-5-")) != 0) {
		d_fprintf(stderr, "Can only delete SIDs, %s is does not start with "
			 "S-1-5-\n", sid);
		return -1;
	}

	key.dptr = sid;
	key.dsize = strlen(key.dptr)+1;

	data = tdb_fetch(idmap_tdb, key);

	if (data.dptr == NULL) {
		d_fprintf(stderr, "Could not find sid %s\n", argv[1]);
		return -1;
	}

	if (tdb_delete(idmap_tdb, key) != 0) {
		d_fprintf(stderr, "Could not delete key %s\n", argv[1]);
		return -1;
	}

	if (tdb_delete(idmap_tdb, data) != 0) {
		d_fprintf(stderr, "Could not delete key %s\n", data.dptr);
		return -1;
	}

	return 0;
}


int net_help_idmap(int argc, const char **argv)
{
	d_printf("net idmap dump <tdbfile>"\
		 "\n  Dump current id mapping\n");

	d_printf("net idmap restore"\
		 "\n  Restore entries from stdin to current local idmap\n");

	/* Deliberately *not* document net idmap delete */

	return -1;
}

/***********************************************************
 Look at the current idmap
 **********************************************************/
int net_idmap(int argc, const char **argv)
{
	struct functable func[] = {
		{"dump", net_idmap_dump},
		{"restore", net_idmap_restore},
		{"delete", net_idmap_delete},
		{"help", net_help_idmap},
		{NULL, NULL}
	};

	return net_run_function(argc, argv, func, net_help_idmap);
}