s3-secrets: only include secrets.h when needed.
[nivanova/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
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(TDB_CONTEXT *tdb,
36                                     TDB_DATA key,
37                                     TDB_DATA data,
38                                     void *unused)
39 {
40         if (strcmp((char *)key.dptr, "USER HWM") == 0) {
41                 printf(_("USER HWM %d\n"), IVAL(data.dptr,0));
42                 return 0;
43         }
44
45         if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
46                 printf(_("GROUP HWM %d\n"), IVAL(data.dptr,0));
47                 return 0;
48         }
49
50         if (strncmp((char *)key.dptr, "S-", 2) != 0)
51                 return 0;
52
53         printf("%s %s\n", data.dptr, 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         TDB_CONTEXT *idmap_tdb;
63
64         if ( argc != 1  || c->display_usage) {
65                 d_printf("%s\n%s",
66                          _("Usage:"),
67                          _("net idmap dump <inputfile>\n"
68                            "  Dump current ID mapping.\n"
69                            "    inputfile\tTDB file to read mappings from.\n"));
70                 return c->display_usage?0:-1;
71         }
72
73         idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
74
75         if (idmap_tdb == NULL) {
76                 d_fprintf(stderr, _("Could not open idmap: %s\n"), argv[0]);
77                 return -1;
78         }
79
80         tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
81
82         tdb_close(idmap_tdb);
83
84         return 0;
85 }
86
87 /***********************************************************
88  Write entries from stdin to current local idmap
89  **********************************************************/
90
91 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
92 {
93         TALLOC_CTX *ctx;
94         FILE *input;
95
96         if (c->display_usage) {
97                 d_printf("%s\n%s",
98                          _("Usage:"),
99                          _("net idmap restore [inputfile]\n"
100                            "  Restore ID mappings from file\n"
101                            "    inputfile\tFile to load ID mappings from. If "
102                            "not given, load data from stdin.\n"));
103                 return 0;
104         }
105
106         if (! winbind_ping()) {
107                 d_fprintf(stderr,
108                           _("To use net idmap Winbindd must be running.\n"));
109                 return -1;
110         }
111
112         ctx = talloc_new(NULL);
113         ALLOC_CHECK(ctx);
114
115         if (argc == 1) {
116                 input = fopen(argv[0], "r");
117         } else {
118                 input = stdin;
119         }
120
121         while (!feof(input)) {
122                 char line[128], sid_string[128];
123                 int len;
124                 struct wbcDomainSid sid;
125                 enum id_type type = ID_TYPE_NOT_SPECIFIED;
126                 unsigned long idval;
127                 wbcErr wbc_status;
128
129                 if (fgets(line, 127, input) == NULL)
130                         break;
131
132                 len = strlen(line);
133
134                 if ( (len > 0) && (line[len-1] == '\n') )
135                         line[len-1] = '\0';
136
137                 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
138                         type = ID_TYPE_GID;
139                 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
140                         type = ID_TYPE_UID;
141                 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
142                         /* set uid hwm */
143                         wbc_status = wbcSetUidHwm(idval);
144                         if (!WBC_ERROR_IS_OK(wbc_status)) {
145                                 d_fprintf(stderr,
146                                           _("Could not set USER HWM: %s\n"),
147                                           wbcErrorString(wbc_status));
148                         }
149                         continue;
150                 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
151                         /* set gid hwm */
152                         wbc_status = wbcSetGidHwm(idval);
153                         if (!WBC_ERROR_IS_OK(wbc_status)) {
154                                 d_fprintf(stderr,
155                                           _("Could not set GROUP HWM: %s\n"),
156                                           wbcErrorString(wbc_status));
157                         }
158                         continue;
159                 } else {
160                         d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
161                                   line);
162                         continue;
163                 }
164
165                 wbc_status = wbcStringToSid(sid_string, &sid);
166                 if (!WBC_ERROR_IS_OK(wbc_status)) {
167                         d_fprintf(stderr, _("ignoring invalid sid [%s]: %s\n"),
168                                   sid_string, wbcErrorString(wbc_status));
169                         continue;
170                 }
171
172                 if (type == ID_TYPE_UID) {
173                         wbc_status = wbcSetUidMapping(idval, &sid);
174                 } else {
175                         wbc_status = wbcSetGidMapping(idval, &sid);
176                 }
177                 if (!WBC_ERROR_IS_OK(wbc_status)) {
178                         d_fprintf(stderr,
179                                   _("Could not set mapping of %s %lu to sid %s: %s\n"),
180                                  (type == ID_TYPE_GID) ? "GID" : "UID",
181                                  idval, sid_string,
182                                  wbcErrorString(wbc_status));
183                         continue;
184                 }
185         }
186
187         if (input != stdin) {
188                 fclose(input);
189         }
190
191         talloc_free(ctx);
192         return 0;
193 }
194
195 /***********************************************************
196  Delete a SID mapping from a winbindd_idmap.tdb
197  **********************************************************/
198 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
199 {
200         d_printf("%s\n", _("Not implemented yet"));
201         return -1;
202 }
203
204 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
205 {
206         d_printf("%s\n", _("Not implemented yet"));
207         return -1;
208 }
209 bool idmap_store_secret(const char *backend, bool alloc,
210                         const char *domain, const char *identity,
211                         const char *secret)
212 {
213         char *tmp;
214         int r;
215         bool ret;
216
217         if (alloc) {
218                 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
219         } else {
220                 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
221         }
222
223         if (r < 0) return false;
224
225         strupper_m(tmp); /* make sure the key is case insensitive */
226         ret = secrets_store_generic(tmp, identity, secret);
227
228         free(tmp);
229         return ret;
230 }
231
232
233 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
234 {
235         TALLOC_CTX *ctx;
236         const char *secret;
237         const char *dn;
238         char *domain;
239         char *backend;
240         char *opt = NULL;
241         bool ret;
242
243         if (argc != 2 || c->display_usage) {
244                 d_printf("%s\n%s",
245                          _("Usage:"),
246                          _("net idmap secret {<DOMAIN>|alloc} <secret>\n"
247                            "  Set the secret for the specified domain "
248                            "(or alloc module)\n"
249                            "    DOMAIN\tDomain to set secret for.\n"
250                            "    alloc\tSet secret for the alloc module\n"
251                            "    secret\tNew secret to set.\n"));
252                 return c->display_usage?0:-1;
253         }
254
255         secret = argv[1];
256
257         ctx = talloc_new(NULL);
258         ALLOC_CHECK(ctx);
259
260         if (strcmp(argv[0], "alloc") == 0) {
261                 domain = NULL;
262                 backend = lp_idmap_alloc_backend();
263         } else {
264                 domain = talloc_strdup(ctx, argv[0]);
265                 ALLOC_CHECK(domain);
266
267                 opt = talloc_asprintf(ctx, "idmap config %s", domain);
268                 ALLOC_CHECK(opt);
269
270                 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
271                 ALLOC_CHECK(backend);
272         }
273
274         if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
275                 d_fprintf(stderr,
276                           _("The only currently supported backend is LDAP\n"));
277                 talloc_free(ctx);
278                 return -1;
279         }
280
281         if (domain) {
282
283                 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
284                 if ( ! dn) {
285                         d_fprintf(stderr,
286                                   _("Missing ldap_user_dn option for domain "
287                                     "%s\n"), domain);
288                         talloc_free(ctx);
289                         return -1;
290                 }
291
292                 ret = idmap_store_secret("ldap", false, domain, dn, secret);
293         } else {
294                 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
295                 if ( ! dn) {
296                         d_fprintf(stderr,
297                                   _("Missing ldap_user_dn option for alloc "
298                                     "backend\n"));
299                         talloc_free(ctx);
300                         return -1;
301                 }
302
303                 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
304         }
305
306         if ( ! ret) {
307                 d_fprintf(stderr, _("Failed to store secret\n"));
308                 talloc_free(ctx);
309                 return -1;
310         }
311
312         d_printf(_("Secret stored\n"));
313         return 0;
314 }
315
316 int net_help_idmap(struct net_context *c, int argc, const char **argv)
317 {
318         d_printf(_("net idmap dump <inputfile>\n"
319                    "    Dump current id mapping\n"));
320
321         d_printf(_("net idmap restore\n"
322                    "    Restore entries from stdin\n"));
323
324         /* Deliberately *not* document net idmap delete */
325
326         d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
327                    "    Set the secret for the specified DOMAIN (or the alloc "
328                    "module)\n"));
329
330         return -1;
331 }
332
333 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
334 {
335         TALLOC_CTX *mem_ctx;
336         int result = -1;
337         struct dom_sid src_sid, dst_sid;
338         char *src, *dst;
339         struct db_context *db;
340         struct db_record *rec;
341         NTSTATUS status;
342
343         if (argc != 3 || c->display_usage) {
344                 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
345                           "<src-sid> <dst-sid>\n", _("Usage:"));
346                 return -1;
347         }
348
349         if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
350                 d_fprintf(stderr, _("talloc_init failed\n"));
351                 return -1;
352         }
353
354         if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
355                            O_RDWR|O_CREAT, 0600))) {
356                 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
357                 goto fail;
358         }
359
360         if (!string_to_sid(&src_sid, argv[1])) {
361                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
362                 goto fail;
363         }
364
365         if (!string_to_sid(&dst_sid, argv[2])) {
366                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
367                 goto fail;
368         }
369
370         if (!(src = sid_string_talloc(mem_ctx, &src_sid))
371             || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
372                 d_fprintf(stderr, _("talloc_strdup failed\n"));
373                 goto fail;
374         }
375
376         if (!(rec = db->fetch_locked(
377                       db, mem_ctx, string_term_tdb_data(src)))) {
378                 d_fprintf(stderr, _("could not fetch db record\n"));
379                 goto fail;
380         }
381
382         status = rec->store(rec, string_term_tdb_data(dst), 0);
383         TALLOC_FREE(rec);
384
385         if (!NT_STATUS_IS_OK(status)) {
386                 d_fprintf(stderr, _("could not store record: %s\n"),
387                           nt_errstr(status));
388                 goto fail;
389         }
390
391         result = 0;
392 fail:
393         TALLOC_FREE(mem_ctx);
394         return result;
395 }
396
397 /***********************************************************
398  Look at the current idmap
399  **********************************************************/
400 int net_idmap(struct net_context *c, int argc, const char **argv)
401 {
402         struct functable func[] = {
403                 {
404                         "dump",
405                         net_idmap_dump,
406                         NET_TRANSPORT_LOCAL,
407                         N_("Dump the current ID mappings"),
408                         N_("net idmap dump\n"
409                            "  Dump the current ID mappings")
410                 },
411                 {
412                         "restore",
413                         net_idmap_restore,
414                         NET_TRANSPORT_LOCAL,
415                         N_("Restore entries from stdin"),
416                         N_("net idmap restore\n"
417                            "  Restore entries from stdin")
418                 },
419                 {
420                         "setmap",
421                         net_idmap_set,
422                         NET_TRANSPORT_LOCAL,
423                         N_("Not implemented yet"),
424                         N_("net idmap setmap\n"
425                            "  Not implemented yet")
426                 },
427                 {
428                         "delete",
429                         net_idmap_delete,
430                         NET_TRANSPORT_LOCAL,
431                         N_("Not implemented yet"),
432                         N_("net idmap delete\n"
433                            "  Not implemented yet")
434                 },
435                 {
436                         "secret",
437                         net_idmap_secret,
438                         NET_TRANSPORT_LOCAL,
439                         N_("Set secret for specified domain"),
440                         N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
441                            "  Set secret for specified domain or alloc module")
442                 },
443                 {
444                         "aclmapset",
445                         net_idmap_aclmapset,
446                         NET_TRANSPORT_LOCAL,
447                         N_("Set acl map"),
448                         N_("net idmap aclmapset\n"
449                            "  Set acl map")
450                 },
451                 {NULL, NULL, 0, NULL, NULL}
452         };
453
454         return net_run_function(c, argc, argv, "net idmap", func);
455 }
456
457