Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[ira/wip.git] / source3 / winbindd / winbindd_dual.c
index d9a42c31dc12f9e76e32838d6b6cc0f47a374f97..1f2972f9b2be5b3a6c9dd7c47f7e109b98f3ca34 100644 (file)
@@ -33,7 +33,8 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-extern BOOL override_logfile;
+extern bool override_logfile;
+extern struct winbindd_methods cache_methods;
 
 /* Read some data from a client connection */
 
@@ -44,7 +45,7 @@ static void child_read_request(struct winbindd_cli_state *state)
        /* Read data */
 
        len = read_data(state->sock, (char *)&state->request,
-                       sizeof(state->request));
+                       sizeof(state->request), NULL);
 
        if (len != sizeof(state->request)) {
                DEBUG(len > 0 ? 0 : 3, ("Got invalid request length: %d\n", (int)len));
@@ -72,7 +73,7 @@ static void child_read_request(struct winbindd_cli_state *state)
        state->request.extra_data.data[state->request.extra_len] = '\0';
 
        len = read_data(state->sock, state->request.extra_data.data,
-                       state->request.extra_len);
+                       state->request.extra_len, NULL);
 
        if (len != state->request.extra_len) {
                DEBUG(0, ("Could not read extra data\n"));
@@ -94,22 +95,22 @@ struct winbindd_async_request {
        struct winbindd_child *child;
        struct winbindd_request *request;
        struct winbindd_response *response;
-       void (*continuation)(void *private_data, BOOL success);
+       void (*continuation)(void *private_data, bool success);
        struct timed_event *reply_timeout_event;
        pid_t child_pid; /* pid of the child we're waiting on. Used to detect
                            a restart of the child (child->pid != child_pid). */
        void *private_data;
 };
 
-static void async_main_request_sent(void *private_data, BOOL success);
-static void async_request_sent(void *private_data, BOOL success);
-static void async_reply_recv(void *private_data, BOOL success);
+static void async_main_request_sent(void *private_data, bool success);
+static void async_request_sent(void *private_data, bool success);
+static void async_reply_recv(void *private_data, bool success);
 static void schedule_async_request(struct winbindd_child *child);
 
 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
                   struct winbindd_request *request,
                   struct winbindd_response *response,
-                  void (*continuation)(void *private_data, BOOL success),
+                  void (*continuation)(void *private_data, bool success),
                   void *private_data)
 {
        struct winbindd_async_request *state;
@@ -138,7 +139,7 @@ void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
        return;
 }
 
-static void async_main_request_sent(void *private_data, BOOL success)
+static void async_main_request_sent(void *private_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data, struct winbindd_async_request);
@@ -211,7 +212,7 @@ static void async_request_fail(struct winbindd_async_request *state)
        state->continuation(state->private_data, False);
 }
 
-static void async_request_sent(void *private_data_data, BOOL success)
+static void async_request_sent(void *private_data_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data_data, struct winbindd_async_request);
@@ -247,7 +248,7 @@ static void async_request_sent(void *private_data_data, BOOL success)
        }
 }
 
-static void async_reply_recv(void *private_data, BOOL success)
+static void async_reply_recv(void *private_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data, struct winbindd_async_request);
@@ -278,7 +279,7 @@ static void async_reply_recv(void *private_data, BOOL success)
        state->continuation(state->private_data, True);
 }
 
