1 /*
2    Unix SMB/CIFS implementation.
3 
4    Copyright (C) Andrew Tridgell 2005
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   composite API helper functions
22 */
23 
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/smb2/smb2.h"
28 #include "libcli/composite/composite.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "libcli/nbt/libnbt.h"
32 
33 /*
34  create a new composite_context structure
35  and initialize it
36 */
composite_create(TALLOC_CTX * mem_ctx,struct event_context * ev)37 _PUBLIC_ struct composite_context *composite_create(TALLOC_CTX *mem_ctx,
38 						    struct event_context *ev)
39 {
40 	struct composite_context *c;
41 
42 	c = talloc_zero(mem_ctx, struct composite_context);
43 	if (!c) return NULL;
44 	c->state = COMPOSITE_STATE_IN_PROGRESS;
45 	c->event_ctx = ev;
46 
47 	return c;
48 }
49 
50 /*
51   block until a composite function has completed, then return the status
52 */
composite_wait(struct composite_context * c)53 _PUBLIC_ NTSTATUS composite_wait(struct composite_context *c)
54 {
55 	if (c == NULL) return NT_STATUS_NO_MEMORY;
56 
57 	c->used_wait = True;
58 
59 	while (c->state < COMPOSITE_STATE_DONE) {
60 		if (event_loop_once(c->event_ctx) != 0) {
61 			return NT_STATUS_UNSUCCESSFUL;
62 		}
63 	}
64 
65 	return c->status;
66 }
67 
68 
69 /*
70  * Some composite helpers that are handy if you write larger composite
71  * functions.
72  */
composite_is_ok(struct composite_context * ctx)73 _PUBLIC_ BOOL composite_is_ok(struct composite_context *ctx)
74 {
75 	if (NT_STATUS_IS_OK(ctx->status)) {
76 		return True;
77 	}
78 	ctx->state = COMPOSITE_STATE_ERROR;
79 	if (ctx->async.fn != NULL) {
80 		ctx->async.fn(ctx);
81 	}
82 	return False;
83 }
84 
85 /*
86    callback from composite_done() and composite_error()
87 
88    this is used to allow for a composite function to complete without
89    going through any state transitions. When that happens the caller
90    has had no opportunity to fill in the async callback fields
91    (ctx->async.fn and ctx->async.private) which means the usual way of
92    dealing with composite functions doesn't work. To cope with this,
93    we trigger a timer event that will happen then the event loop is
94    re-entered. This gives the caller a chance to setup the callback,
95    and allows the caller to ignore the fact that the composite
96    function completed early
97 */
composite_trigger(struct event_context * ev,struct timed_event * te,struct timeval t,void * ptr)98 static void composite_trigger(struct event_context *ev, struct timed_event *te,
99 			      struct timeval t, void *ptr)
100 {
101 	struct composite_context *c = talloc_get_type(ptr, struct composite_context);
102 	if (c->async.fn) {
103 		c->async.fn(c);
104 	}
105 }
106 
107 
composite_error(struct composite_context * ctx,NTSTATUS status)108 _PUBLIC_ void composite_error(struct composite_context *ctx, NTSTATUS status)
109 {
110 	if (!ctx->used_wait && !ctx->async.fn) {
111 		event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
112 	}
113 	ctx->status = status;
114 	SMB_ASSERT(!composite_is_ok(ctx));
115 }
116 
composite_nomem(const void * p,struct composite_context * ctx)117 _PUBLIC_ BOOL composite_nomem(const void *p, struct composite_context *ctx)
118 {
119 	if (p != NULL) {
120 		return False;
121 	}
122 	composite_error(ctx, NT_STATUS_NO_MEMORY);
123 	return True;
124 }
125 
composite_done(struct composite_context * ctx)126 _PUBLIC_ void composite_done(struct composite_context *ctx)
127 {
128 	if (!ctx->used_wait && !ctx->async.fn) {
129 		event_add_timed(ctx->event_ctx, ctx, timeval_zero(), composite_trigger, ctx);
130 	}
131 	ctx->state = COMPOSITE_STATE_DONE;
132 	if (ctx->async.fn != NULL) {
133 		ctx->async.fn(ctx);
134 	}
135 }
136 
composite_continue(struct composite_context * ctx,struct composite_context * new_ctx,void (* continuation)(struct composite_context *),void * private_data)137 _PUBLIC_ void composite_continue(struct composite_context *ctx,
138 				 struct composite_context *new_ctx,
139 				 void (*continuation)(struct composite_context *),
140 				 void *private_data)
141 {
142 	if (composite_nomem(new_ctx, ctx)) return;
143 	new_ctx->async.fn = continuation;
144 	new_ctx->async.private_data = private_data;
145 
146 	/* if we are setting up a continuation, and the context has
147 	   already finished, then we should run the callback with an
148 	   immediate event, otherwise we can be stuck forever */
149 	if (new_ctx->state >= COMPOSITE_STATE_DONE && continuation) {
150 		event_add_timed(new_ctx->event_ctx, new_ctx, timeval_zero(), composite_trigger, new_ctx);
151 	}
152 }
153 
composite_continue_rpc(struct composite_context * ctx,struct rpc_request * new_req,void (* continuation)(struct rpc_request *),void * private_data)154 _PUBLIC_ void composite_continue_rpc(struct composite_context *ctx,
155 				     struct rpc_request *new_req,
156 				     void (*continuation)(struct rpc_request *),
157 				     void *private_data)
158 {
159 	if (composite_nomem(new_req, ctx)) return;
160 	new_req->async.callback = continuation;
161 	new_req->async.private = private_data;
162 }
163 
composite_continue_irpc(struct composite_context * ctx,struct irpc_request * new_req,void (* continuation)(struct irpc_request *),void * private_data)164 _PUBLIC_ void composite_continue_irpc(struct composite_context *ctx,
165 				      struct irpc_request *new_req,
166 				      void (*continuation)(struct irpc_request *),
167 				      void *private_data)
168 {
169 	if (composite_nomem(new_req, ctx)) return;
170 	new_req->async.fn = continuation;
171 	new_req->async.private = private_data;
172 }
173 
composite_continue_smb(struct composite_context * ctx,struct smbcli_request * new_req,void (* continuation)(struct smbcli_request *),void * private_data)174 _PUBLIC_ void composite_continue_smb(struct composite_context *ctx,
175 				     struct smbcli_request *new_req,
176 				     void (*continuation)(struct smbcli_request *),
177 				     void *private_data)
178 {
179 	if (composite_nomem(new_req, ctx)) return;
180 	new_req->async.fn = continuation;
181 	new_req->async.private = private_data;
182 }
183 
composite_continue_smb2(struct composite_context * ctx,struct smb2_request * new_req,void (* continuation)(struct smb2_request *),void * private_data)184 _PUBLIC_ void composite_continue_smb2(struct composite_context *ctx,
185 				      struct smb2_request *new_req,
186 				      void (*continuation)(struct smb2_request *),
187 				      void *private_data)
188 {
189 	if (composite_nomem(new_req, ctx)) return;
190 	new_req->async.fn = continuation;
191 	new_req->async.private = private_data;
192 }
193 
composite_continue_nbt(struct composite_context * ctx,struct nbt_name_request * new_req,void (* continuation)(struct nbt_name_request *),void * private_data)194 _PUBLIC_ void composite_continue_nbt(struct composite_context *ctx,
195 				     struct nbt_name_request *new_req,
196 				     void (*continuation)(struct nbt_name_request *),
197 				     void *private_data)
198 {
199 	if (composite_nomem(new_req, ctx)) return;
200 	new_req->async.fn = continuation;
201 	new_req->async.private = private_data;
202 }
203