Line data Source code
1 : /*
2 : Authors:
3 : Pavel Březina <pbrezina@redhat.com>
4 :
5 : Copyright (C) 2011 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <stdlib.h>
22 : #include <stdio.h>
23 : #include <errno.h>
24 : #include <string.h>
25 : #include <talloc.h>
26 : #include <sys/types.h>
27 : #include <pwd.h>
28 :
29 : #include "sss_client/sss_cli.h"
30 : #include "sss_client/sudo/sss_sudo.h"
31 : #include "sss_client/sudo/sss_sudo_private.h"
32 :
33 : #ifndef EOK
34 : #define EOK 0
35 : #endif
36 :
37 : void print_sss_result(struct sss_sudo_result *result);
38 :
39 0 : int main(int argc, char **argv)
40 : {
41 0 : int ret = 0;
42 0 : struct sss_sudo_result *result = NULL;
43 0 : struct passwd *passwd = NULL;
44 0 : const char *username = NULL;
45 0 : char *domainname = NULL;
46 0 : uid_t uid = 0;
47 0 : uint32_t error = 0;
48 :
49 0 : if (argc != 2 && argc != 3) {
50 0 : fprintf(stderr, "Usage: sss_sudo_cli username [uid]\n");
51 0 : goto fail;
52 : }
53 :
54 0 : username = argv[1];
55 0 : if (argc == 3) {
56 0 : uid = atoi(argv[2]);
57 : } else {
58 0 : passwd = getpwnam(username);
59 0 : if (passwd == NULL) {
60 0 : fprintf(stderr, "Unknown user\n");
61 0 : goto fail;
62 : }
63 0 : uid = passwd->pw_uid;
64 : }
65 :
66 : /* get sss_result - it will send new query to responder */
67 :
68 : /* get default options */
69 :
70 0 : ret = sss_sudo_send_recv_defaults(uid, username, &error,
71 : &domainname, &result);
72 0 : if (ret != EOK) {
73 0 : fprintf(stderr, "sss_sudo_send_recv_defaults() failed: %s\n",
74 : strerror(ret));
75 0 : goto fail;
76 : }
77 :
78 0 : printf("User [%s:%llu] found in domain: %s\n\n",
79 : username, (unsigned long long)uid,
80 0 : domainname != NULL ? domainname : "<NULL>");
81 :
82 0 : printf("=== Printing response data [default options] ===\n");
83 0 : printf("Response code: %d\n\n", error);
84 0 : if (error == SSS_SUDO_ERROR_OK) {
85 0 : print_sss_result(result);
86 : }
87 :
88 0 : sss_sudo_free_result(result);
89 0 : result = NULL;
90 :
91 : /* get rules */
92 :
93 0 : ret = sss_sudo_send_recv(uid, username, domainname, &error, &result);
94 0 : if (ret != EOK) {
95 0 : fprintf(stderr, "sss_sudo_send_recv() failed: %s\n", strerror(ret));
96 0 : goto fail;
97 : }
98 :
99 0 : printf("\n=== Printing response data [rules] ===\n");
100 0 : printf("Response code: %d\n\n", error);
101 0 : if (error == SSS_SUDO_ERROR_OK) {
102 0 : print_sss_result(result);
103 : }
104 :
105 :
106 0 : free(domainname);
107 0 : sss_sudo_free_result(result);
108 0 : return 0;
109 :
110 : fail:
111 0 : free(domainname);
112 0 : sss_sudo_free_result(result);
113 0 : return 1;
114 : }
115 :
116 0 : void print_sss_result(struct sss_sudo_result *result)
117 : {
118 0 : struct sss_sudo_rule *rule = NULL;
119 0 : struct sss_sudo_attr *attr = NULL;
120 0 : int i = 0;
121 0 : int j = 0;
122 0 : int k = 0;
123 :
124 0 : printf("Number of rules: %d\n", result->num_rules);
125 :
126 0 : for (i = 0; i < result->num_rules; i++) {
127 0 : rule = &result->rules[i];
128 0 : printf("=== Rule %d has %d attributes\n", i, rule->num_attrs);
129 0 : for (j = 0; j < rule->num_attrs; j++) {
130 0 : attr = &rule->attrs[j];
131 0 : printf(" === Attribute named %s has %d values:\n", attr->name,
132 : attr->num_values);
133 :
134 0 : for (k = 0; k < attr->num_values; k++) {
135 0 : printf(" %s\n", attr->values[k]);
136 : }
137 : }
138 : }
139 0 : }
|