-static BOOL fork_domain_child(struct winbindd_child *child);
+static bool fork_domain_child(struct winbindd_child *child);
 
 static void schedule_async_request(struct winbindd_child *child)
 {
@@ -319,17 +320,17 @@ struct domain_request_state {
        struct winbindd_domain *domain;
        struct winbindd_request *request;
        struct winbindd_response *response;
-       void (*continuation)(void *private_data_data, BOOL success);
+       void (*continuation)(void *private_data_data, bool success);
        void *private_data_data;
 };
 
-static void domain_init_recv(void *private_data_data, BOOL success);
+static void domain_init_recv(void *private_data_data, bool success);
 
 void async_domain_request(TALLOC_CTX *mem_ctx,
                          struct winbindd_domain *domain,
                          struct winbindd_request *request,
                          struct winbindd_response *response,
-                         void (*continuation)(void *private_data_data, BOOL success),
+                         void (*continuation)(void *private_data_data, bool success),
                          void *private_data_data)
 {
        struct domain_request_state *state;
@@ -357,7 +358,7 @@ void async_domain_request(TALLOC_CTX *mem_ctx,
        init_child_connection(domain, domain_init_recv, state);
 }
 
-static void domain_init_recv(void *private_data_data, BOOL success)
+static void domain_init_recv(void *private_data_data, bool success)
 {
        struct domain_request_state *state =
                talloc_get_type_abort(private_data_data, struct domain_request_state);
@@ -373,7 +374,7 @@ static void domain_init_recv(void *private_data_data, BOOL success)
                      state->continuation, state->private_data_data);
 }
 
-static void recvfrom_child(void *private_data_data, BOOL success)
+static void recvfrom_child(void *private_data_data, bool success)
 {
        struct winbindd_cli_state *state =
                talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
@@ -408,59 +409,11 @@ void sendto_domain(struct winbindd_cli_state *state,
                             recvfrom_child, state);
 }
 
-
-struct winbindd_child_dispatch_table {
-       enum winbindd_cmd cmd;
-       enum winbindd_result (*fn)(struct winbindd_domain *domain,
-                                  struct winbindd_cli_state *state);
-       const char *winbindd_cmd_name;
-};
-
-static struct winbindd_child_dispatch_table child_dispatch_table[] = {
-       
-       { WINBINDD_LOOKUPSID,            winbindd_dual_lookupsid,             "LOOKUPSID" },
-       { WINBINDD_LOOKUPNAME,           winbindd_dual_lookupname,            "LOOKUPNAME" },
-       { WINBINDD_LOOKUPRIDS,           winbindd_dual_lookuprids,            "LOOKUPRIDS" },
-       { WINBINDD_LIST_TRUSTDOM,        winbindd_dual_list_trusted_domains,  "LIST_TRUSTDOM" },
-       { WINBINDD_INIT_CONNECTION,      winbindd_dual_init_connection,       "INIT_CONNECTION" },
-       { WINBINDD_GETDCNAME,            winbindd_dual_getdcname,             "GETDCNAME" },
-       { WINBINDD_DSGETDCNAME,          winbindd_dual_dsgetdcname,           "DSGETDCNAME" },
-       { WINBINDD_SHOW_SEQUENCE,        winbindd_dual_show_sequence,         "SHOW_SEQUENCE" },
-       { WINBINDD_PAM_AUTH,             winbindd_dual_pam_auth,              "PAM_AUTH" },
-       { WINBINDD_PAM_AUTH_CRAP,        winbindd_dual_pam_auth_crap,         "AUTH_CRAP" },
-       { WINBINDD_PAM_LOGOFF,           winbindd_dual_pam_logoff,            "PAM_LOGOFF" },
-       { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
-       { WINBINDD_PAM_CHAUTHTOK,        winbindd_dual_pam_chauthtok,         "PAM_CHAUTHTOK" },
-       { WINBINDD_CHECK_MACHACC,        winbindd_dual_check_machine_acct,    "CHECK_MACHACC" },
-       { WINBINDD_DUAL_SID2UID,         winbindd_dual_sid2uid,               "DUAL_SID2UID" },
-       { WINBINDD_DUAL_SID2GID,         winbindd_dual_sid2gid,               "DUAL_SID2GID" },
-#if 0   /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
-       { WINBINDD_DUAL_SIDS2XIDS,       winbindd_dual_sids2xids,             "DUAL_SIDS2XIDS" },
-#endif  /* end DISABLED */
-       { WINBINDD_DUAL_UID2SID,         winbindd_dual_uid2sid,               "DUAL_UID2SID" },
-       { WINBINDD_DUAL_GID2SID,         winbindd_dual_gid2sid,               "DUAL_GID2SID" },
-       { WINBINDD_DUAL_UID2NAME,        winbindd_dual_uid2name,              "DUAL_UID2NAME" },
-       { WINBINDD_DUAL_NAME2UID,        winbindd_dual_name2uid,              "DUAL_NAME2UID" },
-       { WINBINDD_DUAL_GID2NAME,        winbindd_dual_gid2name,              "DUAL_GID2NAME" },
-       { WINBINDD_DUAL_NAME2GID,        winbindd_dual_name2gid,              "DUAL_NAME2GID" },
-       { WINBINDD_DUAL_SET_MAPPING,     winbindd_dual_set_mapping,           "DUAL_SET_MAPPING" },
-       { WINBINDD_DUAL_SET_HWM,         winbindd_dual_set_hwm,               "DUAL_SET_HWMS" },
-       { WINBINDD_DUAL_DUMP_MAPS,       winbindd_dual_dump_maps,             "DUAL_DUMP_MAPS" },
-       { WINBINDD_DUAL_USERINFO,        winbindd_dual_userinfo,              "DUAL_USERINFO" },
-       { WINBINDD_ALLOCATE_UID,         winbindd_dual_allocate_uid,          "ALLOCATE_UID" },
-       { WINBINDD_ALLOCATE_GID,         winbindd_dual_allocate_gid,          "ALLOCATE_GID" },
-       { WINBINDD_GETUSERDOMGROUPS,     winbindd_dual_getuserdomgroups,      "GETUSERDOMGROUPS" },
-       { WINBINDD_DUAL_GETSIDALIASES,   winbindd_dual_getsidaliases,         "GETSIDALIASES" },
-       { WINBINDD_CCACHE_NTLMAUTH,      winbindd_dual_ccache_ntlm_auth,      "CCACHE_NTLM_AUTH" },
-       /* End of list */
-
-       { WINBINDD_NUM_CMDS, NULL, "NONE" }
-};
-
-static void child_process_request(struct winbindd_domain *domain,
+static void child_process_request(struct winbindd_child *child,
                                  struct winbindd_cli_state *state)
 {
-       struct winbindd_child_dispatch_table *table;
+       struct winbindd_domain *domain = child->domain;
+       const struct winbindd_child_dispatch_table *table = child->table;
 
        /* Free response data - we may be interrupted and receive another
           command before being able to send this data off. */
@@ -473,38 +426,37 @@ static void child_process_request(struct winbindd_domain *domain,
 
        /* Process command */
 
-       for (table = child_dispatch_table; table->fn; table++) {
-               if (state->request.cmd == table->cmd) {
-                       DEBUG(10,("process_request: request fn %s\n",
-                                 table->winbindd_cmd_name ));
-                       state->response.result = table->fn(domain, state);
-                       break;
+       for (; table->name; table++) {
+               if (state->request.cmd == table->struct_cmd) {
+                       DEBUG(10,("child_process_request: request fn %s\n",
+                                 table->name));
+                       state->response.result = table->struct_fn(domain, state);
+                       return;
                }
        }
 
-       if (!table->fn) {
-               DEBUG(10,("process_request: unknown request fn number %d\n",
-                         (int)state->request.cmd ));
-               state->response.result = WINBINDD_ERROR;
-       }
+       DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
+                 (int)state->request.cmd));
+       state->response.result = WINBINDD_ERROR;
 }
 
-void setup_domain_child(struct winbindd_domain *domain,
-                       struct winbindd_child *child,
-                       const char *explicit_logfile)
+void setup_child(struct winbindd_child *child,
+                const struct winbindd_child_dispatch_table *table,
+                const char *logprefix,
+                const char *logname)
 {
-       if (explicit_logfile != NULL) {
-               pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
-                            dyn_LOGFILEBASE, explicit_logfile);
-       } else if (domain != NULL) {
-               pstr_sprintf(child->logfilename, "%s/log.wb-%s",
-                            dyn_LOGFILEBASE, domain->name);
+       if (logprefix && logname) {
+               if (asprintf(&child->logfilename, "%s/%s-%s",
+                            get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
+                       smb_panic("Internal error: asprintf failed");
+               }
        } else {
-               smb_panic("Internal error: domain == NULL && "
-                         "explicit_logfile == NULL");
+               smb_panic("Internal error: logprefix == NULL && "
+                         "logname == NULL");
        }
 
-       child->domain = domain;
+       child->domain = NULL;
+       child->table = table;
 }
 
 struct winbindd_child *children = NULL;
@@ -725,6 +677,88 @@ void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
 
 }
 
+void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
+                                 void *private_data,
+                                 uint32_t msg_type,
+                                 struct server_id server_id,
+                                 DATA_BLOB *data)
+{
+       TALLOC_CTX *mem_ctx;
+       const char *message = NULL;
+       struct server_id *sender = NULL;
+       const char *domain = NULL;
+       char *s = NULL;
+       NTSTATUS status;
+       struct winbindd_domain *dom = NULL;
+
+       DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
+
+       if (!data || !data->data) {
+               return;
+       }
+
+       if (data->length < sizeof(struct server_id)) {
+               return;
+       }
+
+       mem_ctx = talloc_init("winbind_msg_dump_domain_list");
+       if (!mem_ctx) {
+               return;
+       }
+
+       sender = (struct server_id *)data->data;
+       if (data->length > sizeof(struct server_id)) {
+               domain = (const char *)data->data+sizeof(struct server_id);
+       }
+
+       if (domain) {
+
+               DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
+                       domain));
+
+               message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
+                                                 find_domain_from_name_noinit(domain));
+               if (!message) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+
+               messaging_send_buf(msg_ctx, *sender,
+                                  MSG_WINBIND_DUMP_DOMAIN_LIST,
+                                  (uint8_t *)message, strlen(message) + 1);
+
+               talloc_destroy(mem_ctx);
+
+               return;
+       }
+
+       DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
+
+       for (dom = domain_list(); dom; dom=dom->next) {
+               message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
+               if (!message) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+
+               s = talloc_asprintf_append(s, "%s\n", message);
+               if (!s) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+       }
+
+       status = messaging_send_buf(msg_ctx, *sender,
+                                   MSG_WINBIND_DUMP_DOMAIN_LIST,
+                                   (uint8_t *)s, strlen(s) + 1);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("failed to send message: %s\n",
+               nt_errstr(status)));
+       }
+
+       talloc_destroy(mem_ctx);
+}
+
 static void account_lockout_policy_handler(struct event_context *ctx,
                                           struct timed_event *te,
                                           const struct timeval *now,
@@ -920,12 +954,19 @@ static void child_msg_dump_event_list(struct messaging_context *msg,
 }
 
 
-static BOOL fork_domain_child(struct winbindd_child *child)
+static bool fork_domain_child(struct winbindd_child *child)
 {
        int fdpair[2];
        struct winbindd_cli_state state;
        struct winbindd_domain *domain;
 
+       if (child->domain) {
+               DEBUG(10, ("fork_domain_child called for domain '%s'\n",
+                          child->domain->name));
+       } else {
+               DEBUG(10, ("fork_domain_child called without domain.\n"));
+       }
+
        if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
                DEBUG(0, ("Could not open child pipe: %s\n",
                          strerror(errno)));
@@ -995,6 +1036,8 @@ static BOOL fork_domain_child(struct winbindd_child *child)
                             MSG_WINBIND_ONLINESTATUS, NULL);
        messaging_deregister(winbind_messaging_context(),
                             MSG_DUMP_EVENT_LIST, NULL);
+       messaging_deregister(winbind_messaging_context(),
+                            MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
 
        /* Handle online/offline messages. */
        messaging_register(winbind_messaging_context(), NULL,
@@ -1039,6 +1082,16 @@ static BOOL fork_domain_child(struct winbindd_child *child)
                        child);
        }
 
+       /* Special case for Winbindd on a Samba DC,
+        * We want to make sure the child can connect to smbd
+        * but not the main daemon */
+
+       if (child->domain && child->domain->internal && IS_DC) {
+               child->domain->internal = False;
+               child->domain->methods = &cache_methods;
+               child->domain->online = False;
+       }
+
        while (1) {
 
                int ret;
@@ -1106,7 +1159,7 @@ static BOOL fork_domain_child(struct winbindd_child *child)
 
                ZERO_STRUCT(state.response);
                state.request.null_term = '\0';
-               child_process_request(child->domain, &state);
+               child_process_request(child, &state);
 
                SAFE_FREE(state.request.extra_data.data);