repl_secret: Error condition should sound harmless
[nivanova/samba-autobuild/.git] / source4 / dsdb / repl / drepl_secret.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    DSDB replication service - repl secret handling
5
6    Copyright (C) Andrew Tridgell 2010
7    Copyright (C) Andrew Bartlett 2010
8
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 3 of the License, or
12    (at your option) any later version.
13
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.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "includes.h"
25 #include "ldb_module.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "smbd/service.h"
28 #include "dsdb/repl/drepl_service.h"
29 #include "param/param.h"
30
31 struct repl_secret_state {
32         const char *user_dn;
33 };
34
35 /*
36   called when a repl secret has completed
37  */
38 static void drepl_repl_secret_callback(struct dreplsrv_service *service,
39                                        WERROR werr,
40                                        enum drsuapi_DsExtendedError ext_err,
41                                        void *cb_data)
42 {
43         struct repl_secret_state *state = talloc_get_type_abort(cb_data, struct repl_secret_state);
44         if (!W_ERROR_IS_OK(werr)) {
45                 if (W_ERROR_EQUAL(werr, WERR_DS_DRA_SECRETS_DENIED)) {
46                         DEBUG(3,(__location__ ": repl secret disallowed for user "
47                                  "%s - not in allowed replication group\n",
48                                  state->user_dn));
49                 } else {
50                         DEBUG(3,(__location__ ": repl secret failed for user %s - %s: extended_ret[0x%X]\n",
51                                  state->user_dn, win_errstr(werr), ext_err));
52                 }
53         } else {
54                 DEBUG(3,(__location__ ": repl secret completed OK for '%s'\n", state->user_dn));
55         }
56         talloc_free(state);
57 }
58
59
60 /**
61  * Called when the auth code wants us to try and replicate
62  * a users secrets
63  */
64 void drepl_repl_secret(struct dreplsrv_service *service,
65                        const char *user_dn)
66 {
67         WERROR werr;
68         struct ldb_dn *nc_dn, *nc_root, *source_dsa_dn;
69         struct dreplsrv_partition *p;
70         struct GUID *source_dsa_guid;
71         struct repl_secret_state *state;
72         int ret;
73
74         state = talloc_zero(service, struct repl_secret_state);
75         if (state == NULL) {
76                 /* nothing to do, no return value */
77                 return;
78         }
79
80         /* keep a copy for logging in the callback */
81         state->user_dn = talloc_strdup(state, user_dn);
82
83         nc_dn = ldb_dn_new(state, service->samdb, user_dn);
84         if (!ldb_dn_validate(nc_dn)) {
85                 DEBUG(0,(__location__ ": Failed to parse user_dn '%s'\n", user_dn));
86                 talloc_free(state);
87                 return;
88         }
89
90         /* work out which partition this is in */
91         ret = dsdb_find_nc_root(service->samdb, state, nc_dn, &nc_root);
92         if (ret != LDB_SUCCESS) {
93                 DEBUG(0,(__location__ ": Failed to find nc_root for user_dn '%s'\n", user_dn));
94                 talloc_free(state);
95                 return;
96         }
97
98         /* find the partition in our list */
99         for (p=service->partitions; p; p=p->next) {
100                 if (ldb_dn_compare(p->dn, nc_root) == 0) {
101                         break;
102                 }
103         }
104         if (p == NULL) {
105                 DEBUG(0,(__location__ ": Failed to find partition for nc_root '%s'\n", ldb_dn_get_linearized(nc_root)));
106                 talloc_free(state);
107                 return;
108         }
109
110         if (p->sources == NULL) {
111                 DEBUG(0,(__location__ ": No sources for nc_root '%s' for user_dn '%s'\n",
112                          ldb_dn_get_linearized(nc_root), user_dn));
113                 talloc_free(state);
114                 return;
115         }
116
117         /* use the first source, for no particularly good reason */
118         source_dsa_guid = &p->sources->repsFrom1->source_dsa_obj_guid;
119
120         source_dsa_dn = ldb_dn_new(state, service->samdb,
121                                    talloc_asprintf(state, "<GUID=%s>",
122                                                    GUID_string(state, source_dsa_guid)));
123         if (!ldb_dn_validate(source_dsa_dn)) {
124                 DEBUG(0,(__location__ ": Invalid source DSA GUID '%s' for user_dn '%s'\n",
125                          GUID_string(state, source_dsa_guid), user_dn));
126                 talloc_free(state);
127                 return;
128         }
129
130         werr = drepl_request_extended_op(service,
131                                          nc_dn,
132                                          source_dsa_dn,
133                                          DRSUAPI_EXOP_REPL_SECRET,
134                                          0,
135                                          p->sources->repsFrom1->highwatermark.highest_usn,
136                                          drepl_repl_secret_callback, state);
137         if (!W_ERROR_IS_OK(werr)) {
138                 DEBUG(2,(__location__ ": Failed to setup secret replication for user_dn '%s'\n", user_dn));
139                 talloc_free(state);
140                 return;
141         }
142         DEBUG(3,(__location__ ": started secret replication for %s\n", user_dn));
143 }