r17930: Merge noinclude branch:
[gd/samba-autobuild/.git] / source4 / libcli / raw / rawnotify.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client change notify operations
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "lib/util/dlinklist.h"
24
25 /****************************************************************************
26 change notify (async send)
27 ****************************************************************************/
28 struct smbcli_request *smb_raw_changenotify_send(struct smbcli_tree *tree, union smb_notify *parms)
29 {
30         struct smb_nttrans nt;
31         uint16_t setup[4];
32
33         if (parms->nttrans.level != RAW_NOTIFY_NTTRANS) {
34                 return NULL;
35         }
36
37         nt.in.max_setup = 0;
38         nt.in.max_param = parms->nttrans.in.buffer_size;
39         nt.in.max_data = 0;
40         nt.in.setup_count = 4;
41         nt.in.setup = setup;
42         SIVAL(setup, 0, parms->nttrans.in.completion_filter);
43         SSVAL(setup, 4, parms->nttrans.in.file.fnum);
44         SSVAL(setup, 6, parms->nttrans.in.recursive);   
45         nt.in.function = NT_TRANSACT_NOTIFY_CHANGE;
46         nt.in.params = data_blob(NULL, 0);
47         nt.in.data = data_blob(NULL, 0);
48
49         return smb_raw_nttrans_send(tree, &nt);
50 }
51
52 /****************************************************************************
53 change notify (async recv)
54 ****************************************************************************/
55 NTSTATUS smb_raw_changenotify_recv(struct smbcli_request *req, 
56                                    TALLOC_CTX *mem_ctx, union smb_notify *parms)
57 {
58         struct smb_nttrans nt;
59         NTSTATUS status;
60         uint32_t ofs, i;
61         struct smbcli_session *session = req?req->session:NULL;
62
63         if (parms->nttrans.level != RAW_NOTIFY_NTTRANS) {
64                 return NT_STATUS_INVALID_LEVEL;
65         }
66
67         status = smb_raw_nttrans_recv(req, mem_ctx, &nt);
68         if (!NT_STATUS_IS_OK(status)) {
69                 return status;
70         }
71
72         parms->nttrans.out.changes = NULL;
73         parms->nttrans.out.num_changes = 0;
74         
75         /* count them */
76         for (ofs=0; nt.out.params.length - ofs > 12; ) {
77                 uint32_t next = IVAL(nt.out.params.data, ofs);
78                 parms->nttrans.out.num_changes++;
79                 if (next == 0 ||
80                     ofs + next >= nt.out.params.length) break;
81                 ofs += next;
82         }
83
84         /* allocate array */
85         parms->nttrans.out.changes = talloc_array(mem_ctx, struct notify_changes, parms->nttrans.out.num_changes);
86         if (!parms->nttrans.out.changes) {
87                 return NT_STATUS_NO_MEMORY;
88         }
89
90         for (i=ofs=0; i<parms->nttrans.out.num_changes; i++) {
91                 parms->nttrans.out.changes[i].action = IVAL(nt.out.params.data, ofs+4);
92                 smbcli_blob_pull_string(session, mem_ctx, &nt.out.params, 
93                                         &parms->nttrans.out.changes[i].name, 
94                                         ofs+8, ofs+12, STR_UNICODE);
95                 ofs += IVAL(nt.out.params.data, ofs);
96         }
97
98         return NT_STATUS_OK;
99 }
100
101 /****************************************************************************
102   handle ntcancel replies from the server,
103   as the MID of the real reply and the ntcancel reply is the same
104   we need to do find out to what request the reply belongs
105 ****************************************************************************/
106 struct smbcli_request *smbcli_handle_ntcancel_reply(struct smbcli_request *req,
107                                                     size_t len, const uint8_t *hdr)
108 {
109         struct smbcli_request *ntcancel;
110
111         if (!req) return req;
112
113         if (!req->ntcancel) return req;
114
115         if (len >= MIN_SMB_SIZE + NBT_HDR_SIZE &&
116             (CVAL(hdr, HDR_FLG) & FLAG_REPLY) &&
117              CVAL(hdr,HDR_COM) == SMBntcancel) {
118                 ntcancel = req->ntcancel;
119                 DLIST_REMOVE(req->ntcancel, ntcancel);
120
121                 /*
122                  * TODO: untill we understand how the 
123                  *       smb_signing works for this case we 
124                  *       return NULL, to just ignore the packet
125                  */
126                 /*return ntcancel;*/
127                 return NULL;
128         }
129
130         return req;
131 }
132
133 /****************************************************************************
134  Send a NT Cancel request - used to hurry along a pending request. Usually
135  used to cancel a pending change notify request
136  note that this request does not expect a response!
137 ****************************************************************************/
138 NTSTATUS smb_raw_ntcancel(struct smbcli_request *oldreq)
139 {
140         struct smbcli_request *req;
141
142         req = smbcli_request_setup_transport(oldreq->transport, SMBntcancel, 0, 0);
143
144         SSVAL(req->out.hdr, HDR_MID, SVAL(oldreq->out.hdr, HDR_MID));   
145         SSVAL(req->out.hdr, HDR_PID, SVAL(oldreq->out.hdr, HDR_PID));   
146         SSVAL(req->out.hdr, HDR_TID, SVAL(oldreq->out.hdr, HDR_TID));   
147         SSVAL(req->out.hdr, HDR_UID, SVAL(oldreq->out.hdr, HDR_UID));   
148
149         /* this request does not expect a reply, so tell the signing
150            subsystem not to allocate an id for a reply */
151         req->sign_single_increment = 1;
152         req->one_way_request = 1;
153
154         /* 
155          * smbcli_request_send() free's oneway requests
156          * but we want to keep it under oldreq->ntcancel
157          */
158         if (!talloc_reference(oldreq, req)) {
159                 talloc_free(req);
160                 return NT_STATUS_NO_MEMORY;
161         }
162
163         smbcli_request_send(req);
164
165         DLIST_ADD_END(oldreq->ntcancel, req, struct smbcli_request *);
166
167         return NT_STATUS_OK;
168 }