Ensure that only parse_prs.c access internal members of the prs_struct.
[samba.git] / source3 / rpc_server / srv_pipe.c
index a38b86f826e3322ae8d8489f9478fe7a5888ea73..50127005a101f16acc20501277d2d3bcca6c68aa 100644 (file)
@@ -1,11 +1,11 @@
 /* 
- *  Unix SMB/Netbios implementation.
- *  Version 1.9.
+ *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1998
  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
- *  Copyright (C) Paul Ashton                  1997-1998.
- *  Copyright (C) Jeremy Allison                    1999.
+ *  Copyright (C) Paul Ashton                  1997-1998,
+ *  Copyright (C) Jeremy Allison                    1999,
+ *  Copyright (C) Anthony Liguori                   2003.
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -40,6 +40,9 @@
 
 #include "includes.h"
 
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
 static void NTLMSSPcalc_p( pipes_struct *p, unsigned char *data, int len)
 {
     unsigned char *hash = p->ntlmssp_hash;
@@ -81,8 +84,6 @@ BOOL create_next_pdu(pipes_struct *p)
        uint32 data_space_available;
        uint32 data_len_left;
        prs_struct outgoing_pdu;
-       char *data;
-       char *data_from;
        uint32 data_pos;
 
        /*
@@ -184,26 +185,26 @@ BOOL create_next_pdu(pipes_struct *p)
        data_pos = prs_offset(&outgoing_pdu);
 
        /* Copy the data into the PDU. */
-       data_from = prs_data_p(&p->out_data.rdata) + p->out_data.data_sent_length;
 
-       if(!prs_append_data(&outgoing_pdu, data_from, data_len)) {
+       if(!prs_append_some_prs_data(&outgoing_pdu, &p->out_data.rdata, p->out_data.data_sent_length, data_len)) {
                DEBUG(0,("create_next_pdu: failed to copy %u bytes of data.\n", (unsigned int)data_len));
                prs_mem_free(&outgoing_pdu);
                return False;
        }
 
-       /*
-        * Set data to point to where we copied the data into.
-        */
-
-       data = prs_data_p(&outgoing_pdu) + data_pos;
-
        if (p->hdr.auth_len > 0) {
                uint32 crc32 = 0;
+               char *data;
 
                DEBUG(5,("create_next_pdu: sign: %s seal: %s data %d auth %d\n",
                         BOOLSTR(auth_verify), BOOLSTR(auth_seal), data_len, p->hdr.auth_len));
 
+               /*
+                * Set data to point to where we copied the data into.
+                */
+
+               data = prs_data_p(&outgoing_pdu) + data_pos;
+
                if (auth_seal) {
                        crc32 = crc32_calc_buffer(data, data_len);
                        NTLMSSPcalc_p(p, (uchar*)data, data_len);
@@ -274,9 +275,6 @@ static BOOL api_pipe_ntlmssp_verify(pipes_struct *p, RPC_AUTH_NTLMSSP_RESP *ntlm
        auth_usersupplied_info *user_info = NULL;
        auth_serversupplied_info *server_info = NULL;
 
-       uid_t uid;
-       uid_t gid;
-
        DEBUG(5,("api_pipe_ntlmssp_verify: checking user details\n"));
 
        memset(p->user_name, '\0', sizeof(p->user_name));
@@ -423,27 +421,32 @@ failed authentication on named pipe %s.\n", domain, user_name, wks, p->name ));
                return False;
        }
        
-       uid = pdb_get_uid(server_info->sam_account);
-       gid = pdb_get_gid(server_info->sam_account);
-
-       p->pipe_user.uid = uid;
-       p->pipe_user.gid = gid;
+       memcpy(p->session_key, server_info->session_key, sizeof(p->session_key));
 
-       /* Set up pipe user group membership. */
-       initialise_groups(p->pipe_user_name, p->pipe_user.uid, p->pipe_user.gid);
-       get_current_groups( &p->pipe_user.ngroups, &p->pipe_user.groups);
+       p->pipe_user.uid = pdb_get_uid(server_info->sam_account);
+       p->pipe_user.gid = pdb_get_gid(server_info->sam_account);
+       
+       p->pipe_user.ngroups = server_info->n_groups;
+       if (p->pipe_user.ngroups) {
+               if (!(p->pipe_user.groups = memdup(server_info->groups, sizeof(gid_t) * p->pipe_user.ngroups))) {
+                       DEBUG(0,("failed to memdup group list to p->pipe_user.groups\n"));
+                       free_server_info(&server_info);
+                       return False;
+               }
+       }
 
        if (server_info->ptok)
-               add_supplementary_nt_login_groups(&p->pipe_user.ngroups, &p->pipe_user.groups, &server_info->ptok);
-
-       /* Create an NT_USER_TOKEN struct for this user. */
-       p->pipe_user.nt_user_token = create_nt_token(p->pipe_user.uid,p->pipe_user.gid,
-                                                    p->pipe_user.ngroups, p->pipe_user.groups,
-                                                    server_info->guest, server_info->ptok);
+               p->pipe_user.nt_user_token = dup_nt_token(server_info->ptok);
+       else {
+               DEBUG(1,("Error: Authmodule failed to provide nt_user_token\n"));
+               p->pipe_user.nt_user_token = NULL;
+               free_server_info(&server_info);
+               return False;
+       }
 
        p->ntlmssp_auth_validated = True;
 
-       pdb_free_sam(&server_info->sam_account);
+       free_server_info(&server_info);
        return True;
 }
 
@@ -453,24 +456,53 @@ failed authentication on named pipe %s.\n", domain, user_name, wks, p->name ));
 
 struct api_cmd
 {
-  char * pipe_clnt_name;
-  char * pipe_srv_name;
-  BOOL (*fn) (pipes_struct *);
+  const char *name;
+  int (*init)(void);
 };
 
 static struct api_cmd api_fd_commands[] =
 {
-    { "lsarpc",   "lsass",   api_ntlsa_rpc },
-    { "samr",     "lsass",   api_samr_rpc },
-    { "srvsvc",   "ntsvcs",  api_srvsvc_rpc },
-    { "wkssvc",   "ntsvcs",  api_wkssvc_rpc },
-    { "NETLOGON", "lsass",   api_netlog_rpc },
-    { "winreg",   "winreg",  api_reg_rpc },
-    { "spoolss",  "spoolss", api_spoolss_rpc },
-    { "netdfs",   "netdfs" , api_netdfs_rpc },
-    { NULL,       NULL,      NULL }
+#ifndef RPC_LSA_DYNAMIC
+    { "lsarpc",   rpc_lsa_init },
+#endif
+#ifndef RPC_SAMR_DYNAMIC
+    { "samr",     rpc_samr_init },
+#endif
+#ifndef RPC_SVC_DYNAMIC
+    { "srvsvc",   rpc_srv_init },
+#endif
+#ifndef RPC_WKS_DYNAMIC
+    { "wkssvc",   rpc_wks_init },
+#endif
+#ifndef RPC_NETLOG_DYNAMIC
+    { "NETLOGON", rpc_net_init },
+#endif
+#ifndef RPC_REG_DYNAMIC
+    { "winreg",   rpc_reg_init },
+#endif
+#ifndef RPC_SPOOLSS_DYNAMIC
+    { "spoolss",  rpc_spoolss_init },
+#endif
+#ifndef RPC_DFS_DYNAMIC
+    { "netdfs",   rpc_dfs_init },
+#endif
+    { NULL, NULL }
+};
+
+struct rpc_table
+{
+  struct
+  {
+    const char *clnt;
+    const char *srv;
+  } pipe;
+  struct api_struct *cmds;
+  int n_cmds;
 };
 
+static struct rpc_table *rpc_lookup;
+static int rpc_lookup_size;
+
 /*******************************************************************
  This is the client reply to our challenge for an authenticated 
  bind request. The challenge we sent is in p->challenge.
@@ -677,29 +709,129 @@ BOOL check_bind_req(char* pipe_name, RPC_IFACE* abstract,
        fstrcpy(pname,"\\PIPE\\");
        fstrcat(pname,pipe_name);
 
-       for(i=0;pipe_names[i].client_pipe; i++) {
-               if(strequal(pipe_names[i].client_pipe, pname))
+       DEBUG(3,("check_bind_req for %s\n", pname));
+
+#ifndef SUPPORT_NEW_LSARPC_UUID
+
+       /* check for the first pipe matching the name */
+       
+       for ( i=0; pipe_names[i].client_pipe; i++ ) {
+               if ( strequal(pipe_names[i].client_pipe, pname) )
                        break;
        }
+#else
+       /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
+               
+       for ( i=0; pipe_names[i].client_pipe; i++ ) 
+       {
+               if ( strequal(pipe_names[i].client_pipe, pname)
+                       && (abstract->version == pipe_names[i].abstr_syntax.version) 
+                       && (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid, sizeof(RPC_UUID)) == 0)
+                       && (transfer->version == pipe_names[i].trans_syntax.version)
+                       && (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid, sizeof(RPC_UUID)) == 0) )
+               {
+                       break;
+               }
+       }
+#endif
 
        if(pipe_names[i].client_pipe == NULL)
                return False;
 
+#ifndef SUPPORT_NEW_LSARPC_UUID
        /* check the abstract interface */
-       if((abstract->version != pipe_names[i].abstr_syntax.version) ||
-               (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid,
-                       sizeof(RPC_UUID)) != 0))
+       if ( (abstract->version != pipe_names[i].abstr_syntax.version) 
+               || (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid, sizeof(RPC_UUID)) != 0) )
+       {
                return False;
+       }
 
        /* check the transfer interface */
-       if((transfer->version != pipe_names[i].trans_syntax.version) ||
-               (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid,
-                       sizeof(RPC_UUID)) != 0))
+       if ( (transfer->version != pipe_names[i].trans_syntax.version) 
+               || (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid, sizeof(RPC_UUID)) != 0) )
+       {
                return False;
-
+       }
+#endif
        return True;
 }
 
+/*******************************************************************
+ Register commands to an RPC pipe
+*******************************************************************/
+int rpc_pipe_register_commands(const char *clnt, const char *srv, const struct api_struct *cmds, int size)
+{
+        struct rpc_table *rpc_entry;
+
+
+        /* We use a temporary variable because this call can fail and 
+           rpc_lookup will still be valid afterwards.  It could then succeed if
+           called again later */
+        rpc_entry = realloc(rpc_lookup, 
+                            ++rpc_lookup_size*sizeof(struct rpc_table));
+        if (NULL == rpc_entry) {
+                rpc_lookup_size--;
+                DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
+                return 0;
+        } else {
+                rpc_lookup = rpc_entry;
+        }
+        
+        rpc_entry = rpc_lookup + (rpc_lookup_size - 1);
+        ZERO_STRUCTP(rpc_entry);
+        rpc_entry->pipe.clnt = strdup(clnt);
+        rpc_entry->pipe.srv = strdup(srv);
+        rpc_entry->cmds = realloc(rpc_entry->cmds, 
+                                  (rpc_entry->n_cmds + size) *
+                                  sizeof(struct api_struct));
+        memcpy(rpc_entry->cmds + rpc_entry->n_cmds, cmds,
+               size * sizeof(struct api_struct));
+        rpc_entry->n_cmds += size;
+        
+        return size;
+}
+
+/*******************************************************************
+ Register commands to an RPC pipe
+*******************************************************************/
+int rpc_load_module(const char *module)
+{
+#ifdef HAVE_DLOPEN
+        void *handle;
+        int (*module_init)(void);
+        pstring full_path;
+        char *error;
+        
+        pstrcpy(full_path, lib_path("rpc"));
+        pstrcat(full_path, "/librpc_");
+        pstrcat(full_path, module);
+        pstrcat(full_path, ".");
+        pstrcat(full_path, shlib_ext());
+
+        handle = sys_dlopen(full_path, RTLD_LAZY);
+        if (!handle) {
+                DEBUG(0, ("Could not load requested pipe %s as %s\n", 
+                    module, full_path));
+                DEBUG(0, (" Error: %s\n", dlerror()));
+                return 0;
+        }
+        
+        DEBUG(3, ("Module '%s' loaded\n", full_path));
+        
+        module_init = sys_dlsym(handle, "rpc_pipe_init");
+        if ((error = sys_dlerror()) != NULL) {
+                DEBUG(0, ("Error trying to resolve symbol 'rpc_pipe_init' in %s: %s\n",
+                          full_path, error));
+                return 0;
+        }
+        
+        return module_init();
+#else
+        DEBUG(0,("Attempting to load a dynamic RPC pipe when dlopen isn't available\n"));
+        return 0;
+#endif
+}
+
 /*******************************************************************
  Respond to a pipe bind request.
 *******************************************************************/
