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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <exec_attr.h>
#include <rpcsvc/ypclnt.h>
#include <rpcsvc/yp_prot.h>
#include "nis_common.h"
/* extern from nis_common.c */
extern void massage_netdb(const char **, int *);
/* externs from libnsl */
extern int _doexeclist(nss_XbyY_args_t *);
extern char *_exec_wild_id(char *, const char *);
extern void _exec_cleanup(nss_status_t, nss_XbyY_args_t *);
extern char *_strtok_escape(char *, char *, char **);
typedef struct __exec_nis_args {
int *yp_status;
nss_XbyY_args_t *argp;
} _exec_nis_args;
/*
* check_match: returns 1 if - matching entry found and no more entries needed,
* or, entry cannot be found because of error;
* returns 0 if - no matching entry found, or,
* matching entry found and next match needed.
*/
static int
check_match(nss_XbyY_args_t *argp, int check_policy)
{
execstr_t *exec = (execstr_t *)(argp->returnval);
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
const char *name = _priv_exec->name;
const char *type = _priv_exec->type;
const char *id = _priv_exec->id;
const char *policy = _priv_exec->policy;
if (name && id) {
/*
* NSS_DBOP_EXECATTR_BYNAMEID searched for name and id in
* _exec_nis_lookup already.
* If we're talking to pre-Solaris9 nis servers, check policy,
* as policy was not a searchable column then.
*/
if ((check_policy && policy &&
(strcmp(policy, exec->policy) != 0)) ||
(type && (strcmp(type, exec->type) != 0))) {
return (0);
}
} else if ((policy && exec->policy &&
(strcmp(policy, exec->policy) != 0)) ||
(name && exec->name && (strcmp(name, exec->name) != 0)) ||
(type && exec->type && (strcmp(type, exec->type) != 0)) ||
(id && exec->id && (strcmp(id, exec->id) != 0))) {
return (0);
}
return (1);
}
/*
* check_match_strbuf: set up the data needed by check_match()
* and call it to match exec_attr data in strbuf and argp->key.attrp
*/
static int
check_match_strbuf(nss_XbyY_args_t *argp, char *strbuf, int check_policy)
{
char *last = NULL;
char *sep = KV_TOKEN_DELIMIT;
execstr_t exec;
execstr_t *execp = &exec;
void *sp;
int rc;
/*
* Remove newline that yp_match puts at the
* end of the entry it retrieves from the map.
*/
if (strbuf[argp->returnlen] == '\n') {
strbuf[argp->returnlen] = '\0';
}
execp->name = _strtok_escape(strbuf, sep, &last);
execp->policy = _strtok_escape(NULL, sep, &last);
execp->type = _strtok_escape(NULL, sep, &last);
execp->res1 = _strtok_escape(NULL, sep, &last);
execp->res2 = _strtok_escape(NULL, sep, &last);
execp->id = _strtok_escape(NULL, sep, &last);
sp = argp->returnval;
argp->returnval = execp;
rc = check_match(argp, check_policy);
argp->returnval = sp;
free(strbuf);
return (rc);
}
static nss_status_t
_exec_nis_parse(const char *instr,
int instr_len,
nss_XbyY_args_t *argp,
int check_policy)
{
int parse_stat;
nss_status_t res;
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
char *strbuf;
int check_matched;
argp->returnval = NULL;
argp->returnlen = 0;
parse_stat = (*argp->str2ent)(instr, instr_len, argp->buf.result,
argp->buf.buffer, argp->buf.buflen);
switch (parse_stat) {
case NSS_STR_PARSE_SUCCESS:
argp->returnlen = instr_len;
/* if exec_attr file format requested */
if (argp->buf.result == NULL) {
argp->returnval = argp->buf.buffer;
if ((strbuf = strdup(instr)) == NULL)
res = NSS_UNAVAIL;
check_matched = check_match_strbuf(argp,
strbuf, check_policy);
} else {
argp->returnval = argp->buf.result;
check_matched = check_match(argp, check_policy);
}
if (check_matched) {
res = NSS_SUCCESS;
if (IS_GET_ALL(_priv_exec->search_flag)) {
if (_doexeclist(argp) == 0) {
res = NSS_UNAVAIL;
}
}
} else {
res = NSS_NOTFOUND;
}
break;
case NSS_STR_PARSE_ERANGE:
argp->erange = 1;
res = NSS_NOTFOUND;
break;
default:
res = NSS_UNAVAIL;
break;
}
return (res);
}
/*
* This is the callback for yp_all. It returns 0 to indicate that it wants to
* be called again for further key-value pairs, or returns non-zero to stop the
* flow of key-value pairs. If it returns a non-zero value, it is not called
* again. The functional value of yp_all is then 0.
*/
/*ARGSUSED*/
static int
_exec_nis_cb(int instatus,
char *inkey,
int inkeylen,
char *inval,
int invallen,
void *indata)
{
int check_policy = 1; /* always check policy for yp_all */
int stop_cb;
const char *filter;
nss_status_t res;
_exec_nis_args *eargp = (_exec_nis_args *)indata;
nss_XbyY_args_t *argp = eargp->argp;
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
if (instatus != YP_TRUE) {
/*
* If we have no more data to look at, we want to
* keep yp_status from previous key/value pair
* that we processed.
* If this is the 1st time we enter this callback,
* yp_status is already set to YPERR_YPERR
* (see _exec_nis_lookup() for when this callback
* and arguments are set initially).
*/
if (instatus != YP_NOMORE) {
*(eargp->yp_status) = YPERR_YPERR;
}
return (0); /* yp_all may decide otherwise... */
}
filter = (_priv_exec->name) ? _priv_exec->name : _priv_exec->id;
/*
* yp_all does not null terminate the entry it retrieves from the
* map, unlike yp_match. so we do it explicitly here.
*/
inval[invallen] = '\0';
/*
* Optimization: if the entry doesn't contain the filter string then
* it can't be the entry we want, so don't bother looking more closely
* at it.
*/
if ((_priv_exec->policy &&
(strstr(inval, _priv_exec->policy) == NULL)) ||
(strstr(inval, filter) == NULL)) {
*(eargp->yp_status) = YPERR_KEY;
return (0);
}
res = _exec_nis_parse(inval, invallen, argp, check_policy);
switch (res) {
case NSS_SUCCESS:
*(eargp->yp_status) = 0;
stop_cb = IS_GET_ONE(_priv_exec->search_flag);
break;
case NSS_UNAVAIL:
*(eargp->yp_status) = YPERR_KEY;
stop_cb = 1;
break;
default:
*(eargp->yp_status) = YPERR_YPERR;
stop_cb = 0;
break;
}
return (stop_cb);
}
static nss_status_t
_exec_nis_lookup(nis_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag)
{
int ypstatus;
nss_status_t res = NSS_SUCCESS;
nss_status_t ypres;
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
if (getby_flag == NSS_DBOP_EXECATTR_BYNAMEID) {
int check_policy = 0;
int vallen;
char *val;
char key[MAX_INPUT];
/*
* Try using policy as part of search key. If that fails,
* (it will, in case of pre-Solaris9 nis server where policy
* was not searchable), try again without using policy.
*/
if (snprintf(key, MAX_INPUT, "%s%s%s%s%s", _priv_exec->name,
KV_TOKEN_DELIMIT, _priv_exec->policy, KV_TOKEN_DELIMIT,
_priv_exec->id) >= MAX_INPUT)
return (NSS_NOTFOUND);
do {
ypres = _nss_nis_ypmatch(be->domain, NIS_MAP_EXECATTR,
key, &val, &vallen, &ypstatus);
if ((check_policy == 0) && (ypstatus == YPERR_KEY)) {
(void) snprintf(key, MAX_INPUT, "%s%s%s",
_priv_exec->name, KV_TOKEN_DELIMIT,
_priv_exec->id);
check_policy = 1;
continue;
} else if (ypres != NSS_SUCCESS) {
res = ypres;
break;
} else {
char *val_save = val;
massage_netdb((const char **)&val, &vallen);
res = _exec_nis_parse((const char *)val,
vallen, argp, check_policy);
free(val_save);
break;
}
} while (res == NSS_SUCCESS);
} else {
int ypstat = YPERR_YPERR;
struct ypall_callback cback;
_exec_nis_args eargs;
eargs.yp_status = &ypstat;
eargs.argp = argp;
cback.foreach = _exec_nis_cb;
cback.data = (void *)&eargs;
/*
* Instead of calling yp_all() doing hard lookup, we use
* the alternative function, __yp_all_cflookup(), to
* perform soft lookup when binding to nis servers with
* time-out control. Other than that, these two functions
* do exactly the same thing.
*/
ypstatus = __yp_all_cflookup((char *)(be->domain),
(char *)(be->enum_map), &cback, 0);
/*
* For GET_ALL, check if we found anything at all.
*/
if (_priv_exec->head_exec != NULL)
return (NSS_SUCCESS);
switch (ypstat) {
case 0:
res = NSS_SUCCESS;
break;
case YPERR_BUSY:
res = NSS_TRYAGAIN;
break;
case YPERR_KEY:
/*
* If no such key, return NSS_NOTFOUND
* as this looks more relevant; it will
* also help libnsl to try with another
* policy (see _getexecprof()).
*/
res = NSS_NOTFOUND;
break;
default:
res = NSS_UNAVAIL;
break;
}
}
return (res);
}
/*
* If search for exact match for id failed, get_wild checks if we have
* a wild-card entry for that id.
*/
static nss_status_t
get_wild(nis_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag)
{
const char *orig_id;
char *old_id = NULL;
char *wild_id = NULL;
nss_status_t res = NSS_NOTFOUND;
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
orig_id = _priv_exec->id;
old_id = strdup(_priv_exec->id);
wild_id = old_id;
while ((wild_id = _exec_wild_id(wild_id, _priv_exec->type)) != NULL) {
_priv_exec->id = wild_id;
res = _exec_nis_lookup(be, argp, getby_flag);
if (res == NSS_SUCCESS)
break;
}
_priv_exec->id = orig_id;
if (old_id)
free(old_id);
return (res);
}
static nss_status_t
getbynam(nis_backend_ptr_t be, void *a)
{
nss_status_t res;
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
res = _exec_nis_lookup(be, argp, NSS_DBOP_EXECATTR_BYNAME);
_exec_cleanup(res, argp);
return (res);
}
static nss_status_t
getbyid(nis_backend_ptr_t be, void *a)
{
nss_status_t res;
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
/*LINTED*/
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
res = _exec_nis_lookup(be, argp, NSS_DBOP_EXECATTR_BYID);
if (res != NSS_SUCCESS)
res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYID);
_exec_cleanup(res, argp);
return (res);
}
static nss_status_t
getbynameid(nis_backend_ptr_t be, void *a)
{
nss_status_t res;
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
/*LINTED*/
_priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp);
res = _exec_nis_lookup(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
if (res != NSS_SUCCESS)
res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
_exec_cleanup(res, argp);
return (res);
}
static nis_backend_op_t execattr_ops[] = {
_nss_nis_destr,
_nss_nis_endent,
_nss_nis_setent,
_nss_nis_getent_netdb,
getbynam,
getbyid,
getbynameid
};
/*ARGSUSED*/
nss_backend_t *
_nss_nis_exec_attr_constr(const char *dummy1,
const char *dummy2,
const char *dummy3,
const char *dummy4,
const char *dummy5,
const char *dummy6,
const char *dummy7)
{
return (_nss_nis_constr(execattr_ops,
sizeof (execattr_ops)/sizeof (execattr_ops[0]),
NIS_MAP_EXECATTR));
}
|