Some const correctness. Stop tdb being used as a remote backend. If an
[abartlet/samba.git/.git] / source3 / utils / net_idmap.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) 2003 Andrew Bartlett (abartlet@samba.org)
5
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.
10    
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.
15    
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.  */
19
20 #include "includes.h"
21 #include "../utils/net.h"
22
23
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)
31 {
32         if (strcmp(key.dptr, "USER HWM") == 0) {
33                 printf("USER HWM %d\n", IVAL(data.dptr,0));
34                 return 0;
35         }
36
37         if (strcmp(key.dptr, "GROUP HWM") == 0) {
38                 printf("GROUP HWM %d\n", IVAL(data.dptr,0));
39                 return 0;
40         }
41
42         if (strncmp(key.dptr, "S-", 2) != 0)
43                 return 0;
44
45         printf("%s %s\n", data.dptr, key.dptr);
46         return 0;
47 }
48
49 /***********************************************************
50  Dump the current idmap
51  **********************************************************/
52 static int net_idmap_dump(int argc, const char **argv)
53 {
54         TDB_CONTEXT *idmap_tdb;
55
56         if ( argc != 1 )
57                 return net_help_idmap( argc, argv );
58
59         idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
60
61         if (idmap_tdb == NULL) {
62                 d_printf("Could not open idmap: %s\n", argv[0]);
63                 return -1;
64         }
65
66         tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
67
68         tdb_close(idmap_tdb);
69
70         return 0;
71 }
72
73 /***********************************************************
74  Write entries from stdin to current local idmap
75  **********************************************************/
76 static int net_idmap_restore(int argc, const char **argv)
77 {
78         if (!idmap_init(lp_idmap_backend())) {
79                 d_printf("Could not init idmap\n");
80                 return -1;
81         }
82
83         while (!feof(stdin)) {
84                 fstring line, sid_string;
85                 int len;
86                 unid_t id;
87                 int type = ID_EMPTY;
88                 DOM_SID sid;
89
90                 if (fgets(line, sizeof(line)-1, stdin) == NULL)
91                         break;
92
93                 len = strlen(line);
94
95                 if ( (len > 0) && (line[len-1] == '\n') )
96                         line[len-1] = '\0';
97
98                 if (sscanf(line, "GID %d %s", &id.gid, sid_string) == 2) {
99                         type = ID_GROUPID;
100                 }
101
102                 if (sscanf(line, "UID %d %s", &id.uid, sid_string) == 2) {
103                         type = ID_USERID;
104                 }
105
106                 if (type == ID_EMPTY) {
107                         d_printf("ignoring invalid line [%s]\n", line);
108                         continue;
109                 }
110
111                 if (!string_to_sid(&sid, sid_string)) {
112                         d_printf("ignoring invalid sid [%s]\n", sid_string);
113                         continue;
114                 }
115
116                 if (!NT_STATUS_IS_OK(idmap_set_mapping(&sid, id, type))) {
117                         d_printf("Could not set mapping of %s %d to sid %s\n",
118                                  (type == ID_GROUPID) ? "GID" : "UID",
119                                  (type == ID_GROUPID) ? id.gid : id.uid,
120                                  sid_string_static(&sid));
121                         continue;
122                 }
123                                  
124         }
125
126         idmap_close();
127         return 0;
128 }
129
130 int net_help_idmap(int argc, const char **argv)
131 {
132         d_printf("net idmap dump filename"\
133                  "\n  Dump current id mapping\n");
134
135         d_printf("net idmap restore"\
136                  "\n  Restore entries from stdin to current local idmap\n");
137
138         return -1;
139 }
140
141 /***********************************************************
142  Look at the current idmap
143  **********************************************************/
144 int net_idmap(int argc, const char **argv)
145 {
146         struct functable func[] = {
147                 {"dump", net_idmap_dump},
148                 {"restore", net_idmap_restore},
149                 {"help", net_help_idmap},
150                 {NULL, NULL}
151         };
152
153         return net_run_function(argc, argv, func, net_help_idmap);
154 }
155
156