268d5222c85c0904e98411a1a7c82cba61403b0c
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define FOO(x) (x)
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "secrets.h"
24 #include "idmap.h"
25 #include "dbwrap.h"
26
27 #define ALLOC_CHECK(mem) do { \
28         if (!mem) { \
29                 d_fprintf(stderr, _("Out of memory!\n")); \
30                 talloc_free(ctx); \
31                 return -1; \
32         } } while(0)
33
34 /***********************************************************
35  Helper function for net_idmap_dump. Dump one entry.
36  **********************************************************/
37 static int net_idmap_dump_one_entry(struct db_record *rec,
38                                     void *unused)
39 {
40         if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
41                 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
42                 return 0;
43         }
44
45         if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
46                 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
47                 return 0;
48         }
49
50         if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
51                 return 0;
52
53         printf("%s %s\n", rec->value.dptr, rec->key.dptr);
54         return 0;
55 }
56
57 /***********************************************************
58  Dump the current idmap
59  **********************************************************/
60 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
61 {
62         struct db_context *db;
63         TALLOC_CTX *mem_ctx;
64
65         if ( argc != 1  || c->display_usage) {
66                 d_printf("%s\n%s",
67                          _("Usage:"),
68                          _("net idmap dump <inputfile>\n"
69                            "  Dump current ID mapping.\n"
70                            "    inputfile\tTDB file to read mappings from.\n"));
71                 return c->display_usage?0:-1;
72         }
73
74         mem_ctx = talloc_stackframe();
75
76         db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
77         if (db == NULL) {
78                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
79                           argv[0], strerror(errno));
80                 talloc_free(mem_ctx);
81                 return -1;
82         }
83
84         db->traverse_read(db, net_idmap_dump_one_entry, NULL);
85
86         talloc_free(mem_ctx);
87
88         return 0;
89 }
90
91 /***********************************************************
92  Write entries from stdin to current local idmap
93  **********************************************************/
94
95 static int net_idmap_store_id_mapping(struct db_context *db,
96                                       enum id_type type,
97                                       unsigned long idval,
98                                       const char *sid_string)
99 {
100         NTSTATUS status;
101         char *idstr = NULL;
102
103         switch(type) {
104         case ID_TYPE_UID:
105                 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
106                 break;
107         case ID_TYPE_GID:
108                 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
109                 break;
110         default:
111                 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
112                 return -1;
113         }
114
115         status = dbwrap_store_bystring(db, idstr,
116                                        string_term_tdb_data(sid_string),
117                                        TDB_REPLACE);
118         if (!NT_STATUS_IS_OK(status)) {
119                 d_fprintf(stderr, "Error storing ID -> SID: "
120                          "%s\n", nt_errstr(status));
121                 talloc_free(idstr);
122                 return -1;
123         }
124         status = dbwrap_store_bystring(db, sid_string,
125                                        string_term_tdb_data(idstr),
126                                        TDB_REPLACE);
127         if (!NT_STATUS_IS_OK(status)) {
128                 d_fprintf(stderr, "Error storing SID -> ID: "
129                          "%s\n", nt_errstr(status));
130                 talloc_free(idstr);
131                 return -1;
132         }
133
134         return 0;
135 }
136
137 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
138 {
139         TALLOC_CTX *mem_ctx;
140         FILE *input = NULL;
141         struct db_context *db;
142         char *dbfile = NULL;
143         int ret = 0;
144
145         if (c->display_usage) {
146                 d_printf("%s\n%s",
147                          _("Usage:"),
148                          _("net idmap restore [<inputfile>]\n"
149                            "  Restore ID mappings from file\n"
150                            "    inputfile\tFile to load ID mappings from. If not "
151                            "given, load data from stdin.\n"));
152                 return 0;
153         }
154
155         mem_ctx = talloc_stackframe();
156
157         if (strequal(lp_idmap_backend(), "tdb")) {
158                 dbfile = state_path("winbindd_idmap.tdb");
159                 if (dbfile == NULL) {
160                         d_fprintf(stderr, _("Out of memory!\n"));
161                         return -1;
162                 }
163         } else if (strequal(lp_idmap_backend(), "tdb2")) {
164                 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
165                 if (dbfile == NULL) {
166                         dbfile = talloc_asprintf(mem_ctx, "%s/idmap2.tdb",
167                                                  lp_private_dir());
168                 }
169         } else {
170                 char *backend, *args;
171
172                 backend = talloc_strdup(mem_ctx, lp_idmap_backend());
173                 args = strchr(backend, ':');
174                 if (args != NULL) {
175                         *args = '\0';
176                 }
177
178                 d_printf(_("Sorry, 'net idmap restore' is currently not "
179                            "supported for idmap backend = %s.\n"
180                            "Only tdb and tdb2 are supported.\n"),
181                          backend);
182
183                 ret = -1;
184                 goto done;
185         }
186
187         d_fprintf(stderr, _("restoring id mapping to %s data base in '%s'\n"),
188                   lp_idmap_backend(), dbfile);
189
190         if (argc == 1) {
191                 input = fopen(argv[0], "r");
192         } else {
193                 input = stdin;
194         }
195
196         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
197         if (db == NULL) {
198                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
199                           dbfile, strerror(errno));
200                 ret = -1;
201                 goto done;
202         }
203
204         if (db->transaction_start(db) != 0) {
205                 d_fprintf(stderr, _("Failed to start transaction.\n"));
206                 ret = -1;
207                 goto done;
208         }
209
210         while (!feof(input)) {
211                 char line[128], sid_string[128];
212                 int len;
213                 unsigned long idval;
214
215                 if (fgets(line, 127, input) == NULL)
216                         break;
217
218                 len = strlen(line);
219
220                 if ( (len > 0) && (line[len-1] == '\n') )
221                         line[len-1] = '\0';
222
223                 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
224                 {
225                         ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
226                                                          idval, sid_string);
227                         if (ret != 0) {
228                                 break;
229                         }
230                 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
231                 {
232                         ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
233                                                          idval, sid_string);
234                         if (ret != 0) {
235                                 break;
236                         }
237                 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
238                         ret = dbwrap_store_int32(db, "USER HWM", idval);
239                         if (ret != 0) {
240                                 d_fprintf(stderr, _("Could not store USER HWM.\n"));
241                                 break;
242                         }
243                 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
244                         ret = dbwrap_store_int32(db, "GROUP HWM", idval);
245                         if (ret != 0) {
246                                 d_fprintf(stderr,
247                                           _("Could not store GROUP HWM.\n"));
248                                 break;
249                         }
250                 } else {
251                         d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
252                                   line);
253                         continue;
254                 }
255         }
256
257         if (ret == 0) {
258                 if(db->transaction_commit(db) != 0) {
259                         d_fprintf(stderr, _("Failed to commit transaction.\n"));
260                         ret = -1;
261                 }
262         } else {
263                 if (db->transaction_cancel(db) != 0) {
264                         d_fprintf(stderr, _("Failed to cancel transaction.\n"));
265                 }
266         }
267
268 done:
269         if ((input != NULL) && (input != stdin)) {
270                 fclose(input);
271         }
272
273         talloc_free(mem_ctx);
274         return ret;
275 }
276
277 /***********************************************************
278  Delete a SID mapping from a winbindd_idmap.tdb
279  **********************************************************/
280 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
281 {
282         d_printf("%s\n", _("Not implemented yet"));
283         return -1;
284 }
285
286 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
287 {
288         d_printf("%s\n", _("Not implemented yet"));
289         return -1;
290 }
291 bool idmap_store_secret(const char *backend, bool alloc,
292                         const char *domain, const char *identity,
293                         const char *secret)
294 {
295         char *tmp;
296         int r;
297         bool ret;
298
299         if (alloc) {
300                 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
301         } else {
302                 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
303         }
304
305         if (r < 0) return false;
306
307         strupper_m(tmp); /* make sure the key is case insensitive */
308         ret = secrets_store_generic(tmp, identity, secret);
309
310         free(tmp);
311         return ret;
312 }
313
314
315 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
316 {
317         TALLOC_CTX *ctx;
318         const char *secret;
319         const char *dn;
320         char *domain;
321         char *backend;
322         char *opt = NULL;
323         bool ret;
324
325         if (argc != 2 || c->display_usage) {
326                 d_printf("%s\n%s",
327                          _("Usage:\n"),
328                          _("net idmap secret <DOMAIN> <secret>\n"
329                            "  Set the secret for the specified domain\n"
330                            "    DOMAIN\tDomain to set secret for.\n"
331                            "    secret\tNew secret to set.\n"));
332                 return c->display_usage?0:-1;
333         }
334
335         secret = argv[1];
336
337         ctx = talloc_new(NULL);
338         ALLOC_CHECK(ctx);
339
340         domain = talloc_strdup(ctx, argv[0]);
341         ALLOC_CHECK(domain);
342
343         opt = talloc_asprintf(ctx, "idmap config %s", domain);
344         ALLOC_CHECK(opt);
345
346         backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
347         ALLOC_CHECK(backend);
348
349         if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
350                 d_fprintf(stderr,
351                           _("The only currently supported backend is LDAP\n"));
352                 talloc_free(ctx);
353                 return -1;
354         }
355
356         dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
357         if ( ! dn) {
358                 d_fprintf(stderr,
359                           _("Missing ldap_user_dn option for domain %s\n"),
360                           domain);
361                 talloc_free(ctx);
362                 return -1;
363         }
364
365         ret = idmap_store_secret("ldap", false, domain, dn, secret);
366
367         if ( ! ret) {
368                 d_fprintf(stderr, _("Failed to store secret\n"));
369                 talloc_free(ctx);
370                 return -1;
371         }
372
373         d_printf(_("Secret stored\n"));
374         return 0;
375 }
376
377 int net_help_idmap(struct net_context *c, int argc, const char **argv)
378 {
379         d_printf(_("net idmap dump <inputfile>\n"
380                    "    Dump current id mapping\n"));
381
382         d_printf(_("net idmap restore\n"
383                    "    Restore entries from stdin\n"));
384
385         /* Deliberately *not* document net idmap delete */
386
387         d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
388                    "    Set the secret for the specified DOMAIN (or the alloc "
389                    "module)\n"));
390
391         return -1;
392 }
393
394 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
395 {
396         TALLOC_CTX *mem_ctx;
397         int result = -1;
398         struct dom_sid src_sid, dst_sid;
399         char *src, *dst;
400         struct db_context *db;
401         struct db_record *rec;
402         NTSTATUS status;
403
404         if (argc != 3 || c->display_usage) {
405                 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
406                           "<src-sid> <dst-sid>\n", _("Usage:"));
407                 return -1;
408         }
409
410         if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
411                 d_fprintf(stderr, _("talloc_init failed\n"));
412                 return -1;
413         }
414
415         if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
416                            O_RDWR|O_CREAT, 0600))) {
417                 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
418                 goto fail;
419         }
420
421         if (!string_to_sid(&src_sid, argv[1])) {
422                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
423                 goto fail;
424         }
425
426         if (!string_to_sid(&dst_sid, argv[2])) {
427                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
428                 goto fail;
429         }
430
431         if (!(src = sid_string_talloc(mem_ctx, &src_sid))
432             || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
433                 d_fprintf(stderr, _("talloc_strdup failed\n"));
434                 goto fail;
435         }
436
437         if (!(rec = db->fetch_locked(
438                       db, mem_ctx, string_term_tdb_data(src)))) {
439                 d_fprintf(stderr, _("could not fetch db record\n"));
440                 goto fail;
441         }
442
443         status = rec->store(rec, string_term_tdb_data(dst), 0);
444         TALLOC_FREE(rec);
445
446         if (!NT_STATUS_IS_OK(status)) {
447                 d_fprintf(stderr, _("could not store record: %s\n"),
448                           nt_errstr(status));
449                 goto fail;
450         }
451
452         result = 0;
453 fail:
454         TALLOC_FREE(mem_ctx);
455         return result;
456 }
457
458 /***********************************************************
459  Look at the current idmap
460  **********************************************************/
461 int net_idmap(struct net_context *c, int argc, const char **argv)
462 {
463         struct functable func[] = {
464                 {
465                         "dump",
466                         net_idmap_dump,
467                         NET_TRANSPORT_LOCAL,
468                         N_("Dump the current ID mappings"),
469                         N_("net idmap dump\n"
470                            "  Dump the current ID mappings")
471                 },
472                 {
473                         "restore",
474                         net_idmap_restore,
475                         NET_TRANSPORT_LOCAL,
476                         N_("Restore entries from stdin"),
477                         N_("net idmap restore\n"
478                            "  Restore entries from stdin")
479                 },
480                 {
481                         "setmap",
482                         net_idmap_set,
483                         NET_TRANSPORT_LOCAL,
484                         N_("Not implemented yet"),
485                         N_("net idmap setmap\n"
486                            "  Not implemented yet")
487                 },
488                 {
489                         "delete",
490                         net_idmap_delete,
491                         NET_TRANSPORT_LOCAL,
492                         N_("Not implemented yet"),
493                         N_("net idmap delete\n"
494                            "  Not implemented yet")
495                 },
496                 {
497                         "secret",
498                         net_idmap_secret,
499                         NET_TRANSPORT_LOCAL,
500                         N_("Set secret for specified domain"),
501                         N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
502                            "  Set secret for specified domain or alloc module")
503                 },
504                 {
505                         "aclmapset",
506                         net_idmap_aclmapset,
507                         NET_TRANSPORT_LOCAL,
508                         N_("Set acl map"),
509                         N_("net idmap aclmapset\n"
510                            "  Set acl map")
511                 },
512                 {NULL, NULL, 0, NULL, NULL}
513         };
514
515         return net_run_function(c, argc, argv, "net idmap", func);
516 }
517
518