sss_ssh_authorizedkeys.c revision fcbcfa69f9291936f01f24b5fcb5a7672dca46f3
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer/*
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer Authors:
6b26240dca29e026900a83d51c75ca230a072a16Thiemo Wiedemeyer Jan Cholasta <jcholast@redhat.com>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu Copyright (C) 2012 Red Hat
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
2eeec5240b424984e3ee26296da1eeab6c6d739eChristian Maeder This program is free software; you can redistribute it and/or modify
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer it under the terms of the GNU General Public License as published by
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer the Free Software Foundation; either version 3 of the License, or
7520452bb30b5abbd471f82352fc4c1c937e02c5Till Mossakowski (at your option) any later version.
7520452bb30b5abbd471f82352fc4c1c937e02c5Till Mossakowski
7520452bb30b5abbd471f82352fc4c1c937e02c5Till Mossakowski This program is distributed in the hope that it will be useful,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer but WITHOUT ANY WARRANTY; without even the implied warranty of
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer GNU General Public License for more details.
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer You should have received a copy of the GNU General Public License
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer along with this program. If not, see <http://www.gnu.org/licenses/>.
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer*/
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
8bdd8707c8e341f1fc57fcbf884520bfe06a5028Christian Maeder#include <stdio.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include <talloc.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include <popt.h>
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "util/util.h"
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "util/crypto/sss_crypto.h"
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "util/sss_ssh.h"
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer#include "sss_client/sss_cli.h"
b0adcc203b4267d5535b430372935a5f36726db1Simon Ulbricht#include "sss_client/ssh/sss_ssh_client.h"
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder
83263d411f611d9902ef4d98c93be6ad9361c833Christian Maederint main(int argc, const char **argv)
abea93ed557b22ea833e1524ee5ca11afc12208aSimon Ulbricht{
521045d36343cd17dd217a81d4b9422ad6ab6a07Christian Maeder TALLOC_CTX *mem_ctx = NULL;
9a9860760c6f30558e5e60049692b6fc63904590Markus Gross int pc_debug = SSSDBG_DEFAULT;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer const char *pc_domain = NULL;
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer const char *pc_user = NULL;
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer struct poptOption long_options[] = {
aa21e7aa42fef563dea0cc77edbde76f66cdbe88Thiemo Wiedemeyer POPT_AUTOHELP
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer { "debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, 0,
8836fa284a241af325aa6f41234b5130b26ec4f9Thiemo Wiedemeyer _("The debug level to run with"), NULL },
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer { "domain", 'd', POPT_ARG_STRING, &pc_domain, 0,
7ae38566aaf40710cd83ffa3ba25655c4ad22741Thiemo Wiedemeyer _("The SSSD domain to use"), NULL },
1a389234e68da7c3d087b038307ed8c66fc6dc32Thiemo Wiedemeyer POPT_TABLEEND
f059d29e63508be7316ac161428eee7fc804736eChristian Maeder };
1c039dc13801bb9c90ad6a1bac0e56af19fd2fbfMihai Codescu poptContext pc = NULL;
38122cbf09ad3dcc31a826cc4093f630515a5cfcChristian Maeder struct sss_ssh_ent *ent;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer size_t i;
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer char *repr;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer int ret;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
3a6decfd748f532d5cb03fbcb7a42fa37b0faab3Christian Maeder debug_prg_name = argv[0];
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ret = set_locale();
1842453990fed8a1bd7a5ac792d7982c1d2bfcd5Christian Maeder if (ret != EOK) {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE,
c208973c890b8f993297720fd0247bc7481d4304Christian Maeder "set_locale() failed (%d): %s\n", ret, strerror(ret));
b6a59f004903ac7bc96323ee3ef09c01fd221157Christian Maeder ERROR("Error setting the locale\n");
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ret = EXIT_FAILURE;
55c5e901b5c3466300009135585bc70bd576dcb6Christian Maeder goto fini;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
ec25781c1180ea07f66b48c34f93cf5634e9277cChristian Maeder
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer mem_ctx = talloc_new(NULL);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer if (!mem_ctx) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer ERROR("Not enough memory\n");
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ret = EXIT_FAILURE;
239991d3955da0cfb760af4d506069446e1676b7Christian Maeder goto fini;
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
d1066b8fb69179973dcab47154858d77e72760a7Thiemo Wiedemeyer /* parse parameters */
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer pc = poptGetContext(NULL, argc, argv, long_options, 0);
4e9e95ba35a68f3c767bc0b23ebf9e904e442517Christian Maeder poptSetOtherOptionHelp(pc, "USER");
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer while ((ret = poptGetNextOpt(pc)) > 0)
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ;
3587928ae948e8505f5af052dd4dc7cd97b6638eChristian Maeder
a7e5c17a679fa2dae5998cd04c7e9e06c05c8164Christian Maeder DEBUG_INIT(pc_debug);
f9e0b18852b238ddb649d341194e05d7200d1bbeChristian Maeder
697e63e30aa3c309a1ef1f9357745111f8dfc5a9Christian Maeder if (ret != -1) {
1e3aca4178372af672efb237d16087c603fe5564Christian Maeder BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
4b136ad539bd9f4e115dff4eee4d552a42d4437eChristian Maeder
f456529a89bfb620d39e5fd5b0a53b24643db96dDominik Luecke pc_user = poptGetArg(pc);
a79266feb130561fa1a95ae0429d0574f625f6ceChristian Maeder if (pc_user == NULL) {
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer BAD_POPT_PARAMS(pc, _("User not specified\n"), ret, fini);
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer }
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer /* look up public keys */
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ret = sss_ssh_get_ent(mem_ctx, SSS_SSH_GET_USER_PUBKEYS,
f7b9d64160c23654b7288a3b0ee3e2b95af3e752Thiemo Wiedemeyer pc_user, pc_domain, NULL, &ent);
0d0278c34a374b29c2d6c58b39b8b56e283d48e8Christian Maeder if (ret == ERR_NON_SSSD_USER) {
2028dc2c091bb60343e15985948a59b955276cbfChristian Maeder DEBUG(SSSDBG_MINOR_FAILURE,
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer "The user %s is valid, but not handled by sssd\n", pc_user);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer ret = EXIT_SUCCESS;
16e45483b5ce48f0b92d01c817242a8c9b8bae02Christian Maeder goto fini;
ddc662fdf0207eae2034d7b68ae5e2225c575207Thiemo Wiedemeyer } else if (ret != EOK) {
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer DEBUG(SSSDBG_CRIT_FAILURE,
28ca54b0d63d1d26a991711c8c7e85c474994715Christian Maeder "sss_ssh_get_ent() failed (%d): %s\n", ret, strerror(ret));
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu ERROR("Error looking up public keys\n");
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu ret = EXIT_FAILURE;
02a84229da51532505a93fc2abfca1ccf81b4446Razvan Pascanu goto fini;
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu }
66fd8f017efdb8a6c862c3f1856dfaef90865dd5Thiemo Wiedemeyer
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer /* print results */
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer for (i = 0; i < ent->num_pubkeys; i++) {
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer ret = sss_ssh_format_pubkey(mem_ctx, &ent->pubkeys[i], &repr);
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer if (ret != EOK) {
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer DEBUG(SSSDBG_OP_FAILURE,
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer "sss_ssh_format_pubkey() failed (%d): %s\n",
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer ret, strerror(ret));
ea76e25262c3325f293fbdd6560f180ca18f9be4Thiemo Wiedemeyer continue;
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer }
32d98ca5e560cf6c1062a0463be4c350af32bed5Thiemo Wiedemeyer
331603b37dec12e37e2e1df9634ef0f2c5c73ddfThiemo Wiedemeyer printf("%s\n", repr);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer }
c40b7badd217089d8a256dabdf8f7d4e219ca215Thiemo Wiedemeyer
d71a37fb09bce02af6c98e7a5ab0aa5639058e4fThiemo Wiedemeyer ret = EXIT_SUCCESS;
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyerfini:
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer poptFreeContext(pc);
71654489020a03cf6ce9f2947f3da26a996f9c32Razvan Pascanu talloc_free(mem_ctx);
53e76316f409f6b1b57ed3d2e5cb9cfe1cb511e5Thiemo Wiedemeyer
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer return ret;
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer}
d7aa4e1cbe00f7f3add4da911673b3b176b140c3Thiemo Wiedemeyer