vfs_gpfs: Use full_path_tos instead of talloc_asprintf
[Samba.git] / source4 / param / secrets.c
blob987408829ea5bad4caa689cb48d78c3a3c1a3021
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 1992-2001
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Rafal Szczesniak 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* the Samba secrets database stores any generated, private information
22 such as the local SID and machine trust password */
24 #include "includes.h"
25 #include "secrets.h"
26 #include "param/param.h"
27 #include "system/filesys.h"
28 #include "lib/tdb_wrap/tdb_wrap.h"
29 #include "lib/ldb-samba/ldb_wrap.h"
30 #include <ldb.h>
31 #include "../lib/util/util_tdb.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "dsdb/samdb/samdb.h"
35 /**
36 connect to the secrets ldb
38 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx,
39 struct loadparm_context *lp_ctx)
41 return ldb_wrap_connect(mem_ctx, NULL, lp_ctx, "secrets.ldb",
42 NULL, NULL, 0);
45 /**
46 * Retrieve the domain SID from the secrets database.
47 * @return pointer to a SID object if the SID could be obtained, NULL otherwise
49 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
50 struct loadparm_context *lp_ctx,
51 const char *domain,
52 enum netr_SchannelType *sec_channel_type,
53 char **errstring)
55 struct ldb_context *ldb;
56 struct ldb_message *msg;
57 int ldb_ret;
58 const char *attrs[] = { "objectSid", "secureChannelType", NULL };
59 struct dom_sid *result = NULL;
60 const struct ldb_val *v;
61 enum ndr_err_code ndr_err;
63 *errstring = NULL;
65 ldb = secrets_db_connect(mem_ctx, lp_ctx);
66 if (ldb == NULL) {
67 DEBUG(5, ("secrets_db_connect failed\n"));
68 return NULL;
71 ldb_ret = dsdb_search_one(ldb, ldb, &msg,
72 ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN),
73 LDB_SCOPE_ONELEVEL,
74 attrs, 0, SECRETS_PRIMARY_DOMAIN_FILTER, domain);
76 if (ldb_ret != LDB_SUCCESS) {
77 *errstring = talloc_asprintf(mem_ctx, "Failed to find record for %s in %s: %s: %s",
78 domain, (char *) ldb_get_opaque(ldb, "ldb_url"),
79 ldb_strerror(ldb_ret), ldb_errstring(ldb));
80 return NULL;
82 v = ldb_msg_find_ldb_val(msg, "objectSid");
83 if (v == NULL) {
84 *errstring = talloc_asprintf(mem_ctx, "Failed to find a SID on record for %s in %s",
85 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
86 return NULL;
89 if (sec_channel_type) {
90 int t;
91 t = ldb_msg_find_attr_as_int(msg, "secureChannelType", -1);
92 if (t == -1) {
93 *errstring = talloc_asprintf(mem_ctx, "Failed to find secureChannelType for %s in %s",
94 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
95 return NULL;
97 *sec_channel_type = t;
100 result = talloc(mem_ctx, struct dom_sid);
101 if (result == NULL) {
102 talloc_free(ldb);
103 return NULL;
106 ndr_err = ndr_pull_struct_blob(v, result, result,
107 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 *errstring = talloc_asprintf(mem_ctx, "Failed to parse SID on record for %s in %s",
110 domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
111 talloc_free(result);
112 talloc_free(ldb);
113 return NULL;
116 return result;
119 char *keytab_name_from_msg(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct ldb_message *msg)
121 const char *krb5keytab = ldb_msg_find_attr_as_string(msg, "krb5Keytab", NULL);
122 if (krb5keytab) {
123 return talloc_strdup(mem_ctx, krb5keytab);
124 } else {
125 char *file_keytab;
126 char *relative_path;
127 const char *privateKeytab = ldb_msg_find_attr_as_string(msg, "privateKeytab", NULL);
128 if (!privateKeytab) {
129 return NULL;
132 relative_path = ldb_relative_path(ldb, mem_ctx, privateKeytab);
133 if (!relative_path) {
134 return NULL;
136 file_keytab = talloc_asprintf(mem_ctx, "FILE:%s", relative_path);
137 talloc_free(relative_path);
138 return file_keytab;
140 return NULL;