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