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 | #define ALLOC_CHECK(mem) do { \
|
---|
24 | if (!mem) { \
|
---|
25 | d_fprintf(stderr, "Out of memory!\n"); \
|
---|
26 | talloc_free(ctx); \
|
---|
27 | return -1; \
|
---|
28 | } } while(0)
|
---|
29 |
|
---|
30 | /***********************************************************
|
---|
31 | Helper function for net_idmap_dump. Dump one entry.
|
---|
32 | **********************************************************/
|
---|
33 | static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
|
---|
34 | TDB_DATA key,
|
---|
35 | TDB_DATA data,
|
---|
36 | void *unused)
|
---|
37 | {
|
---|
38 | if (strcmp(key.dptr, "USER HWM") == 0) {
|
---|
39 | printf("USER HWM %d\n", IVAL(data.dptr,0));
|
---|
40 | return 0;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (strcmp(key.dptr, "GROUP HWM") == 0) {
|
---|
44 | printf("GROUP HWM %d\n", IVAL(data.dptr,0));
|
---|
45 | return 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (strncmp(key.dptr, "S-", 2) != 0)
|
---|
49 | return 0;
|
---|
50 |
|
---|
51 | printf("%s %s\n", data.dptr, key.dptr);
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /***********************************************************
|
---|
56 | Dump the current idmap
|
---|
57 | **********************************************************/
|
---|
58 | static int net_idmap_dump(int argc, const char **argv)
|
---|
59 | {
|
---|
60 | TDB_CONTEXT *idmap_tdb;
|
---|
61 |
|
---|
62 | if ( argc != 1 )
|
---|
63 | return net_help_idmap( argc, argv );
|
---|
64 |
|
---|
65 | idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
|
---|
66 |
|
---|
67 | if (idmap_tdb == NULL) {
|
---|
68 | d_fprintf(stderr, "Could not open idmap: %s\n", argv[0]);
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
|
---|
73 |
|
---|
74 | tdb_close(idmap_tdb);
|
---|
75 |
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /***********************************************************
|
---|
80 | Write entries from stdin to current local idmap
|
---|
81 | **********************************************************/
|
---|
82 |
|
---|
83 | static int net_idmap_restore(int argc, const char **argv)
|
---|
84 | {
|
---|
85 | TALLOC_CTX *ctx;
|
---|
86 | FILE *input;
|
---|
87 |
|
---|
88 | if (! winbind_ping()) {
|
---|
89 | d_fprintf(stderr, "To use net idmap Winbindd must be running.\n");
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | ctx = talloc_new(NULL);
|
---|
94 | ALLOC_CHECK(ctx);
|
---|
95 |
|
---|
96 | if (argc == 1) {
|
---|
97 | input = fopen(argv[0], "r");
|
---|
98 | } else {
|
---|
99 | input = stdin;
|
---|
100 | }
|
---|
101 |
|
---|
102 | while (!feof(input)) {
|
---|
103 | char line[128], sid_string[128];
|
---|
104 | int len;
|
---|
105 | DOM_SID sid;
|
---|
106 | struct id_map map;
|
---|
107 | unsigned long idval;
|
---|
108 |
|
---|
109 | if (fgets(line, 127, input) == NULL)
|
---|
110 | break;
|
---|
111 |
|
---|
112 | len = strlen(line);
|
---|
113 |
|
---|
114 | if ( (len > 0) && (line[len-1] == '\n') )
|
---|
115 | line[len-1] = '\0';
|
---|
116 |
|
---|
117 | if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
|
---|
118 | map.xid.type = ID_TYPE_GID;
|
---|
119 | map.xid.id = idval;
|
---|
120 | } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
|
---|
121 | map.xid.type = ID_TYPE_UID;
|
---|
122 | map.xid.id = idval;
|
---|
123 | } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
|
---|
124 | /* set uid hwm */
|
---|
125 | if (! winbind_set_uid_hwm(idval)) {
|
---|
126 | d_fprintf(stderr, "Could not set USER HWM\n");
|
---|
127 | }
|
---|
128 | continue;
|
---|
129 | } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
|
---|
130 | /* set gid hwm */
|
---|
131 | if (! winbind_set_gid_hwm(idval)) {
|
---|
132 | d_fprintf(stderr, "Could not set GROUP HWM\n");
|
---|
133 | }
|
---|
134 | continue;
|
---|
135 | } else {
|
---|
136 | d_fprintf(stderr, "ignoring invalid line [%s]\n", line);
|
---|
137 | continue;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (!string_to_sid(&sid, sid_string)) {
|
---|
141 | d_fprintf(stderr, "ignoring invalid sid [%s]\n", sid_string);
|
---|
142 | continue;
|
---|
143 | }
|
---|
144 | map.sid = &sid;
|
---|
145 |
|
---|
146 | if (!winbind_set_mapping(&map)) {
|
---|
147 | d_fprintf(stderr, "Could not set mapping of %s %lu to sid %s\n",
|
---|
148 | (map.xid.type == ID_TYPE_GID) ? "GID" : "UID",
|
---|
149 | (unsigned long)map.xid.id, sid_string_static(map.sid));
|
---|
150 | continue;
|
---|
151 | }
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (input != stdin) {
|
---|
156 | fclose(input);
|
---|
157 | }
|
---|
158 |
|
---|
159 | talloc_free(ctx);
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /***********************************************************
|
---|
164 | Delete a SID mapping from a winbindd_idmap.tdb
|
---|
165 | **********************************************************/
|
---|
166 | static int net_idmap_delete(int argc, const char **argv)
|
---|
167 | {
|
---|
168 | d_printf("Not Implemented yet\n");
|
---|
169 | return -1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | static int net_idmap_set(int argc, const char **argv)
|
---|
173 | {
|
---|
174 | d_printf("Not Implemented yet\n");
|
---|
175 | return -1;
|
---|
176 | }
|
---|
177 | BOOL idmap_store_secret(const char *backend, bool alloc,
|
---|
178 | const char *domain, const char *identity,
|
---|
179 | const char *secret)
|
---|
180 | {
|
---|
181 | char *tmp;
|
---|
182 | int r;
|
---|
183 | BOOL ret;
|
---|
184 |
|
---|
185 | if (alloc) {
|
---|
186 | r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
|
---|
187 | } else {
|
---|
188 | r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (r < 0) return false;
|
---|
192 |
|
---|
193 | strupper_m(tmp); /* make sure the key is case insensitive */
|
---|
194 | ret = secrets_store_generic(tmp, identity, secret);
|
---|
195 |
|
---|
196 | free(tmp);
|
---|
197 | return ret;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | static int net_idmap_secret(int argc, const char **argv)
|
---|
202 | {
|
---|
203 | TALLOC_CTX *ctx;
|
---|
204 | const char *secret;
|
---|
205 | const char *dn;
|
---|
206 | char *domain;
|
---|
207 | char *backend;
|
---|
208 | char *opt = NULL;
|
---|
209 | BOOL ret;
|
---|
210 |
|
---|
211 | if (argc != 2) {
|
---|
212 | return net_help_idmap(argc, argv);
|
---|
213 | }
|
---|
214 |
|
---|
215 | secret = argv[1];
|
---|
216 |
|
---|
217 | ctx = talloc_new(NULL);
|
---|
218 | ALLOC_CHECK(ctx);
|
---|
219 |
|
---|
220 | if (strcmp(argv[0], "alloc") == 0) {
|
---|
221 | domain = NULL;
|
---|
222 | backend = lp_idmap_alloc_backend();
|
---|
223 | } else {
|
---|
224 | domain = talloc_strdup(ctx, argv[0]);
|
---|
225 | ALLOC_CHECK(domain);
|
---|
226 |
|
---|
227 | opt = talloc_asprintf(ctx, "idmap config %s", domain);
|
---|
228 | ALLOC_CHECK(opt);
|
---|
229 |
|
---|
230 | backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
|
---|
231 | ALLOC_CHECK(backend);
|
---|
232 | }
|
---|
233 |
|
---|
234 | if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
|
---|
235 | d_fprintf(stderr, "The only currently supported backend is LDAP\n");
|
---|
236 | talloc_free(ctx);
|
---|
237 | return -1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (domain) {
|
---|
241 |
|
---|
242 | dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
|
---|
243 | if ( ! dn) {
|
---|
244 | d_fprintf(stderr, "Missing ldap_user_dn option for domain %s\n", domain);
|
---|
245 | talloc_free(ctx);
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | ret = idmap_store_secret("ldap", false, domain, dn, secret);
|
---|
250 | } else {
|
---|
251 | dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
|
---|
252 | if ( ! dn) {
|
---|
253 | d_fprintf(stderr, "Missing ldap_user_dn option for alloc backend\n");
|
---|
254 | talloc_free(ctx);
|
---|
255 | return -1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | ret = idmap_store_secret("ldap", true, NULL, dn, secret);
|
---|
259 | }
|
---|
260 |
|
---|
261 | if ( ! ret) {
|
---|
262 | d_fprintf(stderr, "Failed to store secret\n");
|
---|
263 | talloc_free(ctx);
|
---|
264 | return -1;
|
---|
265 | }
|
---|
266 |
|
---|
267 | d_printf("Secret stored\n");
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | int net_help_idmap(int argc, const char **argv)
|
---|
272 | {
|
---|
273 | d_printf("net idmap dump <inputfile>\n"\
|
---|
274 | " Dump current id mapping\n");
|
---|
275 |
|
---|
276 | d_printf("net idmap restore\n"\
|
---|
277 | " Restore entries from stdin\n");
|
---|
278 |
|
---|
279 | /* Deliberately *not* document net idmap delete */
|
---|
280 |
|
---|
281 | d_printf("net idmap secret <DOMAIN>|alloc <secret>\n"\
|
---|
282 | " Set the secret for the specified DOMAIN (or the alloc module)\n");
|
---|
283 |
|
---|
284 | return -1;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /***********************************************************
|
---|
288 | Look at the current idmap
|
---|
289 | **********************************************************/
|
---|
290 | int net_idmap(int argc, const char **argv)
|
---|
291 | {
|
---|
292 | struct functable func[] = {
|
---|
293 | {"dump", net_idmap_dump},
|
---|
294 | {"restore", net_idmap_restore},
|
---|
295 | {"setmap", net_idmap_set },
|
---|
296 | {"delete", net_idmap_delete},
|
---|
297 | {"secret", net_idmap_secret},
|
---|
298 | {"help", net_help_idmap},
|
---|
299 | {NULL, NULL}
|
---|
300 | };
|
---|
301 |
|
---|
302 | return net_run_function(argc, argv, func, net_help_idmap);
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|