@@ -727,23 +859,40 @@ BOOL api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
         * that this is a pipe name we support.
         */
 
-       for (i = 0; api_fd_commands[i].pipe_clnt_name; i++) {
-               if (strequal(api_fd_commands[i].pipe_clnt_name, p->name) &&
-                   api_fd_commands[i].fn != NULL) {
-                       DEBUG(3,("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
-                                  api_fd_commands[i].pipe_clnt_name,
-                                  api_fd_commands[i].pipe_srv_name));
-                       fstrcpy(p->pipe_srv_name, api_fd_commands[i].pipe_srv_name);
-                       break;
-               }
+
+       for (i = 0; i < rpc_lookup_size; i++) {
+               if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
+                  DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
+                            rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
+                  fstrcpy(p->pipe_srv_name, rpc_lookup[i].pipe.srv);
+                  break;
+                }
        }
 
-       if (api_fd_commands[i].fn == NULL) {
-               DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
-                       p->name ));
-               if(!setup_bind_nak(p))
-                       return False;
-               return True;
+       if (i == rpc_lookup_size) {
+                for (i = 0; api_fd_commands[i].name; i++) {
+                       if (strequal(api_fd_commands[i].name, p->name)) {
+                               api_fd_commands[i].init();
+                               break;
+                       }
+                }
+
+                if (!api_fd_commands[i].name && !rpc_load_module(p->name)) {
+                       DEBUG(3,("api_pipe_bind_req: Unknown pipe name %s in bind request.\n",
+                                p->name ));
+                       if(!setup_bind_nak(p))
+                               return False;
+                       return True;
+                }
+
+                for (i = 0; i < rpc_lookup_size; i++) {
+                       if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
+                               DEBUG(3, ("api_pipe_bind_req: \\PIPE\\%s -> \\PIPE\\%s\n",
+                                         rpc_lookup[i].pipe.clnt, rpc_lookup[i].pipe.srv));
+                               fstrcpy(p->pipe_srv_name, rpc_lookup[i].pipe.srv);
+                               break;
+                       }
+                }
        }
 
        /* decode the bind request */
@@ -1126,14 +1275,46 @@ BOOL api_pipe_request(pipes_struct *p)
                }
        }
 
-       for (i = 0; api_fd_commands[i].pipe_clnt_name; i++) {
-               if (strequal(api_fd_commands[i].pipe_clnt_name, p->name) &&
-                   api_fd_commands[i].fn != NULL) {
-                       DEBUG(3,("Doing \\PIPE\\%s\n", api_fd_commands[i].pipe_clnt_name));
-                       set_current_rpc_talloc(p->mem_ctx);
-                       ret = api_fd_commands[i].fn(p);
-                       set_current_rpc_talloc(NULL);
-               }
+       DEBUG(5, ("Requested \\PIPE\\%s\n", p->name));
+
+       for (i = 0; i < rpc_lookup_size; i++) {
+               if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
+                        DEBUG(3,("Doing \\PIPE\\%s\n", 
+                                 rpc_lookup[i].pipe.clnt));
+                        set_current_rpc_talloc(p->mem_ctx);
+                        ret = api_rpcTNP(p, rpc_lookup[i].pipe.clnt,
+                                         rpc_lookup[i].cmds,
+                                         rpc_lookup[i].n_cmds);
+                        set_current_rpc_talloc(NULL);
+                        break;
+                }
+       }
+
+
+       if (i == rpc_lookup_size) {
+               for (i = 0; api_fd_commands[i].name; i++) {
+                        if (strequal(api_fd_commands[i].name, p->name)) {
+                                api_fd_commands[i].init();
+                                break;
+                        }
+                }
+
+                if (!api_fd_commands[i].name) {
+                       rpc_load_module(p->name);
+                }
+
+                for (i = 0; i < rpc_lookup_size; i++) {
+                        if (strequal(rpc_lookup[i].pipe.clnt, p->name)) {
+                                DEBUG(3,("Doing \\PIPE\\%s\n",
+                                         rpc_lookup[i].pipe.clnt));
+                                set_current_rpc_talloc(p->mem_ctx);
+                                ret = api_rpcTNP(p, rpc_lookup[i].pipe.clnt,
+                                                 rpc_lookup[i].cmds,
+                                                 rpc_lookup[i].n_cmds);
+                                set_current_rpc_talloc(NULL);
+                                break;
+                        }
+                }
        }
 
        if(p->ntlmssp_auth_validated)
