r4868: Add "net rpc user RENAME"-command.
[Samba/nascimento.git] / source3 / utils / net_idmap.c
blobf7ebd94f346fff166c647a0699e60bf892f8c2f7
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2003 Andrew Bartlett (abartlet@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 #include "includes.h"
21 #include "utils/net.h"
24 /***********************************************************
25 Helper function for net_idmap_dump. Dump one entry.
26 **********************************************************/
27 static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
28 TDB_DATA key,
29 TDB_DATA data,
30 void *unused)
32 if (strcmp(key.dptr, "USER HWM") == 0) {
33 printf("USER HWM %d\n", IVAL(data.dptr,0));
34 return 0;
37 if (strcmp(key.dptr, "GROUP HWM") == 0) {
38 printf("GROUP HWM %d\n", IVAL(data.dptr,0));
39 return 0;
42 if (strncmp(key.dptr, "S-", 2) != 0)
43 return 0;
45 printf("%s %s\n", data.dptr, key.dptr);
46 return 0;
49 /***********************************************************
50 Dump the current idmap
51 **********************************************************/
52 static int net_idmap_dump(int argc, const char **argv)
54 TDB_CONTEXT *idmap_tdb;
56 if ( argc != 1 )
57 return net_help_idmap( argc, argv );
59 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
61 if (idmap_tdb == NULL) {
62 d_printf("Could not open idmap: %s\n", argv[0]);
63 return -1;
66 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
68 tdb_close(idmap_tdb);
70 return 0;
73 /***********************************************************
74 Fix up the HWMs after a idmap restore.
75 **********************************************************/
77 struct hwms {
78 BOOL ok;
79 int user_hwm;
80 int group_hwm;
83 static int net_idmap_find_max_id(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data,
84 void *handle)
86 struct hwms *hwms = (struct hwms *)handle;
87 int *idptr = NULL;
88 int id;
90 if (strncmp(key.dptr, "S-", 2) != 0)
91 return 0;
93 if (sscanf(data.dptr, "GID %d", &id) == 1) {
94 idptr = &hwms->group_hwm;
97 if (sscanf(data.dptr, "UID %d", &id) == 1) {
98 idptr = &hwms->user_hwm;
101 if (idptr == NULL) {
102 d_printf("Illegal idmap entry: [%s]->[%s]\n",
103 key.dptr, data.dptr);
104 hwms->ok = False;
105 return -1;
108 if (*idptr <= id)
109 *idptr = id+1;
111 return 0;
114 static NTSTATUS net_idmap_fixup_hwm(void)
116 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
117 TDB_CONTEXT *idmap_tdb;
118 char *tdbfile = NULL;
120 struct hwms hwms;
121 struct hwms highest;
123 if (!lp_idmap_uid(&hwms.user_hwm, &highest.user_hwm) ||
124 !lp_idmap_gid(&hwms.group_hwm, &highest.group_hwm)) {
125 d_printf("idmap range missing\n");
126 return NT_STATUS_UNSUCCESSFUL;
129 tdbfile = SMB_STRDUP(lock_path("winbindd_idmap.tdb"));
130 if (!tdbfile) {
131 DEBUG(0, ("idmap_init: out of memory!\n"));
132 return NT_STATUS_NO_MEMORY;
135 idmap_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0);
137 if (idmap_tdb == NULL) {
138 d_printf("Could not open idmap: %s\n", tdbfile);
139 return NT_STATUS_NO_SUCH_FILE;
142 hwms.ok = True;
144 tdb_traverse(idmap_tdb, net_idmap_find_max_id, &hwms);
146 if (!hwms.ok) {
147 goto done;
150 d_printf("USER HWM: %d GROUP HWM: %d\n",
151 hwms.user_hwm, hwms.group_hwm);
153 if (hwms.user_hwm >= highest.user_hwm) {
154 d_printf("Highest UID out of uid range\n");
155 goto done;
158 if (hwms.group_hwm >= highest.group_hwm) {
159 d_printf("Highest GID out of gid range\n");
160 goto done;
163 if ((tdb_store_int32(idmap_tdb, "USER HWM", hwms.user_hwm) != 0) ||
164 (tdb_store_int32(idmap_tdb, "GROUP HWM", hwms.group_hwm) != 0)) {
165 d_printf("Could not store HWMs\n");
166 goto done;
169 result = NT_STATUS_OK;
170 done:
171 tdb_close(idmap_tdb);
172 return result;
175 /***********************************************************
176 Write entries from stdin to current local idmap
177 **********************************************************/
178 static int net_idmap_restore(int argc, const char **argv)
180 if (!idmap_init(lp_idmap_backend())) {
181 d_printf("Could not init idmap\n");
182 return -1;
185 while (!feof(stdin)) {
186 fstring line, sid_string;
187 int len;
188 unid_t id;
189 int type = ID_EMPTY;
190 DOM_SID sid;
192 if (fgets(line, sizeof(line)-1, stdin) == NULL)
193 break;
195 len = strlen(line);
197 if ( (len > 0) && (line[len-1] == '\n') )
198 line[len-1] = '\0';
200 /* Yuck - this is broken for sizeof(gid_t) != sizeof(int) */
202 if (sscanf(line, "GID %d %s", &id.gid, sid_string) == 2) {
203 type = ID_GROUPID;
206 /* Yuck - this is broken for sizeof(uid_t) != sizeof(int) */
208 if (sscanf(line, "UID %d %s", &id.uid, sid_string) == 2) {
209 type = ID_USERID;
212 if (type == ID_EMPTY) {
213 d_printf("ignoring invalid line [%s]\n", line);
214 continue;
217 if (!string_to_sid(&sid, sid_string)) {
218 d_printf("ignoring invalid sid [%s]\n", sid_string);
219 continue;
222 if (!NT_STATUS_IS_OK(idmap_set_mapping(&sid, id, type))) {
223 d_printf("Could not set mapping of %s %lu to sid %s\n",
224 (type == ID_GROUPID) ? "GID" : "UID",
225 (type == ID_GROUPID) ? (unsigned long)id.gid:
226 (unsigned long)id.uid,
227 sid_string_static(&sid));
228 continue;
233 idmap_close();
235 return NT_STATUS_IS_OK(net_idmap_fixup_hwm()) ? 0 : -1;
238 /***********************************************************
239 Delete a SID mapping from a winbindd_idmap.tdb
240 **********************************************************/
241 static int net_idmap_delete(int argc, const char **argv)
243 TDB_CONTEXT *idmap_tdb;
244 TDB_DATA key, data;
245 fstring sid;
247 if (argc != 2)
248 return net_help_idmap(argc, argv);
250 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDWR, 0);
252 if (idmap_tdb == NULL) {
253 d_printf("Could not open idmap: %s\n", argv[0]);
254 return -1;
257 fstrcpy(sid, argv[1]);
259 if (strncmp(sid, "S-1-5-", strlen("S-1-5-")) != 0) {
260 d_printf("Can only delete SIDs, %s is does not start with "
261 "S-1-5-\n", sid);
262 return -1;
265 key.dptr = sid;
266 key.dsize = strlen(key.dptr)+1;
268 data = tdb_fetch(idmap_tdb, key);
270 if (data.dptr == NULL) {
271 d_printf("Could not find sid %s\n", argv[1]);
272 return -1;
275 if (tdb_delete(idmap_tdb, key) != 0) {
276 d_printf("Could not delete key %s\n", argv[1]);
277 return -1;
280 if (tdb_delete(idmap_tdb, data) != 0) {
281 d_printf("Could not delete key %s\n", data.dptr);
282 return -1;
285 return 0;
289 int net_help_idmap(int argc, const char **argv)
291 d_printf("net idmap dump filename"\
292 "\n Dump current id mapping\n");
294 d_printf("net idmap restore"\
295 "\n Restore entries from stdin to current local idmap\n");
297 /* Deliberately *not* document net idmap delete */
299 return -1;
302 /***********************************************************
303 Look at the current idmap
304 **********************************************************/
305 int net_idmap(int argc, const char **argv)
307 struct functable func[] = {
308 {"dump", net_idmap_dump},
309 {"restore", net_idmap_restore},
310 {"delete", net_idmap_delete},
311 {"help", net_help_idmap},
312 {NULL, NULL}
315 return net_run_function(argc, argv, func, net_help_idmap);