r10500: More progress to getting ldb tools building. Create a list of ldb modules
[abartlet/samba.git/.git] / source4 / libcli / smb_composite / fsinfo.c
1 /*
2   a composite API for quering file system information
3 */
4
5 #include "includes.h"
6 #include "libcli/raw/libcliraw.h"
7 #include "libcli/composite/composite.h"
8 #include "libcli/smb_composite/smb_composite.h"
9 #include "librpc/gen_ndr/ndr_security.h"
10
11 /* the stages of this call */
12 enum fsinfo_stage {FSINFO_CONNECT, FSINFO_QUERY};
13
14
15 static void fsinfo_raw_handler(struct smbcli_request *req);
16 static void fsinfo_composite_handler(struct composite_context *c);
17 static void fsinfo_state_handler(struct composite_context *c);
18
19 struct fsinfo_state {
20         enum fsinfo_stage stage;
21         struct composite_context *creq;
22         struct smb_composite_fsinfo *io;
23         struct smb_composite_connect *connect;
24         union smb_fsinfo *fsinfo;
25         struct smbcli_tree *tree;
26         struct smbcli_request *req;
27 };
28
29 static NTSTATUS fsinfo_connect(struct composite_context *c,
30                                struct smb_composite_fsinfo *io)
31 {
32         NTSTATUS status;
33         struct fsinfo_state *state;
34         state = talloc_get_type(c->private_data, struct fsinfo_state);
35
36         status = smb_composite_connect_recv(state->creq, c);
37         NT_STATUS_NOT_OK_RETURN(status);
38
39         state->fsinfo = talloc(state, union smb_fsinfo);
40         NT_STATUS_HAVE_NO_MEMORY(state->fsinfo);
41
42         state->fsinfo->generic.level = io->in.level;
43
44         state->req = smb_raw_fsinfo_send(state->connect->out.tree,
45                                          state,
46                                          state->fsinfo);
47         NT_STATUS_HAVE_NO_MEMORY(state->req);
48
49         state->req->async.private = c;
50         state->req->async.fn = fsinfo_raw_handler;
51
52         state->stage = FSINFO_QUERY;
53         c->event_ctx = talloc_reference(c, state->req->session->transport->socket->event.ctx);
54
55         return NT_STATUS_OK;
56 }
57
58 static NTSTATUS fsinfo_query(struct composite_context *c,
59                                struct smb_composite_fsinfo *io)
60 {
61         NTSTATUS status;
62         struct fsinfo_state *state;
63         state = talloc_get_type(c->private_data, struct fsinfo_state);
64
65         status = smb_raw_fsinfo_recv(state->req, state, state->fsinfo);
66         NT_STATUS_NOT_OK_RETURN(status);
67
68         state->io->out.fsinfo = state->fsinfo;
69
70         c->state = COMPOSITE_STATE_DONE;
71
72         if (c->async.fn)
73                 c->async.fn(c);
74
75         return NT_STATUS_OK;
76
77 }
78
79 /*
80   handler for completion of a sub-request in fsinfo
81 */
82 static void fsinfo_state_handler(struct composite_context *creq)
83 {
84         struct fsinfo_state *state = talloc_get_type(creq->private_data, struct fsinfo_state);
85
86         /* when this handler is called, the stage indicates what
87            call has just finished */
88         switch (state->stage) {
89         case FSINFO_CONNECT:
90                 creq->status = fsinfo_connect(creq, state->io);
91                 break;
92
93         case FSINFO_QUERY:
94                 creq->status = fsinfo_query(creq, state->io);
95                 break;
96         }
97
98         if (!NT_STATUS_IS_OK(creq->status)) {
99                 creq->state = COMPOSITE_STATE_ERROR;
100         }
101
102         if (creq->state >= COMPOSITE_STATE_DONE && creq->async.fn) {
103                 creq->async.fn(creq);
104         }
105 }
106
107 /* 
108    As raw and composite handlers take different requests, we need to handlers
109    to adapt both for the same state machine in fsinfo_state_handler()
110 */
111 static void fsinfo_raw_handler(struct smbcli_request *req)
112 {
113         struct composite_context *c = talloc_get_type(req->async.private, 
114                                                       struct composite_context);
115         fsinfo_state_handler(c);
116 }
117
118 static void fsinfo_composite_handler(struct composite_context *creq)
119 {
120         struct composite_context *c = talloc_get_type(creq->async.private_data, 
121                                                       struct composite_context);
122         fsinfo_state_handler(c);
123 }
124
125 /*
126   composite fsinfo call - connects to a tree and queries a file system information
127 */
128 struct composite_context *smb_composite_fsinfo_send(struct smbcli_tree *tree, 
129                                                     struct smb_composite_fsinfo *io)
130 {
131         struct composite_context *c;
132         struct fsinfo_state *state;
133
134         c = talloc_zero(tree, struct composite_context);
135         if (c == NULL) goto failed;
136
137         state = talloc(c, struct fsinfo_state);
138         if (state == NULL) goto failed;
139
140         state->io = io;
141
142         state->connect = talloc(state, struct smb_composite_connect);
143
144         if (state->connect == NULL) goto failed;
145
146         state->connect->in.dest_host    = io->in.dest_host;
147         state->connect->in.port         = io->in.port;
148         state->connect->in.called_name  = io->in.called_name;
149         state->connect->in.service      = io->in.service;
150         state->connect->in.service_type = io->in.service_type;
151         state->connect->in.credentials  = io->in.credentials;
152         state->connect->in.workgroup    = io->in.workgroup;
153
154         c->state = COMPOSITE_STATE_IN_PROGRESS;
155         state->stage = FSINFO_CONNECT;
156         c->event_ctx = talloc_reference(c,  tree->session->transport->socket->event.ctx);
157         c->private_data = state;
158
159         state->creq = smb_composite_connect_send(state->connect, c->event_ctx);
160
161         if (state->creq == NULL) goto failed;
162   
163         state->creq->async.private_data = c;
164         state->creq->async.fn = fsinfo_composite_handler;
165   
166         return c;
167 failed:
168         talloc_free(c);
169         return NULL;
170 }
171
172 /*
173   composite fsinfo call - recv side
174 */
175 NTSTATUS smb_composite_fsinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
176 {
177         NTSTATUS status;
178
179         status = composite_wait(c);
180
181         if (NT_STATUS_IS_OK(status)) {
182                 struct fsinfo_state *state = talloc_get_type(c->private_data, struct fsinfo_state);
183                 talloc_steal(mem_ctx, state->io->out.fsinfo);
184         }
185
186         talloc_free(c);
187         return status;
188 }
189
190
191 /*
192   composite fsinfo call - sync interface
193 */
194 NTSTATUS smb_composite_fsinfo(struct smbcli_tree *tree, 
195                               TALLOC_CTX *mem_ctx,
196                               struct smb_composite_fsinfo *io)
197 {
198         struct composite_context *c = smb_composite_fsinfo_send(tree, io);
199         return smb_composite_fsinfo_recv(c, mem_ctx);
200 }
201