idmap_rfc2307: Fix wbinfo --gid-to-sid query
[Samba.git] / source3 / librpc / rpc / rpc_common.c
blob1219b2d7a43f9213317e6c505d4a4773c0427f04
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Largely rewritten by Jeremy Allison 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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "librpc/rpc/dcerpc.h"
22 #include "../librpc/gen_ndr/ndr_lsa.h"
23 #include "../librpc/gen_ndr/ndr_dssetup.h"
24 #include "../librpc/gen_ndr/ndr_samr.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "../librpc/gen_ndr/ndr_srvsvc.h"
27 #include "../librpc/gen_ndr/ndr_wkssvc.h"
28 #include "../librpc/gen_ndr/ndr_winreg.h"
29 #include "../librpc/gen_ndr/ndr_spoolss.h"
30 #include "../librpc/gen_ndr/ndr_dfs.h"
31 #include "../librpc/gen_ndr/ndr_echo.h"
32 #include "../librpc/gen_ndr/ndr_initshutdown.h"
33 #include "../librpc/gen_ndr/ndr_svcctl.h"
34 #include "../librpc/gen_ndr/ndr_eventlog.h"
35 #include "../librpc/gen_ndr/ndr_ntsvcs.h"
36 #include "../librpc/gen_ndr/ndr_epmapper.h"
37 #include "../librpc/gen_ndr/ndr_drsuapi.h"
38 #include "../librpc/gen_ndr/ndr_fsrvp.h"
40 static const char *get_pipe_name_from_iface(
41 TALLOC_CTX *mem_ctx, const struct ndr_interface_table *interface)
43 int i;
44 const struct ndr_interface_string_array *ep = interface->endpoints;
45 char *p;
47 for (i=0; i<ep->count; i++) {
48 if (strncmp(ep->names[i], "ncacn_np:[\\pipe\\", 16) == 0) {
49 break;
52 if (i == ep->count) {
53 return NULL;
57 * extract the pipe name without \\pipe from for example
58 * ncacn_np:[\\pipe\\epmapper]
60 p = strchr(ep->names[i]+15, ']');
61 if (p == NULL) {
62 return "PIPE";
64 return talloc_strndup(mem_ctx, ep->names[i]+15, p - ep->names[i] - 15);
67 static const struct ndr_interface_table **interfaces;
69 bool smb_register_ndr_interface(const struct ndr_interface_table *interface)
71 int num_interfaces = talloc_array_length(interfaces);
72 const struct ndr_interface_table **tmp;
73 int i;
75 for (i=0; i<num_interfaces; i++) {
76 if (ndr_syntax_id_equal(&interfaces[i]->syntax_id,
77 &interface->syntax_id)) {
78 return true;
82 tmp = talloc_realloc(NULL, interfaces,
83 const struct ndr_interface_table *,
84 num_interfaces + 1);
85 if (tmp == NULL) {
86 DEBUG(1, ("smb_register_ndr_interface: talloc failed\n"));
87 return false;
89 interfaces = tmp;
90 interfaces[num_interfaces] = interface;
91 return true;
94 static bool initialize_interfaces(void)
96 if (!smb_register_ndr_interface(&ndr_table_lsarpc)) {
97 return false;
99 if (!smb_register_ndr_interface(&ndr_table_dssetup)) {
100 return false;
102 if (!smb_register_ndr_interface(&ndr_table_samr)) {
103 return false;
105 if (!smb_register_ndr_interface(&ndr_table_netlogon)) {
106 return false;
108 if (!smb_register_ndr_interface(&ndr_table_srvsvc)) {
109 return false;
111 if (!smb_register_ndr_interface(&ndr_table_wkssvc)) {
112 return false;
114 if (!smb_register_ndr_interface(&ndr_table_winreg)) {
115 return false;
117 if (!smb_register_ndr_interface(&ndr_table_spoolss)) {
118 return false;
120 if (!smb_register_ndr_interface(&ndr_table_netdfs)) {
121 return false;
123 if (!smb_register_ndr_interface(&ndr_table_rpcecho)) {
124 return false;
126 if (!smb_register_ndr_interface(&ndr_table_initshutdown)) {
127 return false;
129 if (!smb_register_ndr_interface(&ndr_table_svcctl)) {
130 return false;
132 if (!smb_register_ndr_interface(&ndr_table_eventlog)) {
133 return false;
135 if (!smb_register_ndr_interface(&ndr_table_ntsvcs)) {
136 return false;
138 if (!smb_register_ndr_interface(&ndr_table_epmapper)) {
139 return false;
141 if (!smb_register_ndr_interface(&ndr_table_drsuapi)) {
142 return false;
144 if (!smb_register_ndr_interface(&ndr_table_FileServerVssAgent)) {
145 return false;
147 return true;
150 const struct ndr_interface_table *get_iface_from_syntax(
151 const struct ndr_syntax_id *syntax)
153 int num_interfaces;
154 int i;
156 if (interfaces == NULL) {
157 if (!initialize_interfaces()) {
158 return NULL;
161 num_interfaces = talloc_array_length(interfaces);
163 for (i=0; i<num_interfaces; i++) {
164 if (ndr_syntax_id_equal(&interfaces[i]->syntax_id, syntax)) {
165 return interfaces[i];
169 return NULL;
172 /****************************************************************************
173 Return the pipe name from the interface.
174 ****************************************************************************/
176 const char *get_pipe_name_from_syntax(TALLOC_CTX *mem_ctx,
177 const struct ndr_syntax_id *syntax)
179 const struct ndr_interface_table *interface;
180 char *guid_str;
181 const char *result;
183 interface = get_iface_from_syntax(syntax);
184 if (interface != NULL) {
185 result = get_pipe_name_from_iface(mem_ctx, interface);
186 if (result != NULL) {
187 return result;
192 * Here we should ask \\epmapper, but for now our code is only
193 * interested in the known pipes mentioned in pipe_names[]
196 guid_str = GUID_string(talloc_tos(), &syntax->uuid);
197 if (guid_str == NULL) {
198 return NULL;
200 result = talloc_asprintf(mem_ctx, "Interface %s.%d", guid_str,
201 (int)syntax->if_version);
202 TALLOC_FREE(guid_str);
204 if (result == NULL) {
205 return "PIPE";
207 return result;