@@ -1146,8 +1327,8 @@ BOOL api_pipe_request(pipes_struct *p)
  Calls the underlying RPC function for a named pipe.
  ********************************************************************/
 
-BOOL api_rpcTNP(pipes_struct *p, char *rpc_name, 
-               struct api_struct *api_rpc_cmds)
+BOOL api_rpcTNP(pipes_struct *p, const char *rpc_name, 
+               const struct api_struct *api_rpc_cmds, int n_cmds)
 {
        int fn_num;
        fstring name;
@@ -1159,14 +1340,14 @@ BOOL api_rpcTNP(pipes_struct *p, char *rpc_name,
        slprintf(name, sizeof(name)-1, "in_%s", rpc_name);
        prs_dump(name, p->hdr_req.opnum, &p->in_data.data);
 
-       for (fn_num = 0; api_rpc_cmds[fn_num].name; fn_num++) {
+       for (fn_num = 0; fn_num < n_cmds; fn_num++) {
                if (api_rpc_cmds[fn_num].opnum == p->hdr_req.opnum && api_rpc_cmds[fn_num].fn != NULL) {
                        DEBUG(3,("api_rpcTNP: rpc command: %s\n", api_rpc_cmds[fn_num].name));
                        break;
                }
        }
 
-       if (api_rpc_cmds[fn_num].name == NULL) {
+       if (fn_num == n_cmds) {
                /*
                 * For an unknown RPC just return a fault PDU but
                 * return True to allow RPC's on the pipe to continue
@@ -1179,6 +1360,8 @@ BOOL api_rpcTNP(pipes_struct *p, char *rpc_name,
 
        offset1 = prs_offset(&p->out_data.rdata);
 
+        DEBUG(6, ("api_rpc_cmds[%d].fn == %p\n", 
+                fn_num, api_rpc_cmds[fn_num].fn));
        /* do the actual command */
        if(!api_rpc_cmds[fn_num].fn(p)) {
                DEBUG(0,("api_rpcTNP: %s: %s failed.\n", rpc_name, api_rpc_cmds[fn_num].name));
@@ -1186,6 +1369,13 @@ BOOL api_rpcTNP(pipes_struct *p, char *rpc_name,
                return False;
        }
 
+       if (p->bad_handle_fault_state) {
+               DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
+               p->bad_handle_fault_state = False;
+               setup_fault_pdu(p, NT_STATUS(0x1C00001A));
+               return True;
+       }
+
        slprintf(name, sizeof(name)-1, "out_%s", rpc_name);
        offset2 = prs_offset(&p->out_data.rdata);
        prs_set_offset(&p->out_data.rdata, offset1);
@@ -1197,17 +1387,15 @@ BOOL api_rpcTNP(pipes_struct *p, char *rpc_name,
        /* Check for buffer underflow in rpc parsing */
 
        if ((DEBUGLEVEL >= 10) && 
-           (p->in_data.data.data_offset != p->in_data.data.buffer_size)) {
-               int data_len = p->in_data.data.buffer_size - 
-                       p->in_data.data.data_offset;
+           (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
+               size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
                char *data;
 
                data = malloc(data_len);
 
                DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
                if (data) {
-                       prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data,
-                                  data_len);
+                       prs_uint8s(False, "", &p->in_data.data, 0, (unsigned char *)data, (uint32)data_len);
                        SAFE_FREE(data);
                }