2 Unix SMB/Netbios implementation.
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.
27 struct ntuser_creds *usr_creds = NULL;
29 extern int DEBUGLEVEL;
31 extern pstring global_myname;
35 uint32 num_connections;
38 struct ntuser_creds usr_creds;
39 struct cli_state *cli;
43 static struct cli_connection **con_list = NULL;
46 void init_connections(void)
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);
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));
88 memset(con, 0, sizeof(*con));
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, True, reuse);
101 if (con->cli == NULL)
103 cli_connection_free(con);
106 add_con_to_array(&num_cons, &con_list, con);
110 /****************************************************************************
111 terminate client connection
112 ****************************************************************************/
113 void cli_connection_free(struct cli_connection *con)
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);
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;
141 if (con->srv_name != NULL)
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])
165 /****************************************************************************
166 terminate client state
167 ****************************************************************************/
168 void cli_connection_unlink(struct cli_connection *con)
172 cli_connection_free(con);
177 /****************************************************************************
179 ****************************************************************************/
180 BOOL cli_connection_init(const char* srv_name, const char* pipe_name,
181 struct cli_connection **con)
190 *con = cli_con_get(srv_name, pipe_name, reuse);
197 res = res ? cli_nt_session_open((*con)->cli, pipe_name,
198 &(*con)->fnum) : False;
203 /****************************************************************************
205 ****************************************************************************/
206 BOOL cli_connection_getsrv(const char* srv_name, const char* pipe_name,
207 struct cli_connection **con)
210 if (con_list == NULL || num_cons == 0)
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];
228 /****************************************************************************
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))
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
254 ****************************************************************************/
255 BOOL cli_get_con_usr_sesskey(struct cli_connection *con, uchar usr_sess_key[16])
261 memcpy(usr_sess_key, con->cli->usr.pwd.sess_key, 16);
266 /****************************************************************************
267 get a user session key associated with a connection associated with a
269 ****************************************************************************/
270 BOOL cli_get_con_sesskey(struct cli_connection *con, uchar sess_key[16])
276 memcpy(sess_key, con->cli->sess_key, sizeof(con->cli->sess_key));
281 /****************************************************************************
282 get a user session key associated with a connection associated with a
284 ****************************************************************************/
285 BOOL cli_con_get_srvname(struct cli_connection *con, char *srv_name)
292 if (strnequal("\\\\", con->cli->desthost, 2))
294 fstrcpy(srv_name, con->cli->desthost);
298 fstrcpy(srv_name, "\\\\");
299 fstrcat(srv_name, con->cli->desthost);
305 /****************************************************************************
306 get a user session key associated with a connection associated with a
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))
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
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))
334 return cli_get_con_sesskey(con, sess_key);
337 /****************************************************************************
338 get a user session key associated with a connection associated with a
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))
350 return cli_get_con_sesskey(con, sess_key);
353 /****************************************************************************
354 get a user session key associated with a connection associated with a
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
366 ****************************************************************************/
367 void cli_con_get_cli_cred(struct cli_connection *con,
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
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,
384 /****************************************************************************
385 get a user session key associated with a connection associated with a
387 ****************************************************************************/
388 BOOL cli_con_set_creds(const char* srv_name, const uchar sess_key[16],
391 struct cli_connection *con = NULL;
393 if (!cli_connection_getsrv(srv_name, PIPE_NETLOGON, &con))
398 memcpy(con->cli->sess_key, sess_key, 16);
399 memcpy(&con->cli->clnt_cred, cred, sizeof(*cred));
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))
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);