rpc_server: Move socket creation to rpc_sock_helper.[ch]
authorVolker Lendecke <vl@samba.org>
Sat, 16 Jan 2021 21:24:05 +0000 (22:24 +0100)
committerJeremy Allison <jra@samba.org>
Tue, 26 Jan 2021 00:10:31 +0000 (00:10 +0000)
dcesrv_create_ncacn_ip_tcp_sockets() already was there, move the rest
as well. This makes dcesrv_create_ncacn_np_socket() static to
rpc_sock_helper.c.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/rpc_server/rpc_server.c
source3/rpc_server/rpc_server.h
source3/rpc_server/rpc_sock_helper.c
source3/rpc_server/rpc_sock_helper.h

index 9820be3216f8fb2d59701702f26b32674c4b05ab..badb63ad84b4f84dec6140c571d908840a60b442 100644 (file)
@@ -52,193 +52,6 @@ struct dcerpc_ncacn_listen_state {
        void *termination_data;
 };
 
-NTSTATUS dcesrv_create_ncacn_np_socket(struct dcerpc_binding *b, int *out_fd)
-{
-       char *np_dir = NULL;
-       int fd = -1;
-       NTSTATUS status;
-       const char *endpoint;
-       char *endpoint_normalized = NULL;
-       char *p = NULL;
-
-       endpoint = dcerpc_binding_get_string_option(b, "endpoint");
-       if (endpoint == NULL) {
-               DBG_ERR("Endpoint mandatory for named pipes\n");
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       /* The endpoint string from IDL can be mixed uppercase and case is
-        * normalized by smbd on connection */
-       endpoint_normalized = strlower_talloc(talloc_tos(), endpoint);
-       if (endpoint_normalized == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /* The endpoint string from IDL can be prefixed by \pipe\ */
-       p = endpoint_normalized;
-       if (strncmp(p, "\\pipe\\", 6) == 0) {
-               p += 6;
-       }
-
-       /*
-        * As lp_ncalrpc_dir() should have 0755, but
-        * lp_ncalrpc_dir()/np should have 0700, we need to
-        * create lp_ncalrpc_dir() first.
-        */
-       if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
-               status = map_nt_error_from_unix_common(errno);
-               DBG_ERR("Failed to create pipe directory %s - %s\n",
-                       lp_ncalrpc_dir(), strerror(errno));
-               goto out;
-       }
-
-       np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
-       if (!np_dir) {
-               status = NT_STATUS_NO_MEMORY;
-               DBG_ERR("Out of memory\n");
-               goto out;
-       }
-
-       if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
-               status = map_nt_error_from_unix_common(errno);
-               DBG_ERR("Failed to create pipe directory %s - %s\n",
-                       np_dir, strerror(errno));
-               goto out;
-       }
-
-       fd = create_pipe_sock(np_dir, p, 0700);
-       if (fd == -1) {
-               status = map_nt_error_from_unix_common(errno);
-               DBG_ERR("Failed to create ncacn_np socket! '%s/%s': %s\n",
-                       np_dir, p, strerror(errno));
-               goto out;
-       }
-
-       DBG_DEBUG("Opened pipe socket fd %d for %s\n", fd, p);
-
-       *out_fd = fd;
-
-       status = NT_STATUS_OK;
-
-out:
-       TALLOC_FREE(endpoint_normalized);
-       TALLOC_FREE(np_dir);
-       return status;
-}
-
-/********************************************************************
- * Start listening on the tcp/ip socket
- ********************************************************************/
-
-NTSTATUS dcesrv_create_ncacn_ip_tcp_socket(const struct sockaddr_storage *ifss,
-                                          uint16_t *port,
-                                          int *out_fd)
-{
-       int fd = -1;
-
-       if (*port == 0) {
-               uint16_t i;
-
-               for (i = lp_rpc_low_port(); i <= lp_rpc_high_port(); i++) {
-                       fd = open_socket_in(SOCK_STREAM,
-                                           i,
-                                           0,
-                                           ifss,
-                                           false);
-                       if (fd >= 0) {
-                               *port = i;
-                               break;
-                       }
-               }
-       } else {
-               fd = open_socket_in(SOCK_STREAM,
-                                   *port,
-                                   0,
-                                   ifss,
-                                   true);
-       }
-       if (fd == -1) {
-               DBG_ERR("Failed to create socket on port %u!\n", *port);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* ready to listen */
-       set_socket_options(fd, "SO_KEEPALIVE");
-       set_socket_options(fd, lp_socket_options());
-
-       DBG_DEBUG("Opened ncacn_ip_tcp socket fd %d for port %u\n", fd, *port);
-
-       *out_fd = fd;
-
-       return NT_STATUS_OK;
-}
-
-/********************************************************************
- * Start listening on the ncalrpc socket
- ********************************************************************/
-
-NTSTATUS dcesrv_create_ncalrpc_socket(struct dcerpc_binding *b, int *out_fd)
-{
-       int fd = -1;
-       const char *endpoint = NULL;
-       NTSTATUS status;
-
-       endpoint = dcerpc_binding_get_string_option(b, "endpoint");
-       if (endpoint == NULL) {
-               /*
-                * No identifier specified: use DEFAULT or SMBD.
-                *
-                * When role is AD DC we run two rpc server instances, the one
-                * started by 'samba' and the one embedded in 'smbd'.
-                * Avoid listening in DEFAULT socket for NCALRPC as both
-                * servers will race to accept connections. In this case smbd
-                * will listen in SMBD socket and rpcint binding handle
-                * implementation will pick the right socket to use.
-                *
-                * TODO: DO NOT hardcode this value anywhere else. Rather,
-                * specify no endpoint and let the epmapper worry about it.
-                */
-               if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
-                       endpoint = "SMBD";
-               } else {
-                       endpoint = "DEFAULT";
-               }
-               status = dcerpc_binding_set_string_option(
-                       b, "endpoint", endpoint);
-               if (!NT_STATUS_IS_OK(status)) {
-                       DBG_ERR("Failed to set ncalrpc 'endpoint' binding "
-                               "string option to '%s': %s\n",
-                               endpoint, nt_errstr(status));
-                       return status;
-               }
-       }
-
-       if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
-               status = map_nt_error_from_unix_common(errno);
-               DBG_ERR("Failed to create ncalrpc directory '%s': %s\n",
-                       lp_ncalrpc_dir(), strerror(errno));
-               goto out;
-       }
-
-       fd = create_pipe_sock(lp_ncalrpc_dir(), endpoint, 0755);
-       if (fd == -1) {
-               status = map_nt_error_from_unix_common(errno);
-               DBG_ERR("Failed to create ncalrpc socket '%s/%s': %s\n",
-                       lp_ncalrpc_dir(), endpoint, strerror(errno));
-               goto out;
-       }
-
-       DBG_DEBUG("Opened ncalrpc socket fd '%d' for '%s/%s'\n",
-                 fd, lp_ncalrpc_dir(), endpoint);
-
-       *out_fd = fd;
-
-       return NT_STATUS_OK;
-
-out:
-       return status;
-}
-
 static void dcesrv_ncacn_listener(
        struct tevent_context *ev,
        struct tevent_fd *fde,
index 4365b0460c411415042b20b2276d312fd61615be..f735c394e8322d1ca5763243f2f4f82fda9fd60a 100644 (file)
@@ -67,13 +67,6 @@ NTSTATUS dcerpc_ncacn_conn_init(TALLOC_CTX *mem_ctx,
 
 void set_incoming_fault(struct pipes_struct *p);
 void process_complete_pdu(struct pipes_struct *p, struct ncacn_packet *pkt);
-NTSTATUS dcesrv_create_ncacn_np_socket(struct dcerpc_binding *b, int *out_fd);
-
-NTSTATUS dcesrv_create_ncacn_ip_tcp_socket(const struct sockaddr_storage *ifss,
-                                          uint16_t *port,
-                                          int *out_fd);
-
-NTSTATUS dcesrv_create_ncalrpc_socket(struct dcerpc_binding *b, int *fd);
 
 struct dcerpc_ncacn_listen_state;
 int dcesrv_setup_ncacn_listener(
index 75a432fb15b636c87831abfd60e90647a2553153..852c87498ce463b6037858437d666ae4947bd635 100644 (file)
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
+NTSTATUS dcesrv_create_ncacn_np_socket(struct dcerpc_binding *b, int *out_fd)
+{
+       char *np_dir = NULL;
+       int fd = -1;
+       NTSTATUS status;
+       const char *endpoint;
+       char *endpoint_normalized = NULL;
+       char *p = NULL;
+
+       endpoint = dcerpc_binding_get_string_option(b, "endpoint");
+       if (endpoint == NULL) {
+               DBG_ERR("Endpoint mandatory for named pipes\n");
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /* The endpoint string from IDL can be mixed uppercase and case is
+        * normalized by smbd on connection */
+       endpoint_normalized = strlower_talloc(talloc_tos(), endpoint);
+       if (endpoint_normalized == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* The endpoint string from IDL can be prefixed by \pipe\ */
+       p = endpoint_normalized;
+       if (strncmp(p, "\\pipe\\", 6) == 0) {
+               p += 6;
+       }
+
+       /*
+        * As lp_ncalrpc_dir() should have 0755, but
+        * lp_ncalrpc_dir()/np should have 0700, we need to
+        * create lp_ncalrpc_dir() first.
+        */
+       if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
+               status = map_nt_error_from_unix_common(errno);
+               DBG_ERR("Failed to create pipe directory %s - %s\n",
+                       lp_ncalrpc_dir(), strerror(errno));
+               goto out;
+       }
+
+       np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
+       if (!np_dir) {
+               status = NT_STATUS_NO_MEMORY;
+               DBG_ERR("Out of memory\n");
+               goto out;
+       }
+
+       if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
+               status = map_nt_error_from_unix_common(errno);
+               DBG_ERR("Failed to create pipe directory %s - %s\n",
+                       np_dir, strerror(errno));
+               goto out;
+       }
+
+       fd = create_pipe_sock(np_dir, p, 0700);
+       if (fd == -1) {
+               status = map_nt_error_from_unix_common(errno);
+               DBG_ERR("Failed to create ncacn_np socket! '%s/%s': %s\n",
+                       np_dir, p, strerror(errno));
+               goto out;
+       }
+
+       DBG_DEBUG("Opened pipe socket fd %d for %s\n", fd, p);
+
+       *out_fd = fd;
+
+       status = NT_STATUS_OK;
+
+out:
+       TALLOC_FREE(endpoint_normalized);
+       TALLOC_FREE(np_dir);
+       return status;
+}
+
+/********************************************************************
+ * Start listening on the tcp/ip socket
+ ********************************************************************/
+
+static NTSTATUS dcesrv_create_ncacn_ip_tcp_socket(
+       const struct sockaddr_storage *ifss, uint16_t *port, int *out_fd)
+{
+       int fd = -1;
+
+       if (*port == 0) {
+               uint16_t i;
+
+               for (i = lp_rpc_low_port(); i <= lp_rpc_high_port(); i++) {
+                       fd = open_socket_in(SOCK_STREAM,
+                                           i,
+                                           0,
+                                           ifss,
+                                           false);
+                       if (fd >= 0) {
+                               *port = i;
+                               break;
+                       }
+               }
+       } else {
+               fd = open_socket_in(SOCK_STREAM,
+                                   *port,
+                                   0,
+                                   ifss,
+                                   true);
+       }
+       if (fd == -1) {
+               DBG_ERR("Failed to create socket on port %u!\n", *port);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       /* ready to listen */
+       set_socket_options(fd, "SO_KEEPALIVE");
+       set_socket_options(fd, lp_socket_options());
+
+       DBG_DEBUG("Opened ncacn_ip_tcp socket fd %d for port %u\n", fd, *port);
+
+       *out_fd = fd;
+
+       return NT_STATUS_OK;
+}
+
 NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets(
        struct dcerpc_binding *b,
        TALLOC_CTX *mem_ctx,
@@ -152,4 +272,70 @@ fail:
        return status;
 }
 
+/********************************************************************
+ * Start listening on the ncalrpc socket
+ ********************************************************************/
+
+NTSTATUS dcesrv_create_ncalrpc_socket(struct dcerpc_binding *b, int *out_fd)
+{
+       int fd = -1;
+       const char *endpoint = NULL;
+       NTSTATUS status;
+
+       endpoint = dcerpc_binding_get_string_option(b, "endpoint");
+       if (endpoint == NULL) {
+               /*
+                * No identifier specified: use DEFAULT or SMBD.
+                *
+                * When role is AD DC we run two rpc server instances, the one
+                * started by 'samba' and the one embedded in 'smbd'.
+                * Avoid listening in DEFAULT socket for NCALRPC as both
+                * servers will race to accept connections. In this case smbd
+                * will listen in SMBD socket and rpcint binding handle
+                * implementation will pick the right socket to use.
+                *
+                * TODO: DO NOT hardcode this value anywhere else. Rather,
+                * specify no endpoint and let the epmapper worry about it.
+                */
+               if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
+                       endpoint = "SMBD";
+               } else {
+                       endpoint = "DEFAULT";
+               }
+               status = dcerpc_binding_set_string_option(
+                       b, "endpoint", endpoint);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DBG_ERR("Failed to set ncalrpc 'endpoint' binding "
+                               "string option to '%s': %s\n",
+                               endpoint, nt_errstr(status));
+                       return status;
+               }
+       }
+
+       if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
+               status = map_nt_error_from_unix_common(errno);
+               DBG_ERR("Failed to create ncalrpc directory '%s': %s\n",
+                       lp_ncalrpc_dir(), strerror(errno));
+               goto out;
+       }
+
+       fd = create_pipe_sock(lp_ncalrpc_dir(), endpoint, 0755);
+       if (fd == -1) {
+               status = map_nt_error_from_unix_common(errno);
+               DBG_ERR("Failed to create ncalrpc socket '%s/%s': %s\n",
+                       lp_ncalrpc_dir(), endpoint, strerror(errno));
+               goto out;
+       }
+
+       DBG_DEBUG("Opened ncalrpc socket fd '%d' for '%s/%s'\n",
+                 fd, lp_ncalrpc_dir(), endpoint);
+
+       *out_fd = fd;
+
+       return NT_STATUS_OK;
+
+out:
+       return status;
+}
+
 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */
index 0d673eb49598f505f2b63348a7a851642e95caf0..0404a0e8d6c65341a1db0a510b8348a57d6f4b69 100644 (file)
@@ -27,6 +27,8 @@
 
 struct pf_listen_fd;
 
+NTSTATUS dcesrv_create_ncacn_np_socket(struct dcerpc_binding *b, int *out_fd);
+NTSTATUS dcesrv_create_ncalrpc_socket(struct dcerpc_binding *b, int *fd);
 NTSTATUS dcesrv_create_ncacn_ip_tcp_sockets(
        struct dcerpc_binding *b,
        TALLOC_CTX *mem_ctx,