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