OK. This code works on a RedHat 6.0 system. However smbpasswd
[Samba/gbeck.git] / source / rpc_client / cli_connect.c
blobfcca3e44fffcd20d9993002a24662a10455af456
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB client generic functions
5 Copyright (C) Andrew Tridgell 1994-1999
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1999
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.
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.
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.
23 #define NO_SYSLOG
25 #include "includes.h"
27 struct ntuser_creds *usr_creds = NULL;
29 extern int DEBUGLEVEL;
30 extern pstring scope;
31 extern pstring global_myname;
33 struct cli_connection
35 uint32 num_connections;
36 char *srv_name;
37 char *pipe_name;
38 struct ntuser_creds usr_creds;
39 struct cli_state *cli;
40 uint16 fnum;
43 static struct cli_connection **con_list = NULL;
44 uint32 num_cons = 0;
46 void init_connections(void)
48 con_list = NULL;
49 num_cons = 0;
51 init_cli_use();
54 static void free_con_array(uint32 num_entries, struct cli_connection **entries)
56 void(*fn)(void*) = (void(*)(void*))&cli_connection_free;
57 free_void_array(num_entries, (void**)entries, *fn);
60 static struct cli_connection* add_con_to_array(uint32 *len,
61 struct cli_connection ***array,
62 struct cli_connection *con)
64 return (struct cli_connection*)add_item_to_array(len,
65 (void***)array, (void*)con);
68 void free_connections(void)
70 free_con_array(num_cons, con_list);
71 free_cli_use();
73 init_connections();
76 static struct cli_connection *cli_con_get(const char* srv_name,
77 const char* pipe_name, BOOL reuse)
79 struct cli_connection *con = NULL;
81 con = (struct cli_connection*)malloc(sizeof(*con));
83 if (con == NULL)
85 return NULL;
88 memset(con, 0, sizeof(*con));
90 if (srv_name != NULL)
92 con->srv_name = strdup(srv_name);
94 if (pipe_name != NULL)
96 con->pipe_name = strdup(pipe_name);
99 con->cli = cli_net_use_add(srv_name, usr_creds, False, reuse);
101 if (con->cli == NULL)
103 cli_connection_free(con);
104 return NULL;
106 add_con_to_array(&num_cons, &con_list, con);
107 return con;
110 /****************************************************************************
111 terminate client connection
112 ****************************************************************************/
113 void cli_connection_free(struct cli_connection *con)
115 BOOL closed;
116 int i;
118 if (con->cli != NULL)
120 cli_nt_session_close(con->cli, con->fnum);
121 cli_net_use_del(con->srv_name, &con->usr_creds, False, &closed);
124 if (closed)
126 for (i = 0; i < num_cons; i++)
128 if (con_list[i] != NULL &&
129 con != con_list[i] &&
130 con_list[i]->cli == con->cli)
132 /* WHOOPS! fnum already open: too bad!!! */
133 con_list[i]->cli = NULL;
134 con_list[i]->fnum = 0xffff;
139 con->cli = NULL;
141 if (con->srv_name != NULL)
143 free(con->srv_name);
144 con->srv_name = NULL;
146 if (con->pipe_name != NULL)
148 free(con->pipe_name);
149 con->pipe_name = NULL;
152 memset(&con->usr_creds, 0, sizeof(con->usr_creds));
154 for (i = 0; i < num_cons; i++)
156 if (con == con_list[i])
158 con_list[i] = NULL;
162 free(con);
165 /****************************************************************************
166 terminate client state
167 ****************************************************************************/
168 void cli_connection_unlink(struct cli_connection *con)
170 if (con != NULL)
172 cli_connection_free(con);
174 return;
177 /****************************************************************************
178 init client state
179 ****************************************************************************/
180 BOOL cli_connection_init(const char* srv_name, const char* pipe_name,
181 struct cli_connection **con)
183 BOOL res = True;
184 BOOL reuse = False;
187 * allocate
190 *con = cli_con_get(srv_name, pipe_name, reuse);
192 if ((*con) == NULL)
194 return False;
197 res = res ? cli_nt_session_open((*con)->cli, pipe_name,
198 &(*con)->fnum) : False;
200 return res;
203 /****************************************************************************
204 obtain client state
205 ****************************************************************************/
206 BOOL cli_connection_getsrv(const char* srv_name, const char* pipe_name,
207 struct cli_connection **con)
209 int i;
210 if (con_list == NULL || num_cons == 0)
212 return False;
215 for (i = 0; i < num_cons; i++)
217 if (con_list[i] != NULL &&
218 strequal(con_list[i]->srv_name , srv_name ) &&
219 strequal(con_list[i]->pipe_name, pipe_name))
221 (*con) = con_list[i];
222 return True;
225 return False;
228 /****************************************************************************
229 obtain client state
230 ****************************************************************************/
231 BOOL cli_connection_get(const POLICY_HND *pol, struct cli_connection **con)
233 return get_policy_con(pol, con);
236 /****************************************************************************
237 link a child policy handle to a parent one
238 ****************************************************************************/
239 BOOL cli_pol_link(POLICY_HND *to, const POLICY_HND *from)
241 struct cli_connection *con = NULL;
243 if (!cli_connection_get(from, &con))
245 return False;
248 return register_policy_hnd(to) && set_policy_con(to, con, NULL);
251 /****************************************************************************
252 get a user session key associated with a connection associated with a
253 policy handle.
254 ****************************************************************************/
255 BOOL cli_get_con_usr_sesskey(struct cli_connection *con, uchar usr_sess_key[16])
257 if (con == NULL)
259 return False;
261 memcpy(usr_sess_key, con->cli->usr.pwd.sess_key, 16);
263 return True;
266 /****************************************************************************
267 get a user session key associated with a connection associated with a
268 policy handle.
269 ****************************************************************************/
270 BOOL cli_get_con_sesskey(struct cli_connection *con, uchar sess_key[16])
272 if (con == NULL)
274 return False;
276 memcpy(sess_key, con->cli->sess_key, sizeof(con->cli->sess_key));
278 return True;
281 /****************************************************************************
282 get a user session key associated with a connection associated with a
283 policy handle.
284 ****************************************************************************/
285 BOOL cli_con_get_srvname(struct cli_connection *con, char *srv_name)
287 if (con == NULL)
289 return False;
292 if (strnequal("\\\\", con->cli->desthost, 2))
294 fstrcpy(srv_name, con->cli->desthost);
296 else
298 fstrcpy(srv_name, "\\\\");
299 fstrcat(srv_name, con->cli->desthost);
302 return True;
305 /****************************************************************************
306 get a user session key associated with a connection associated with a
307 policy handle.
308 ****************************************************************************/
309 BOOL cli_get_usr_sesskey(const POLICY_HND *pol, uchar usr_sess_key[16])
311 struct cli_connection *con = NULL;
313 if (!cli_connection_get(pol, &con))
315 return False;
318 return cli_get_con_usr_sesskey(con, usr_sess_key);
321 /****************************************************************************
322 get a user session key associated with a connection associated with a
323 policy handle.
324 ****************************************************************************/
325 BOOL cli_get_sesskey(const POLICY_HND *pol, uchar sess_key[16])
327 struct cli_connection *con = NULL;
329 if (!cli_connection_get(pol, &con))
331 return False;
334 return cli_get_con_sesskey(con, sess_key);
337 /****************************************************************************
338 get a user session key associated with a connection associated with a
339 policy handle.
340 ****************************************************************************/
341 BOOL cli_get_sesskey_srv(const char* srv_name, uchar sess_key[16])
343 struct cli_connection *con = NULL;
345 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
347 return False;
350 return cli_get_con_sesskey(con, sess_key);
353 /****************************************************************************
354 get a user session key associated with a connection associated with a
355 policy handle.
356 ****************************************************************************/
357 void cli_con_gen_next_creds(struct cli_connection *con,
358 DOM_CRED *new_clnt_cred)
360 gen_next_creds(con->cli, new_clnt_cred);
363 /****************************************************************************
364 get a user session key associated with a connection associated with a
365 policy handle.
366 ****************************************************************************/
367 void cli_con_get_cli_cred(struct cli_connection *con,
368 DOM_CRED *clnt_cred)
370 memcpy(clnt_cred, &con->cli->clnt_cred, sizeof(*clnt_cred));
373 /****************************************************************************
374 get a user session key associated with a connection associated with a
375 policy handle.
376 ****************************************************************************/
377 BOOL cli_con_deal_with_creds(struct cli_connection *con,
378 DOM_CRED *rcv_srv_cred)
380 return clnt_deal_with_creds(con->cli->sess_key, &con->cli->clnt_cred,
381 rcv_srv_cred);
384 /****************************************************************************
385 get a user session key associated with a connection associated with a
386 policy handle.
387 ****************************************************************************/
388 BOOL cli_con_set_creds(const char* srv_name, const uchar sess_key[16],
389 DOM_CRED *cred)
391 struct cli_connection *con = NULL;
393 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
395 return False;
398 memcpy(con->cli->sess_key, sess_key, 16);
399 memcpy(&con->cli->clnt_cred, cred, sizeof(*cred));
401 return True;
404 /****************************************************************************
405 send a request on an rpc pipe.
406 ****************************************************************************/
407 BOOL rpc_hnd_pipe_req(const POLICY_HND *hnd, uint8 op_num,
408 prs_struct *data, prs_struct *rdata)
410 struct cli_connection *con = NULL;
412 if (!cli_connection_get(hnd, &con))
414 return False;
417 return rpc_con_pipe_req(con, op_num, data, rdata);
420 /****************************************************************************
421 send a request on an rpc pipe.
422 ****************************************************************************/
423 BOOL rpc_con_pipe_req(struct cli_connection *con, uint8 op_num,
424 prs_struct *data, prs_struct *rdata)
426 return rpc_api_pipe_req(con->cli, con->fnum, op_num, data, rdata);