558998ce664055a75595371118f818084d8f2b23Jan Cholasta/*
558998ce664055a75595371118f818084d8f2b23Jan Cholasta Authors:
558998ce664055a75595371118f818084d8f2b23Jan Cholasta Jan Cholasta <jcholast@redhat.com>
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta Copyright (C) 2012 Red Hat
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta This program is free software; you can redistribute it and/or modify
558998ce664055a75595371118f818084d8f2b23Jan Cholasta it under the terms of the GNU General Public License as published by
558998ce664055a75595371118f818084d8f2b23Jan Cholasta the Free Software Foundation; either version 3 of the License, or
558998ce664055a75595371118f818084d8f2b23Jan Cholasta (at your option) any later version.
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta This program is distributed in the hope that it will be useful,
558998ce664055a75595371118f818084d8f2b23Jan Cholasta but WITHOUT ANY WARRANTY; without even the implied warranty of
558998ce664055a75595371118f818084d8f2b23Jan Cholasta MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
558998ce664055a75595371118f818084d8f2b23Jan Cholasta GNU General Public License for more details.
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta You should have received a copy of the GNU General Public License
558998ce664055a75595371118f818084d8f2b23Jan Cholasta along with this program. If not, see <http://www.gnu.org/licenses/>.
558998ce664055a75595371118f818084d8f2b23Jan Cholasta*/
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include <stdio.h>
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include <talloc.h>
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include <popt.h>
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include "util/util.h"
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include "util/crypto/sss_crypto.h"
748ba184db97b7534254f97018fa04e8aa458faeJan Cholasta#include "util/sss_ssh.h"
558998ce664055a75595371118f818084d8f2b23Jan Cholasta#include "sss_client/sss_cli.h"
748ba184db97b7534254f97018fa04e8aa458faeJan Cholasta#include "sss_client/ssh/sss_ssh_client.h"
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholastaint main(int argc, const char **argv)
558998ce664055a75595371118f818084d8f2b23Jan Cholasta{
cb929e7df08443b7633920a15e56860c384fc012Jan Zeleny TALLOC_CTX *mem_ctx = NULL;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta int pc_debug = SSSDBG_DEFAULT;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta const char *pc_domain = NULL;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta const char *pc_user = NULL;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta struct poptOption long_options[] = {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta POPT_AUTOHELP
558998ce664055a75595371118f818084d8f2b23Jan Cholasta { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0,
558998ce664055a75595371118f818084d8f2b23Jan Cholasta _("The debug level to run with"), NULL },
558998ce664055a75595371118f818084d8f2b23Jan Cholasta { "domain", 'd', POPT_ARG_STRING, &pc_domain, 0,
558998ce664055a75595371118f818084d8f2b23Jan Cholasta _("The SSSD domain to use"), NULL },
558998ce664055a75595371118f818084d8f2b23Jan Cholasta POPT_TABLEEND
558998ce664055a75595371118f818084d8f2b23Jan Cholasta };
558998ce664055a75595371118f818084d8f2b23Jan Cholasta poptContext pc = NULL;
748ba184db97b7534254f97018fa04e8aa458faeJan Cholasta struct sss_ssh_ent *ent;
748ba184db97b7534254f97018fa04e8aa458faeJan Cholasta size_t i;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta char *repr;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta int ret;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta debug_prg_name = argv[0];
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ret = set_locale();
558998ce664055a75595371118f818084d8f2b23Jan Cholasta if (ret != EOK) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "set_locale() failed (%d): %s\n", ret, strerror(ret));
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ERROR("Error setting the locale\n");
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ret = EXIT_FAILURE;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta goto fini;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta mem_ctx = talloc_new(NULL);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta if (!mem_ctx) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ERROR("Not enough memory\n");
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ret = EXIT_FAILURE;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta goto fini;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta /* parse parameters */
558998ce664055a75595371118f818084d8f2b23Jan Cholasta pc = poptGetContext(NULL, argc, argv, long_options, 0);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta poptSetOtherOptionHelp(pc, "USER");
558998ce664055a75595371118f818084d8f2b23Jan Cholasta while ((ret = poptGetNextOpt(pc)) > 0)
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
9e2c64c6d4f5560e27207193efea6536a566865eMichal Zidek DEBUG_INIT(pc_debug);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta if (ret != -1) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta pc_user = poptGetArg(pc);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta if (pc_user == NULL) {
558998ce664055a75595371118f818084d8f2b23Jan Cholasta BAD_POPT_PARAMS(pc, _("User not specified\n"), ret, fini);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta /* look up public keys */
bd03e67c9d2fc4ad0275e7a573385ee5b7b9307aJan Cholasta ret = sss_ssh_get_ent(mem_ctx, SSS_SSH_GET_USER_PUBKEYS,
28e55560008f21a532b103b3f612c6fca2a54d76Jan Cholasta pc_user, pc_domain, NULL, &ent);
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek if (ret == ERR_NON_SSSD_USER) {
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek "The user %s is valid, but not handled by sssd\n", pc_user);
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek ret = EXIT_SUCCESS;
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek goto fini;
fcbcfa69f9291936f01f24b5fcb5a7672dca46f3Jakub Hrozek } else if (ret != EOK) {
e5359baeac6e2d49769aceddbdbc606833b1ec69Jan Cholasta DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "sss_ssh_get_ent() failed (%d): %s\n", ret, strerror(ret));
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ERROR("Error looking up public keys\n");
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ret = EXIT_FAILURE;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta goto fini;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta /* print results */
748ba184db97b7534254f97018fa04e8aa458faeJan Cholasta for (i = 0; i < ent->num_pubkeys; i++) {
4a628b83d129463e7886c8cdaa31739512947e42Jan Cholasta ret = sss_ssh_format_pubkey(mem_ctx, &ent->pubkeys[i], &repr);
ef9f85751b26995093cc9782fe48ddeacc8e2d3fJan Cholasta if (ret != EOK) {
ef9f85751b26995093cc9782fe48ddeacc8e2d3fJan Cholasta DEBUG(SSSDBG_OP_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "sss_ssh_format_pubkey() failed (%d): %s\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ret, strerror(ret));
ef9f85751b26995093cc9782fe48ddeacc8e2d3fJan Cholasta continue;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta printf("%s\n", repr);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta ret = EXIT_SUCCESS;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholastafini:
558998ce664055a75595371118f818084d8f2b23Jan Cholasta poptFreeContext(pc);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta talloc_free(mem_ctx);
558998ce664055a75595371118f818084d8f2b23Jan Cholasta
558998ce664055a75595371118f818084d8f2b23Jan Cholasta return ret;
558998ce664055a75595371118f818084d8f2b23Jan Cholasta}