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