tests/krb5: Fix method for creating invalid length zeroed checksum
[Samba.git] / source3 / rpc_server / rpc_sock_helper.c
blob28f319868fa9964747bb65613ae9fcb5080bafd8
1 /*
2 * Unix SMB/CIFS implementation.
4 * RPC Socket Helper
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "ntdomain.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "librpc/rpc/dcerpc_ep.h"
27 #include "librpc/rpc/dcesrv_core.h"
28 #include "rpc_server/rpc_sock_helper.h"
29 #include "lib/server_prefork.h"
30 #include "librpc/ndr/ndr_table.h"
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_RPC_SRV
35 NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets(struct dcesrv_endpoint *e,
36 struct pf_listen_fd *listen_fd,
37 int *listen_fd_size)
39 TALLOC_CTX *tmp_ctx;
40 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
41 uint16_t port = 0;
42 char port_str[6];
43 const char *endpoint = NULL;
45 tmp_ctx = talloc_stackframe();
46 if (tmp_ctx == NULL) {
47 return NT_STATUS_NO_MEMORY;
50 endpoint = dcerpc_binding_get_string_option(e->ep_description,
51 "endpoint");
52 if (endpoint != NULL) {
53 port = atoi(endpoint);
56 if (lp_interfaces() && lp_bind_interfaces_only()) {
57 uint32_t num_ifs = iface_count();
58 uint32_t i;
61 * We have been given an interfaces line, and been told to only
62 * bind to those interfaces. Create a socket per interface and
63 * bind to only these.
66 /* Now open a listen socket for each of the interfaces. */
67 for (i = 0; i < num_ifs; i++) {
68 const struct sockaddr_storage *ifss =
69 iface_n_sockaddr_storage(i);
70 int fd = -1;
72 status = dcesrv_create_ncacn_ip_tcp_socket(ifss,
73 &port,
74 &fd);
75 if (!NT_STATUS_IS_OK(status)) {
76 goto done;
78 listen_fd[*listen_fd_size].fd = fd;
79 listen_fd[*listen_fd_size].fd_data = e;
80 (*listen_fd_size)++;
82 } else {
83 const char *sock_addr;
84 const char *sock_ptr;
85 char *sock_tok;
87 #ifdef HAVE_IPV6
88 sock_addr = "::,0.0.0.0";
89 #else
90 sock_addr = "0.0.0.0";
91 #endif
93 for (sock_ptr = sock_addr;
94 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
95 ) {
96 struct sockaddr_storage ss;
97 int fd = -1;
99 /* open an incoming socket */
100 if (!interpret_string_addr(&ss,
101 sock_tok,
102 AI_NUMERICHOST|AI_PASSIVE)) {
103 continue;
106 status = dcesrv_create_ncacn_ip_tcp_socket(&ss,
107 &port,
108 &fd);
109 if (!NT_STATUS_IS_OK(status)) {
110 goto done;
112 listen_fd[*listen_fd_size].fd = fd;
113 listen_fd[*listen_fd_size].fd_data = e;
114 (*listen_fd_size)++;
118 /* Set the port in the endpoint */
119 snprintf(port_str, sizeof(port_str), "%u", port);
121 status = dcerpc_binding_set_string_option(e->ep_description,
122 "endpoint", port_str);
123 if (!NT_STATUS_IS_OK(status)) {
124 DBG_ERR("Failed to set binding endpoint '%s': %s\n",
125 port_str, nt_errstr(status));
126 goto done;
129 status = NT_STATUS_OK;
130 done:
131 talloc_free(tmp_ctx);
132 return status;
135 NTSTATUS dcesrv_setup_ncacn_ip_tcp_sockets(struct tevent_context *ev_ctx,
136 struct messaging_context *msg_ctx,
137 struct dcesrv_context *dce_ctx,
138 struct dcesrv_endpoint *e,
139 dcerpc_ncacn_termination_fn t_fn,
140 void *t_data)
142 TALLOC_CTX *tmp_ctx;
143 NTSTATUS status;
145 tmp_ctx = talloc_stackframe();
146 if (tmp_ctx == NULL) {
147 return NT_STATUS_NO_MEMORY;
150 if (lp_interfaces() && lp_bind_interfaces_only()) {
151 uint32_t num_ifs = iface_count();
152 uint32_t i;
155 * We have been given an interfaces line, and been told to only
156 * bind to those interfaces. Create a socket per interface and
157 * bind to only these.
160 /* Now open a listen socket for each of the interfaces. */
161 for (i = 0; i < num_ifs; i++) {
162 const struct sockaddr_storage *ifss =
163 iface_n_sockaddr_storage(i);
165 status = dcesrv_setup_ncacn_ip_tcp_socket(ev_ctx,
166 msg_ctx,
167 dce_ctx,
169 ifss,
170 t_fn,
171 t_data);
172 if (!NT_STATUS_IS_OK(status)) {
173 goto done;
176 } else {
177 const char *sock_addr;
178 const char *sock_ptr;
179 char *sock_tok;
181 #ifdef HAVE_IPV6
182 sock_addr = "::,0.0.0.0";
183 #else
184 sock_addr = "0.0.0.0";
185 #endif
187 for (sock_ptr = sock_addr;
188 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
190 struct sockaddr_storage ss;
192 /* open an incoming socket */
193 if (!interpret_string_addr(&ss,
194 sock_tok,
195 AI_NUMERICHOST|AI_PASSIVE)) {
196 continue;
199 status = dcesrv_setup_ncacn_ip_tcp_socket(ev_ctx,
200 msg_ctx,
201 dce_ctx,
203 &ss,
204 t_fn,
205 t_data);
206 if (!NT_STATUS_IS_OK(status)) {
207 goto done;
212 status = NT_STATUS_OK;
213 done:
214 talloc_free(tmp_ctx);
215 return status;
218 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */