netcmd/ldapcmp: use set instead of list to find missing DNs
[amitay/samba.git] / source3 / rpc_server / dcesrv_auth_generic.c
1 /*
2  *  NTLMSSP Acceptor
3  *  DCERPC Server functions
4  *  Copyright (C) Simo Sorce 2010.
5  *  Copyright (C) Andrew Bartlett 2011.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21
22 #include "includes.h"
23 #include "rpc_server/dcesrv_auth_generic.h"
24 #include "auth.h"
25 #include "auth/gensec/gensec.h"
26
27 static NTSTATUS auth_generic_server_authtype_start_as_root(TALLOC_CTX *mem_ctx,
28                                                            uint8_t auth_type, uint8_t auth_level,
29                                                            const struct tsocket_address *remote_address,
30                                                            const struct tsocket_address *local_address,
31                                                            const char *service_description,
32                                                            struct gensec_security **ctx)
33 {
34         struct gensec_security *gensec_security = NULL;
35         NTSTATUS status;
36
37         status = auth_generic_prepare(talloc_tos(),
38                                       remote_address,
39                                       local_address,
40                                       service_description,
41                                       &gensec_security);
42         if (!NT_STATUS_IS_OK(status)) {
43                 DEBUG(0, (__location__ ": auth_generic_prepare failed: %s\n",
44                           nt_errstr(status)));
45                 return status;
46         }
47
48         status = gensec_start_mech_by_authtype(gensec_security, auth_type, auth_level);
49         if (!NT_STATUS_IS_OK(status)) {
50                 DEBUG(0, (__location__ ": auth_generic_start failed: %s\n",
51                           nt_errstr(status)));
52                 TALLOC_FREE(gensec_security);
53                 return status;
54         }
55
56         /* steal gensec context to the caller */
57         *ctx = talloc_move(mem_ctx, &gensec_security);
58         return status;
59 }
60
61 NTSTATUS auth_generic_server_authtype_start(TALLOC_CTX *mem_ctx,
62                                             uint8_t auth_type, uint8_t auth_level,
63                                             const struct tsocket_address *remote_address,
64                                             const struct tsocket_address *local_address,
65                                             const char *service_description,
66                                             struct gensec_security **ctx)
67 {
68         NTSTATUS status;
69         become_root();
70
71         /* this has to be done as root in order to create the messaging socket */
72         status = auth_generic_server_authtype_start_as_root(mem_ctx,
73                                                             auth_type, auth_level,
74                                                             remote_address,
75                                                             local_address,
76                                                             service_description,
77                                                             ctx);
78         unbecome_root();
79         return status;
80 }
81
82 NTSTATUS auth_generic_server_step(struct gensec_security *gensec_security,
83                              TALLOC_CTX *mem_ctx,
84                              DATA_BLOB *token_in,
85                              DATA_BLOB *token_out)
86 {
87         NTSTATUS status;
88
89         if (gensec_security == NULL) {
90                 return NT_STATUS_INTERNAL_ERROR;
91         }
92
93         /* this has to be done as root in order to verify the password */
94         become_root();
95         status = gensec_update(gensec_security, mem_ctx, *token_in, token_out);
96         unbecome_root();
97
98         return status;
99 }
100
101 NTSTATUS auth_generic_server_check_flags(struct gensec_security *gensec_security,
102                                     bool do_sign, bool do_seal)
103 {
104         if (do_sign && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
105                 DEBUG(1, (__location__ "Integrity was requested but client "
106                           "failed to negotiate signing.\n"));
107                 return NT_STATUS_ACCESS_DENIED;
108         }
109
110         if (do_seal && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
111                 DEBUG(1, (__location__ "Privacy was requested but client "
112                           "failed to negotiate sealing.\n"));
113                 return NT_STATUS_ACCESS_DENIED;
114         }
115
116         return NT_STATUS_OK;
117 }
118
119 NTSTATUS auth_generic_server_get_user_info(struct gensec_security *gensec_security,
120                                       TALLOC_CTX *mem_ctx,
121                                       struct auth_session_info **session_info)
122 {
123         NTSTATUS status;
124
125         /* this has to be done as root in order to get to the
126          * messaging sockets for IDMAP and privilege.ldb in the AD
127          * DC */
128         become_root();
129         status = gensec_session_info(gensec_security, mem_ctx, session_info);
130         unbecome_root();
131         if (!NT_STATUS_IS_OK(status)) {
132                 DEBUG(1, (__location__ ": Failed to get authenticated user "
133                           "info: %s\n", nt_errstr(status)));
134                 return status;
135         }
136
137         DEBUG(5, (__location__ "OK: user: %s domain: %s\n",
138                   (*session_info)->info->account_name,
139                   (*session_info)->info->domain_name));
140
141         return NT_STATUS_OK;
142 }