first pass at merging rpcclient from TNG to HEAD. You can get a
[sfrench/samba-autobuild/.git] / source / rpc_client / ncacn_np_use.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client generic functions
5    Copyright (C) Andrew Tridgell              1994-2000
6    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
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 #define NO_SYSLOG
24
25 #include "includes.h"
26 #include "rpc_parse.h"
27 #include "rpc_client.h"
28 #include "trans2.h"
29
30 extern int DEBUGLEVEL;
31 extern pstring global_myname;
32
33 struct ncacn_np_use
34 {
35         struct ncacn_np *cli;
36         uint32 num_users;
37 };
38
39 static struct ncacn_np_use **msrpcs = NULL;
40 static uint32 num_msrpcs = 0;
41
42 /****************************************************************************
43 terminate client connection
44 ****************************************************************************/
45 static void ncacn_np_shutdown(struct ncacn_np *cli)
46 {
47         if (cli != NULL)
48         {
49                 if (cli->smb != NULL)
50                 {
51                         if (cli->smb->initialised)
52                         {
53                                 /* cli_nt_session_close(cli->smb, cli->fnum); JERRY */
54                                 cli_nt_session_close(cli->smb);
55                         }
56 #if 0  /* commented out by JERRY */
57                         cli_net_use_del(cli->smb->desthost,
58                                         &cli->smb->usr, False, False);
59 #endif
60                 }
61         }
62 }
63
64
65 /****************************************************************************
66 terminate client connection
67 ****************************************************************************/
68 static void ncacn_np_use_free(struct ncacn_np_use *cli)
69 {
70         if (cli->cli != NULL)
71         {
72                 if (cli->cli->initialised)
73                 {
74                         ncacn_np_shutdown(cli->cli);
75                 }
76                 ZERO_STRUCTP(cli->cli);
77                 free(cli->cli);
78         }
79         ZERO_STRUCTP(cli);
80         free(cli);
81 }
82
83
84 /****************************************************************************
85 delete a client state
86 ****************************************************************************/
87 BOOL ncacn_np_use_del(const char *srv_name, const char *pipe_name,
88                       const vuser_key * key,
89                       BOOL force_close, BOOL *connection_closed)
90 {
91         int i;
92         DEBUG(10, ("ncacn_np_net_use_del: %s. force close: %s ",
93                    pipe_name, BOOLSTR(force_close)));
94         if (key != NULL)
95         {
96                 DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
97         }
98         DEBUG(10, ("\n"));
99
100         if (connection_closed != NULL)
101         {
102                 *connection_closed = False;
103         }
104
105         if (strnequal("\\PIPE\\", pipe_name, 6))
106         {
107                 pipe_name = &pipe_name[6];
108         }
109
110         if (strnequal("\\\\", srv_name, 2))
111         {
112                 srv_name = &srv_name[6];
113         }
114
115         for (i = 0; i < num_msrpcs; i++)
116         {
117                 char *ncacn_np_name = NULL;
118                 char *ncacn_np_srv_name = NULL;
119                 struct ncacn_np_use *c = msrpcs[i];
120                 vuser_key k;
121
122                 if (c == NULL || c->cli == NULL || c->cli->smb == NULL)
123                         continue;
124
125                 ncacn_np_name = c->cli->pipe_name;
126                 ncacn_np_srv_name = c->cli->smb->desthost;
127
128                 k = c->cli->smb->key;
129
130                 DEBUG(10, ("use_del[%d]: %s %s %s %s [%d,%x]\n",
131                            i, ncacn_np_name, ncacn_np_srv_name,
132                            c->cli->smb->user_name,
133                            c->cli->smb->domain, k.pid, k.vuid));
134
135                 if (strnequal("\\PIPE\\", ncacn_np_name, 6))
136                 {
137                         ncacn_np_name = &ncacn_np_name[6];
138                 }
139                 if (!strequal(ncacn_np_srv_name, srv_name))
140                 {
141                         continue;
142                 }
143                 if (strnequal("\\\\", ncacn_np_srv_name, 2))
144                 {
145                         ncacn_np_srv_name = &ncacn_np_srv_name[6];
146                 }
147                 if (!strequal(ncacn_np_name, pipe_name))
148                 {
149                         continue;
150                 }
151                 if (key->pid != k.pid || key->vuid != k.vuid)
152                 {
153                         continue;
154                 }
155                 /* decrement number of users */
156                 c->num_users--;
157                 DEBUG(10, ("idx: %i num_users now: %d\n",
158                            i, c->num_users));
159                 if (force_close || c->num_users == 0)
160                 {
161                         ncacn_np_use_free(c);
162                         msrpcs[i] = NULL;
163                         if (connection_closed != NULL)
164                         {
165                                 *connection_closed = True;
166                         }
167                 }
168                 return True;
169         }
170
171         return False;
172 }
173