sudo_testcli.c revision f8c829e72968b574e1c9bda96f4d5f206622358f
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/*
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen Authors:
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen Pavel Březina <pbrezina@redhat.com>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen Copyright (C) 2011 Red Hat
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen This program is free software; you can redistribute it and/or modify
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen it under the terms of the GNU General Public License as published by
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen the Free Software Foundation; either version 3 of the License, or
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen (at your option) any later version.
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen This program is distributed in the hope that it will be useful,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen GNU General Public License for more details.
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen You should have received a copy of the GNU General Public License
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen*/
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include <stdlib.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdio.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <errno.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <string.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <talloc.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "sss_client/sss_cli.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "sss_client/sudo/sss_sudo.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "sss_client/sudo/sss_sudo_private.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#ifndef EOK
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#define EOK 0
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#endif
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenint create_query(const char *username, char **_query, int *_query_len);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenvoid print_sss_result(struct sss_result *result);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenint main(int argc, char **argv)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct sss_cli_req_data request;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *username = NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen char *query = NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int query_len = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int errnop = 0;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen int ret = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint8_t *reply_buf = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen char *reply_buf_char = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen size_t reply_len;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen int i = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct sss_result *result = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen uint32_t error = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (argc != 2) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen fprintf(stderr, "Usage: sss_sudo_cli username\n");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen goto fail;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen username = argv[1];
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* create query */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = create_query(username, &query, &query_len);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ret != EOK) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen fprintf(stderr, "Unable to create query: %s\n", strerror(ret));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen goto fail;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen }
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen request.len = query_len;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen request.data = (const void*)query;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* send query and recieve response */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen errnop = 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = sss_sudo_make_request(SSS_SUDO_GET_SUDORULES, &request, &reply_buf,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &reply_len, &errnop);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (errnop != EOK) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen fprintf(stderr, "Unable to contact SSSD responder: %s\n", strerror(errnop));
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen goto fail;
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen reply_buf_char = (char*)reply_buf;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (reply_len > 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("Reply length: %d\n", (int)reply_len);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("Reply data:\n");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; i < reply_len; i++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (reply_buf_char[i] == '\0') {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("\\0");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } else {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("%c", reply_buf_char[i]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("\n");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } else {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen printf("No reply received!\n");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* get sss_result - it will send new query to responder */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ret = sss_sudo_get_result(username, &error, &result);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (ret != EOK) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen fprintf(stderr, "Usss_sudo_get_result() failed: %s\n", strerror(ret));
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen goto fail;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen printf("\n=== Printing response data ===\n");
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen printf("Response code: %d\n\n", error);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (error == SSS_SUDO_ERROR_OK) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen print_sss_result(result);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sss_sudo_free_result(result);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen free(query);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenfail:
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen sss_sudo_free_result(result);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen free(query);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenint create_query(const char *username, char **_query, int *_query_len)
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen char *data = NULL;
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen int data_len = strlen(username) + 1;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen if (data_len <= 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return EINVAL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen data = (char*)malloc(data_len * sizeof(char));
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (data == NULL) {
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return ENOMEM;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen }
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen memcpy(data, username, data_len);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen *_query = data;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen *_query_len = data_len;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return EOK;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen}
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenvoid print_sss_result(struct sss_result *result)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen{
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct sss_rule *rule = NULL;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct sss_attr *attr = NULL;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen int i = 0;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen int j = 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int k = 0;
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen printf("Number of rules: %d\n", result->num_rules);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < result->num_rules; i++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen rule = &result->rules[i];
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen printf("=== Rule %d has %d attributes\n", i, rule->num_attrs);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (j = 0; j < rule->num_attrs; j++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen attr = &rule->attrs[j];
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen printf(" === Attribute named %s has %d values:\n", attr->name,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen attr->num_values);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (k = 0; k < attr->num_values; k++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen printf(" %s\n", attr->values[k]);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen