fixed some memory leaks in the dcerpc use of ntlmssp signing
[kai/samba.git] / source4 / librpc / rpc / dcerpc_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc authentication operations
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26   do a simple ntlm style authentication on a dcerpc pipe
27 */
28 NTSTATUS dcerpc_bind_auth_ntlm(struct dcerpc_pipe *p,
29                                const char *uuid, unsigned version,
30                                const char *domain,
31                                const char *username,
32                                const char *password)
33 {
34         NTSTATUS status;
35         struct ntlmssp_state *state;
36         TALLOC_CTX *mem_ctx;
37         DATA_BLOB credentials;
38
39         mem_ctx = talloc_init("dcerpc_bind_auth_ntlm");
40         if (!mem_ctx) {
41                 return NT_STATUS_NO_MEMORY;
42         }
43
44         status = ntlmssp_client_start(&state);
45         if (!NT_STATUS_IS_OK(status)) {
46                 return status;
47         }
48
49         status = ntlmssp_set_domain(state, domain);
50         if (!NT_STATUS_IS_OK(status)) {
51                 goto done;
52         }
53         
54         status = ntlmssp_set_username(state, username);
55         if (!NT_STATUS_IS_OK(status)) {
56                 goto done;
57         }
58
59         status = ntlmssp_set_password(state, password);
60         if (!NT_STATUS_IS_OK(status)) {
61                 goto done;
62         }
63
64         p->auth_info = talloc(p->mem_ctx, sizeof(*p->auth_info));
65         if (!p->auth_info) {
66                 status = NT_STATUS_NO_MEMORY;
67                 goto done;
68         }
69
70         p->auth_info->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
71         p->auth_info->auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
72         p->auth_info->auth_pad_length = 0;
73         p->auth_info->auth_reserved = 0;
74         p->auth_info->auth_context_id = random();
75         p->auth_info->credentials = data_blob(NULL, 0);
76         p->ntlmssp_state = NULL;
77
78         status = ntlmssp_update(state, 
79                                 p->auth_info->credentials,
80                                 &credentials);
81         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
82                 goto done;
83         }
84
85         p->auth_info->credentials = data_blob_talloc(mem_ctx, 
86                                                      credentials.data, 
87                                                      credentials.length);
88         data_blob_free(&credentials);
89
90         status = dcerpc_bind_byuuid(p, mem_ctx, uuid, version);
91         if (!NT_STATUS_IS_OK(status)) {
92                 goto done;
93         }
94
95
96         status = ntlmssp_update(state, 
97                                 p->auth_info->credentials, 
98                                 &credentials);
99         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
100                 goto done;
101         }
102
103         p->auth_info->credentials = data_blob_talloc(mem_ctx, 
104                                                      credentials.data, 
105                                                      credentials.length);
106         data_blob_free(&credentials);
107
108         status = dcerpc_auth3(p, mem_ctx);
109
110         if (!NT_STATUS_IS_OK(status)) {
111                 goto done;
112         }
113
114         p->ntlmssp_state = state;
115
116         /* setup for signing */
117         status = ntlmssp_sign_init(state);
118
119 done:
120         talloc_destroy(mem_ctx);
121
122         if (!NT_STATUS_IS_OK(status)) {
123                 p->ntlmssp_state = NULL;
124         }
125
126         return status;
127 }
128
129
130 /*
131   do a non-athenticated dcerpc bind
132 */
133 NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
134                                const char *uuid, unsigned version)
135 {
136         TALLOC_CTX *mem_ctx;
137         NTSTATUS status;
138
139         mem_ctx = talloc_init("dcerpc_bind_auth_ntlm");
140         if (!mem_ctx) {
141                 return NT_STATUS_NO_MEMORY;
142         }
143
144         status = dcerpc_bind_byuuid(p, mem_ctx, uuid, version);
145         talloc_destroy(mem_ctx);
146
147         return status;
148 }