proxy_child.c revision 545f49b72cdf8453fb0b85c9d87e7d4711da57da
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher Pam Proxy Child
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher Sumit Bose <sbose@redhat.com>
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher Copyright (C) 2010 Red Hat
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher This program is free software; you can redistribute it and/or modify
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher it under the terms of the GNU General Public License as published by
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher the Free Software Foundation; either version 3 of the License, or
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher (at your option) any later version.
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher This program is distributed in the hope that it will be useful,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher GNU General Public License for more details.
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher You should have received a copy of the GNU General Public License
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherstatic int pc_pam_handler(DBusMessage *message, struct sbus_connection *conn);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherstatic int proxy_internal_conv(int num_msg, const struct pam_message **msgm,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher auth_data = talloc_get_type(appdata_ptr, struct authtok_conv);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher reply = (struct pam_response *) calloc(num_msg,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher sizeof(struct pam_response));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher for (i=0; i < num_msg; i++) {
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(4, ("Conversation message: [%s]\n", msgm[i]->msg));
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik ret = sss_authtok_get_password(auth_data->authtok,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Conversation style %d not supported.\n",
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagherstatic int proxy_chauthtok_conv(int num_msg, const struct pam_message **msgm,
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher auth_data = talloc_get_type(appdata_ptr, struct authtok_conv);
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher reply = (struct pam_response *) calloc(num_msg,
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher sizeof(struct pam_response));
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher for (i=0; i < num_msg; i++) {
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher DEBUG(4, ("Conversation message: [%s]\n", msgm[i]->msg));
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher /* The first prompt will be asking for the old authtok */
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik ret = sss_authtok_get_password(auth_data->authtok,
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher /* Subsequent prompts are looking for the new authtok */
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik ret = sss_authtok_get_password(auth_data->newauthtok,
35c70c767d366fc82a50f6f29793ab7f1477f79dStephen Gallagher DEBUG(1, ("Conversation style %d not supported.\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherstatic errno_t call_pam_stack(const char *pam_target, struct pam_data *pd)
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher auth_data = talloc_zero(pd, struct authtok_conv);
545f49b72cdf8453fb0b85c9d87e7d4711da57daLukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_zero failed.\n"));
545f49b72cdf8453fb0b85c9d87e7d4711da57daLukas Slebodnik auth_data->authtok = sss_authtok_new(auth_data);
545f49b72cdf8453fb0b85c9d87e7d4711da57daLukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, ("sss_authtok_new failed.\n"));
545f49b72cdf8453fb0b85c9d87e7d4711da57daLukas Slebodnik auth_data->newauthtok = sss_authtok_new(auth_data);
545f49b72cdf8453fb0b85c9d87e7d4711da57daLukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, ("sss_authtok_new failed.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = pam_start(pam_target, pd->user, &conv, &pamh);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(7, ("Pam transaction started with service name [%s].\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = pam_set_item(pamh, PAM_TTY, pd->tty);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Setting PAM_TTY failed: %s.\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = pam_set_item(pamh, PAM_RUSER, pd->ruser);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Setting PAM_RUSER failed: %s.\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = pam_set_item(pamh, PAM_RHOST, pd->rhost);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Setting PAM_RHOST failed: %s.\n",
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik sss_authtok_copy(pd->authtok, auth_data->authtok);
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik sss_authtok_copy(pd->authtok, auth_data->authtok);
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik sss_authtok_copy(pd->newauthtok, auth_data->newauthtok);
9acfb09f7969a69f58bd45c856b01700541853caLukas Slebodnik sss_authtok_copy(pd->authtok, auth_data->authtok);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(4, ("Pam result: [%d][%s]\n", pam_status,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Cannot terminate pam transaction.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Failed to initialize pam transaction.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherstatic int pc_pam_handler(DBusMessage *message, struct sbus_connection *conn)
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher user_data = sbus_conn_get_private_data(conn);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher pc_ctx = talloc_get_type(user_data, struct pc_ctx);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher reply = dbus_message_new_method_return(message);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("dbus_message_new_method_return failed, "
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher "cannot send reply.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = dp_unpack_pam_request(message, pc_ctx, &pd, &dbus_error);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher pd->domain = talloc_strdup(pd, pc_ctx->domain->name);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(4, ("Got request with the following data\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = call_pam_stack(pc_ctx->pam_target, pd);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Failed to generate dbus reply\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher /* We'll return the message and let the
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher * parent process kill us.
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherint proxy_child_send_id(struct sbus_connection *conn,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherstatic int proxy_cli_init(struct pc_ctx *ctx)
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher sbus_address = talloc_asprintf(ctx, "unix:path=%s/%s_%s",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = sbus_client_init(ctx, ctx->ev, sbus_address,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = proxy_child_send_id(ctx->conn, DATA_PROVIDER_VERSION, ctx->id);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("dp_common_send_id failed.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherint proxy_child_send_id(struct sbus_connection *conn,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher /* create the message */
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(4, ("Sending ID to Proxy Backend: (%d,%ld)\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher retval = sbus_conn_send(conn, msg, 30000, dp_id_callback, NULL, NULL);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagherint proxy_child_process_init(TALLOC_CTX *mem_ctx, const char *domain,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher struct tevent_context *ev, struct confdb_ctx *cdb,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ctx = talloc_zero(mem_ctx, struct pc_ctx);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("fatal error initializing pc_ctx\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ctx->pam_target = talloc_steal(ctx, pam_target);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ctx->conf_path = talloc_asprintf(ctx, CONFDB_DOMAIN_PATH_TMPL, domain);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = confdb_get_domain(cdb, domain, &ctx->domain);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("fatal error retrieving domain configuration\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("fatal error setting up server bus\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher {"domain", 0, POPT_ARG_STRING, &domain, 0,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher _("Domain of the information provider (mandatory)"), NULL },
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher _("Child identifier (mandatory)"), NULL },
89caf5edcc99f5731e89bd51e6ffaad3ec11c304Pavel Březina /* Set debug level to invalid value so we can deside if -d 0 was used. */
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher pc = poptGetContext(argv[0], argc, argv, long_options, 0);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher fprintf(stderr, "\nInvalid option %s: %s\n\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher "--domain is a mandatory option.\n\n");
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher "--id is a mandatory option.\n\n");
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher /* set up things like debug , signals, daemonization, etc... */
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher debug_log_file = talloc_asprintf(NULL, "proxy_child_%s", domain);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher srv_name = talloc_asprintf(NULL, "sssd[proxy_child[%s]]", domain);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher conf_entry = talloc_asprintf(NULL, CONFDB_DOMAIN_PATH_TMPL, domain);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = server_setup(srv_name, 0, conf_entry, &main_ctx);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("Could not set up mainloop [%d]\n", ret));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher "pam modules might not work as expected.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = confdb_get_string(main_ctx->confdb_ctx, main_ctx, conf_entry,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher CONFDB_PROXY_PAM_TARGET, NULL, &pam_target);
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Missing option proxy_pam_target.\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher /* This is not fatal, don't return */
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(2, ("Could not set up to exit when parent process does\n"));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher ret = proxy_child_process_init(main_ctx, domain, main_ctx->event_ctx,
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(0, ("Could not initialize proxy child [%d].\n", ret));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher DEBUG(1, ("Proxy child for domain [%s] started!\n", domain));
10afbe39cb81a1810dba486c4b8e46578bb300bbStephen Gallagher /* loop on main */