2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Simo Sorce 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "auth/gensec/gensec.h"
29 #include "system/time.h"
37 return 1 if a specific attribute has been requested
39 static int do_attribute(const char * const *attrs, const char *name)
41 return attrs == NULL ||
42 ldb_attr_in_list(attrs, name) ||
43 ldb_attr_in_list(attrs, "*");
47 add dynamically generated attributes to rootDSE result
49 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_request *req)
51 struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
52 struct ldb_search *s = &req->op.search;
53 struct ldb_message *msg;
54 struct cli_credentials *server_creds;
56 /* this is gross, and will be removed when I change ldb_result not
57 to be so pointer crazy :-) */
58 if (s->res->msgs == NULL) {
62 msg = s->res->msgs[0];
64 msg->dn = ldb_dn_explode(msg, "");
66 if (do_attribute(s->attrs, "currentTime")) {
67 if (ldb_msg_add_string(msg, "currentTime",
68 ldb_timestring(msg, time(NULL))) != 0) {
73 if (do_attribute(s->attrs, "supportedControl")) {
75 for (i = 0; i < priv->num_controls; i++) {
76 if (ldb_msg_add_string(msg, "supportedControl",
77 priv->controls[i]) != 0) {
83 server_creds = talloc_get_type(ldb_get_opaque(module->ldb, "server_credentials"),
84 struct cli_credentials);
85 if (do_attribute(s->attrs, "supportedSASLMechanisms")) {
86 struct gensec_security_ops **backends = gensec_security_all();
87 enum credentials_use_kerberos use_kerberos
88 = cli_credentials_get_kerberos_state(server_creds);
89 struct gensec_security_ops **ops
90 = gensec_use_kerberos_mechs(req, backends, use_kerberos);
92 for (i = 0; ops && ops[i]; i++) {
93 if (ops[i]->sasl_name) {
94 const char *sasl_name = talloc_strdup(msg, ops[i]->sasl_name);
98 if (ldb_msg_add_string(msg, "supportedSASLMechanisms",
106 /* TODO: lots more dynamic attributes should be added here */
111 return LDB_ERR_OPERATIONS_ERROR;
115 handle search requests
117 static int rootdse_search_bytree(struct ldb_module *module, struct ldb_request *req)
119 struct ldb_search *s = &req->op.search;
123 /* see if its for the rootDSE */
124 if (s->scope != LDB_SCOPE_BASE ||
125 (s->base && s->base->comp_num != 0)) {
126 return ldb_next_request(module, req);
129 tmp_ctx = talloc_new(module);
131 /* in our db we store the rootDSE with a DN of cn=rootDSE */
132 s->base = ldb_dn_explode(tmp_ctx, "cn=rootDSE");
133 s->tree = ldb_parse_tree(tmp_ctx, "dn=*");
134 if (s->base == NULL || s->tree == NULL) {
135 ldb_oom(module->ldb);
136 talloc_free(tmp_ctx);
137 return LDB_ERR_OPERATIONS_ERROR;
140 /* grab the static contents of the record */
141 ret = ldb_next_request(module, req);
143 req->op.search.res = s->res;
145 if (ret == LDB_SUCCESS) {
146 ret = rootdse_add_dynamic(module, req);
149 talloc_free(tmp_ctx);
154 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
156 struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
159 list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
161 return LDB_ERR_OPERATIONS_ERROR;
164 list[priv->num_controls] = talloc_strdup(list, req->op.reg.oid);
165 if (!list[priv->num_controls]) {
166 return LDB_ERR_OPERATIONS_ERROR;
169 priv->num_controls += 1;
170 priv->controls = list;
176 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
178 switch (req->operation) {
180 return rootdse_search_bytree(module, req);
181 case LDB_REQ_REGISTER:
182 return rootdse_register_control(module, req);
186 return ldb_next_request(module, req);
189 static const struct ldb_module_ops rootdse_ops = {
191 .request = rootdse_request
194 struct ldb_module *rootdse_module_init(struct ldb_context *ldb, const char *options[])
196 struct ldb_module *ctx;
197 struct private_data *data;
199 ctx = talloc(ldb, struct ldb_module);
203 data = talloc(ctx, struct private_data);
209 data->num_controls = 0;
210 data->controls = NULL;
211 ctx->private_data = data;
214 ctx->prev = ctx->next = NULL;
215 ctx->ops = &rootdse_ops;