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