added rpcclient program
[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,
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, 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,
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, 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                                         fstrcpy(domain_name, dom_name);
152                                         sid_to_string(domain_sid, &(r_q.dom.id3.dom_sid.sid));
153
154                                         valid_response = True;
155                                         break;
156                                 }
157                                 case 5:
158                                 {
159                                         char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
160                                                                   r_q.dom.id5.uni_domain_name.uni_str_len);
161                                         fstrcpy(domain_name, dom_name);
162                                         sid_to_string(domain_sid, &(r_q.dom.id5.dom_sid.sid));
163
164                                         valid_response = True;
165                                         break;
166                                 }
167                                 default:
168                                 {
169                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
170                                         domain_name[0] = 0;
171                                         domain_sid [0] = 0;
172
173                                         break;
174                                 }
175                         }
176                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
177                                   r_q.info_class, domain_name, domain_sid));
178                 }
179         }
180
181         prs_mem_free(&rbuf);
182         prs_mem_free(&buf );
183
184         return valid_response;
185 }
186
187 /****************************************************************************
188 do a LSA Close
189 ****************************************************************************/
190 BOOL do_lsa_close(struct cli_state *cli, POLICY_HND *hnd)
191 {
192         prs_struct rbuf;
193         prs_struct buf; 
194         LSA_Q_CLOSE q_c;
195     BOOL valid_close = False;
196
197         if (hnd == NULL) return False;
198
199         /* create and send a MSRPC command with api LSA_OPENPOLICY */
200
201         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
202         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
203
204         DEBUG(4,("LSA Close\n"));
205
206         /* store the parameters */
207         make_lsa_q_close(&q_c, hnd);
208
209         /* turn parameters into data stream */
210         lsa_io_q_close("", &q_c, &buf, 0);
211
212         /* send the data on \PIPE\ */
213         if (rpc_api_pipe_req(cli, LSA_CLOSE, &buf, &rbuf))
214         {
215                 LSA_R_CLOSE r_c;
216                 BOOL p;
217
218                 lsa_io_r_close("", &r_c, &rbuf, 0);
219                 p = rbuf.offset != 0;
220
221                 if (p && r_c.status != 0)
222                 {
223                         /* report error code */
224                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
225                         p = False;
226                 }
227
228                 if (p)
229                 {
230                         /* check that the returned policy handle is all zeros */
231                         int i;
232                         valid_close = True;
233
234                         for (i = 0; i < sizeof(r_c.pol.data); i++)
235                         {
236                                 if (r_c.pol.data[i] != 0)
237                                 {
238                                         valid_close = False;
239                                         break;
240                                 }
241                         }       
242                         if (!valid_close)
243                         {
244                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
245                         }
246                 }
247         }
248
249         prs_mem_free(&rbuf);
250         prs_mem_free(&buf );
251
252         return valid_close;
253 }
254
255