| 1 |
################################################### |
| 2 |
# DCOM parser for Samba |
| 3 |
# Basically the glue between COM and DCE/RPC with NDR |
| 4 |
# Copyright [email protected] 2003-2005 |
| 5 |
# released under the GNU GPL |
| 6 |
|
| 7 |
package Parse::Pidl::Samba4::COM::Proxy; |
| 8 |
|
| 9 |
use Parse::Pidl::Samba4::COM::Header; |
| 10 |
use Parse::Pidl::Typelist qw(mapTypeName); |
| 11 |
use Parse::Pidl::Util qw(has_property); |
| 12 |
|
| 13 |
use vars qw($VERSION); |
| 14 |
$VERSION = '0.01'; |
| 15 |
|
| 16 |
use strict; |
| 17 |
use warnings; |
| 18 |
|
| 19 |
my($res); |
| 20 |
|
| 21 |
sub ParseVTable($$) |
| 22 |
{ |
| 23 |
my ($interface, $name) = @_; |
| 24 |
|
| 25 |
# Generate the vtable |
| 26 |
$res .="\tstruct $interface->{NAME}_vtable $name = {"; |
| 27 |
|
| 28 |
if (defined($interface->{BASE})) { |
| 29 |
$res .= "\n\t\t{},"; |
| 30 |
} |
| 31 |
|
| 32 |
my $data = $interface->{DATA}; |
| 33 |
|
| 34 |
foreach my $d (@{$data}) { |
| 35 |
if ($d->{TYPE} eq "FUNCTION") { |
| 36 |
$res .= "\n\t\tdcom_proxy_$interface->{NAME}_$d->{NAME}"; |
| 37 |
$res .= ","; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$res .= "\n\t};\n\n"; |
| 42 |
} |
| 43 |
|
| 44 |
sub ParseRegFunc($) |
| 45 |
{ |
| 46 |
my $interface = shift; |
| 47 |
|
| 48 |
$res .= "static NTSTATUS dcom_proxy_$interface->{NAME}_init(TALLOC_CTX *ctx) |
| 49 |
{ |
| 50 |
struct $interface->{NAME}_vtable *proxy_vtable = talloc(ctx, struct $interface->{NAME}_vtable); |
| 51 |
"; |
| 52 |
|
| 53 |
if (defined($interface->{BASE})) { |
| 54 |
$res.= " |
| 55 |
struct GUID base_iid; |
| 56 |
const void *base_vtable; |
| 57 |
|
| 58 |
base_iid = ndr_table_$interface->{BASE}.syntax_id.uuid; |
| 59 |
|
| 60 |
base_vtable = dcom_proxy_vtable_by_iid(&base_iid); |
| 61 |
if (base_vtable == NULL) { |
| 62 |
DEBUG(0, (\"No proxy registered for base interface '$interface->{BASE}'\\n\")); |
| 63 |
return NT_STATUS_FOOBAR; |
| 64 |
} |
| 65 |
|
| 66 |
memcpy(&proxy_vtable, base_vtable, sizeof(struct $interface->{BASE}_vtable)); |
| 67 |
|
| 68 |
"; |
| 69 |
} |
| 70 |
foreach my $x (@{$interface->{DATA}}) { |
| 71 |
next unless ($x->{TYPE} eq "FUNCTION"); |
| 72 |
|
| 73 |
$res .= "\tproxy_vtable->$x->{NAME} = dcom_proxy_$interface->{NAME}_$x->{NAME};\n"; |
| 74 |
} |
| 75 |
|
| 76 |
$res.= " |
| 77 |
proxy_vtable->iid = ndr_table_$interface->{NAME}.syntax_id.uuid; |
| 78 |
|
| 79 |
return dcom_register_proxy(ctx, (struct IUnknown_vtable *)proxy_vtable); |
| 80 |
}\n\n"; |
| 81 |
} |
| 82 |
|
| 83 |
##################################################################### |
| 84 |
# parse a function |
| 85 |
sub ParseFunction($$) |
| 86 |
{ |
| 87 |
my ($interface, $fn) = @_; |
| 88 |
my $name = $fn->{NAME}; |
| 89 |
my $uname = uc $name; |
| 90 |
|
| 91 |
my $tn = mapTypeName($fn->{RETURN_TYPE}); |
| 92 |
|
| 93 |
$res.=" |
| 94 |
static $tn dcom_proxy_$interface->{NAME}_$name(struct $interface->{NAME} *d, TALLOC_CTX *mem_ctx" . Parse::Pidl::Samba4::COM::Header::GetArgumentProtoList($fn) . ") |
| 95 |
{ |
| 96 |
struct dcerpc_pipe *p; |
| 97 |
NTSTATUS status = dcom_get_pipe(d, &p); |
| 98 |
struct $name r; |
| 99 |
struct rpc_request *req; |
| 100 |
|
| 101 |
if (NT_STATUS_IS_ERR(status)) { |
| 102 |
return status; |
| 103 |
} |
| 104 |
|
| 105 |
NDR_ZERO_STRUCT(r.in.ORPCthis); |
| 106 |
r.in.ORPCthis.version.MajorVersion = COM_MAJOR_VERSION; |
| 107 |
r.in.ORPCthis.version.MinorVersion = COM_MINOR_VERSION; |
| 108 |
"; |
| 109 |
|
| 110 |
# Put arguments into r |
| 111 |
foreach my $a (@{$fn->{ELEMENTS}}) { |
| 112 |
next unless (has_property($a, "in")); |
| 113 |
if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { |
| 114 |
$res .="\tNDR_CHECK(dcom_OBJREF_from_IUnknown(mem_ctx, &r.in.$a->{NAME}.obj, $a->{NAME}));\n"; |
| 115 |
} else { |
| 116 |
$res .= "\tr.in.$a->{NAME} = $a->{NAME};\n"; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$res .=" |
| 121 |
if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) { |
| 122 |
NDR_PRINT_IN_DEBUG($name, &r); |
| 123 |
} |
| 124 |
|
| 125 |
status = dcerpc_ndr_request(p, &d->ipid, &ndr_table_$interface->{NAME}, NDR_$uname, mem_ctx, &r); |
| 126 |
|
| 127 |
if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) { |
| 128 |
NDR_PRINT_OUT_DEBUG($name, r); |
| 129 |
} |
| 130 |
|
| 131 |
"; |
| 132 |
|
| 133 |
# Put r info back into arguments |
| 134 |
foreach my $a (@{$fn->{ELEMENTS}}) { |
| 135 |
next unless (has_property($a, "out")); |
| 136 |
|
| 137 |
if (Parse::Pidl::Typelist::typeIs($a->{TYPE}, "INTERFACE")) { |
| 138 |
$res .="\tNDR_CHECK(dcom_IUnknown_from_OBJREF(d->ctx, &$a->{NAME}, r.out.$a->{NAME}.obj));\n"; |
| 139 |
} else { |
| 140 |
$res .= "\t*$a->{NAME} = r.out.$a->{NAME};\n"; |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
if ($fn->{RETURN_TYPE} eq "NTSTATUS") { |
| 146 |
$res .= "\tif (NT_STATUS_IS_OK(status)) status = r.out.result;\n"; |
| 147 |
} |
| 148 |
|
| 149 |
$res .= |
| 150 |
" |
| 151 |
return r.out.result; |
| 152 |
}\n\n"; |
| 153 |
} |
| 154 |
|
| 155 |
##################################################################### |
| 156 |
# parse the interface definitions |
| 157 |
sub ParseInterface($) |
| 158 |
{ |
| 159 |
my($interface) = shift; |
| 160 |
my($data) = $interface->{DATA}; |
| 161 |
$res = "/* DCOM proxy for $interface->{NAME} generated by pidl */\n\n"; |
| 162 |
foreach my $d (@{$data}) { |
| 163 |
($d->{TYPE} eq "FUNCTION") && |
| 164 |
ParseFunction($interface, $d); |
| 165 |
} |
| 166 |
|
| 167 |
ParseRegFunc($interface); |
| 168 |
} |
| 169 |
|
| 170 |
sub RegistrationFunction($$) |
| 171 |
{ |
| 172 |
my $idl = shift; |
| 173 |
my $basename = shift; |
| 174 |
|
| 175 |
my $res = "\n\nNTSTATUS dcom_$basename\_init(void)\n"; |
| 176 |
$res .= "{\n"; |
| 177 |
$res .="\tNTSTATUS status = NT_STATUS_OK;\n"; |
| 178 |
foreach my $interface (@{$idl}) { |
| 179 |
next if $interface->{TYPE} ne "INTERFACE"; |
| 180 |
next if not has_property($interface, "object"); |
| 181 |
|
| 182 |
my $data = $interface->{DATA}; |
| 183 |
my $count = 0; |
| 184 |
foreach my $d (@{$data}) { |
| 185 |
if ($d->{TYPE} eq "FUNCTION") { $count++; } |
| 186 |
} |
| 187 |
|
| 188 |
next if ($count == 0); |
| 189 |
|
| 190 |
$res .= "\tstatus = dcom_$interface->{NAME}_init();\n"; |
| 191 |
$res .= "\tif (NT_STATUS_IS_ERR(status)) {\n"; |
| 192 |
$res .= "\t\treturn status;\n"; |
| 193 |
$res .= "\t}\n\n"; |
| 194 |
} |
| 195 |
$res .= "\treturn status;\n"; |
| 196 |
$res .= "}\n\n"; |
| 197 |
|
| 198 |
return $res; |
| 199 |
} |
| 200 |
|
| 201 |
sub Parse($$) |
| 202 |
{ |
| 203 |
my ($pidl,$comh_filename) = @_; |
| 204 |
my $res = ""; |
| 205 |
my $has_obj = 0; |
| 206 |
|
| 207 |
$res .= "#include \"includes.h\"\n" . |
| 208 |
"#include \"lib/com/dcom/dcom.h\"\n" . |
| 209 |
"#include \"$comh_filename\"\n" . |
| 210 |
"#include \"librpc/rpc/dcerpc.h\"\n"; |
| 211 |
|
| 212 |
foreach (@{$pidl}) { |
| 213 |
next if ($_->{TYPE} ne "INTERFACE"); |
| 214 |
next if has_property($_, "local"); |
| 215 |
next unless has_property($_, "object"); |
| 216 |
|
| 217 |
$res .= ParseInterface($_); |
| 218 |
|
| 219 |
$has_obj = 1; |
| 220 |
} |
| 221 |
|
| 222 |
return $res if ($has_obj); |
| 223 |
return undef; |
| 224 |
} |
| 225 |
|
| 226 |
1; |