sudo_testcli.c revision 968e2bc3bb28458b25c3b19868ea50f0325d8b0a
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/*
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen Authors:
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen Pavel Březina <pbrezina@redhat.com>
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen Copyright (C) 2011 Red Hat
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen This program is free software; you can redistribute it and/or modify
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen it under the terms of the GNU General Public License as published by
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen the Free Software Foundation; either version 3 of the License, or
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen (at your option) any later version.
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen This program is distributed in the hope that it will be useful,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen GNU General Public License for more details.
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen You should have received a copy of the GNU General Public License
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen*/
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen
42dbeeb3462895b03e7633dbc59e8e191199734bTimo Sirainen#include <stdlib.h>
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen#include <stdio.h>
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen#include <errno.h>
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen#include <string.h>
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen#include <talloc.h>
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen#include "sss_client/sss_cli.h"
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen#include "sss_client/sudo/sss_sudo.h"
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen#include "sss_client/sudo/sss_sudo_private.h"
1a266561b099269bef75eee1a3742e61130ef780Timo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen#ifndef EOK
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen#define EOK 0
dd2df6a67f10792ce31a3666197c0b6885893a3aTimo Sirainen#endif
14175321ddb88619015866978c05a27786ca4814Timo Sirainen
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainenvoid print_sss_result(struct sss_sudo_result *result);
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainenint main(int argc, char **argv)
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen{
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen int ret = 0;
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen struct sss_sudo_result *result = NULL;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen uint32_t error = 0;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (argc > 2) {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen fprintf(stderr, "Usage: sss_sudo_cli username\n");
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen goto fail;
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen }
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen /* get sss_result - it will send new query to responder */
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (argc == 1) {
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen ret = sss_sudo_send_recv_defaults(&error, &result);
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen if (ret != EOK) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen fprintf(stderr, "sss_sudo_send_recv_defaults() failed: %s\n", strerror(ret));
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen goto fail;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen } else {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen ret = sss_sudo_send_recv(argv[1], &error, &result);
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen if (ret != EOK) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen fprintf(stderr, "sss_sudo_send_recv() failed: %s\n", strerror(ret));
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen goto fail;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf("=== Printing response data ===\n");
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf("Response code: %d\n\n", error);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (error == SSS_SUDO_ERROR_OK) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen print_sss_result(result);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen sss_sudo_free_result(result);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen return 0;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainenfail:
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen sss_sudo_free_result(result);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen return 1;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen}
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainenvoid print_sss_result(struct sss_sudo_result *result)
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen{
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen struct sss_sudo_rule *rule = NULL;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen struct sss_sudo_attr *attr = NULL;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen int i = 0;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen int j = 0;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen int k = 0;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf("Number of rules: %d\n", result->num_rules);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen for (i = 0; i < result->num_rules; i++) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen rule = &result->rules[i];
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf("=== Rule %d has %d attributes\n", i, rule->num_attrs);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen for (j = 0; j < rule->num_attrs; j++) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen attr = &rule->attrs[j];
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf(" === Attribute named %s has %d values:\n", attr->name,
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen attr->num_values);
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen for (k = 0; k < attr->num_values; k++) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen printf(" %s\n", attr->values[k]);
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen }
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen}
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen