"For I have laboured mightily on Luke's code, and hath broken
[tprouty/samba.git] / source3 / rpc_client / cli_lsarpc.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34
35 /****************************************************************************
36 do a LSA Open Policy
37 ****************************************************************************/
38 BOOL do_lsa_open_policy(struct cli_state *cli, int t_idx, uint16 fnum,
39                         char *server_name, POLICY_HND *hnd)
40 {
41         prs_struct rbuf;
42         prs_struct buf; 
43         LSA_Q_OPEN_POL q_o;
44     BOOL valid_pol = False;
45
46         if (hnd == NULL) return False;
47
48         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
49         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
50
51         /* create and send a MSRPC command with api LSA_OPENPOLICY */
52
53         DEBUG(4,("LSA Open Policy\n"));
54
55         /* store the parameters */
56         make_q_open_pol(&q_o, server_name, 0, 0, 0x1);
57
58         /* turn parameters into data stream */
59         lsa_io_q_open_pol("", &q_o, &buf, 0);
60
61         /* send the data on \PIPE\ */
62         if (rpc_api_pipe_req(cli, t_idx, fnum, LSA_OPENPOLICY, &buf, &rbuf))
63         {
64                 LSA_R_OPEN_POL r_o;
65                 BOOL p;
66
67                 lsa_io_r_open_pol("", &r_o, &rbuf, 0);
68                 p = rbuf.offset != 0;
69
70                 if (p && r_o.status != 0)
71                 {
72                         /* report error code */
73                         DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
74                         p = False;
75                 }
76
77                 if (p)
78                 {
79                         /* ok, at last: we're happy. return the policy handle */
80                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
81                         valid_pol = True;
82                 }
83         }
84
85         prs_mem_free(&rbuf);
86         prs_mem_free(&buf );
87
88         return valid_pol;
89 }
90
91 /****************************************************************************
92 do a LSA Query Info Policy
93 ****************************************************************************/
94 BOOL do_lsa_query_info_pol(struct cli_state *cli, int t_idx, uint16 fnum,
95                         POLICY_HND *hnd, uint16 info_class,
96                         fstring domain_name, fstring domain_sid)
97 {
98         prs_struct rbuf;
99         prs_struct buf; 
100         LSA_Q_QUERY_INFO q_q;
101     BOOL valid_response = False;
102
103         if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
104
105         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
106         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
107
108         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
109
110         DEBUG(4,("LSA Query Info Policy\n"));
111
112         /* store the parameters */
113         make_q_query(&q_q, hnd, info_class);
114
115         /* turn parameters into data stream */
116         lsa_io_q_query("", &q_q, &buf, 0);
117
118         /* send the data on \PIPE\ */
119         if (rpc_api_pipe_req(cli, t_idx, fnum, LSA_QUERYINFOPOLICY, &buf, &rbuf))
120         {
121                 LSA_R_QUERY_INFO r_q;
122                 BOOL p;
123
124                 lsa_io_r_query("", &r_q, &rbuf, 0);
125                 p = rbuf.offset != 0;
126                 
127                 if (p && r_q.status != 0)
128                 {
129                         /* report error code */
130                         DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
131                         p = False;
132                 }
133
134                 if (p && r_q.info_class != q_q.info_class)
135                 {
136                         /* report different info classes */
137                         DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
138                                         q_q.info_class, r_q.info_class));
139                         p = False;
140                 }
141
142                 if (p)
143                 {
144                         /* ok, at last: we're happy. */
145                         switch (r_q.info_class)
146                         {
147                                 case 3:
148                                 {
149                                         char *dom_name = unistrn2(r_q.dom.id3.uni_domain_name.buffer,
150                                                                   r_q.dom.id3.uni_domain_name.uni_str_len);
151                                         char *dom_sid  = dom_sid_to_string(&(r_q.dom.id3.dom_sid.sid));
152                                         fstrcpy(domain_name, dom_name);
153                                         pstrcpy(domain_sid , dom_sid);
154
155                                         valid_response = True;
156                                         break;
157                                 }
158                                 case 5:
159                                 {
160                                         char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
161                                                                   r_q.dom.id5.uni_domain_name.uni_str_len);
162                                         char *dom_sid  = dom_sid_to_string(&(r_q.dom.id5.dom_sid.sid));
163                                         fstrcpy(domain_name, dom_name);
164                                         pstrcpy(domain_sid , dom_sid);
165
166                                         valid_response = True;
167                                         break;
168                                 }
169                                 default:
170                                 {
171                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
172                                         domain_name[0] = 0;
173                                         domain_sid [0] = 0;
174
175                                         break;
176                                 }
177                         }
178                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
179                                   r_q.info_class, domain_name, domain_sid));
180                 }
181         }
182
183         prs_mem_free(&rbuf);
184         prs_mem_free(&buf );
185
186         return valid_response;
187 }
188
189 /****************************************************************************
190 do a LSA Close
191 ****************************************************************************/
192 BOOL do_lsa_close(struct cli_state *cli, int t_idx, uint16 fnum, POLICY_HND *hnd)
193 {
194         prs_struct rbuf;
195         prs_struct buf; 
196         LSA_Q_CLOSE q_c;
197     BOOL valid_close = False;
198
199         if (hnd == NULL) return False;
200
201         /* create and send a MSRPC command with api LSA_OPENPOLICY */
202
203         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
204         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
205
206         DEBUG(4,("LSA Close\n"));
207
208         /* store the parameters */
209         make_lsa_q_close(&q_c, hnd);
210
211         /* turn parameters into data stream */
212         lsa_io_q_close("", &q_c, &buf, 0);
213
214         /* send the data on \PIPE\ */
215         if (rpc_api_pipe_req(cli, t_idx, fnum, LSA_CLOSE, &buf, &rbuf))
216         {
217                 LSA_R_CLOSE r_c;
218                 BOOL p;
219
220                 lsa_io_r_close("", &r_c, &rbuf, 0);
221                 p = rbuf.offset != 0;
222
223                 if (p && r_c.status != 0)
224                 {
225                         /* report error code */
226                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
227                         p = False;
228                 }
229
230                 if (p)
231                 {
232                         /* check that the returned policy handle is all zeros */
233                         int i;
234                         valid_close = True;
235
236                         for (i = 0; i < sizeof(r_c.pol.data); i++)
237                         {
238                                 if (r_c.pol.data[i] != 0)
239                                 {
240                                         valid_close = False;
241                                         break;
242                                 }
243                         }       
244                         if (!valid_close)
245                         {
246                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
247                         }
248                 }
249         }
250
251         prs_mem_free(&rbuf);
252         prs_mem_free(&buf );
253
254         return valid_close;
255 }