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