Line data Source code
1 : /*
2 : Authors:
3 : Pavel Březina <pbrezina@redhat.com>
4 :
5 : Copyright (C) 2014 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 <dbus/dbus.h>
22 :
23 : #include "lib/sifp/sss_sifp.h"
24 : #include "lib/sifp/sss_sifp_dbus.h"
25 : #include "lib/sifp/sss_sifp_private.h"
26 :
27 : #define SSS_SIFP_ATTR_NAME "name"
28 :
29 : static sss_sifp_error
30 1 : sss_sifp_fetch_object_by_attr(sss_sifp_ctx *ctx,
31 : const char *method,
32 : int attr_type,
33 : const void *attr,
34 : sss_sifp_object **_object)
35 : {
36 1 : sss_sifp_object *object = NULL;
37 1 : char *object_path = NULL;
38 1 : const char *interface = NULL;
39 : sss_sifp_error ret;
40 :
41 1 : if (method == NULL || attr == NULL || attr_type == DBUS_TYPE_INVALID) {
42 0 : return SSS_SIFP_INVALID_ARGUMENT;
43 : }
44 :
45 1 : ret = sss_sifp_invoke_find(ctx, method, &object_path,
46 : attr_type, attr,
47 : DBUS_TYPE_INVALID);
48 1 : if (ret != SSS_SIFP_OK) {
49 0 : goto done;
50 : }
51 :
52 1 : interface = sss_sifp_get_iface_for_object(object_path);
53 1 : if (interface == NULL) {
54 0 : return SSS_SIFP_INTERNAL_ERROR;
55 : }
56 :
57 1 : ret = sss_sifp_fetch_object(ctx, object_path, interface, &object);
58 1 : if (ret != SSS_SIFP_OK) {
59 0 : goto done;
60 : }
61 :
62 1 : *_object = object;
63 :
64 1 : ret = SSS_SIFP_OK;
65 :
66 : done:
67 1 : sss_sifp_free_string(ctx, &object_path);
68 :
69 1 : return ret;
70 : }
71 :
72 : static sss_sifp_error
73 1 : sss_sifp_fetch_object_by_name(sss_sifp_ctx *ctx,
74 : const char *method,
75 : const char *name,
76 : sss_sifp_object **_object)
77 : {
78 1 : return sss_sifp_fetch_object_by_attr(ctx, method, DBUS_TYPE_STRING, &name,
79 : _object);
80 : }
81 :
82 : sss_sifp_error
83 1 : sss_sifp_list_domains(sss_sifp_ctx *ctx,
84 : char ***_domains)
85 : {
86 1 : sss_sifp_attr **attrs = NULL;
87 1 : char **object_paths = NULL;
88 1 : char **domains = NULL;
89 1 : const char *name = NULL;
90 : unsigned int size;
91 : unsigned int i;
92 : sss_sifp_error ret;
93 :
94 1 : if (_domains == NULL) {
95 0 : return SSS_SIFP_INVALID_ARGUMENT;
96 : }
97 :
98 1 : ret = sss_sifp_invoke_list(ctx, "Domains", &object_paths,
99 : DBUS_TYPE_INVALID);
100 1 : if (ret != SSS_SIFP_OK) {
101 0 : goto done;
102 : }
103 :
104 : /* calculate number of paths acquired and allocate memory for domains */
105 1 : for (size = 0; object_paths[size] != NULL; size++);
106 :
107 1 : domains = _alloc_zero(ctx, char *, size + 1);
108 1 : if (domains == NULL) {
109 0 : ret = SSS_SIFP_OUT_OF_MEMORY;
110 0 : goto done;
111 : }
112 :
113 : /* fetch domain name */
114 3 : for (i = 0; i < size; i++) {
115 2 : ret = sss_sifp_fetch_attr(ctx, object_paths[i],
116 : SSS_SIFP_IFACE_DOMAINS,
117 : SSS_SIFP_ATTR_NAME, &attrs);
118 2 : if (ret != SSS_SIFP_OK) {
119 0 : goto done;
120 : }
121 :
122 2 : ret = sss_sifp_find_attr_as_string(attrs, SSS_SIFP_ATTR_NAME, &name);
123 2 : if (ret != SSS_SIFP_OK) {
124 0 : goto done;
125 : }
126 :
127 2 : domains[i] = sss_sifp_strdup(ctx, name);
128 2 : if (domains[i] == NULL) {
129 0 : ret = SSS_SIFP_OUT_OF_MEMORY;
130 0 : goto done;
131 : }
132 :
133 2 : sss_sifp_free_attrs(ctx, &attrs);
134 : }
135 :
136 1 : domains[i] = NULL;
137 :
138 1 : *_domains = domains;
139 :
140 1 : ret = SSS_SIFP_OK;
141 :
142 : done:
143 1 : sss_sifp_free_attrs(ctx, &attrs);
144 1 : sss_sifp_free_string_array(ctx, &object_paths);
145 :
146 1 : if (ret != SSS_SIFP_OK) {
147 0 : sss_sifp_free_string_array(ctx, &domains);
148 : }
149 :
150 1 : return ret;
151 : }
152 :
153 : sss_sifp_error
154 1 : sss_sifp_fetch_domain_by_name(sss_sifp_ctx *ctx,
155 : const char *name,
156 : sss_sifp_object **_domain)
157 : {
158 1 : return sss_sifp_fetch_object_by_name(ctx, "DomainByName", name, _domain);
159 : }
160 :
161 : sss_sifp_error
162 0 : sss_sifp_fetch_user_by_uid(sss_sifp_ctx *ctx,
163 : uid_t uid,
164 : sss_sifp_object **_user)
165 : {
166 0 : uint64_t _uid = uid;
167 :
168 0 : return sss_sifp_fetch_object_by_attr(ctx, "UserByID",
169 : DBUS_TYPE_UINT64, &_uid, _user);
170 : }
171 :
172 : sss_sifp_error
173 0 : sss_sifp_fetch_user_by_name(sss_sifp_ctx *ctx,
174 : const char *name,
175 : sss_sifp_object **_user)
176 : {
177 0 : return sss_sifp_fetch_object_by_name(ctx, "UserByName", name, _user);
178 : }
|