| 1 |
################################################### |
| 2 |
# Samba3 client generator for IDL structures |
| 3 |
# on top of Samba4 style NDR functions |
| 4 |
# Copyright [email protected] 2005-2006 |
| 5 |
# Copyright [email protected] 2008 |
| 6 |
# released under the GNU GPL |
| 7 |
|
| 8 |
package Parse::Pidl::Samba3::ClientNDR; |
| 9 |
use base Parse::Pidl::Base; |
| 10 |
|
| 11 |
use strict; |
| 12 |
use warnings; |
| 13 |
use Parse::Pidl qw(fatal warning error); |
| 14 |
use Parse::Pidl::Util qw(has_property ParseExpr genpad); |
| 15 |
use Parse::Pidl::NDR qw(ContainsPipe); |
| 16 |
use Parse::Pidl::Typelist qw(mapTypeName); |
| 17 |
use Parse::Pidl::Samba4 qw(DeclLong); |
| 18 |
use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv); |
| 19 |
|
| 20 |
|
| 21 |
use vars qw($VERSION); |
| 22 |
$VERSION = '0.01'; |
| 23 |
|
| 24 |
sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); } |
| 25 |
|
| 26 |
sub new($) |
| 27 |
{ |
| 28 |
my ($class) = shift; |
| 29 |
my $self = { res => "", res_hdr => "", tabs => "" }; |
| 30 |
bless($self, $class); |
| 31 |
} |
| 32 |
|
| 33 |
sub ElementDirection($) |
| 34 |
{ |
| 35 |
my ($e) = @_; |
| 36 |
|
| 37 |
return "[in,out]" if (has_property($e, "in") and has_property($e, "out")); |
| 38 |
return "[in]" if (has_property($e, "in")); |
| 39 |
return "[out]" if (has_property($e, "out")); |
| 40 |
return "[in,out]"; |
| 41 |
} |
| 42 |
|
| 43 |
sub HeaderProperties($$) |
| 44 |
{ |
| 45 |
my($props,$ignores) = @_; |
| 46 |
my $ret = ""; |
| 47 |
|
| 48 |
foreach my $d (sort(keys %{$props})) { |
| 49 |
next if (grep(/^$d$/, @$ignores)); |
| 50 |
if($props->{$d} ne "1") { |
| 51 |
$ret.= "$d($props->{$d}),"; |
| 52 |
} else { |
| 53 |
$ret.="$d,"; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ($ret) { |
| 58 |
return "[" . substr($ret, 0, -1) . "]"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
sub ParseInvalidResponse($$) |
| 63 |
{ |
| 64 |
my ($self, $type) = @_; |
| 65 |
|
| 66 |
if ($type eq "sync") { |
| 67 |
$self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;"); |
| 68 |
} elsif ($type eq "async") { |
| 69 |
$self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);"); |
| 70 |
$self->pidl("return;"); |
| 71 |
} else { |
| 72 |
die("ParseInvalidResponse($type)"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
sub ParseFunctionAsyncState($$$) |
| 77 |
{ |
| 78 |
my ($self, $if, $fn) = @_; |
| 79 |
|
| 80 |
my $state_str = "struct rpccli_$fn->{NAME}_state"; |
| 81 |
my $done_fn = "rpccli_$fn->{NAME}_done"; |
| 82 |
|
| 83 |
$self->pidl("$state_str {"); |
| 84 |
$self->indent; |
| 85 |
$self->pidl("TALLOC_CTX *out_mem_ctx;"); |
| 86 |
if (defined($fn->{RETURN_TYPE})) { |
| 87 |
$self->pidl(mapTypeName($fn->{RETURN_TYPE}). " result;"); |
| 88 |
} |
| 89 |
$self->deindent; |
| 90 |
$self->pidl("};"); |
| 91 |
$self->pidl(""); |
| 92 |
$self->pidl("static void $done_fn(struct tevent_req *subreq);"); |
| 93 |
$self->pidl(""); |
| 94 |
} |
| 95 |
|
| 96 |
sub ParseFunctionAsyncSend($$$) |
| 97 |
{ |
| 98 |
my ($self, $if, $fn) = @_; |
| 99 |
|
| 100 |
my $fn_args = ""; |
| 101 |
my $uif = uc($if); |
| 102 |
my $ufn = "NDR_".uc($fn->{NAME}); |
| 103 |
my $state_str = "struct rpccli_$fn->{NAME}_state"; |
| 104 |
my $done_fn = "rpccli_$fn->{NAME}_done"; |
| 105 |
my $out_mem_ctx = "rpccli_$fn->{NAME}_out_memory"; |
| 106 |
my $fn_str = "struct tevent_req *rpccli_$fn->{NAME}_send"; |
| 107 |
my $pad = genpad($fn_str); |
| 108 |
|
| 109 |
$fn_args .= "TALLOC_CTX *mem_ctx"; |
| 110 |
$fn_args .= ",\n" . $pad . "struct tevent_context *ev"; |
| 111 |
$fn_args .= ",\n" . $pad . "struct rpc_pipe_client *cli"; |
| 112 |
|
| 113 |
foreach (@{$fn->{ELEMENTS}}) { |
| 114 |
my $dir = ElementDirection($_); |
| 115 |
my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); |
| 116 |
$fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; |
| 117 |
} |
| 118 |
|
| 119 |
$self->fn_declare("$fn_str($fn_args)"); |
| 120 |
$self->pidl("{"); |
| 121 |
$self->indent; |
| 122 |
$self->pidl("struct tevent_req *req;"); |
| 123 |
$self->pidl("$state_str *state;"); |
| 124 |
$self->pidl("struct tevent_req *subreq;"); |
| 125 |
$self->pidl(""); |
| 126 |
$self->pidl("req = tevent_req_create(mem_ctx, &state,"); |
| 127 |
$self->pidl("\t\t\t$state_str);"); |
| 128 |
$self->pidl("if (req == NULL) {"); |
| 129 |
$self->indent; |
| 130 |
$self->pidl("return NULL;"); |
| 131 |
$self->deindent; |
| 132 |
$self->pidl("}"); |
| 133 |
$self->pidl("state->out_mem_ctx = NULL;"); |
| 134 |
$self->pidl(""); |
| 135 |
|
| 136 |
my $out_params = 0; |
| 137 |
foreach (@{$fn->{ELEMENTS}}) { |
| 138 |
if (grep(/out/, @{$_->{DIRECTION}})) { |
| 139 |
$out_params++; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ($out_params > 0) { |
| 144 |
$self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,"); |
| 145 |
$self->pidl("\t\t \"$out_mem_ctx\");"); |
| 146 |
$self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); |
| 147 |
$self->indent; |
| 148 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 149 |
$self->deindent; |
| 150 |
$self->pidl("}"); |
| 151 |
$self->pidl(""); |
| 152 |
} |
| 153 |
|
| 154 |
$fn_str = "subreq = dcerpc_$fn->{NAME}_send"; |
| 155 |
$pad = "\t" . genpad($fn_str); |
| 156 |
$fn_args = "state,\n" . $pad . "ev,\n" . $pad . "cli->binding_handle"; |
| 157 |
foreach (@{$fn->{ELEMENTS}}) { |
| 158 |
$fn_args .= ",\n" . $pad . "_". $_->{NAME}; |
| 159 |
} |
| 160 |
|
| 161 |
$self->pidl("$fn_str($fn_args);"); |
| 162 |
$self->pidl("if (tevent_req_nomem(subreq, req)) {"); |
| 163 |
$self->indent; |
| 164 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 165 |
$self->deindent; |
| 166 |
$self->pidl("}"); |
| 167 |
$self->pidl("tevent_req_set_callback(subreq, $done_fn, req);"); |
| 168 |
$self->pidl("return req;"); |
| 169 |
$self->deindent; |
| 170 |
$self->pidl("}"); |
| 171 |
$self->pidl(""); |
| 172 |
} |
| 173 |
|
| 174 |
sub ParseFunctionAsyncDone($$$) |
| 175 |
{ |
| 176 |
my ($self, $if, $fn) = @_; |
| 177 |
|
| 178 |
my $state_str = "struct rpccli_$fn->{NAME}_state"; |
| 179 |
my $done_fn = "rpccli_$fn->{NAME}_done"; |
| 180 |
|
| 181 |
$self->pidl("static void $done_fn(struct tevent_req *subreq)"); |
| 182 |
$self->pidl("{"); |
| 183 |
$self->indent; |
| 184 |
$self->pidl("struct tevent_req *req = tevent_req_callback_data("); |
| 185 |
$self->pidl("\tsubreq, struct tevent_req);"); |
| 186 |
$self->pidl("$state_str *state = tevent_req_data("); |
| 187 |
$self->pidl("\treq, $state_str);"); |
| 188 |
$self->pidl("NTSTATUS status;"); |
| 189 |
$self->pidl("TALLOC_CTX *mem_ctx;"); |
| 190 |
$self->pidl(""); |
| 191 |
|
| 192 |
$self->pidl("if (state->out_mem_ctx) {"); |
| 193 |
$self->indent; |
| 194 |
$self->pidl("mem_ctx = state->out_mem_ctx;"); |
| 195 |
$self->deindent; |
| 196 |
$self->pidl("} else {"); |
| 197 |
$self->indent; |
| 198 |
$self->pidl("mem_ctx = state;"); |
| 199 |
$self->deindent; |
| 200 |
$self->pidl("}"); |
| 201 |
$self->pidl(""); |
| 202 |
|
| 203 |
my $fn_str = "status = dcerpc_$fn->{NAME}_recv"; |
| 204 |
my $pad = "\t" . genpad($fn_str); |
| 205 |
my $fn_args = "subreq,\n" . $pad . "mem_ctx"; |
| 206 |
if (defined($fn->{RETURN_TYPE})) { |
| 207 |
$fn_args .= ",\n" . $pad . "&state->result"; |
| 208 |
} |
| 209 |
|
| 210 |
$self->pidl("$fn_str($fn_args);"); |
| 211 |
$self->pidl("TALLOC_FREE(subreq);"); |
| 212 |
$self->pidl("if (!NT_STATUS_IS_OK(status)) {"); |
| 213 |
$self->indent; |
| 214 |
$self->pidl("tevent_req_nterror(req, status);"); |
| 215 |
$self->pidl("return;"); |
| 216 |
$self->deindent; |
| 217 |
$self->pidl("}"); |
| 218 |
$self->pidl(""); |
| 219 |
|
| 220 |
$self->pidl("tevent_req_done(req);"); |
| 221 |
$self->deindent; |
| 222 |
$self->pidl("}"); |
| 223 |
$self->pidl(""); |
| 224 |
} |
| 225 |
|
| 226 |
sub ParseFunctionAsyncRecv($$$) |
| 227 |
{ |
| 228 |
my ($self, $if, $fn) = @_; |
| 229 |
|
| 230 |
my $fn_args = ""; |
| 231 |
my $state_str = "struct rpccli_$fn->{NAME}_state"; |
| 232 |
my $fn_str = "NTSTATUS rpccli_$fn->{NAME}_recv"; |
| 233 |
my $pad = genpad($fn_str); |
| 234 |
|
| 235 |
$fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx"; |
| 236 |
|
| 237 |
if (defined($fn->{RETURN_TYPE})) { |
| 238 |
$fn_args .= ",\n" . $pad . "$fn->{RETURN_TYPE} *result"; |
| 239 |
} |
| 240 |
|
| 241 |
$self->fn_declare("$fn_str($fn_args)"); |
| 242 |
$self->pidl("{"); |
| 243 |
$self->indent; |
| 244 |
$self->pidl("$state_str *state = tevent_req_data("); |
| 245 |
$self->pidl("\treq, $state_str);"); |
| 246 |
$self->pidl("NTSTATUS status;"); |
| 247 |
$self->pidl(""); |
| 248 |
$self->pidl("if (tevent_req_is_nterror(req, &status)) {"); |
| 249 |
$self->indent; |
| 250 |
$self->pidl("tevent_req_received(req);"); |
| 251 |
$self->pidl("return status;"); |
| 252 |
$self->deindent; |
| 253 |
$self->pidl("}"); |
| 254 |
$self->pidl(""); |
| 255 |
|
| 256 |
$self->pidl("/* Steal possible out parameters to the callers context */"); |
| 257 |
$self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); |
| 258 |
$self->pidl(""); |
| 259 |
|
| 260 |
if (defined($fn->{RETURN_TYPE})) { |
| 261 |
$self->pidl("/* Return result */"); |
| 262 |
$self->pidl("*result = state->result;"); |
| 263 |
$self->pidl(""); |
| 264 |
} |
| 265 |
|
| 266 |
$self->pidl("tevent_req_received(req);"); |
| 267 |
$self->pidl("return NT_STATUS_OK;"); |
| 268 |
$self->deindent; |
| 269 |
$self->pidl("}"); |
| 270 |
$self->pidl(""); |
| 271 |
} |
| 272 |
|
| 273 |
sub ParseFunctionSync($$$) |
| 274 |
{ |
| 275 |
my ($self, $if, $fn) = @_; |
| 276 |
|
| 277 |
my $fn_args = ""; |
| 278 |
my $uif = uc($if); |
| 279 |
my $ufn = "NDR_".uc($fn->{NAME}); |
| 280 |
my $fn_str = "NTSTATUS rpccli_$fn->{NAME}"; |
| 281 |
my $pad = genpad($fn_str); |
| 282 |
|
| 283 |
$fn_args .= "struct rpc_pipe_client *cli,\n" . $pad . "TALLOC_CTX *mem_ctx"; |
| 284 |
|
| 285 |
foreach (@{$fn->{ELEMENTS}}) { |
| 286 |
my $dir = ElementDirection($_); |
| 287 |
my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); |
| 288 |
$fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; |
| 289 |
} |
| 290 |
|
| 291 |
if (defined($fn->{RETURN_TYPE}) && ($fn->{RETURN_TYPE} eq "WERROR")) { |
| 292 |
$fn_args .= ",\n" . $pad . "WERROR *werror"; |
| 293 |
} |
| 294 |
|
| 295 |
$self->fn_declare("$fn_str($fn_args)"); |
| 296 |
$self->pidl("{"); |
| 297 |
$self->indent; |
| 298 |
if (defined($fn->{RETURN_TYPE})) { |
| 299 |
$self->pidl(mapTypeName($fn->{RETURN_TYPE})." result;"); |
| 300 |
} |
| 301 |
$self->pidl("NTSTATUS status;"); |
| 302 |
$self->pidl(""); |
| 303 |
|
| 304 |
$fn_str = "status = dcerpc_$fn->{NAME}"; |
| 305 |
$pad = "\t" . genpad($fn_str); |
| 306 |
$fn_args = "cli->binding_handle,\n" . $pad . "mem_ctx"; |
| 307 |
foreach (@{$fn->{ELEMENTS}}) { |
| 308 |
$fn_args .= ",\n" . $pad . "_". $_->{NAME}; |
| 309 |
} |
| 310 |
if (defined($fn->{RETURN_TYPE})) { |
| 311 |
$fn_args .= ",\n" . $pad . "&result"; |
| 312 |
} |
| 313 |
|
| 314 |
$self->pidl("$fn_str($fn_args);"); |
| 315 |
$self->pidl("if (!NT_STATUS_IS_OK(status)) {"); |
| 316 |
$self->indent; |
| 317 |
$self->pidl("return status;"); |
| 318 |
$self->deindent; |
| 319 |
$self->pidl("}"); |
| 320 |
$self->pidl(""); |
| 321 |
|
| 322 |
$self->pidl("/* Return result */"); |
| 323 |
if (not $fn->{RETURN_TYPE}) { |
| 324 |
$self->pidl("return NT_STATUS_OK;"); |
| 325 |
} elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") { |
| 326 |
$self->pidl("return result;"); |
| 327 |
} elsif ($fn->{RETURN_TYPE} eq "WERROR") { |
| 328 |
$self->pidl("if (werror) {"); |
| 329 |
$self->indent; |
| 330 |
$self->pidl("*werror = result;"); |
| 331 |
$self->deindent; |
| 332 |
$self->pidl("}"); |
| 333 |
$self->pidl(""); |
| 334 |
$self->pidl("return werror_to_ntstatus(result);"); |
| 335 |
} else { |
| 336 |
warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS"); |
| 337 |
$self->pidl("return NT_STATUS_OK;"); |
| 338 |
} |
| 339 |
|
| 340 |
$self->deindent; |
| 341 |
$self->pidl("}"); |
| 342 |
$self->pidl(""); |
| 343 |
} |
| 344 |
|
| 345 |
sub ParseFunction($$$) |
| 346 |
{ |
| 347 |
my ($self, $if, $fn) = @_; |
| 348 |
|
| 349 |
$self->ParseFunctionAsyncState($if, $fn); |
| 350 |
$self->ParseFunctionAsyncSend($if, $fn); |
| 351 |
$self->ParseFunctionAsyncDone($if, $fn); |
| 352 |
$self->ParseFunctionAsyncRecv($if, $fn); |
| 353 |
|
| 354 |
$self->ParseFunctionSync($if, $fn); |
| 355 |
} |
| 356 |
|
| 357 |
sub ParseInterface($$) |
| 358 |
{ |
| 359 |
my ($self, $if) = @_; |
| 360 |
|
| 361 |
my $uif = uc($if->{NAME}); |
| 362 |
|
| 363 |
$self->pidl_hdr("#ifndef __CLI_$uif\__"); |
| 364 |
$self->pidl_hdr("#define __CLI_$uif\__"); |
| 365 |
foreach my $fn (@{$if->{FUNCTIONS}}) { |
| 366 |
next if has_property($fn, "noopnum"); |
| 367 |
next if has_property($fn, "todo"); |
| 368 |
|
| 369 |
my $skip = 0; |
| 370 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 371 |
if (ContainsPipe($e, $e->{LEVELS}[0])) { |
| 372 |
$skip = 1; |
| 373 |
last; |
| 374 |
} |
| 375 |
} |
| 376 |
next if $skip; |
| 377 |
|
| 378 |
$self->ParseFunction($if->{NAME}, $fn); |
| 379 |
} |
| 380 |
$self->pidl_hdr("#endif /* __CLI_$uif\__ */"); |
| 381 |
} |
| 382 |
|
| 383 |
sub Parse($$$$) |
| 384 |
{ |
| 385 |
my($self,$ndr,$header,$c_header) = @_; |
| 386 |
|
| 387 |
$self->pidl("/*"); |
| 388 |
$self->pidl(" * Unix SMB/CIFS implementation."); |
| 389 |
$self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!"); |
| 390 |
$self->pidl(" */"); |
| 391 |
$self->pidl(""); |
| 392 |
$self->pidl("#include \"includes.h\""); |
| 393 |
$self->pidl("#include \"$header\""); |
| 394 |
$self->pidl_hdr("#include \"$c_header\""); |
| 395 |
$self->pidl(""); |
| 396 |
|
| 397 |
foreach (@$ndr) { |
| 398 |
$self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE"); |
| 399 |
} |
| 400 |
|
| 401 |
return ($self->{res}, $self->{res_hdr}); |
| 402 |
} |
| 403 |
|
| 404 |
1; |