4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include "idmap_priv.h"
32 #include "nldaputils.h"
39 #include <sys/types.h>
44 #include <auth_attr.h>
46 #include <sys/u8_textprep.h>
49 #define _VALIDATE_LIST_CB_DATA(col, val, siz)\
50 retcode = validate_list_cb_data(cb_data, argc, argv, col,\
51 (uchar_t **)val, siz);\
52 if (retcode == IDMAP_NEXT) {\
53 result->retcode = IDMAP_NEXT;\
55 } else if (retcode < 0) {\
56 result->retcode = retcode;\
60 #define PROCESS_LIST_SVC_SQL(rcode, db, dbname, sql, limit, flag, cb, res, len)\
61 rcode = process_list_svc_sql(db, dbname, sql, limit, flag, cb, res);\
62 if (rcode == IDMAP_ERR_BUSY)\
63 res->retcode = IDMAP_ERR_BUSY;\
64 else if (rcode == IDMAP_SUCCESS && len == 0)\
65 res->retcode = IDMAP_ERR_NOTFOUND;
68 #define STRDUP_OR_FAIL(to, from) \
72 if ((to = strdup(from)) == NULL) \
76 #define STRDUP_CHECK(to, from) \
77 if ((from) != NULL) { \
80 result->retcode = IDMAP_ERR_MEMORY; \
87 idmap_null_1_svc(void *result
, struct svc_req
*rqstp
)
93 * RPC layer allocates empty strings to replace NULL char *.
94 * This utility function frees these empty strings.
98 sanitize_mapping_request(idmap_mapping
*req
)
100 if (EMPTY_STRING(req
->id1name
)) {
104 if (EMPTY_STRING(req
->id1domain
)) {
105 free(req
->id1domain
);
106 req
->id1domain
= NULL
;
108 if (EMPTY_STRING(req
->id2name
)) {
112 if (EMPTY_STRING(req
->id2domain
)) {
113 free(req
->id2domain
);
114 req
->id2domain
= NULL
;
116 req
->direction
= _IDMAP_F_DONE
;
121 validate_mapped_id_by_name_req(idmap_mapping
*req
)
125 if (IS_ID_UID(req
->id1
) || IS_ID_GID(req
->id1
))
126 return (IDMAP_SUCCESS
);
128 if (IS_ID_SID(req
->id1
)) {
129 if (!EMPTY_STRING(req
->id1name
) &&
130 u8_validate(req
->id1name
, strlen(req
->id1name
),
131 NULL
, U8_VALIDATE_ENTIRE
, &e
) < 0)
132 return (IDMAP_ERR_BAD_UTF8
);
133 if (!EMPTY_STRING(req
->id1domain
) &&
134 u8_validate(req
->id1domain
, strlen(req
->id1domain
),
135 NULL
, U8_VALIDATE_ENTIRE
, &e
) < 0)
136 return (IDMAP_ERR_BAD_UTF8
);
139 return (IDMAP_SUCCESS
);
144 validate_rule(idmap_namerule
*rule
)
148 if (!EMPTY_STRING(rule
->winname
) &&
149 u8_validate(rule
->winname
, strlen(rule
->winname
),
150 NULL
, U8_VALIDATE_ENTIRE
, &e
) < 0)
151 return (IDMAP_ERR_BAD_UTF8
);
153 if (!EMPTY_STRING(rule
->windomain
) &&
154 u8_validate(rule
->windomain
, strlen(rule
->windomain
),
155 NULL
, U8_VALIDATE_ENTIRE
, &e
) < 0)
156 return (IDMAP_ERR_BAD_UTF8
);
158 return (IDMAP_SUCCESS
);
164 validate_rules(idmap_update_batch
*batch
)
169 for (i
= 0; i
< batch
->idmap_update_batch_len
; i
++) {
170 up
= &(batch
->idmap_update_batch_val
[i
]);
171 if (validate_rule(&(up
->idmap_update_op_u
.rule
))
173 return (IDMAP_ERR_BAD_UTF8
);
176 return (IDMAP_SUCCESS
);
181 idmap_get_mapped_ids_1_svc(idmap_mapping_batch batch
,
182 idmap_ids_res
*result
, struct svc_req
*rqstp
)
184 sqlite
*cache
= NULL
, *db
= NULL
;
185 lookup_state_t state
;
186 idmap_retcode retcode
;
190 boolean_t any_tracing
;
193 (void) memset(result
, 0, sizeof (*result
));
194 (void) memset(&state
, 0, sizeof (state
));
196 /* Return success if nothing was requested */
197 if (batch
.idmap_mapping_batch_len
< 1)
200 /* Get cache handle */
201 result
->retcode
= get_cache_handle(&cache
);
202 if (result
->retcode
!= IDMAP_SUCCESS
)
207 result
->retcode
= get_db_handle(&db
);
208 if (result
->retcode
!= IDMAP_SUCCESS
)
212 /* Allocate result array */
213 result
->ids
.ids_val
= calloc(batch
.idmap_mapping_batch_len
,
214 sizeof (idmap_id_res
));
215 if (result
->ids
.ids_val
== NULL
) {
216 idmapdlog(LOG_ERR
, "Out of memory");
217 result
->retcode
= IDMAP_ERR_MEMORY
;
220 result
->ids
.ids_len
= batch
.idmap_mapping_batch_len
;
222 /* Allocate hash table to check for duplicate sids */
223 state
.sid_history
= calloc(batch
.idmap_mapping_batch_len
,
224 sizeof (*state
.sid_history
));
225 if (state
.sid_history
== NULL
) {
226 idmapdlog(LOG_ERR
, "Out of memory");
227 result
->retcode
= IDMAP_ERR_MEMORY
;
230 state
.sid_history_size
= batch
.idmap_mapping_batch_len
;
231 for (i
= 0; i
< state
.sid_history_size
; i
++) {
232 state
.sid_history
[i
].key
= state
.sid_history_size
;
233 state
.sid_history
[i
].next
= state
.sid_history_size
;
235 state
.batch
= &batch
;
236 state
.result
= result
;
238 /* Get directory-based name mapping info */
239 result
->retcode
= load_cfg_in_state(&state
);
240 if (result
->retcode
!= IDMAP_SUCCESS
)
243 /* Init our 'done' flags */
244 state
.sid2pid_done
= state
.pid2sid_done
= TRUE
;
246 any_tracing
= B_FALSE
;
249 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
250 req
= &batch
.idmap_mapping_batch_val
[i
];
251 res
= &result
->ids
.ids_val
[i
];
253 any_tracing
= B_TRUE
;
255 (void) sanitize_mapping_request(req
);
256 TRACE(req
, res
, "Start mapping");
257 if (IS_ID_SID(req
->id1
)) {
258 retcode
= sid2pid_first_pass(
262 } else if (IS_ID_UID(req
->id1
)) {
263 retcode
= pid2sid_first_pass(
267 } else if (IS_ID_GID(req
->id1
)) {
268 retcode
= pid2sid_first_pass(
273 res
->retcode
= IDMAP_ERR_IDTYPE
;
276 if (IDMAP_FATAL_ERROR(retcode
)) {
277 result
->retcode
= retcode
;
282 /* Check if we are done */
283 if (state
.sid2pid_done
== TRUE
&& state
.pid2sid_done
== TRUE
)
287 * native LDAP lookups:
289 * - nldap or mixed mode. Lookup nldap by pid or unixname to get
292 * - nldap mode. Got winname and sid (either given or found in
293 * name_cache). Lookup nldap by winname to get pid and
296 if (state
.nldap_nqueries
) {
297 retcode
= nldap_lookup_batch(&state
, &batch
, result
);
298 if (IDMAP_FATAL_ERROR(retcode
)) {
299 TRACE(req
, res
, "Native LDAP lookup error=%d", retcode
);
300 result
->retcode
= retcode
;
304 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
305 res
= &result
->ids
.ids_val
[i
];
306 req
= &batch
.idmap_mapping_batch_val
[i
];
307 if (IDMAP_ERROR(res
->retcode
)) {
309 "Native LDAP lookup error=%d",
312 TRACE(req
, res
, "Native LDAP lookup");
321 * - nldap or mixed mode. Got winname from nldap lookup.
322 * winname2sid could not be resolved locally. Lookup AD
323 * by winname to get sid.
324 * - ad mode. Got unixname. Lookup AD by unixname to get
327 * - ad or mixed mode. Lookup AD by sid or winname to get
328 * winname, sid and unixname.
329 * - any mode. Got either sid or winname but not both. Lookup
330 * AD by sid or winname to get winname, sid.
332 if (state
.ad_nqueries
) {
333 retcode
= ad_lookup_batch(&state
, &batch
, result
);
334 if (IDMAP_FATAL_ERROR(retcode
)) {
335 TRACE(req
, res
, "AD lookup error=%d", retcode
);
336 result
->retcode
= retcode
;
339 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
340 res
= &result
->ids
.ids_val
[i
];
341 req
= &batch
.idmap_mapping_batch_val
[i
];
342 if (res
->retcode
== IDMAP_ERR_DOMAIN_NOTFOUND
&&
343 req
->id1
.idmap_id_u
.sid
.prefix
!= NULL
&&
344 req
->id1name
!= NULL
) {
346 * If AD lookup failed Domain Not Found but
347 * we have a winname and SID, it means that
349 * - it's a request a cross-forest trust
351 * - we were looking for directory-based
352 * mapping information.
353 * In that case, we're OK, just go on.
355 * If this seems more convoluted than it
356 * should be, it is - really, we probably
357 * shouldn't even be attempting AD lookups
358 * in this situation, but that's a more
359 * intricate cleanup that will have to wait
362 res
->retcode
= IDMAP_SUCCESS
;
364 "AD lookup - domain not found (ignored)");
371 * native LDAP lookups:
373 * - nldap mode. Got winname and sid from AD lookup. Lookup nldap
374 * by winname to get pid and unixname.
376 if (state
.nldap_nqueries
) {
377 retcode
= nldap_lookup_batch(&state
, &batch
, result
);
378 if (IDMAP_FATAL_ERROR(retcode
)) {
379 TRACE(req
, res
, "Native LDAP lookup error=%d", retcode
);
380 result
->retcode
= retcode
;
384 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
385 res
= &result
->ids
.ids_val
[i
];
386 req
= &batch
.idmap_mapping_batch_val
[i
];
387 TRACE(req
, res
, "Native LDAP lookup");
392 /* Reset 'done' flags */
393 state
.sid2pid_done
= state
.pid2sid_done
= TRUE
;
396 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
397 req
= &batch
.idmap_mapping_batch_val
[i
];
398 res
= &result
->ids
.ids_val
[i
];
400 if (IS_ID_SID(req
->id1
)) {
401 retcode
= sid2pid_second_pass(
405 } else if (IS_ID_UID(req
->id1
)) {
406 retcode
= pid2sid_second_pass(
410 } else if (IS_ID_GID(req
->id1
)) {
411 retcode
= pid2sid_second_pass(
416 /* First stage has already set the error */
419 if (IDMAP_FATAL_ERROR(retcode
)) {
420 result
->retcode
= retcode
;
425 /* Check if we are done */
426 if (state
.sid2pid_done
== TRUE
&& state
.pid2sid_done
== TRUE
)
429 /* Reset our 'done' flags */
430 state
.sid2pid_done
= state
.pid2sid_done
= TRUE
;
432 /* Update cache in a single transaction */
433 if (sql_exec_no_cb(cache
, IDMAP_CACHENAME
, "BEGIN TRANSACTION;")
437 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
438 req
= &batch
.idmap_mapping_batch_val
[i
];
439 res
= &result
->ids
.ids_val
[i
];
441 if (IS_ID_SID(req
->id1
)) {
442 (void) update_cache_sid2pid(
446 } else if ((IS_ID_UID(req
->id1
)) ||
447 (IS_ID_GID(req
->id1
))) {
448 (void) update_cache_pid2sid(
455 /* Commit if we have at least one successful update */
456 if (state
.sid2pid_done
== FALSE
|| state
.pid2sid_done
== FALSE
)
457 (void) sql_exec_no_cb(cache
, IDMAP_CACHENAME
,
458 "COMMIT TRANSACTION;");
460 (void) sql_exec_no_cb(cache
, IDMAP_CACHENAME
,
464 cleanup_lookup_state(&state
);
465 if (IDMAP_ERROR(result
->retcode
)) {
467 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
468 req
= &batch
.idmap_mapping_batch_val
[i
];
469 res
= &result
->ids
.ids_val
[i
];
471 "Failure code %d", result
->retcode
);
474 xdr_free(xdr_idmap_ids_res
, (caddr_t
)result
);
475 result
->ids
.ids_len
= 0;
476 result
->ids
.ids_val
= NULL
;
479 for (i
= 0; i
< batch
.idmap_mapping_batch_len
; i
++) {
480 req
= &batch
.idmap_mapping_batch_val
[i
];
481 res
= &result
->ids
.ids_val
[i
];
482 TRACE(req
, res
, "Done");
486 result
->retcode
= idmap_stat4prot(result
->retcode
);
488 for (i
= 0; i
< result
->ids
.ids_len
; i
++) {
489 req
= &batch
.idmap_mapping_batch_val
[i
];
490 res
= &result
->ids
.ids_val
[i
];
492 if (!(req
->flag
& IDMAP_REQ_FLG_MAPPING_INFO
) &&
493 res
->retcode
== IDMAP_SUCCESS
)
494 idmap_how_clear(&res
->info
.how
);
503 list_mappings_cb(void *parg
, int argc
, char **argv
, char **colnames
)
505 list_cb_data_t
*cb_data
;
507 idmap_mappings_res
*result
;
508 idmap_retcode retcode
;
511 static int validated_column_names
= 0;
514 cb_data
= (list_cb_data_t
*)parg
;
516 if (!validated_column_names
) {
517 assert(strcmp(colnames
[0], "rowid") == 0);
518 assert(strcmp(colnames
[1], "sidprefix") == 0);
519 assert(strcmp(colnames
[2], "rid") == 0);
520 assert(strcmp(colnames
[3], "pid") == 0);
521 assert(strcmp(colnames
[4], "w2u") == 0);
522 assert(strcmp(colnames
[5], "u2w") == 0);
523 assert(strcmp(colnames
[6], "windomain") == 0);
524 assert(strcmp(colnames
[7], "canon_winname") == 0);
525 assert(strcmp(colnames
[8], "unixname") == 0);
526 assert(strcmp(colnames
[9], "is_user") == 0);
527 assert(strcmp(colnames
[10], "is_wuser") == 0);
528 assert(strcmp(colnames
[11], "map_type") == 0);
529 assert(strcmp(colnames
[12], "map_dn") == 0);
530 assert(strcmp(colnames
[13], "map_attr") == 0);
531 assert(strcmp(colnames
[14], "map_value") == 0);
532 assert(strcmp(colnames
[15], "map_windomain") == 0);
533 assert(strcmp(colnames
[16], "map_winname") == 0);
534 assert(strcmp(colnames
[17], "map_unixname") == 0);
535 assert(strcmp(colnames
[18], "map_is_nt4") == 0);
536 validated_column_names
= 1;
539 result
= (idmap_mappings_res
*)cb_data
->result
;
541 _VALIDATE_LIST_CB_DATA(19, &result
->mappings
.mappings_val
,
542 sizeof (idmap_mapping
));
544 result
->mappings
.mappings_len
++;
546 if ((str
= strdup(argv
[1])) == NULL
)
548 result
->mappings
.mappings_val
[cb_data
->next
].id1
.idmap_id_u
.sid
.prefix
=
550 result
->mappings
.mappings_val
[cb_data
->next
].id1
.idmap_id_u
.sid
.rid
=
551 strtoul(argv
[2], &end
, 10);
552 result
->mappings
.mappings_val
[cb_data
->next
].id1
.idtype
=
553 strtol(argv
[10], &end
, 10) ? IDMAP_USID
: IDMAP_GSID
;
555 result
->mappings
.mappings_val
[cb_data
->next
].id2
.idmap_id_u
.uid
=
556 strtoul(argv
[3], &end
, 10);
557 result
->mappings
.mappings_val
[cb_data
->next
].id2
.idtype
=
558 strtol(argv
[9], &end
, 10) ? IDMAP_UID
: IDMAP_GID
;
560 w2u
= argv
[4] ? strtol(argv
[4], &end
, 10) : 0;
561 u2w
= argv
[5] ? strtol(argv
[5], &end
, 10) : 0;
563 if (w2u
> 0 && u2w
== 0)
564 result
->mappings
.mappings_val
[cb_data
->next
].direction
=
566 else if (w2u
== 0 && u2w
> 0)
567 result
->mappings
.mappings_val
[cb_data
->next
].direction
=
570 result
->mappings
.mappings_val
[cb_data
->next
].direction
=
573 STRDUP_OR_FAIL(result
->mappings
.mappings_val
[cb_data
->next
].id1domain
,
576 STRDUP_OR_FAIL(result
->mappings
.mappings_val
[cb_data
->next
].id1name
,
579 STRDUP_OR_FAIL(result
->mappings
.mappings_val
[cb_data
->next
].id2name
,
582 if (cb_data
->flag
& IDMAP_REQ_FLG_MAPPING_INFO
) {
583 how
= &result
->mappings
.mappings_val
[cb_data
->next
].info
.how
;
584 how
->map_type
= strtoul(argv
[11], &end
, 10);
585 switch (how
->map_type
) {
586 case IDMAP_MAP_TYPE_DS_AD
:
587 how
->idmap_how_u
.ad
.dn
=
589 how
->idmap_how_u
.ad
.attr
=
591 how
->idmap_how_u
.ad
.value
=
595 case IDMAP_MAP_TYPE_DS_NLDAP
:
596 how
->idmap_how_u
.nldap
.dn
=
598 how
->idmap_how_u
.nldap
.attr
=
600 how
->idmap_how_u
.nldap
.value
=
604 case IDMAP_MAP_TYPE_RULE_BASED
:
605 how
->idmap_how_u
.rule
.windomain
=
607 how
->idmap_how_u
.rule
.winname
=
609 how
->idmap_how_u
.rule
.unixname
=
611 how
->idmap_how_u
.rule
.is_nt4
=
612 strtoul(argv
[18], &end
, 10);
613 how
->idmap_how_u
.rule
.is_user
=
614 strtol(argv
[9], &end
, 10);
615 how
->idmap_how_u
.rule
.is_wuser
=
616 strtol(argv
[10], &end
, 10);
619 case IDMAP_MAP_TYPE_EPHEMERAL
:
622 case IDMAP_MAP_TYPE_LOCAL_SID
:
625 case IDMAP_MAP_TYPE_IDMU
:
626 how
->idmap_how_u
.idmu
.dn
=
628 how
->idmap_how_u
.idmu
.attr
=
630 how
->idmap_how_u
.idmu
.value
=
635 /* Unknown mapping type */
641 result
->lastrowid
= strtoll(argv
[0], &end
, 10);
643 result
->retcode
= IDMAP_SUCCESS
;
650 idmap_list_mappings_1_svc(int64_t lastrowid
, uint64_t limit
, int32_t flag
,
651 idmap_mappings_res
*result
, struct svc_req
*rqstp
)
653 sqlite
*cache
= NULL
;
654 char lbuf
[30], rbuf
[30];
656 idmap_retcode retcode
;
660 (void) memset(result
, 0, sizeof (*result
));
664 if ((curtime
= time(NULL
)) == (time_t)-1) {
665 idmapdlog(LOG_ERR
, "Failed to get current time (%s)",
667 retcode
= IDMAP_ERR_INTERNAL
;
672 maxlimit
= _idmapdstate
.cfg
->pgcfg
.list_size_limit
;
675 /* Get cache handle */
676 result
->retcode
= get_cache_handle(&cache
);
677 if (result
->retcode
!= IDMAP_SUCCESS
)
680 result
->retcode
= IDMAP_ERR_INTERNAL
;
682 /* Create LIMIT expression. */
683 if (limit
== 0 || (maxlimit
> 0 && maxlimit
< limit
))
686 (void) snprintf(lbuf
, sizeof (lbuf
),
687 "LIMIT %" PRIu64
, limit
+ 1ULL);
691 (void) snprintf(rbuf
, sizeof (rbuf
), "rowid > %" PRIu64
, lastrowid
);
694 * Combine all the above into a giant SELECT statement that
695 * will return the requested mappings
698 sql
= sqlite_mprintf("SELECT rowid, sidprefix, rid, pid, w2u, "
699 "u2w, windomain, canon_winname, unixname, is_user, is_wuser, "
700 "map_type, map_dn, map_attr, map_value, map_windomain, "
701 "map_winname, map_unixname, map_is_nt4 "
702 "FROM idmap_cache WHERE %s AND "
703 "(pid >= 2147483648 OR (expiration = 0 OR "
704 "expiration ISNULL OR expiration > %d)) "
706 rbuf
, curtime
, lbuf
);
708 result
->retcode
= IDMAP_ERR_MEMORY
;
709 idmapdlog(LOG_ERR
, "Out of memory");
713 /* Execute the SQL statement and update the return buffer */
714 PROCESS_LIST_SVC_SQL(retcode
, cache
, IDMAP_CACHENAME
, sql
, limit
,
715 flag
, list_mappings_cb
, result
, result
->mappings
.mappings_len
);
720 if (IDMAP_ERROR(result
->retcode
))
721 xdr_free(xdr_idmap_mappings_res
, (caddr_t
)result
);
722 result
->retcode
= idmap_stat4prot(result
->retcode
);
730 list_namerules_cb(void *parg
, int argc
, char **argv
, char **colnames
)
732 list_cb_data_t
*cb_data
;
733 idmap_namerules_res
*result
;
734 idmap_retcode retcode
;
735 int w2u_order
, u2w_order
;
737 static int validated_column_names
= 0;
739 if (!validated_column_names
) {
740 assert(strcmp(colnames
[0], "rowid") == 0);
741 assert(strcmp(colnames
[1], "is_user") == 0);
742 assert(strcmp(colnames
[2], "is_wuser") == 0);
743 assert(strcmp(colnames
[3], "windomain") == 0);
744 assert(strcmp(colnames
[4], "winname_display") == 0);
745 assert(strcmp(colnames
[5], "is_nt4") == 0);
746 assert(strcmp(colnames
[6], "unixname") == 0);
747 assert(strcmp(colnames
[7], "w2u_order") == 0);
748 assert(strcmp(colnames
[8], "u2w_order") == 0);
749 validated_column_names
= 1;
752 cb_data
= (list_cb_data_t
*)parg
;
753 result
= (idmap_namerules_res
*)cb_data
->result
;
755 _VALIDATE_LIST_CB_DATA(9, &result
->rules
.rules_val
,
756 sizeof (idmap_namerule
));
758 result
->rules
.rules_len
++;
760 result
->rules
.rules_val
[cb_data
->next
].is_user
=
761 strtol(argv
[1], &end
, 10);
763 result
->rules
.rules_val
[cb_data
->next
].is_wuser
=
764 strtol(argv
[2], &end
, 10);
766 STRDUP_OR_FAIL(result
->rules
.rules_val
[cb_data
->next
].windomain
,
769 STRDUP_OR_FAIL(result
->rules
.rules_val
[cb_data
->next
].winname
,
772 result
->rules
.rules_val
[cb_data
->next
].is_nt4
=
773 strtol(argv
[5], &end
, 10);
775 STRDUP_OR_FAIL(result
->rules
.rules_val
[cb_data
->next
].unixname
,
778 w2u_order
= argv
[7] ? strtol(argv
[7], &end
, 10) : 0;
779 u2w_order
= argv
[8] ? strtol(argv
[8], &end
, 10) : 0;
781 if (w2u_order
> 0 && u2w_order
== 0)
782 result
->rules
.rules_val
[cb_data
->next
].direction
=
784 else if (w2u_order
== 0 && u2w_order
> 0)
785 result
->rules
.rules_val
[cb_data
->next
].direction
=
788 result
->rules
.rules_val
[cb_data
->next
].direction
=
791 result
->lastrowid
= strtoll(argv
[0], &end
, 10);
793 result
->retcode
= IDMAP_SUCCESS
;
800 idmap_list_namerules_1_svc(idmap_namerule rule
, uint64_t lastrowid
,
801 uint64_t limit
, idmap_namerules_res
*result
, struct svc_req
*rqstp
)
805 char lbuf
[30], rbuf
[30];
809 idmap_retcode retcode
;
811 (void) memset(result
, 0, sizeof (*result
));
813 result
->retcode
= validate_rule(&rule
);
814 if (result
->retcode
!= IDMAP_SUCCESS
)
818 maxlimit
= _idmapdstate
.cfg
->pgcfg
.list_size_limit
;
822 result
->retcode
= get_db_handle(&db
);
823 if (result
->retcode
!= IDMAP_SUCCESS
)
826 result
->retcode
= gen_sql_expr_from_rule(&rule
, &expr
);
827 if (result
->retcode
!= IDMAP_SUCCESS
)
830 /* Create LIMIT expression. */
831 if (limit
== 0 || (maxlimit
> 0 && maxlimit
< limit
))
834 (void) snprintf(lbuf
, sizeof (lbuf
),
835 "LIMIT %" PRIu64
, limit
+ 1ULL);
839 (void) snprintf(rbuf
, sizeof (rbuf
), "rowid > %" PRIu64
, lastrowid
);
842 * Combine all the above into a giant SELECT statement that
843 * will return the requested rules
845 sql
= sqlite_mprintf("SELECT rowid, is_user, is_wuser, windomain, "
846 "winname_display, is_nt4, unixname, w2u_order, u2w_order "
847 "FROM namerules WHERE "
852 result
->retcode
= IDMAP_ERR_MEMORY
;
853 idmapdlog(LOG_ERR
, "Out of memory");
857 /* Execute the SQL statement and update the return buffer */
858 PROCESS_LIST_SVC_SQL(retcode
, db
, IDMAP_DBNAME
, sql
, limit
,
859 0, list_namerules_cb
, result
, result
->rules
.rules_len
);
863 sqlite_freemem(expr
);
866 if (IDMAP_ERROR(result
->retcode
))
867 xdr_free(xdr_idmap_namerules_res
, (caddr_t
)result
);
868 result
->retcode
= idmap_stat4prot(result
->retcode
);
872 #define IDMAP_RULES_AUTH "solaris.admin.idmap.rules"
874 verify_rules_auth(struct svc_req
*rqstp
)
880 struct passwd
*result
;
882 if (svc_getcallerucred(rqstp
->rq_xprt
, &uc
) != 0) {
883 idmapdlog(LOG_ERR
, "svc_getcallerucred failed during "
884 "authorization (%s)", strerror(errno
));
888 uid
= ucred_geteuid(uc
);
889 if (uid
== (uid_t
)-1) {
890 idmapdlog(LOG_ERR
, "ucred_geteuid failed during "
891 "authorization (%s)", strerror(errno
));
896 getpwuid_r(uid
, &pwd
, buf
, sizeof (buf
), &result
);
898 idmapdlog(LOG_ERR
, "getpwuid_r(%u) failed during "
899 "authorization (%s)", uid
, strerror(errno
));
904 if (chkauthattr(IDMAP_RULES_AUTH
, pwd
.pw_name
) != 1) {
905 idmapdlog(LOG_INFO
, "%s is not authorized (%s)",
906 pwd
.pw_name
, IDMAP_RULES_AUTH
);
916 * Meaning of the return values is the following: For retcode ==
917 * IDMAP_SUCCESS, everything went OK and error_index is
918 * undefined. Otherwise, error_index >=0 shows the failed batch
919 * element. errro_index == -1 indicates failure at the beginning,
920 * error_index == -2 at the end.
925 idmap_update_1_svc(idmap_update_batch batch
, idmap_update_res
*res
,
926 struct svc_req
*rqstp
)
933 res
->error_index
= -1;
934 (void) memset(&res
->error_rule
, 0, sizeof (res
->error_rule
));
935 (void) memset(&res
->conflict_rule
, 0, sizeof (res
->conflict_rule
));
937 if (verify_rules_auth(rqstp
) < 0) {
938 res
->retcode
= IDMAP_ERR_PERMISSION_DENIED
;
942 if (batch
.idmap_update_batch_len
== 0 ||
943 batch
.idmap_update_batch_val
== NULL
) {
944 res
->retcode
= IDMAP_SUCCESS
;
948 res
->retcode
= validate_rules(&batch
);
949 if (res
->retcode
!= IDMAP_SUCCESS
)
953 res
->retcode
= get_db_handle(&db
);
954 if (res
->retcode
!= IDMAP_SUCCESS
)
957 res
->retcode
= sql_exec_no_cb(db
, IDMAP_DBNAME
, "BEGIN TRANSACTION;");
958 if (res
->retcode
!= IDMAP_SUCCESS
)
962 for (i
= 0; i
< batch
.idmap_update_batch_len
; i
++) {
963 up
= &batch
.idmap_update_batch_val
[i
];
966 res
->retcode
= IDMAP_SUCCESS
;
968 case OP_ADD_NAMERULE
:
969 res
->retcode
= add_namerule(db
,
970 &up
->idmap_update_op_u
.rule
);
973 res
->retcode
= rm_namerule(db
,
974 &up
->idmap_update_op_u
.rule
);
976 case OP_FLUSH_NAMERULES
:
977 res
->retcode
= flush_namerules(db
);
980 res
->retcode
= IDMAP_ERR_NOTSUPPORTED
;
984 if (res
->retcode
!= IDMAP_SUCCESS
) {
985 res
->error_index
= i
;
986 if (up
->opnum
== OP_ADD_NAMERULE
||
987 up
->opnum
== OP_RM_NAMERULE
) {
989 idmap_namerule_cpy(&res
->error_rule
,
990 &up
->idmap_update_op_u
.rule
);
991 if (r2
!= IDMAP_SUCCESS
)
1000 if (res
->retcode
== IDMAP_SUCCESS
) {
1002 sql_exec_no_cb(db
, IDMAP_DBNAME
,
1003 "COMMIT TRANSACTION;");
1004 if (res
->retcode
== IDMAP_SUCCESS
) {
1006 * We've updated the rules. Expire the cache
1007 * so that existing mappings will be
1011 idmap_cache_flush(IDMAP_FLUSH_EXPIRE
);
1013 res
->error_index
= -2;
1017 (void) sql_exec_no_cb(db
, IDMAP_DBNAME
,
1018 "ROLLBACK TRANSACTION;");
1021 res
->retcode
= idmap_stat4prot(res
->retcode
);
1028 copy_string(char **to
, char *from
)
1030 if (EMPTY_STRING(from
)) {
1035 idmapdlog(LOG_ERR
, "Out of memory");
1036 return (IDMAP_ERR_MEMORY
);
1039 return (IDMAP_SUCCESS
);
1044 copy_id(idmap_id
*to
, idmap_id
*from
)
1046 (void) memset(to
, 0, sizeof (*to
));
1048 to
->idtype
= from
->idtype
;
1049 if (IS_ID_SID(*from
)) {
1050 idmap_retcode retcode
;
1052 to
->idmap_id_u
.sid
.rid
= from
->idmap_id_u
.sid
.rid
;
1053 retcode
= copy_string(&to
->idmap_id_u
.sid
.prefix
,
1054 from
->idmap_id_u
.sid
.prefix
);
1058 to
->idmap_id_u
.uid
= from
->idmap_id_u
.uid
;
1059 return (IDMAP_SUCCESS
);
1065 copy_mapping(idmap_mapping
*mapping
, idmap_mapping
*request
)
1067 idmap_retcode retcode
;
1069 (void) memset(mapping
, 0, sizeof (*mapping
));
1071 mapping
->flag
= request
->flag
;
1072 mapping
->direction
= _IDMAP_F_DONE
;
1074 retcode
= copy_id(&mapping
->id1
, &request
->id1
);
1075 if (retcode
!= IDMAP_SUCCESS
)
1078 retcode
= copy_string(&mapping
->id1domain
, request
->id1domain
);
1079 if (retcode
!= IDMAP_SUCCESS
)
1082 retcode
= copy_string(&mapping
->id1name
, request
->id1name
);
1083 if (retcode
!= IDMAP_SUCCESS
)
1086 retcode
= copy_id(&mapping
->id2
, &request
->id2
);
1087 if (retcode
!= IDMAP_SUCCESS
)
1090 retcode
= copy_string(&mapping
->id2domain
, request
->id2domain
);
1091 if (retcode
!= IDMAP_SUCCESS
)
1093 retcode
= copy_string(&mapping
->id2name
, request
->id2name
);
1094 if (retcode
!= IDMAP_SUCCESS
)
1097 return (IDMAP_SUCCESS
);
1100 if (IS_ID_SID(mapping
->id1
))
1101 free(mapping
->id1
.idmap_id_u
.sid
.prefix
);
1102 free(mapping
->id1domain
);
1103 free(mapping
->id1name
);
1104 if (IS_ID_SID(mapping
->id2
))
1105 free(mapping
->id2
.idmap_id_u
.sid
.prefix
);
1106 free(mapping
->id2domain
);
1107 free(mapping
->id2name
);
1109 (void) memset(mapping
, 0, sizeof (*mapping
));
1116 idmap_get_mapped_id_by_name_1_svc(idmap_mapping request
,
1117 idmap_mappings_res
*result
, struct svc_req
*rqstp
)
1119 idmap_mapping_batch batch_request
;
1120 idmap_ids_res batch_result
;
1123 /* Clear out things we might want to xdr_free on error */
1124 (void) memset(&batch_result
, 0, sizeof (batch_result
));
1125 (void) memset(result
, 0, sizeof (*result
));
1127 result
->retcode
= validate_mapped_id_by_name_req(&request
);
1128 if (result
->retcode
!= IDMAP_SUCCESS
)
1132 * Copy the request. We need to modify it, and
1133 * what we have is a shallow copy. Freeing pointers from
1134 * our copy will lead to problems, since the RPC framework
1135 * has its own copy of those pointers. Besides, we need
1138 map
= calloc(1, sizeof (idmap_mapping
));
1140 idmapdlog(LOG_ERR
, "Out of memory");
1141 result
->retcode
= IDMAP_ERR_MEMORY
;
1146 * Set up to return the filled-in mapping structure.
1147 * Note that we xdr_free result on error, and that'll take
1148 * care of freeing the mapping structure.
1150 result
->mappings
.mappings_val
= map
;
1151 result
->mappings
.mappings_len
= 1;
1153 result
->retcode
= copy_mapping(map
, &request
);
1154 if (result
->retcode
!= IDMAP_SUCCESS
)
1157 /* Set up for the request to the batch API */
1158 batch_request
.idmap_mapping_batch_val
= map
;
1159 batch_request
.idmap_mapping_batch_len
= 1;
1161 /* Do the real work. */
1162 (void) idmap_get_mapped_ids_1_svc(batch_request
,
1163 &batch_result
, rqstp
);
1165 /* Copy what we need out of the batch response */
1167 if (batch_result
.retcode
!= IDMAP_SUCCESS
) {
1168 result
->retcode
= batch_result
.retcode
;
1172 result
->retcode
= copy_id(&map
->id2
, &batch_result
.ids
.ids_val
[0].id
);
1173 if (result
->retcode
!= IDMAP_SUCCESS
)
1176 map
->direction
= batch_result
.ids
.ids_val
[0].direction
;
1178 result
->retcode
= batch_result
.ids
.ids_val
[0].retcode
;
1180 idmap_info_mov(&map
->info
, &batch_result
.ids
.ids_val
[0].info
);
1183 if (IDMAP_FATAL_ERROR(result
->retcode
)) {
1184 xdr_free(xdr_idmap_mappings_res
, (caddr_t
)result
);
1185 result
->mappings
.mappings_len
= 0;
1186 result
->mappings
.mappings_val
= NULL
;
1188 result
->retcode
= idmap_stat4prot(result
->retcode
);
1190 xdr_free(xdr_idmap_ids_res
, (char *)&batch_result
);
1197 idmap_get_prop_1_svc(idmap_prop_type request
,
1198 idmap_prop_res
*result
, struct svc_req
*rqstp
)
1200 idmap_pg_config_t
*pgcfg
;
1203 (void) memset(result
, 0, sizeof (*result
));
1204 result
->retcode
= IDMAP_SUCCESS
;
1205 result
->value
.prop
= request
;
1209 /* Just shortcuts: */
1210 pgcfg
= &_idmapdstate
.cfg
->pgcfg
;
1214 case PROP_LIST_SIZE_LIMIT
:
1215 result
->value
.idmap_prop_val_u
.intval
= pgcfg
->list_size_limit
;
1216 result
->auto_discovered
= FALSE
;
1218 case PROP_DEFAULT_DOMAIN
:
1219 result
->auto_discovered
= FALSE
;
1220 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1221 pgcfg
->default_domain
);
1223 case PROP_DOMAIN_NAME
:
1224 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1225 pgcfg
->domain_name
);
1226 result
->auto_discovered
=
1227 pgcfg
->domain_name_auto_disc
;
1229 case PROP_MACHINE_SID
:
1230 result
->auto_discovered
= FALSE
;
1231 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1232 pgcfg
->machine_sid
);
1234 case PROP_DOMAIN_CONTROLLER
:
1235 if (pgcfg
->domain_controller
!= NULL
) {
1236 (void) memcpy(&result
->value
.idmap_prop_val_u
.dsval
,
1237 pgcfg
->domain_controller
,
1238 sizeof (idmap_ad_disc_ds_t
));
1240 result
->auto_discovered
= pgcfg
->domain_controller_auto_disc
;
1242 case PROP_FOREST_NAME
:
1243 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1244 pgcfg
->forest_name
);
1245 result
->auto_discovered
= pgcfg
->forest_name_auto_disc
;
1247 case PROP_SITE_NAME
:
1248 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1250 result
->auto_discovered
= pgcfg
->site_name_auto_disc
;
1252 case PROP_GLOBAL_CATALOG
:
1253 if (pgcfg
->global_catalog
!= NULL
) {
1254 (void) memcpy(&result
->value
.idmap_prop_val_u
.dsval
,
1255 pgcfg
->global_catalog
, sizeof (idmap_ad_disc_ds_t
));
1257 result
->auto_discovered
= pgcfg
->global_catalog_auto_disc
;
1259 case PROP_AD_UNIXUSER_ATTR
:
1260 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1261 pgcfg
->ad_unixuser_attr
);
1262 result
->auto_discovered
= FALSE
;
1264 case PROP_AD_UNIXGROUP_ATTR
:
1265 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1266 pgcfg
->ad_unixgroup_attr
);
1267 result
->auto_discovered
= FALSE
;
1269 case PROP_NLDAP_WINNAME_ATTR
:
1270 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1271 pgcfg
->nldap_winname_attr
);
1272 result
->auto_discovered
= FALSE
;
1274 case PROP_DIRECTORY_BASED_MAPPING
:
1275 STRDUP_CHECK(result
->value
.idmap_prop_val_u
.utf8val
,
1276 enum_lookup(pgcfg
->directory_based_mapping
,
1277 directory_mapping_map
));
1278 result
->auto_discovered
= FALSE
;
1281 result
->retcode
= IDMAP_ERR_PROP_UNKNOWN
;
1287 if (IDMAP_FATAL_ERROR(result
->retcode
)) {
1288 xdr_free(xdr_idmap_prop_res
, (caddr_t
)result
);
1289 result
->value
.prop
= PROP_UNKNOWN
;
1291 result
->retcode
= idmap_stat4prot(result
->retcode
);
1298 idmap_retcode
*result
,
1299 struct svc_req
*rqstp
)
1301 NOTE(ARGUNUSED(rqstp
))
1302 if (verify_rules_auth(rqstp
) < 0) {
1303 *result
= IDMAP_ERR_PERMISSION_DENIED
;
1307 *result
= idmap_cache_flush(op
);
1314 idmap_prog_1_freeresult(SVCXPRT
*transp
, xdrproc_t xdr_result
,
1317 xdr_free(xdr_result
, result
);
1322 * This function is called by rpc_svc.c when it encounters an error.
1326 idmap_rpc_msgout(const char *fmt
, ...)
1332 (void) vsnprintf(buf
, sizeof (buf
), fmt
, va
);
1335 idmapdlog(LOG_ERR
, "idmap RPC: %s", buf
);