1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*
Authors:
Pavel Březina <pbrezina@redhat.com>
Copyright (C) 2016 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "responder/nss/nss_protocol.h"
#include "util/sss_nss.h"
static uint32_t
sss_nss_get_gid(struct sss_domain_info *domain,
struct ldb_message *msg)
{
uint32_t gid;
/* First, try to return overridden gid. */
if (DOM_HAS_VIEWS(domain)) {
gid = ldb_msg_find_attr_as_uint64(msg, OVERRIDE_PREFIX SYSDB_GIDNUM,
0);
if (gid != 0) {
return gid;
}
}
/* Try to return domain gid override. */
if (domain->override_gid != 0) {
return domain->override_gid;
}
/* Return original gid. */
return ldb_msg_find_attr_as_uint64(msg, SYSDB_GIDNUM, 0);
}
static const char *
sss_nss_get_homedir_override(TALLOC_CTX *mem_ctx,
struct ldb_message *msg,
struct sss_nss_ctx *nctx,
struct sss_domain_info *dom,
struct sss_nss_homedir_ctx *homedir_ctx)
{
const char *homedir;
bool is_override = false;
homedir = sss_view_ldb_msg_find_attr_as_string_ex(dom, msg, SYSDB_HOMEDIR,
NULL, &is_override);
homedir_ctx->original = homedir;
/* Check to see which homedir_prefix to use. */
if (dom->homedir_substr != NULL) {
homedir_ctx->config_homedir_substr = dom->homedir_substr;
} else if (nctx->homedir_substr != NULL) {
homedir_ctx->config_homedir_substr = nctx->homedir_substr;
}
/* Individual overrides have the highest priority, only templates will be
* expanded and no further options will be evaluated. */
if (is_override) {
return expand_homedir_template(mem_ctx, homedir,
dom->case_preserve, homedir_ctx);
}
/* Here we skip the files provider as it should always return *only*
* what's in the files and nothing else.
*/
if (!is_files_provider(dom)) {
/* Check whether we are unconditionally overriding the server
* for home directory locations.
*/
if (dom->override_homedir) {
return expand_homedir_template(mem_ctx, dom->override_homedir,
dom->case_preserve, homedir_ctx);
} else if (nctx->override_homedir) {
return expand_homedir_template(mem_ctx, nctx->override_homedir,
dom->case_preserve, homedir_ctx);
}
}
if (!homedir || *homedir == '\0') {
/* In the case of a NULL or empty homedir, check to see if
* we have a fallback homedir to use.
*/
if (dom->fallback_homedir) {
return expand_homedir_template(mem_ctx, dom->fallback_homedir,
dom->case_preserve, homedir_ctx);
} else if (nctx->fallback_homedir) {
return expand_homedir_template(mem_ctx, nctx->fallback_homedir,
dom->case_preserve, homedir_ctx);
}
}
/* Provider can also return template, try to expand it.*/
return expand_homedir_template(mem_ctx, homedir,
dom->case_preserve, homedir_ctx);
}
static const char *
sss_nss_get_homedir(TALLOC_CTX *mem_ctx,
struct sss_nss_ctx *nss_ctx,
struct sss_domain_info *domain,
struct ldb_message *msg,
const char *orig_name,
const char *upn,
uid_t uid)
{
struct sss_nss_homedir_ctx hd_ctx = { 0 };
const char *homedir;
hd_ctx.username = orig_name;
hd_ctx.uid = uid;
hd_ctx.domain = domain->name;
hd_ctx.upn = upn;
homedir = sss_nss_get_homedir_override(mem_ctx, msg, nss_ctx, domain, &hd_ctx);
if (homedir == NULL) {
return "";
}
return homedir;
}
static errno_t
sss_nss_get_shell(struct sss_nss_ctx *nss_ctx,
struct sss_domain_info *domain,
struct ldb_message *msg,
const char *name,
uint32_t uid,
const char **_shell)
{
const char *shell = NULL;
if (nss_ctx->rctx->sr_conf.scope != SESSION_RECORDING_SCOPE_NONE) {
const char *sr_enabled;
sr_enabled = ldb_msg_find_attr_as_string(
msg, SYSDB_SESSION_RECORDING, NULL);
if (sr_enabled == NULL) {
DEBUG(SSSDBG_OP_FAILURE,
"%s attribute not found for %s[%u]! Skipping\n",
SYSDB_SESSION_RECORDING, name, uid);
return EINVAL;
} else if (strcmp(sr_enabled, "TRUE") == 0) {
shell = SESSION_RECORDING_SHELL;
} else if (strcmp(sr_enabled, "FALSE") != 0) {
DEBUG(SSSDBG_OP_FAILURE,
"Skipping %s[%u] "
"because its %s attribute value is invalid: %s\n",
name, uid, SYSDB_SESSION_RECORDING, sr_enabled);
return EINVAL;
}
}
if (shell == NULL) {
shell = sss_resp_get_shell_override(msg, nss_ctx->rctx, domain);
}
*_shell = shell;
return EOK;
}
static errno_t
sss_nss_get_pwent(TALLOC_CTX *mem_ctx,
struct sss_nss_ctx *nss_ctx,
struct sss_domain_info *domain,
struct ldb_message *msg,
uint32_t *_uid,
uint32_t *_gid,
struct sized_string **_name,
struct sized_string *_gecos,
struct sized_string *_homedir,
struct sized_string *_shell)
{
const char *upn;
const char *name;
const char *gecos;
const char *homedir;
const char *shell;
uint32_t gid;
uint32_t uid;
errno_t ret;
/* Get fields. */
upn = ldb_msg_find_attr_as_string(msg, SYSDB_UPN, NULL);
name = sss_get_name_from_msg(domain, msg);
gid = sss_nss_get_gid(domain, msg);
uid = sss_view_ldb_msg_find_attr_as_uint64(domain, msg, SYSDB_UIDNUM, 0);
if (name == NULL || uid == 0 || gid == 0) {
DEBUG(SSSDBG_OP_FAILURE,
"Incomplete user object for %s[%u]! Skipping\n",
name ? name : "<NULL>", uid);
return EINVAL;
}
gecos = sss_view_ldb_msg_find_attr_as_string(domain, msg, SYSDB_GECOS,
NULL);
homedir = sss_nss_get_homedir(mem_ctx, nss_ctx, domain, msg, name, upn, uid);
ret = sss_nss_get_shell(nss_ctx, domain, msg, name, uid, &shell);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"failed retrieving shell for %s[%u], skipping [%d]: %s\n",
name, uid, ret, sss_strerror(ret));
return ret;
}
/* Convert to sized strings. */
ret = sized_output_name(mem_ctx, nss_ctx->rctx, name, domain, _name);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
"sized_output_name failed, skipping [%d]: %s\n",
ret, sss_strerror(ret));
return ret;
}
to_sized_string(_gecos, gecos == NULL ? "" : gecos);
to_sized_string(_shell, shell);
to_sized_string(_homedir, homedir);
*_gid = gid;
*_uid = uid;
return EOK;
}
errno_t
sss_nss_protocol_fill_pwent(struct sss_nss_ctx *nss_ctx,
struct sss_nss_cmd_ctx *cmd_ctx,
struct sss_packet *packet,
struct cache_req_result *result)
{
TALLOC_CTX *tmp_ctx;
struct ldb_message *msg;
struct sized_string pwfield;
struct sized_string *name;
struct sized_string gecos;
struct sized_string homedir;
struct sized_string shell;
uint32_t gid;
uint32_t uid;
uint32_t num_results;
size_t rp;
size_t body_len;
uint8_t *body;
int i;
errno_t ret;
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
return ENOMEM;
}
/* First two fields (length and reserved), filled up later. */
ret = sss_packet_grow(packet, 2 * sizeof(uint32_t));
if (ret != EOK) {
return ret;
}
rp = 2 * sizeof(uint32_t);
num_results = 0;
for (i = 0; i < result->count; i++) {
talloc_free_children(tmp_ctx);
msg = result->msgs[i];
/* Password field content. */
to_sized_string(&pwfield, sss_nss_get_pwfield(nss_ctx, result->domain));
ret = sss_nss_get_pwent(tmp_ctx, nss_ctx, result->domain, msg, &uid, &gid,
&name, &gecos, &homedir, &shell);
if (ret != EOK) {
continue;
}
/* Adjust packet size: uid, gid + string fields. */
ret = sss_packet_grow(packet, 2 * sizeof(uint32_t)
+ name->len + gecos.len + homedir.len
+ shell.len + pwfield.len);
if (ret != EOK) {
goto done;
}
sss_packet_get_body(packet, &body, &body_len);
/* Fill packet. */
SAFEALIGN_SET_UINT32(&body[rp], uid, &rp);
SAFEALIGN_SET_UINT32(&body[rp], gid, &rp);
SAFEALIGN_SET_STRING(&body[rp], name->str, name->len, &rp);
SAFEALIGN_SET_STRING(&body[rp], pwfield.str, pwfield.len, &rp);
SAFEALIGN_SET_STRING(&body[rp], gecos.str, gecos.len, &rp);
SAFEALIGN_SET_STRING(&body[rp], homedir.str, homedir.len, &rp);
SAFEALIGN_SET_STRING(&body[rp], shell.str, shell.len, &rp);
num_results++;
/* Do not store entry in memory cache during enumeration or when
* requested or if cache explicitly disabled. */
if (!cmd_ctx->enumeration
&& ((cmd_ctx->flags & SSS_NSS_EX_FLAG_INVALIDATE_CACHE) == 0)
&& (nss_ctx->pwd_mc_ctx != NULL)) {
ret = sss_mmap_cache_pw_store(&nss_ctx->pwd_mc_ctx, name, &pwfield,
uid, gid, &gecos, &homedir, &shell);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"Failed to store user %s (%s) in mmap cache [%d]: %s!\n",
name->str, result->domain->name, ret, sss_strerror(ret));
}
}
}
ret = EOK;
done:
talloc_free(tmp_ctx);
if (ret != EOK) {
sss_packet_set_size(packet, 0);
return ret;
}
sss_packet_get_body(packet, &body, &body_len);
SAFEALIGN_COPY_UINT32(body, &num_results, NULL);
SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
return EOK;
}
|