summaryrefslogtreecommitdiff
path: root/source3/rpc_client/ncacn_np_use.c
blob: 0ab2802cc12d602de2a2afb8bd162b44c7c3ebf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB client generic functions
   Copyright (C) Andrew Tridgell              1994-2000
   Copyright (C) Luke Kenneth Casson Leighton 1996-2000

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*************************************************************
 Ever wonder where Luke comes up with some of these names?

	(N)etwork (C)omputing (A)rchitechture ???

 get it?   --jerry
*************************************************************/


#define NO_SYSLOG

#include "includes.h"
#include "rpc_parse.h"
#include "rpc_client.h"
#include "trans2.h"

extern int DEBUGLEVEL;
extern pstring global_myname;

struct ncacn_np_use
{
        struct ncacn_np *cli;
        uint32 num_users;
};

static struct ncacn_np_use **msrpcs = NULL;
static uint32 num_msrpcs = 0;

/****************************************************************************
terminate client connection
****************************************************************************/
static void ncacn_np_shutdown(struct ncacn_np *cli)
{
        if (cli != NULL)
        {
                if (cli->smb != NULL)
                {
                        if (cli->smb->initialised)
                        {
                                /* cli_nt_session_close(cli->smb, cli->fnum); JERRY */
                                cli_nt_session_close(cli->smb);
                        }
#if 0  /* commented out by JERRY */
                        cli_net_use_del(cli->smb->desthost,
                                        &cli->smb->usr, False, False);
#endif
                }
        }
}

static BOOL ncacn_np_establish_connection(struct ncacn_np *cli,
                                   const char *srv_name,
                                   const struct ntuser_creds *ntc,
                                   const char *pipe_name,
                                   BOOL reuse)
{
        BOOL new_smb_conn;
        cli->smb = cli_net_use_add(srv_name, ntc,
                                   True, &new_smb_conn);
        if (cli->smb == NULL)
        {
                return False;
        }
        /* if (!cli_nt_session_open(cli->smb, pipe_name, &cli->fnum))  by JERRY */
        if (!cli_nt_session_open(cli->smb, pipe_name))
        {
                cli_net_use_del(srv_name, ntc, False, NULL);
                return False;
        }
        fstrcpy(cli->pipe_name, pipe_name);
        return True;
}



/****************************************************************************
terminate client connection
****************************************************************************/
static void ncacn_np_use_free(struct ncacn_np_use *cli)
{
        if (cli->cli != NULL)
        {
                if (cli->cli->initialised)
                {
                        ncacn_np_shutdown(cli->cli);
                }
                ZERO_STRUCTP(cli->cli);
                free(cli->cli);
        }
        ZERO_STRUCTP(cli);
        free(cli);
}

/****************************************************************************
add a client state to the array
****************************************************************************/
static struct ncacn_np_use *add_ncacn_np_to_array(uint32 * len,
                                                  struct ncacn_np_use
                                                  ***array,
                                                  struct ncacn_np_use *cli)
{
        int i;
        for (i = 0; i < num_msrpcs; i++)
        {
                if (msrpcs[i] == NULL)
                {
                        msrpcs[i] = cli;
                        return cli;
                }
        }

        return (struct ncacn_np_use *)add_item_to_array(len,
                                                        (void ***)array,
                                                        (void *)cli);

}



/****************************************************************************
delete a client state
****************************************************************************/
BOOL ncacn_np_use_del(const char *srv_name, const char *pipe_name,
                      const vuser_key * key,
                      BOOL force_close, BOOL *connection_closed)
{
        int i;
        DEBUG(10, ("ncacn_np_net_use_del: %s. force close: %s ",
                   pipe_name, BOOLSTR(force_close)));
        if (key != NULL)
        {
                DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
        }
        DEBUG(10, ("\n"));

        if (connection_closed != NULL)
        {
                *connection_closed = False;
        }

        if (strnequal("\\PIPE\\", pipe_name, 6))
        {
                pipe_name = &pipe_name[6];
        }

        if (strnequal("\\\\", srv_name, 2))
        {
                srv_name = &srv_name[6];
        }

        for (i = 0; i < num_msrpcs; i++)
        {
                char *ncacn_np_name = NULL;
                char *ncacn_np_srv_name = NULL;
                struct ncacn_np_use *c = msrpcs[i];
                vuser_key k;

                if (c == NULL || c->cli == NULL || c->cli->smb == NULL)
                        continue;

                ncacn_np_name = c->cli->pipe_name;
                ncacn_np_srv_name = c->cli->smb->desthost;

                k = c->cli->smb->key;

                DEBUG(10, ("use_del[%d]: %s %s %s %s [%d,%x]\n",
                           i, ncacn_np_name, ncacn_np_srv_name,
                           c->cli->smb->user_name,
                           c->cli->smb->domain, k.pid, k.vuid));

                if (strnequal("\\PIPE\\", ncacn_np_name, 6))
                {
                        ncacn_np_name = &ncacn_np_name[6];
                }
                if (!strequal(ncacn_np_srv_name, srv_name))
                {
                        continue;
                }
                if (strnequal("\\\\", ncacn_np_srv_name, 2))
                {
                        ncacn_np_srv_name = &ncacn_np_srv_name[6];
                }
                if (!strequal(ncacn_np_name, pipe_name))
                {
                        continue;
                }
                if (key->pid != k.pid || key->vuid != k.vuid)
                {
                        continue;
                }
                /* decrement number of users */
                c->num_users--;
                DEBUG(10, ("idx: %i num_users now: %d\n",
                           i, c->num_users));
                if (force_close || c->num_users == 0)
                {
                        ncacn_np_use_free(c);
                        msrpcs[i] = NULL;
                        if (connection_closed != NULL)
                        {
                                *connection_closed = True;
                        }
                }
                return True;
        }

        return False;
}

/****************************************************************************
find client state.  server name, user name, domain name and password must all
match.
****************************************************************************/
static struct ncacn_np_use *ncacn_np_find(const char *srv_name,
                                          const char *pipe_name,
                                          const vuser_key * key,
                                          const struct ntuser_creds
                                          *usr_creds, BOOL reuse)
{
        int i;
        const char *sv_name = srv_name;

        if (strnequal("\\PIPE\\", pipe_name, 6))
        {
                pipe_name = &pipe_name[6];
        }

        if (strnequal("\\\\", sv_name, 2))
        {
                sv_name = &sv_name[2];
        }

        if (usr_creds != NULL)
        {
                DEBUG(10, ("ncacn_np_find: %s %s %s",
                           srv_name, usr_creds->user_name, usr_creds->domain));
        }
        else
        {
                DEBUG(10,("ncacn_np_find: %s (no creds)\n", srv_name));
        }

        if (key != NULL)
        {
                DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
        }
        DEBUG(10, ("\n"));

        for (i = 0; i < num_msrpcs; i++)
        {
                char *ncacn_np_srv_name = NULL;
                struct ncacn_np_use *c = msrpcs[i];
                vuser_key k;

                char *ncacn_np_name = NULL;

                if (c == NULL || c->cli == NULL || c->cli->smb == NULL ||
                    c->cli->smb->fd == -1 ||
                    !c->cli->initialised)
                {
                        continue;
                }

                ncacn_np_name = c->cli->pipe_name;
                ncacn_np_srv_name = c->cli->smb->desthost;

                k = c->cli->smb->key;

                DEBUG(10, ("ncacn_np_find[%d]: %s %s %s %s [%d,%x]\n",
                           i, ncacn_np_name, ncacn_np_srv_name,
                           c->cli->smb->user_name,
                           c->cli->smb->domain, k.pid, k.vuid));

                if (strnequal("\\\\", ncacn_np_srv_name, 2))
                {
                        ncacn_np_srv_name = &ncacn_np_srv_name[2];
                }

                if (strnequal("\\PIPE\\", ncacn_np_name, 6))
                {
                        ncacn_np_name = &ncacn_np_name[6];
                }

                if (!strequal(ncacn_np_name, pipe_name))
                {
                        continue;
                }
                if (!strequal(ncacn_np_srv_name, sv_name))
                {
                        continue;
                }
                if (key != NULL && (k.pid != key->pid || k.vuid != key->vuid))
                {
                        continue;
                }
                if (usr_creds == NULL)
                {
                        if (reuse)
                        {
                                return c;
                        }
                        else
                        {
                                continue;
                        }
                }
                if (!strequal
                    (usr_creds->user_name, c->cli->smb->user_name))
                {
                        continue;
                }
                if (!reuse
                    && !pwd_compare(&usr_creds->pwd, &c->cli->smb->pwd))
                {
                        DEBUG(100, ("password doesn't match\n"));
                        continue;
                }
                if (usr_creds->domain[0] == 0)
                {
                        return c;
                }
                if (strequal(usr_creds->domain, c->cli->smb->domain))
                {
                        return c;
                }
        }

        return NULL;
}


/****************************************************************************
initialise a msrpcent structure
****************************************************************************/
struct ncacn_np *ncacn_np_initialise(struct ncacn_np *msrpc,
                                     const vuser_key * key)
{
        if (!msrpc)
        {
                msrpc = (struct ncacn_np *)malloc(sizeof(*msrpc));
                if (!msrpc)
                        return NULL;
                ZERO_STRUCTP(msrpc);
        }

        if (msrpc->initialised)
        {
                ncacn_np_shutdown(msrpc);
        }

        ZERO_STRUCTP(msrpc);

        msrpc->fnum = -1;
        msrpc->initialised = 1;

        return msrpc;
}

/****************************************************************************
create a new client state from user credentials
****************************************************************************/
static struct ncacn_np_use *ncacn_np_use_get(const char *pipe_name,
                                             const vuser_key * key)
{
        struct ncacn_np_use *cli =
                (struct ncacn_np_use *)malloc(sizeof(*cli));

        if (cli == NULL)
        {
                return NULL;
        }

        memset(cli, 0, sizeof(*cli));

        cli->cli = ncacn_np_initialise(NULL, key);

        if (cli->cli == NULL)
        {
                return NULL;
        }

        return cli;
}

/****************************************************************************
init client state
****************************************************************************/
struct ncacn_np *ncacn_np_use_add(const char *pipe_name,
                                  const vuser_key * key,
                                  const char *srv_name,
                                  const struct ntuser_creds *ntc,
                                  BOOL reuse, BOOL *is_new_connection)
{
        struct ncacn_np_use *cli;
        DEBUG(10, ("ncacn_np_use_add: %s\n", pipe_name));

        (*is_new_connection) = False;
        cli = ncacn_np_find(srv_name, pipe_name, key, ntc, reuse);

        if (cli != NULL)
        {
                cli->num_users++;
                return cli->cli;
        }

        /*
         * allocate
         */

        (*is_new_connection) = True;

        cli = ncacn_np_use_get(pipe_name, key);

        if (!ncacn_np_establish_connection
            (cli->cli, srv_name, ntc, pipe_name, True))
        {
                DEBUG(0, ("ncacn_np_use_add: connection failed\n"));
                cli->cli = NULL;
                ncacn_np_use_free(cli);
                return NULL;
        }

        if (key != NULL)
        {
                cli->cli->smb->key = *key;
        }
        else
        {
                cli->cli->smb->key.pid = sys_getpid();
                cli->cli->smb->key.vuid = UID_FIELD_INVALID;
        }

        add_ncacn_np_to_array(&num_msrpcs, &msrpcs, cli);
        cli->num_users++;
        return cli->cli;
}