/*
* 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
* 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 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Database related utility routines
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <pthread.h>
#include <assert.h>
#include <sys/u8_textprep.h>
#include <alloca.h>
#include <libuutil.h>
#include <note.h>
#include "idmapd.h"
#include "adutils.h"
#include "string.h"
#include "idmap_priv.h"
#include "schema.h"
#include "nldaputils.h"
#include "idmap_lsa.h"
sqlite_vm **, int *, int, const char ***);
const char *, char **, char **, idmap_rid_t *, idmap_id_type *);
typedef enum init_db_option {
FAIL_IF_CORRUPT = 0,
/*
* Thread specific data to hold the database handles so that the
* databases are not opened and closed for every request. It also
* contains the sqlite busy handler structure.
*/
struct idmap_busy {
const char *name;
const int *delays;
int delay_size;
int total;
int sec;
};
typedef struct idmap_tsd {
} idmap_tsd_t;
/*
* Flags to indicate how local the directory we're consulting is.
* If neither is set, it means the directory belongs to a remote forest.
*/
static const int cache_delay_table[] =
{ 1, 2, 5, 10, 15, 20, 25, 30, 35, 40,
50, 50, 60, 70, 80, 90, 100};
static const int db_delay_table[] =
{ 5, 10, 15, 20, 30, 40, 55, 70, 100};
void
{
if (tsd) {
}
}
void
idmap_init_tsd_key(void)
{
int rc;
}
idmap_get_tsd(void)
{
/* No thread specific data so create it */
/* Initialize thread specific data */
/* save the trhread specific data */
/* Can't store key */
}
} else {
}
}
return (tsd);
}
/*
* A simple wrapper around u8_textprep_str() that returns the Unicode
* lower-case version of some string. The result must be freed.
*/
char *
tolower_u8(const char *s)
{
char *outs;
/*
* u8_textprep_str() does not allocate memory. The input and
* output buffers may differ in size (though that would be more
* likely when normalization is done). We have to loop over it...
*
* To improve the chances that we can avoid looping we add 10
* bytes of output buffer room the first go around.
*/
return (NULL);
return (NULL);
s += (inlen - inbytesleft);
/* adjust outbytesleft and outlen */
outlen += inbytesleft;
}
if (rc < 0) {
return (NULL);
}
return (res);
}
const char *while_doing);
/*
* Initialize 'dbname' using 'sql'
*/
static
int
const char *detect_version_sql, char * const *sql,
{
*created = 0;
*upgraded = 0;
if (opt == REMOVE_IF_CORRUPT)
tries = 3;
if (tries == 0) {
return (-1);
}
if (tries-- == 1)
/* Last try, log errors */
if (opt == REMOVE_IF_CORRUPT)
goto rinse_repeat;
}
/* Detect current version of schema in the db, if any */
curr_version = 0;
if (detect_version_sql != NULL) {
int nrow;
#ifdef IDMAPD_DEBUG
#endif /* IDMAPD_DEBUG */
"Error detecting schema version of db %s (%s)",
return (-1);
}
if (nrow != 1) {
"Error detecting schema version of db %s", dbname);
return (-1);
}
}
if (curr_version < 0) {
if (opt == REMOVE_IF_CORRUPT)
goto rinse_repeat;
}
if (curr_version == version)
goto done;
/* Install or upgrade schema */
#ifdef IDMAPD_DEBUG
sql[curr_version]);
#endif /* IDMAPD_DEBUG */
if (rc != 0) {
(curr_version == 0) ? "installing schema" :
"upgrading schema");
if (opt == REMOVE_IF_CORRUPT)
goto rinse_repeat;
}
*upgraded = (curr_version > 0);
*created = (curr_version == 0);
done:
(void) sqlite_close(db);
return (0);
}
/*
* This is the SQLite database busy handler that retries the SQL
* operation until it is successful.
*/
int
/* LINTED E_FUNC_ARG_UNUSED */
{
int delay;
if (count == 1) {
}
"Thread %d waited %d sec for the %s database",
}
} else {
}
return (1);
}
/*
* Get the database handle
*/
{
char *errmsg;
/*
* Retrieve the db handle from thread-specific storage
* If none exists, open and store in thread-specific storage.
*/
"Error getting thread specific data for %s", IDMAP_DBNAME);
return (IDMAP_ERR_MEMORY);
}
return (IDMAP_ERR_DB);
}
sizeof (int);
}
return (IDMAP_SUCCESS);
}
/*
* Get the cache handle
*/
{
char *errmsg;
/*
* Retrieve the db handle from thread-specific storage
* If none exists, open and store in thread-specific storage.
*/
return (IDMAP_ERR_MEMORY);
}
return (IDMAP_ERR_DB);
}
sizeof (int);
&tsd->cache_busy);
}
return (IDMAP_SUCCESS);
}
/*
* Initialize cache and db
*/
int
init_dbs()
{
/* name-based mappings; probably OK to blow away in a pinch(?) */
sql[0] = DB_INSTALL_SQL;
return (-1);
sql[0] = CACHE_INSTALL_SQL;
return (-1);
return (0);
}
/*
* Finalize databases
*/
void
fini_dbs()
{
}
/*
* This table is a listing of status codes that will be returned to the
* client when a SQL command fails with the corresponding error message.
*/
"columns unixname, is_user, u2w_order are not unique"},
"columns winname, windomain, is_user, is_wuser, w2u_order are not"
" unique"},
{IDMAP_ERR_W2U_NAMERULE_CONFLICT, "Conflicting w2u namerules"},
{-1, NULL}
};
/*
* idmapd's version of string2stat to map SQLite messages to
* status codes
*/
{
int i;
for (i = 0; sqlmsgtable[i].msg; i++) {
return (sqlmsgtable[i].retcode);
}
return (IDMAP_ERR_OTHER);
}
/*
* Executes some SQL in a transaction.
*
* Returns 0 on success, -1 if it failed but the rollback succeeded, -2
* if the rollback failed.
*/
static
int
const char *while_doing)
{
int rc;
return (-1);
}
goto rollback;
}
return (0);
}
return (-2);
}
return (-1);
}
/*
* Execute the given SQL statment without using any callbacks
*/
{
int r;
if (r != SQLITE_OK) {
return (retcode);
}
return (IDMAP_SUCCESS);
}
/*
* Generate expression that can be used in WHERE statements.
* Examples:
* <prefix> <col> <op> <value> <suffix>
* "" "unixuser" "=" "foo" "AND"
*/
{
char *dir;
char *lower_winname;
return (IDMAP_ERR_ARG);
if (s_windomain == NULL) {
goto out;
}
}
"AND winname = %Q AND is_wuser = %d ",
goto out;
}
}
"AND unixname = %Q AND is_user = %d ",
if (s_unixname == NULL) {
goto out;
}
}
case IDMAP_DIRECTION_BI:
dir = "AND w2u_order > 0 AND u2w_order > 0";
break;
case IDMAP_DIRECTION_W2U:
dir = "AND w2u_order > 0"
" AND (u2w_order = 0 OR u2w_order ISNULL)";
break;
case IDMAP_DIRECTION_U2W:
dir = "AND u2w_order > 0"
" AND (w2u_order = 0 OR w2u_order ISNULL)";
break;
default:
dir = "";
break;
}
dir);
goto out;
}
out:
if (s_windomain != NULL)
if (s_unixname != NULL)
return (retcode);
}
/*
* Generate and execute SQL statement for LIST RPC calls
*/
{
int r;
switch (r) {
case SQLITE_OK:
break;
default:
break;
}
return (retcode);
}
/*
* This routine is called by callbacks that process the results of
* LIST RPC calls to validate data and to allocate memory for
* the result array.
*/
{
void *tmplist;
return (IDMAP_NEXT);
return (IDMAP_ERR_INTERNAL);
}
/* alloc in bulk to reduce number of reallocs */
return (IDMAP_ERR_MEMORY);
}
}
return (IDMAP_SUCCESS);
}
static
{
*w2u_order = 0;
*u2w_order = 0;
/*
* Windows to UNIX lookup order:
* 1. winname@domain (or winname) to ""
* 2. winname@domain (or winname) to unixname
* 3. winname@* to ""
* 4. winname@* to unixname
* 5. *@domain (or *) to *
* 6. *@domain (or *) to ""
* 7. *@domain (or *) to unixname
* 8. *@* to *
* 9. *@* to ""
* 10. *@* to unixname
*
* winname is a special case of winname@domain when domain is the
* default domain. Similarly * is a special case of *@domain when
* domain is the default domain.
*
* Note that "" has priority over specific names because "" inhibits
* mappings and traditionally deny rules always had higher priority.
*/
if (direction != IDMAP_DIRECTION_U2W) {
/* bi-directional or from windows to unix */
return (IDMAP_ERR_W2U_NAMERULE);
return (IDMAP_ERR_W2U_NAMERULE);
else if (EMPTY_NAME(winname))
return (IDMAP_ERR_W2U_NAMERULE);
if (*unixname == '*')
*w2u_order = 8;
else if (EMPTY_NAME(unixname))
*w2u_order = 9;
else /* unixname == name */
*w2u_order = 10;
} else if (*winname == '*') {
if (*unixname == '*')
*w2u_order = 5;
else if (EMPTY_NAME(unixname))
*w2u_order = 6;
else /* name */
*w2u_order = 7;
/* winname == name */
if (*unixname == '*')
return (IDMAP_ERR_W2U_NAMERULE);
else if (EMPTY_NAME(unixname))
*w2u_order = 3;
else /* name */
*w2u_order = 4;
} else {
/* winname == name && windomain == null or name */
if (*unixname == '*')
return (IDMAP_ERR_W2U_NAMERULE);
else if (EMPTY_NAME(unixname))
*w2u_order = 1;
else /* name */
*w2u_order = 2;
}
}
/*
* 1. unixname to "", non-diagonal
* 2. unixname to winname@domain (or winname), non-diagonal
* 3. unixname to "", diagonal
* 4. unixname to winname@domain (or winname), diagonal
* 5. * to *@domain (or *), non-diagonal
* 5. * to *@domain (or *), diagonal
* 7. * to ""
* 8. * to winname@domain (or winname)
* 9. * to "", non-diagonal
* 10. * to winname@domain (or winname), diagonal
*/
if (direction != IDMAP_DIRECTION_W2U) {
/* bi-directional or from unix to windows */
return (IDMAP_ERR_U2W_NAMERULE);
return (IDMAP_ERR_U2W_NAMERULE);
return (IDMAP_ERR_U2W_NAMERULE);
else if (*unixname == '*') {
if (*winname == '*')
else if (EMPTY_NAME(winname))
else
} else {
if (*winname == '*')
return (IDMAP_ERR_U2W_NAMERULE);
else if (EMPTY_NAME(winname))
else
}
}
return (IDMAP_SUCCESS);
}
/*
* Generate and execute SQL statement to add name-based mapping rule
*/
{
char *name;
if (retcode != IDMAP_SUCCESS)
goto out;
if (w2u_order)
if (u2w_order)
/*
* For the triggers on namerules table to work correctly:
* 1) Use NULL instead of 0 for w2u_order and u2w_order
* 2) Use "" instead of NULL for "no domain"
*/
&canonname, &canondomain,
dom = canondomain;
} else if (EMPTY_STRING(dom)) {
else
dom = "";
}
"(is_user, is_wuser, windomain, winname_display, is_nt4, "
"unixname, w2u_order, u2w_order) "
"VALUES(%d, %d, %Q, %Q, %d, %Q, %q, %q);",
goto out;
}
if (retcode == IDMAP_ERR_OTHER)
out:
return (retcode);
}
/*
* Flush name-based mapping rules
*/
{
return (retcode);
}
/*
* Generate and execute SQL statement to remove a name-based mapping rule
*/
{
return (IDMAP_SUCCESS);
if (retcode != IDMAP_SUCCESS)
goto out;
goto out;
}
out:
return (retcode);
}
/*
* Compile the given SQL query and step just once.
*
* Input:
* db - db handle
* sql - SQL statement
*
* Output:
* vm - virtual SQL machine
* ncol - number of columns in the result
* values - column values
*
* Return values:
* IDMAP_SUCCESS
* IDMAP_ERR_NOTFOUND
* IDMAP_ERR_INTERNAL
*/
static
{
int r;
CHECK_NULL(errmsg));
return (IDMAP_ERR_INTERNAL);
}
if (r == SQLITE_ROW) {
return (IDMAP_ERR_INTERNAL);
}
/* Caller will call finalize after using the results */
return (IDMAP_SUCCESS);
} else if (r == SQLITE_DONE) {
return (IDMAP_ERR_NOTFOUND);
}
CHECK_NULL(errmsg));
return (IDMAP_ERR_INTERNAL);
}
/*
* Load config in the state.
*
* nm_siduid and nm_sidgid fields:
* state->nm_siduid represents mode used by sid2uid and uid2sid
* requests for directory-based name mappings. Similarly,
* state->nm_sidgid represents mode used by sid2gid and gid2sid
* requests.
*
* none -> directory_based_mapping != DIRECTORY_MAPPING_NAME
* AD-mode -> !nldap_winname_attr && ad_unixuser_attr
* nldap-mode -> nldap_winname_attr && !ad_unixuser_attr
* mixed-mode -> nldap_winname_attr && ad_unixuser_attr
*
* none -> directory_based_mapping != DIRECTORY_MAPPING_NAME
* AD-mode -> !nldap_winname_attr && ad_unixgroup_attr
* nldap-mode -> nldap_winname_attr && !ad_unixgroup_attr
* mixed-mode -> nldap_winname_attr && ad_unixgroup_attr
*/
{
state->eph_map_unres_sids = 0;
return (IDMAP_ERR_MEMORY);
}
} else {
return (IDMAP_SUCCESS);
}
return (IDMAP_SUCCESS);
}
} else {
? IDMAP_NM_AD : IDMAP_NM_NONE;
? IDMAP_NM_AD : IDMAP_NM_NONE;
}
return (IDMAP_ERR_MEMORY);
}
}
return (IDMAP_ERR_MEMORY);
}
}
return (IDMAP_ERR_MEMORY);
}
}
return (IDMAP_SUCCESS);
}
/*
* Set the rule with specified values.
* All the strings are copied.
*/
static void
{
/*
* Only update if they differ because we have to free
* and duplicate the strings
*/
}
}
}
}
}
}
}
/*
* Lookup well-known SIDs table either by winname or by SID.
*
* If the given winname or SID is a well-known SID then we set is_wksid
* variable and then proceed to see if the SID has a hard mapping to
* fixed ephemeral ids). The direction flag indicates whether we have
* a mapping; UNDEF indicates that we do not.
*
* If we find a mapping then we return success, except for the
* special case of IDMAP_SENTINEL_PID which indicates an inhibited mapping.
*
* If we find a matching entry, but no mapping, we supply SID, name, and type
* information and return "not found". Higher layers will probably
* do ephemeral mapping.
*
* If we do not find a match, we return "not found" and leave the question
* to higher layers.
*/
static
{
*is_wksid = 0;
} else {
}
return (IDMAP_ERR_NOTFOUND);
/* Found matching entry. */
/* Fill in name if it was not already there. */
return (IDMAP_ERR_MEMORY);
}
/* Fill in SID if it was not already there */
} else {
}
return (IDMAP_ERR_MEMORY);
}
/* Fill in the canonical domain if not already there */
const char *dom;
else
return (IDMAP_ERR_MEMORY);
}
*is_wksid = 1;
}
/*
* We don't have a mapping
* (But note that we may have supplied SID, name, or type
* information.)
*/
return (IDMAP_ERR_NOTFOUND);
}
/*
* We have an explicit mapping.
*/
/*
* ... which is that mapping is inhibited.
*/
return (IDMAP_ERR_NOMAPPING);
}
case IDMAP_UID:
break;
case IDMAP_GID:
break;
default:
/* IDMAP_POSIXID is eliminated above */
return (IDMAP_ERR_NOTSUPPORTED);
}
return (IDMAP_SUCCESS);
}
/*
* Look for an entry mapping a PID to a SID.
*
* Note that direction=UNDEF entries do not specify a mapping,
* and that IDMAP_SENTINEL_PID entries represent either an inhibited
* mapping or an ephemeral mapping. We don't handle either here;
* they are filtered out by find_wksid_by_pid.
*/
static
{
return (IDMAP_ERR_NOTFOUND);
}
} else {
}
return (IDMAP_ERR_MEMORY);
}
/* Fill in name if it was not already there. */
return (IDMAP_ERR_MEMORY);
}
/* Fill in the canonical domain if not already there */
const char *dom;
else
return (IDMAP_ERR_MEMORY);
}
return (IDMAP_SUCCESS);
}
/*
* Look up a name in the wksids list, matching name and, if supplied, domain,
* and extract data.
*
* Given:
* name Windows user name
* domain Windows domain name (or NULL)
*
* Return: Error code
*
* *canonname canonical name (if canonname non-NULL) [1]
* *canondomain canonical domain (if canondomain non-NULL) [1]
* *sidprefix SID prefix (if sidprefix non-NULL) [1]
* *rid RID (if rid non-NULL) [2]
* *type Type (if type non-NULL) [2]
*
* [1] malloc'ed, NULL on error
* [2] Undefined on error
*/
const char *name,
const char *domain,
char **canonname,
char **canondomain,
char **sidprefix,
{
if (canondomain != NULL)
*canondomain = NULL;
return (IDMAP_ERR_NOTFOUND);
} else {
}
goto nomem;
}
goto nomem;
}
if (canondomain != NULL) {
} else {
}
if (*canondomain == NULL)
goto nomem;
}
return (IDMAP_SUCCESS);
}
}
if (canondomain != NULL) {
free(*canondomain);
*canondomain = NULL;
}
return (IDMAP_ERR_MEMORY);
}
static
{
char *end;
const char **values;
/* Current time */
errno = 0;
goto out;
}
case IDMAP_UID:
is_user_string = "1";
break;
case IDMAP_GID:
is_user_string = "0";
break;
case IDMAP_POSIXID:
/* the non-diagonal mapping */
is_user_string = "is_wuser";
break;
default:
goto out;
}
/* SQL to lookup the cache */
"unixname, u2w, is_wuser, "
"map_type, map_dn, map_attr, map_value, "
"map_windomain, map_winname, map_unixname, map_is_nt4 "
"FROM idmap_cache WHERE is_user = %s AND "
"sidprefix = %Q AND rid = %u AND w2u = 1 AND "
"(pid >= 2147483648 OR "
"(expiration = 0 OR expiration ISNULL OR "
"expiration > %d));",
"unixname, u2w, is_wuser, "
"map_type, map_dn, map_attr, map_value, "
"map_windomain, map_winname, map_unixname, map_is_nt4 "
"FROM idmap_cache WHERE is_user = %s AND "
"winname = %Q AND windomain = %Q AND w2u = 1 AND "
"(pid >= 2147483648 OR "
"(expiration = 0 OR expiration ISNULL OR "
"expiration > %d));",
curtime);
} else {
goto out;
}
goto out;
}
14, &values);
if (retcode == IDMAP_ERR_NOTFOUND) {
goto out;
} else if (retcode == IDMAP_SUCCESS) {
/* sanity checks */
goto out;
}
if (is_user) {
} else {
}
/*
* We may have an expired ephemeral mapping. Consider
* the expired entry as valid if we are not going to
* perform name-based mapping. But do not renew the
* expiration.
* If we will be doing name-based mapping then store the
* ephemeral pid in the result so that we can use it
* if we end up doing dynamic mapping again.
*/
if (!DO_NOT_ALLOC_NEW_ID_MAPPING(req) &&
!AVOID_NAMESERVICE(req) &&
/* Store the ephemeral pid */
}
}
}
out:
if (retcode == IDMAP_SUCCESS) {
else
}
}
case IDMAP_MAP_TYPE_DS_AD:
break;
case IDMAP_MAP_TYPE_DS_NLDAP:
break;
break;
case IDMAP_MAP_TYPE_EPHEMERAL:
break;
case IDMAP_MAP_TYPE_LOCAL_SID:
break;
case IDMAP_MAP_TYPE_KNOWN_SID:
break;
case IDMAP_MAP_TYPE_IDMU:
break;
default:
/* Unknown mapping type */
}
}
}
return (retcode);
}
/*
* Previous versions used two enumerations for representing types.
* One of those has largely been eliminated, but was used in the
* name cache table and so during an upgrade might still be visible.
* In addition, the test suite prepopulates the cache with these values.
*
* This function translates those old values into the new values.
*
* This code deliberately does not use symbolic values for the legacy
* values. This is the *only* place where they should be used.
*/
static
{
switch (type) {
case -1004: /* _IDMAP_T_USER */
return (IDMAP_USID);
case -1005: /* _IDMAP_T_GROUP */
return (IDMAP_GSID);
default:
return (type);
}
}
static
{
char *end;
const char **values;
int ncol;
/* Get current time */
errno = 0;
goto out;
}
/* SQL to lookup the cache */
"FROM name_cache WHERE "
"sidprefix = %Q AND rid = %u AND "
"(expiration = 0 OR expiration ISNULL OR "
"expiration > %d);",
goto out;
}
if (retcode == IDMAP_SUCCESS) {
goto out;
}
}
goto out;
}
}
}
goto out;
}
}
}
out:
return (retcode);
}
/*
* Given SID, find winname using name_cache OR
* Given winname, find SID using name_cache.
* Used when mapping win to unix i.e. req->id1 is windows id and
* req->id2 is unix id
*/
static
{
/* Done if we've both sid and winname */
/* Don't bother TRACE()ing, too boring */
return (IDMAP_SUCCESS);
}
/* Lookup sid to winname */
} else {
/* Lookup winame to sid */
}
if (retcode != IDMAP_SUCCESS) {
if (retcode == IDMAP_ERR_NOTFOUND) {
} else {
}
return (retcode);
}
/*
* If we found canonical names or domain, use them instead of
* the existing values.
*/
}
}
}
return (retcode);
}
static int
int *num_processed)
{
int next_request;
char **unixname;
*num_processed = 0;
/*
* Since req->id2.idtype is unused, we will use it here
* to retrieve the value of sid_type. But it needs to be
* reset to IDMAP_NONE before we return to prevent xdr
* from mis-interpreting req->id2 when it tries to free
* the input argument. Other option is to allocate an
* array of integers and use it instead for the batched
* call. But why un-necessarily allocate memory. That may
* be an option if req->id2.idtype cannot be re-used in
* future.
*
* Similarly, we use req->id2.idmap_id_u.uid to return
* uidNumber or gidNumber supplied by IDMU, and reset it
* back to IDMAP_SENTINEL_PID when we're done. Note that
* the query always puts the result in req->id2.idmap_id_u.uid,
* not .gid.
*/
&qs);
if (retcode != IDMAP_SUCCESS) {
if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
goto retry;
goto out;
}
num_queued = 0;
restore_svc();
if (how_local & FOREST_IS_LOCAL) {
/*
* Directory based name mapping is only performed within the
* joined forest. We don't trust other "trusted"
* forests to provide DS-based name mapping information because
* AD's definition of "cross-forest trust" does not encompass
* this sort of behavior.
*/
}
for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
/* Skip if no AD lookup required */
continue;
/* Skip if we've already tried and gotten a "not found" */
continue;
/* Skip if we've already either succeeded or failed */
continue;
/* win2unix request: */
if (state->directory_based_mapping ==
}
/*
* Get how info for DS-based name
* mapping only if AD or MIXED
* mode is enabled.
*/
}
} else if (state->directory_based_mapping ==
(how_local & DOMAIN_IS_LOCAL)) {
/*
* Ensure that we only do IDMU processing
* when querying the domain we've joined.
*/
/*
* Get how info for IDMU based mapping.
*/
}
/* Lookup AD by SID */
pid,
if (retcode == IDMAP_SUCCESS)
num_queued++;
} else {
/* Lookup AD by winname */
pid,
if (retcode == IDMAP_SUCCESS)
num_queued++;
}
/* unix2win request: */
/* Already have SID and winname. done */
continue;
}
/*
* SID but no winname -- lookup AD by
* SID to get winname.
* how info is not needed here because
* we are not retrieving unixname from
* AD.
*/
if (retcode == IDMAP_SUCCESS)
num_queued++;
/*
* winname but no SID -- lookup AD by
* winname to get SID.
* how info is not needed here because
* we are not retrieving unixname from
* AD.
*/
NULL,
if (retcode == IDMAP_SUCCESS)
num_queued++;
} else if (state->directory_based_mapping ==
(how_local & DOMAIN_IS_LOCAL)) {
is_wuser = 1;
is_wuser = 0;
else
/* IDMU can't do diagonal mappings */
continue;
if (retcode == IDMAP_SUCCESS)
num_queued++;
/*
* No SID and no winname but we've unixname.
* Lookup AD by unixname to get SID.
*/
is_wuser = 1;
is_wuser = 0;
else
if (retcode == IDMAP_SUCCESS)
num_queued++;
}
}
if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
} else if (retcode != IDMAP_SUCCESS) {
break;
}
} /* End of for loop */
if (retcode == IDMAP_SUCCESS) {
/* add keeps track if we added an entry to the batch */
if (num_queued > 0)
else
} else {
num_queued = 0;
next_request = i + 1;
}
if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
goto retry;
else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
if (retcode != IDMAP_SUCCESS) {
/* Mark any unproccessed requests for an other AD */
i++) {
}
}
if (retcode != IDMAP_SUCCESS)
out:
/*
* This loop does the following:
* 1. Reset _IDMAP_F_LOOKUP_AD flag from the request.
* 2. Reset req->id2.idtype to IDMAP_NONE
* 3. If batch_start or batch_add failed then set the status
* of each request marked for AD lookup to that error.
* 4. Evaluate the type of the AD object (i.e. user or group)
* and update the idtype in request.
*/
for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
/*
* If it didn't need AD lookup, ignore it.
*/
continue;
/*
* If we deferred it this time, reset for the next
* AD server.
*/
continue;
}
/* Count number processed */
(*num_processed)++;
/* Reset AD lookup flag */
/*
* If batch_start or batch_add failed then set the
* status of each request marked for AD lookup to
* that error.
*/
if (retcode != IDMAP_SUCCESS) {
continue;
}
/* Nothing found - remove the preset info */
}
continue;
}
continue;
}
/* Evaluate result type */
switch (type) {
case IDMAP_USID:
/*
* We found a user. If we got information
* from IDMU and we were expecting a user,
* copy the id.
*/
if (posix_id != IDMAP_SENTINEL_PID &&
}
break;
case IDMAP_GSID:
/*
* We found a group. If we got information
* from IDMU and we were expecting a group,
* copy the id.
*/
if (posix_id != IDMAP_SENTINEL_PID &&
}
break;
default:
break;
}
state->nldap_nqueries++;
}
/*
* If AD lookup by unixname or pid
* failed with non fatal error
* then clear the error (ie set
* res->retcode to success).
* This allows the next pass to
* process other mapping
* mechanisms for this request.
*/
/* This is not an error */
"Not found in AD");
} else {
"AD lookup error (ignored)");
}
} else {
}
continue;
}
/* Evaluate result type */
switch (type) {
case IDMAP_USID:
case IDMAP_GSID:
break;
default:
break;
}
}
}
return (retcode);
}
/*
* Batch AD lookups
*/
{
int i, j;
int num_queries;
int num_processed;
if (state->ad_nqueries == 0)
return (IDMAP_SUCCESS);
for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
/* Skip if not marked for AD lookup or already in error. */
continue;
/* Init status */
}
/* Case of no ADs */
for (i = 0; i < batch->idmap_mapping_batch_len; i++) {
continue;
}
goto out;
}
i == 0 ? DOMAIN_IS_LOCAL|FOREST_IS_LOCAL : 0,
}
}
_idmapdstate.gcs[i],
i == 0 ? FOREST_IS_LOCAL : 0,
}
/*
* There are no more ADs to try. Return errors for any
* remaining requests.
*/
if (num_queries > 0) {
for (j = 0; j < batch->idmap_mapping_batch_len; j++) {
continue;
}
}
out:
/* AD lookups done. Reset state->ad_nqueries and return */
state->ad_nqueries = 0;
return (retcode);
}
/*
* Convention when processing win2unix requests:
*
* Windows identity:
* req->id1name =
* winname if given otherwise winname found will be placed
* here.
* req->id1domain =
* windomain if given otherwise windomain found will be
* placed here.
* req->id1.idtype =
* be set to IDMAP_USID/GSID depending upon whether the
* given SID is user or group respectively. The user/group-ness
* is determined either when looking up well-known SIDs table OR
* if the SID is found in namecache OR by ad_lookup_batch().
* req->id1..sid.[prefix, rid] =
* SID if given otherwise SID found will be placed here.
*
* Unix identity:
* req->id2name =
* unixname found will be placed here.
* req->id2domain =
* NOT USED
* res->id.idtype =
* Target type initialized from req->id2.idtype. If
* will be placed here.
* res->id..[uid or gid] =
*
* Others:
* res->retcode =
* Return status for this request will be placed here.
* res->direction =
* Direction found will be placed here. Direction
* meaning whether the resultant mapping is valid
* only from win2unix or bi-directional.
* req->direction =
* INTERNAL USE. Used by idmapd to set various
* flags (_IDMAP_F_xxxx) to aid in processing
* of the request.
* req->id2.idtype =
* INTERNAL USE. Initially this is the requested target
* type and is used to initialize res->id.idtype.
* ad_lookup_batch() uses this field temporarily to store
* sid_type obtained by the batched AD lookups and after
* use resets it to IDMAP_NONE to prevent xdr from
* mis-interpreting the contents of req->id2.
* req->id2.idmap_id_u.uid =
* INTERNAL USE. If the AD lookup finds IDMU data
* (uidNumber or gidNumber, depending on the type of
* the entry), it's left here.
*/
/*
* This function does the following:
* 1. Lookup well-known SIDs table.
* 3. Lookup cache.
* 4. Check if the client does not want new mapping to be allocated
* in which case this pass is the final pass.
* 5. Set AD lookup flag if it determines that the next stage needs
* to do AD lookup.
*/
{
int wksid;
/* Initialize result */
wksid = 0;
/* They have to give us *something* to work with! */
goto out;
}
/* sanitize sidprefix */
/* Allow for a fully-qualified name in the "name" parameter */
char *p;
if (p != NULL) {
char *q;
free(q);
goto out;
}
}
}
}
/* Lookup well-known SIDs table */
if (retcode == IDMAP_SUCCESS) {
/* Found a well-known account with a hardwired mapping */
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
"Well-known account lookup failed, code %d", retcode);
goto out;
}
if (wksid) {
/* Found a well-known account, but no mapping */
} else {
/* Check if this is a localsid */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
"Local SID lookup error=%d", retcode);
goto out;
}
if (ALLOW_WK_OR_LOCAL_SIDS_ONLY(req)) {
goto out;
}
}
/*
* If this is a name-based request and we don't have a domain,
* use the default domain. Note that the well-known identity
* cases will have supplied a SID prefix already, and that we
* don't (yet?) support looking up a local user through a Windows
* style name.
*/
goto out;
}
goto out;
}
}
/* Lookup cache */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
goto out;
}
goto out;
}
/*
* Failed to find non-expired entry in cache. Next step is
* to determine if this request needs to be batched for AD lookup.
*
* At this point we have either sid or winname or both. If we don't
* have both then lookup name_cache for the sid or winname
* whichever is missing. If not found then this request will be
* batched for AD lookup.
*/
if (retcode == IDMAP_SUCCESS) {
else
}
} else if (retcode != IDMAP_ERR_NOTFOUND)
goto out;
/*
* If we don't have both name and SID, try looking up the
* entry with LSA.
*/
if (retcode == IDMAP_SUCCESS) {
} else if (retcode == IDMAP_ERR_NOTFOUND) {
} else {
goto out;
}
char *canonname;
char *canondomain;
&canonname, &canondomain,
if (retcode == IDMAP_SUCCESS) {
} else if (retcode == IDMAP_ERR_NOTFOUND) {
} else {
goto out;
}
}
}
/*
* Set the flag to indicate that we are not done yet so that
* subsequent passes considers this request for name-based
* mapping and ephemeral mapping.
*/
/*
* Even if we have both sid and winname, we still may need to batch
* this request for AD lookup if we don't have unixname and
* directory-based name mapping (AD or mixed) is enabled.
* We avoid AD lookup for well-known SIDs because they don't have
* regular AD objects.
*/
if (retcode != IDMAP_SUCCESS ||
state->ad_nqueries++;
state->nldap_nqueries++;
}
out:
/*
* If we are done and there was an error then set fallback pid
* in the result.
*/
return (retcode);
}
/*
* Generate SID using the following convention
* <machine-sid-prefix>-<1000 + uid>
* <machine-sid-prefix>-<2^31 + gid>
*/
static
int fallback)
{
/*
* Diagonal mapping for localSIDs not supported because of the
* way we generate localSIDs.
*/
return (IDMAP_ERR_NOTGROUP);
return (IDMAP_ERR_NOTUSER);
/* Skip 1000 UIDs */
if (is_user &&
return (IDMAP_ERR_NOMAPPING);
/*
* machine_sid is never NULL because if it is we won't be here.
* No need to assert because strdup(NULL) will core anyways.
*/
return (IDMAP_ERR_MEMORY);
}
if (!fallback) {
}
/*
* Don't update name_cache because local sids don't have
* valid windows names.
*/
return (IDMAP_SUCCESS);
}
static
{
char *sidprefix;
int s;
/*
* If the sidprefix == localsid then UID = last RID - 1000 or
* GID = last RID - 2^31.
*/
/* This means we are looking up by winname */
return (IDMAP_ERR_NOTFOUND);
/*
* If the given sidprefix does not match machine_sid then this is
* not a local SID.
*/
if (s != 0)
return (IDMAP_ERR_NOTFOUND);
case IDMAP_UID:
return (IDMAP_ERR_ARG);
break;
case IDMAP_GID:
if (rid < LOCALRID_GID_MIN)
return (IDMAP_ERR_ARG);
break;
case IDMAP_POSIXID:
if (rid >= LOCALRID_GID_MIN) {
} else if (rid >= LOCALRID_UID_MIN) {
} else {
return (IDMAP_ERR_ARG);
}
break;
default:
return (IDMAP_ERR_NOTSUPPORTED);
}
return (IDMAP_SUCCESS);
}
/*
* Name service lookup by unixname to get pid
*/
static
{
char *buf;
case IDMAP_UID:
if (pwdbufsiz == 0)
if (errno == 0)
return (IDMAP_ERR_NOTFOUND);
else
return (IDMAP_ERR_INTERNAL);
}
break;
case IDMAP_GID:
if (grpbufsiz == 0)
if (errno == 0)
return (IDMAP_ERR_NOTFOUND);
else
return (IDMAP_ERR_INTERNAL);
}
break;
default:
return (IDMAP_ERR_ARG);
}
return (IDMAP_SUCCESS);
}
/*
* Name service lookup by pid to get unixname
*/
static
{
char *buf;
if (is_user) {
if (pwdbufsiz == 0)
errno = 0;
if (errno == 0)
return (IDMAP_ERR_NOTFOUND);
else
return (IDMAP_ERR_INTERNAL);
}
} else {
if (grpbufsiz == 0)
errno = 0;
if (errno == 0)
return (IDMAP_ERR_NOTFOUND);
else
return (IDMAP_ERR_INTERNAL);
}
}
return (IDMAP_ERR_MEMORY);
return (IDMAP_SUCCESS);
}
/*
* Name-based mapping
*
* Case 1: If no rule matches do ephemeral
*
* Case 2: If rule matches and unixname is "" then return no mapping.
*
* Case 3: If rule matches and unixname is specified then lookup name
* service using the unixname. If unixname not found then return no mapping.
*
* Case 4: If rule matches and unixname is * then lookup name service
* using winname as the unixname. If unixname not found then process
* other rules using the lookup order. If no other rule matches then do
* ephemeral. Otherwise, based on the matched rule do Case 2 or 3 or 4.
* This allows us to specify a fallback unixname per _domain_ or no mapping
* instead of the default behaviour of doing ephemeral mapping.
*
* Example 1:
* *@sfbay == *
* If looking up windows users foo@sfbay and foo does not exists in
* the name service then foo@sfbay will be mapped to an ephemeral id.
*
* Example 2:
* *@sfbay == *
* *@sfbay => guest
* If looking up windows users foo@sfbay and foo does not exists in
* the name service then foo@sfbay will be mapped to guest.
*
* Example 3:
* *@sfbay == *
* *@sfbay => ""
* If looking up windows users foo@sfbay and foo does not exists in
* the name service then we will return no mapping for foo@sfbay.
*
*/
static
{
const char **values;
int direction;
case IDMAP_USID:
is_wuser = 1;
break;
case IDMAP_GSID:
is_wuser = 0;
break;
default:
"given Windows id is user or group.", me);
return (IDMAP_ERR_INTERNAL);
}
case IDMAP_UID:
is_user = 1;
break;
case IDMAP_GID:
is_user = 0;
break;
case IDMAP_POSIXID:
break;
}
windomain = "";
"SELECT unixname, u2w_order, winname_display, windomain, is_nt4 "
"FROM namerules WHERE "
"w2u_order > 0 AND is_user = %d AND is_wuser = %d AND "
"(winname = %Q OR winname = '*') AND "
"(windomain = %Q OR windomain = '*') "
"ORDER BY w2u_order ASC;",
goto out;
}
CHECK_NULL(errmsg));
goto out;
}
for (;;) {
if (r == SQLITE_ROW) {
if (ncol < 5) {
goto out;
}
goto out;
}
else
if (EMPTY_NAME(values[0])) {
goto out;
}
if (values[0][0] == '*') {
} else {
}
if (retcode == IDMAP_SUCCESS) {
break;
} else if (retcode == IDMAP_ERR_NOTFOUND) {
if (values[0][0] == '*') {
"%s not found, continuing",
unixname);
/* Case 4 */
continue;
} else {
"%s not found, error", unixname);
/* Case 3 */
}
} else {
}
goto out;
} else if (r == SQLITE_DONE) {
goto out;
} else {
CHECK_NULL(errmsg));
goto out;
}
}
/* Found */
else
goto out;
}
out:
if (retcode != IDMAP_SUCCESS &&
retcode != IDMAP_ERR_NOTFOUND &&
retcode != IDMAP_ERR_NOMAPPING) {
}
if (retcode != IDMAP_ERR_NOTFOUND) {
}
return (retcode);
}
static
int
{
int err;
return (err);
}
return (0);
}
static
int
{
int err;
return (err);
}
return (0);
}
static
int
{
return (0);
}
}
}
static
int
{
return (0);
return (1);
}
}
return (0);
}
static
void
{
next++;
}
return;
}
void
{
}
/* ARGSUSED */
static
{
return (IDMAP_SUCCESS);
}
return (IDMAP_SUCCESS);
}
if (get_next_eph_uid(&next_pid) != 0)
return (IDMAP_ERR_INTERNAL);
} else {
if (get_next_eph_gid(&next_pid) != 0)
return (IDMAP_ERR_INTERNAL);
}
return (IDMAP_SUCCESS);
}
{
/* Check if second pass is needed */
/* Get status from previous pass */
/*
* We are asked to map an unresolvable SID to a UID or
* GID, but, which? We'll treat all unresolvable SIDs
* as users unless the caller specified which of a UID
* or GID they want.
*/
}
goto do_eph;
}
if (retcode != IDMAP_SUCCESS)
goto out;
/*
* There are two ways we might get here with a Posix ID:
* - It could be from an expired ephemeral cache entry.
* - It could be from IDMU.
* If it's from IDMU, we need to look up the name, for name-based
* requests and the cache.
*/
/*
* If the lookup fails, go ahead anyway.
* The general UNIX rule is that it's OK to
* have a UID or GID that isn't in the
* name service.
*/
if (IDMAP_ERROR(retcode2)) {
"Getting UNIX name, error=%d (ignored)",
retcode2);
} else {
}
}
goto out;
}
/*
* If directory-based name mapping is enabled then the unixname
* may already have been retrieved from the AD object (AD-mode or
* mixed-mode) or from native LDAP object (nldap-mode) -- done.
*/
/*
* Special case: (1) If the ad_unixuser_attr and
* ad_unixgroup_attr uses the same attribute
* name and (2) if this is a diagonal mapping
* request and (3) the unixname has been retrieved
* from the AD object -- then we ignore it and fallback
* to name-based mapping rules and ephemeral mapping
*
* Example:
* Properties:
* config/ad_unixuser_attr = "unixname"
* config/ad_unixgroup_attr = "unixname"
* AD user object:
* dn: cn=bob ...
* objectclass: user
* sam: bob
* unixname: bob1234
* AD group object:
* dn: cn=winadmins ...
* objectclass: group
* sam: winadmins
* unixname: unixadmins
*
* In this example whether "unixname" refers to a unixuser
* or unixgroup depends upon the AD object.
*
* $idmap show -c winname:bob gid
* AD lookup by "samAccountName=bob" for
* "ad_unixgroup_attr (i.e unixname)" for directory-based
* mapping would get "bob1234" which is not what we want.
* Now why not getgrnam_r("bob1234") and use it if it
* is indeed a unixgroup? That's because Unix can have
* users and groups with the same name and we clearly
* don't know the intention of the admin here.
* Therefore we ignore this and fallback to name-based
* mapping rules or ephemeral mapping.
*/
state->ad_unixgroup_attr) == 0 &&
/* fallback */
} else {
if (retcode != IDMAP_SUCCESS) {
/*
* If ns_lookup_byname() fails that
* means the unixname (req->id2name),
* which was obtained from the AD
* object by directory-based mapping,
* therefore we return the error to the
* client instead of doing rule-based
* mapping or ephemeral mapping. This
* way the client can detect the issue.
*/
"UNIX lookup error=%d", retcode);
goto out;
}
}
goto out;
}
}
/* Free any mapping info from Directory based mapping */
/*
* If we don't have unixname then evaluate local name-based
* mapping rules.
*/
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
goto out;
}
/* If not found, do ephemeral mapping */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
goto out;
}
out:
}
return (retcode);
}
{
/* Check if we need to cache anything */
return (IDMAP_SUCCESS);
/* We don't cache negative entries */
return (IDMAP_SUCCESS);
/*
* If we've gotten to this point and we *still* don't know the
* unixname, well, we'd like to have it now for the cache.
*
* If we truly always need it for the cache, we should probably
* look it up once at the beginning, rather than "at need" in
* several places as is now done. However, it's not really clear
* that we *do* need it in the cache; there's a decent argument
* that the cache should contain only SIDs and PIDs, so we'll
* leave our options open by doing it "at need" here too.
*
* If we can't find it... c'est la vie.
*/
if (retcode2 == IDMAP_SUCCESS)
else
}
case IDMAP_MAP_TYPE_DS_AD:
break;
case IDMAP_MAP_TYPE_DS_NLDAP:
break;
break;
case IDMAP_MAP_TYPE_EPHEMERAL:
break;
case IDMAP_MAP_TYPE_LOCAL_SID:
break;
case IDMAP_MAP_TYPE_IDMU:
break;
default:
/* Don't cache other mapping types */
}
/*
* Using NULL for u2w instead of 0 so that our trigger allows
* the same pid to be the destination in multiple entries
*/
"(sidprefix, rid, windomain, canon_winname, pid, unixname, "
"is_user, is_wuser, expiration, w2u, u2w, "
"map_type, map_dn, map_attr, map_value, map_windomain, "
"map_winname, map_unixname, map_is_nt4) "
"VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
"strftime('%%s','now') + %u, %q, 1, "
"%d, %Q, %Q, %Q, %Q, %Q, %Q, %d); ",
goto out;
}
if (retcode != IDMAP_SUCCESS)
goto out;
/* Check if we need to update namecache */
goto out;
goto out;
"(sidprefix, rid, canon_name, domain, type, expiration) "
"VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + %u); ",
goto out;
}
out:
return (retcode);
}
{
int is_eph_user;
/* Check if we need to cache anything */
return (IDMAP_SUCCESS);
/* We don't cache negative entries */
return (IDMAP_SUCCESS);
is_eph_user = 1;
is_eph_user = 0;
else
is_eph_user = -1;
if (is_eph_user >= 0 &&
"SET w2u = 0 WHERE "
"sidprefix = %Q AND rid = %u AND w2u = 1 AND "
"pid >= 2147483648 AND is_user = %d;",
goto out;
}
if (retcode != IDMAP_SUCCESS)
goto out;
}
case IDMAP_MAP_TYPE_DS_AD:
break;
case IDMAP_MAP_TYPE_DS_NLDAP:
break;
break;
case IDMAP_MAP_TYPE_EPHEMERAL:
break;
case IDMAP_MAP_TYPE_IDMU:
break;
default:
/* Don't cache other mapping types */
}
"(sidprefix, rid, windomain, canon_winname, pid, unixname, "
"is_user, is_wuser, expiration, w2u, u2w, "
"map_type, map_dn, map_attr, map_value, map_windomain, "
"map_winname, map_unixname, map_is_nt4) "
"VALUES(%Q, %u, %Q, %Q, %u, %Q, %d, %d, "
"strftime('%%s','now') + %u, 1, %q, "
"%d, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
goto out;
}
if (retcode != IDMAP_SUCCESS)
goto out;
/* Check if we need to update namecache */
goto out;
goto out;
"(sidprefix, rid, canon_name, domain, type, expiration) "
"VALUES(%Q, %u, %Q, %Q, %d, strftime('%%s','now') + %u); ",
goto out;
}
out:
return (retcode);
}
static
int is_user)
{
char *end;
const char **values;
int ncol;
/* Current time */
errno = 0;
goto out;
}
/* SQL to lookup the cache by pid or by unixname */
"canon_winname, windomain, w2u, is_wuser, "
"map_type, map_dn, map_attr, map_value, map_windomain, "
"map_winname, map_unixname, map_is_nt4 "
"FROM idmap_cache WHERE "
"pid = %u AND u2w = 1 AND is_user = %d AND "
"(pid >= 2147483648 OR "
"(expiration = 0 OR expiration ISNULL OR "
"expiration > %d));",
"canon_winname, windomain, w2u, is_wuser, "
"map_type, map_dn, map_attr, map_value, map_windomain, "
"map_winname, map_unixname, map_is_nt4 "
"FROM idmap_cache WHERE "
"unixname = %Q AND u2w = 1 AND is_user = %d AND "
"(pid >= 2147483648 OR "
"(expiration = 0 OR expiration ISNULL OR "
"expiration > %d));",
} else {
goto out;
}
goto out;
}
if (retcode == IDMAP_ERR_NOTFOUND)
goto out;
else if (retcode == IDMAP_SUCCESS) {
/* sanity checks */
goto out;
}
case IDMAP_SID:
case IDMAP_USID:
case IDMAP_GSID:
? IDMAP_USID : IDMAP_GSID;
idtype != IDMAP_USID) {
goto out;
idtype != IDMAP_GSID) {
goto out;
}
goto out;
}
else
break;
goto out;
}
break;
goto out;
}
break;
default:
break;
}
case IDMAP_MAP_TYPE_DS_AD:
break;
case IDMAP_MAP_TYPE_DS_NLDAP:
break;
break;
case IDMAP_MAP_TYPE_EPHEMERAL:
break;
case IDMAP_MAP_TYPE_LOCAL_SID:
break;
case IDMAP_MAP_TYPE_KNOWN_SID:
break;
case IDMAP_MAP_TYPE_IDMU:
break;
default:
/* Unknown mapping type */
}
}
}
out:
return (retcode);
}
/*
* Given:
* cache sqlite handle
* name Windows user name
* domain Windows domain name
*
* Return: Error code
*
* *canonname Canonical name (if canonname is non-NULL) [1]
* *sidprefix SID prefix [1]
* *rid RID
* *type Type of name
*
* [1] malloc'ed, NULL on error
*/
static
const char *name,
const char *domain,
char **canonname,
char **sidprefix,
{
char *sql;
const char **values;
int ncol;
/* Get current time */
errno = 0;
goto out;
}
/* SQL to lookup the cache */
lower_name = (char *)name;
"FROM name_cache WHERE name = %Q AND domain = %Q AND "
"(expiration = 0 OR expiration ISNULL OR "
if (lower_name != name)
goto out;
}
if (retcode != IDMAP_SUCCESS)
goto out;
goto out;
}
}
goto out;
}
goto out;
}
}
goto out;
}
out:
if (retcode != IDMAP_SUCCESS) {
}
}
return (retcode);
}
static
char **unixname)
{
int retries;
int i;
int found_ad = 0;
if (_idmapdstate.num_gcs > 0) {
retries = 0;
_idmapdstate.gcs[i],
1,
&qs);
if (retcode != IDMAP_SUCCESS) {
if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
goto retry;
"AD lookup by winname");
return (retcode);
}
restore_svc();
/*
* Directory based name mapping is only
* performed within the joined forest (i == 0).
* We don't trust other "trusted" forests to
* provide DS-based name mapping information
* because AD's definition of "cross-forest
* trust" does not encompass this sort of
* behavior.
*/
}
if (retcode == IDMAP_ERR_DOMAIN_NOTFOUND) {
continue;
}
found_ad = 1;
if (retcode != IDMAP_SUCCESS)
else
if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR &&
goto retry;
else if (retcode == IDMAP_ERR_RETRIABLE_NET_ERR)
degrade_svc(1,
"some AD lookups timed out repeatedly");
}
} else {
/* No AD case */
}
if (retcode != IDMAP_SUCCESS) {
"AD lookup of winname %s@%s failed, error code %d",
retcode);
return (retcode);
}
return (rc);
}
/*
* Given:
* cache sqlite handle to cache
* name Windows user name
* domain Windows domain name
* local_only if true, don't try AD lookups
*
* Returns: Error code
*
* *canonname Canonical name (if non-NULL) [1]
* *canondomain Canonical domain (if non-NULL) [1]
* *sidprefix SID prefix [1]
* *rid RID
* *req Request (direction is updated)
*
* [1] malloc'ed, NULL on error
*/
const char *name,
const char *domain,
int want_wuser,
char **canonname,
char **canondomain,
char **sidprefix,
int local_only)
{
if (canondomain != NULL)
*canondomain = NULL;
/* Lookup well-known SIDs table */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
return (retcode);
}
/* Lookup cache */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
return (retcode);
}
/*
* The caller may be using this function to determine if this
* request needs to be marked for AD lookup or not
* (i.e. _IDMAP_F_LOOKUP_AD) and therefore may not want this
* function to AD lookup now.
*/
if (local_only)
return (retcode);
type);
if (retcode == IDMAP_SUCCESS)
goto out;
else if (retcode != IDMAP_ERR_NOTFOUND)
return (retcode);
}
/* Lookup AD */
if (retcode != IDMAP_SUCCESS)
return (retcode);
out:
/*
* Entry found (cache or Windows lookup)
*/
else if (want_wuser == -1) {
/*
* Caller wants to know if its user or group
* Verify that it's one or the other.
*/
}
if (retcode == IDMAP_SUCCESS) {
/*
* If we were asked for a canonical domain and none
* of the searches have provided one, assume it's the
* supplied domain.
*/
if (*canondomain == NULL)
}
}
if (retcode != IDMAP_SUCCESS) {
}
if (canondomain != NULL) {
free(*canondomain);
*canondomain = NULL;
}
}
return (retcode);
}
static
{
char *canonname;
char *canondomain;
char *end;
const char **values;
int ncol, r;
int want_wuser;
int direction;
"SELECT winname_display, windomain, w2u_order, "
"is_wuser, unixname, is_nt4 "
"FROM namerules WHERE "
"u2w_order > 0 AND is_user = %d AND "
"(unixname = %Q OR unixname = '*') "
goto out;
}
CHECK_NULL(errmsg));
goto out;
}
for (;;) {
if (r == SQLITE_ROW) {
if (ncol < 6) {
goto out;
}
/* values [1] and [2] can be null */
goto out;
}
else
if (EMPTY_NAME(values[0])) {
goto out;
}
if (values[0][0] == '*') {
} else {
}
: -1;
"Added default domain %s to rule",
} else {
"No domain in rule, and no default domain");
goto out;
}
if (retcode == IDMAP_SUCCESS) {
break;
} else if (retcode == IDMAP_ERR_NOTFOUND) {
if (values[0][0] == '*') {
"%s@%s not found, continuing",
continue;
} else {
"%s@%s not found",
}
} else {
"Looking up %s@%s error=%d",
}
goto out;
} else if (r == SQLITE_DONE) {
goto out;
} else {
CHECK_NULL(errmsg));
goto out;
}
}
else
out:
if (retcode != IDMAP_ERR_NOTFOUND) {
}
return (retcode);
}
/*
* Convention when processing unix2win requests:
*
* Unix identity:
* req->id1name =
* unixname if given otherwise unixname found will be placed
* here.
* req->id1domain =
* NOT USED
* req->id1.idtype =
* Given type (IDMAP_UID or IDMAP_GID)
* req->id1..[uid or gid] =
*
* Windows identity:
* req->id2name =
* winname found will be placed here.
* req->id2domain =
* windomain found will be placed here.
* res->id.idtype =
* Target type initialized from req->id2.idtype. If
* it is IDMAP_SID then actual type (IDMAP_USID/GSID) found
* will be placed here.
* req->id..sid.[prefix, rid] =
* SID found will be placed here.
*
* Others:
* res->retcode =
* Return status for this request will be placed here.
* res->direction =
* Direction found will be placed here. Direction
* meaning whether the resultant mapping is valid
* only from unix2win or bi-directional.
* req->direction =
* INTERNAL USE. Used by idmapd to set various
* flags (_IDMAP_F_xxxx) to aid in processing
* of the request.
* req->id2.idtype =
* INTERNAL USE. Initially this is the requested target
* type and is used to initialize res->id.idtype.
* ad_lookup_batch() uses this field temporarily to store
* sid_type obtained by the batched AD lookups and after
* use resets it to IDMAP_NONE to prevent xdr from
* mis-interpreting the contents of req->id2.
* req->id2..[uid or gid or sid] =
* NOT USED
*/
/*
* This function does the following:
* 1. Lookup well-known SIDs table.
* 2. Lookup cache.
* 3. Check if the client does not want new mapping to be allocated
* in which case this pass is the final pass.
*/
{
/* Initialize result */
/* sanitize sidprefix */
}
/* Find pid */
goto out;
}
if (retcode != IDMAP_SUCCESS) {
goto out;
}
}
/* Lookup in well-known SIDs table */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
"Well-known account lookup error=%d", retcode);
goto out;
}
/* Lookup in cache */
if (retcode == IDMAP_SUCCESS) {
goto out;
} else if (retcode != IDMAP_ERR_NOTFOUND) {
"Mapping cache lookup error=%d", retcode);
goto out;
}
/* Ephemeral ids cannot be allocated during pid2sid */
goto out;
}
if (DO_NOT_ALLOC_NEW_ID_MAPPING(req)) {
goto out;
}
if (AVOID_NAMESERVICE(req)) {
goto out;
}
/* Set flags for the next stage */
state->ad_nqueries++;
/*
* If AD-based name mapping is enabled then the next stage
* will need to lookup AD using unixname to get the
* corresponding winname.
*/
/* Get unixname if only pid is given. */
if (retcode != IDMAP_SUCCESS) {
"Getting UNIX name error=%d", retcode);
goto out;
}
}
state->ad_nqueries++;
/*
* If native LDAP or mixed mode is enabled for name mapping
* then the next stage will need to lookup native LDAP using
*/
state->nldap_nqueries++;
}
/*
* Failed to find non-expired entry in cache. Set the flag to
* indicate that we are not done yet.
*/
out:
if (gen_localsid_on_err == TRUE) {
if (retcode2 == IDMAP_SUCCESS)
else
"Generate local SID error=%d", retcode2);
}
}
return (retcode);
}
{
/* Check if second pass is needed */
/* Get status from previous pass */
if (retcode != IDMAP_SUCCESS)
goto out;
/*
* If directory-based name mapping is enabled then the winname
* may already have been retrieved from the AD object (AD-mode)
* or from native LDAP object (nldap-mode or mixed-mode).
* Note that if we have winname but no SID then it's an error
* because this implies that the Native LDAP entry contains
* winname which does not exist and it's better that we return
* an error instead of doing rule-based mapping so that the user
* can detect the issue and take appropriate action.
*/
/* Return notfound if we've winname but no SID. */
goto out;
}
goto out;
/*
* We've SID but no winname. This is fine because
* the caller may have only requested SID.
*/
goto out;
}
/* Free any mapping info from Directory based mapping */
/* Get unixname from name service */
if (retcode != IDMAP_SUCCESS) {
"Getting UNIX name error=%d", retcode);
goto out;
}
/* Get pid from name service */
if (retcode != IDMAP_SUCCESS) {
"Getting UNIX ID error=%d", retcode);
goto out;
}
}
/* Use unixname to evaluate local name-based mapping rules */
if (retcode == IDMAP_ERR_NOTFOUND) {
if (retcode == IDMAP_SUCCESS) {
} else {
"Generating local SID error=%d", retcode);
}
}
out:
if (gen_localsid_on_err == TRUE) {
if (retcode2 == IDMAP_SUCCESS)
else
"Generate local SID error=%d", retcode2);
} else {
}
}
return (retcode);
}
{
char *sql1;
char *sql2;
switch (op) {
case IDMAP_FLUSH_EXPIRE:
sql1 =
"UPDATE idmap_cache SET expiration=1 WHERE expiration>0;";
sql2 =
"UPDATE name_cache SET expiration=1 WHERE expiration>0;";
break;
case IDMAP_FLUSH_DELETE:
sql1 = "DELETE FROM idmap_cache;";
sql2 = "DELETE FROM name_cache;";
break;
default:
return (IDMAP_ERR_INTERNAL);
}
if (rc != IDMAP_SUCCESS)
return (rc);
/*
* Note that we flush the idmapd cache first, before the kernel
* cache. If we did it the other way 'round, a request could come
* in after the kernel cache flush and pull a soon-to-be-flushed
* idmapd cache entry back into the kernel cache. This way the
* worst that will happen is that a new entry will be added to
* the kernel cache and then immediately flushed.
*/
if (rc != IDMAP_SUCCESS)
return (rc);
(void) __idmap_flush_kcache();
return (rc);
}