2018-01-01 00:32:25 +00:00
|
|
|
|
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
2000-12-08 22:39:54 +00:00
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
|
Lesser General Public License for more details.
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include "nsswitch.h"
|
2017-06-30 19:10:23 +00:00
|
|
|
|
#include <resolv/resolv_context.h>
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
/* Set up NIP to run through the services. If ALL is zero, use NIP's
|
|
|
|
|
current location if it's not nil. Return nonzero if there are no
|
|
|
|
|
services (left). */
|
|
|
|
|
static int
|
|
|
|
|
setup (const char *func_name, db_lookup_function lookup_fct,
|
|
|
|
|
void **fctp, service_user **nip, service_user **startp, int all)
|
|
|
|
|
{
|
|
|
|
|
int no_more;
|
|
|
|
|
if (*startp == NULL)
|
|
|
|
|
{
|
2007-10-30 00:48:09 +00:00
|
|
|
|
no_more = lookup_fct (nip, func_name, NULL, fctp);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
*startp = no_more ? (service_user *) -1l : *nip;
|
|
|
|
|
}
|
|
|
|
|
else if (*startp == (service_user *) -1l)
|
|
|
|
|
/* No services at all. */
|
|
|
|
|
return 1;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (all || !*nip)
|
|
|
|
|
/* Reset to the beginning of the service list. */
|
|
|
|
|
*nip = *startp;
|
|
|
|
|
/* Look up the first function. */
|
2007-10-30 00:48:09 +00:00
|
|
|
|
no_more = __nss_lookup (nip, func_name, NULL, fctp);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
return no_more;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__nss_setent (const char *func_name, db_lookup_function lookup_fct,
|
|
|
|
|
service_user **nip, service_user **startp,
|
|
|
|
|
service_user **last_nip, int stayopen, int *stayopen_tmp,
|
|
|
|
|
int res)
|
|
|
|
|
{
|
2002-11-10 11:06:36 +00:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
setent_function f;
|
|
|
|
|
void *ptr;
|
|
|
|
|
} fct;
|
2000-12-08 22:39:54 +00:00
|
|
|
|
int no_more;
|
|
|
|
|
|
2017-06-30 19:10:23 +00:00
|
|
|
|
struct resolv_context *res_ctx = NULL;
|
|
|
|
|
if (res)
|
2000-12-08 22:39:54 +00:00
|
|
|
|
{
|
2017-06-30 19:10:23 +00:00
|
|
|
|
res_ctx = __resolv_context_get ();
|
|
|
|
|
if (res_ctx == NULL)
|
|
|
|
|
{
|
|
|
|
|
__set_h_errno (NETDB_INTERNAL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cycle through the services and run their `setXXent' functions until
|
|
|
|
|
we find an available service. */
|
2002-11-10 11:06:36 +00:00
|
|
|
|
no_more = setup (func_name, lookup_fct, &fct.ptr, nip,
|
2000-12-08 22:39:54 +00:00
|
|
|
|
startp, 1);
|
|
|
|
|
while (! no_more)
|
|
|
|
|
{
|
|
|
|
|
int is_last_nip = *nip == *last_nip;
|
|
|
|
|
enum nss_status status;
|
2002-08-31 00:02:18 +00:00
|
|
|
|
|
2000-12-08 22:39:54 +00:00
|
|
|
|
if (stayopen_tmp)
|
2002-11-10 11:06:36 +00:00
|
|
|
|
status = DL_CALL_FCT (fct.f, (*stayopen_tmp));
|
2000-12-08 22:39:54 +00:00
|
|
|
|
else
|
2002-11-10 11:06:36 +00:00
|
|
|
|
status = DL_CALL_FCT (fct.f, (0));
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
NSS: Implement group merging support.
https://sourceware.org/glibc/wiki/Proposals/GroupMerging
== Justification ==
It is common today for users to rely on centrally-managed user stores for
handling their user accounts. However, much software existing today does
not have an innate understanding of such accounts. Instead, they commonly
rely on membership in known groups for managing access-control (for
example the "wheel" group on Fedora and RHEL systems or the "adm" group
on Debian-derived systems). In the present incarnation of nsswitch, the
only way to have such groups managed by a remote user store such as
FreeIPA or Active Directory would be to manually remove the groups from
/etc/group on the clients so that nsswitch would then move past nss_files
and into the SSSD, nss-ldap or other remote user database.
== Solution ==
With this patch, a new action is introduced for nsswitch:
NSS_ACTION_MERGE. To take advantage of it, one will add [SUCCESS=merge]
between two database entries in the nsswitch.conf file. When a group is
located in the first of the two group entries, processing will continue
on to the next one. If the group is also found in the next entry (and the
group name and GID are an exact match), the member list of the second
entry will be added to the group object to be returned.
== Implementation ==
After each DL_LOOKUP_FN() returns, the next action is checked. If the
function returned NSS_STATUS_SUCCESS and the next action is
NSS_ACTION_MERGE, a copy of the result buffer is saved for the next pass
through the loop. If on this next pass through the loop the database
returns another instance of a group matching both the group name and GID,
the member list is added to the previous list and it is returned as a
single object. If the following database does not contain the same group,
then the original is copied back into the destination buffer.
This patch implements merge functionality only for the group database.
For other databases, there is a default implementation that will return
the EINVAL errno if a merge is requested. The merge functionality can be
implemented for other databases at a later time if such is needed. Each
database must provide a unique implementation of the deep-copy and merge
functions.
If [SUCCESS=merge] is present in nsswitch.conf for a glibc version that
does not support it, glibc will process results up until that operation,
at which time it will return results if it has found them or else will
simply return an error. In practical terms, this ends up behaving like
the remainder of the nsswitch.conf line does not exist.
== Iterators ==
This feature does not modify the iterator functionality from its current
behavior. If getgrnam() or getgrgid() is called, glibc will iterate
through all entries in the `group` line in nsswitch.conf and display the
list of members without attempting to merge them. This is consistent with
the behavior of nss_files where if two separate lines are specified for
the same group in /etc/groups, getgrnam()/getgrgid() will display both.
Clients are already expected to handle this gracefully.
== No Premature Optimizations ==
The following is a list of places that might be eligible for
optimization, but were not overengineered for this initial contribution:
* Any situation where a merge may occur will result in one malloc() of
the same size as the input buffer.
* Any situation where a merge does occur will result in a second
malloc() to hold the list of pointers to member name strings.
* The list of members is simply concatenated together and is not tested
for uniqueness (which is identical to the behavior for nss_files,
which will simply return identical values if they both exist on the
line in the file. This could potentially be optimized to reduce space
usage in the buffer, but it is both complex and computationally
expensive to do so.
== Testing ==
I performed testing by running the getent utility against my newly-built
glibc and configuring /etc/nsswitch.conf with the following entry:
group: group: files [SUCCESS=merge] sss
In /etc/group I included the line:
wheel:x:10:sgallagh
I then configured my local SSSD using the id_provider=local to respond
with:
wheel:*:10:localuser,localuser2
I then ran `getent group wheel` against the newly-built glibc in
multiple situations and received the expected output as described
above:
* When SSSD was running.
* When SSSD was configured in nsswitch.conf but the daemon was not
running.
* When SSSD was configured in nsswitch.conf but nss_sss.so.2 was not
installed on the system.
* When the order of 'sss' and 'files' was reversed.
* All of the above with the [SUCCESS=merge] removed (to ensure no
regressions).
* All of the above with `getent group 10`.
* All of the above with `getent group` with and without
`enumerate=true` set in SSSD.
* All of the above with and without nscd enabled on the system.
2016-04-30 02:11:09 +00:00
|
|
|
|
|
|
|
|
|
/* This is a special-case. When [SUCCESS=merge] is in play,
|
|
|
|
|
_nss_next2() will skip to the next database. Due to the
|
|
|
|
|
implementation of that function, we can't know whether we're
|
|
|
|
|
in an enumeration or an individual lookup, which behaves
|
|
|
|
|
differently with regards to merging. We'll treat SUCCESS as
|
|
|
|
|
an indication to start the enumeration at this database. */
|
|
|
|
|
if (nss_next_action (*nip, status) == NSS_ACTION_MERGE)
|
|
|
|
|
no_more = 1;
|
|
|
|
|
else
|
|
|
|
|
no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
|
|
|
|
|
|
2000-12-08 22:39:54 +00:00
|
|
|
|
if (is_last_nip)
|
|
|
|
|
*last_nip = *nip;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 19:10:23 +00:00
|
|
|
|
__resolv_context_put (res_ctx);
|
|
|
|
|
|
2000-12-08 22:39:54 +00:00
|
|
|
|
if (stayopen_tmp)
|
|
|
|
|
*stayopen_tmp = stayopen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__nss_endent (const char *func_name, db_lookup_function lookup_fct,
|
|
|
|
|
service_user **nip, service_user **startp,
|
|
|
|
|
service_user **last_nip, int res)
|
|
|
|
|
{
|
2002-11-10 11:06:36 +00:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
endent_function f;
|
|
|
|
|
void *ptr;
|
|
|
|
|
} fct;
|
2000-12-08 22:39:54 +00:00
|
|
|
|
int no_more;
|
|
|
|
|
|
2017-06-30 19:10:23 +00:00
|
|
|
|
struct resolv_context *res_ctx = NULL;
|
|
|
|
|
if (res)
|
2000-12-08 22:39:54 +00:00
|
|
|
|
{
|
2017-06-30 19:10:23 +00:00
|
|
|
|
res_ctx = __resolv_context_get ();
|
|
|
|
|
if (res_ctx == NULL)
|
|
|
|
|
{
|
|
|
|
|
__set_h_errno (NETDB_INTERNAL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cycle through all the services and run their endXXent functions. */
|
2002-11-10 11:06:36 +00:00
|
|
|
|
no_more = setup (func_name, lookup_fct, &fct.ptr, nip, startp, 1);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
while (! no_more)
|
|
|
|
|
{
|
|
|
|
|
/* Ignore status, we force check in __NSS_NEXT. */
|
2002-11-10 11:06:36 +00:00
|
|
|
|
DL_CALL_FCT (fct.f, ());
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
if (*nip == *last_nip)
|
|
|
|
|
/* We have processed all services which were used. */
|
|
|
|
|
break;
|
|
|
|
|
|
2007-10-30 00:48:09 +00:00
|
|
|
|
no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, 0, 1);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
*last_nip = *nip = NULL;
|
2017-06-30 19:10:23 +00:00
|
|
|
|
|
|
|
|
|
__resolv_context_put (res_ctx);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
__nss_getent_r (const char *getent_func_name,
|
|
|
|
|
const char *setent_func_name,
|
|
|
|
|
db_lookup_function lookup_fct,
|
|
|
|
|
service_user **nip, service_user **startp,
|
|
|
|
|
service_user **last_nip, int *stayopen_tmp, int res,
|
|
|
|
|
void *resbuf, char *buffer, size_t buflen,
|
|
|
|
|
void **result, int *h_errnop)
|
|
|
|
|
{
|
2002-11-10 11:06:36 +00:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
getent_function f;
|
|
|
|
|
void *ptr;
|
|
|
|
|
} fct;
|
2000-12-08 22:39:54 +00:00
|
|
|
|
int no_more;
|
|
|
|
|
enum nss_status status;
|
|
|
|
|
|
2017-06-30 19:10:23 +00:00
|
|
|
|
struct resolv_context *res_ctx = NULL;
|
|
|
|
|
if (res)
|
2000-12-08 22:39:54 +00:00
|
|
|
|
{
|
2017-06-30 19:10:23 +00:00
|
|
|
|
res_ctx = __resolv_context_get ();
|
|
|
|
|
if (res_ctx == NULL)
|
|
|
|
|
{
|
|
|
|
|
*h_errnop = NETDB_INTERNAL;
|
|
|
|
|
*result = NULL;
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Initialize status to return if no more functions are found. */
|
|
|
|
|
status = NSS_STATUS_NOTFOUND;
|
|
|
|
|
|
|
|
|
|
/* Run through available functions, starting with the same function last
|
|
|
|
|
run. We will repeat each function as long as it succeeds, and then go
|
|
|
|
|
on to the next service action. */
|
2002-11-10 11:06:36 +00:00
|
|
|
|
no_more = setup (getent_func_name, lookup_fct, &fct.ptr, nip,
|
2000-12-08 22:39:54 +00:00
|
|
|
|
startp, 0);
|
|
|
|
|
while (! no_more)
|
|
|
|
|
{
|
|
|
|
|
int is_last_nip = *nip == *last_nip;
|
|
|
|
|
|
2002-11-10 11:06:36 +00:00
|
|
|
|
status = DL_CALL_FCT (fct.f,
|
|
|
|
|
(resbuf, buffer, buflen, &errno, &h_errno));
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
2011-04-23 01:34:32 +00:00
|
|
|
|
/* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
|
2000-12-08 22:39:54 +00:00
|
|
|
|
provided buffer is too small. In this case we should give
|
|
|
|
|
the user the possibility to enlarge the buffer and we should
|
|
|
|
|
not simply go on with the next service (even if the TRYAGAIN
|
|
|
|
|
action tells us so). */
|
|
|
|
|
if (status == NSS_STATUS_TRYAGAIN
|
|
|
|
|
&& (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
|
|
|
|
|
&& errno == ERANGE)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
NSS: Implement group merging support.
https://sourceware.org/glibc/wiki/Proposals/GroupMerging
== Justification ==
It is common today for users to rely on centrally-managed user stores for
handling their user accounts. However, much software existing today does
not have an innate understanding of such accounts. Instead, they commonly
rely on membership in known groups for managing access-control (for
example the "wheel" group on Fedora and RHEL systems or the "adm" group
on Debian-derived systems). In the present incarnation of nsswitch, the
only way to have such groups managed by a remote user store such as
FreeIPA or Active Directory would be to manually remove the groups from
/etc/group on the clients so that nsswitch would then move past nss_files
and into the SSSD, nss-ldap or other remote user database.
== Solution ==
With this patch, a new action is introduced for nsswitch:
NSS_ACTION_MERGE. To take advantage of it, one will add [SUCCESS=merge]
between two database entries in the nsswitch.conf file. When a group is
located in the first of the two group entries, processing will continue
on to the next one. If the group is also found in the next entry (and the
group name and GID are an exact match), the member list of the second
entry will be added to the group object to be returned.
== Implementation ==
After each DL_LOOKUP_FN() returns, the next action is checked. If the
function returned NSS_STATUS_SUCCESS and the next action is
NSS_ACTION_MERGE, a copy of the result buffer is saved for the next pass
through the loop. If on this next pass through the loop the database
returns another instance of a group matching both the group name and GID,
the member list is added to the previous list and it is returned as a
single object. If the following database does not contain the same group,
then the original is copied back into the destination buffer.
This patch implements merge functionality only for the group database.
For other databases, there is a default implementation that will return
the EINVAL errno if a merge is requested. The merge functionality can be
implemented for other databases at a later time if such is needed. Each
database must provide a unique implementation of the deep-copy and merge
functions.
If [SUCCESS=merge] is present in nsswitch.conf for a glibc version that
does not support it, glibc will process results up until that operation,
at which time it will return results if it has found them or else will
simply return an error. In practical terms, this ends up behaving like
the remainder of the nsswitch.conf line does not exist.
== Iterators ==
This feature does not modify the iterator functionality from its current
behavior. If getgrnam() or getgrgid() is called, glibc will iterate
through all entries in the `group` line in nsswitch.conf and display the
list of members without attempting to merge them. This is consistent with
the behavior of nss_files where if two separate lines are specified for
the same group in /etc/groups, getgrnam()/getgrgid() will display both.
Clients are already expected to handle this gracefully.
== No Premature Optimizations ==
The following is a list of places that might be eligible for
optimization, but were not overengineered for this initial contribution:
* Any situation where a merge may occur will result in one malloc() of
the same size as the input buffer.
* Any situation where a merge does occur will result in a second
malloc() to hold the list of pointers to member name strings.
* The list of members is simply concatenated together and is not tested
for uniqueness (which is identical to the behavior for nss_files,
which will simply return identical values if they both exist on the
line in the file. This could potentially be optimized to reduce space
usage in the buffer, but it is both complex and computationally
expensive to do so.
== Testing ==
I performed testing by running the getent utility against my newly-built
glibc and configuring /etc/nsswitch.conf with the following entry:
group: group: files [SUCCESS=merge] sss
In /etc/group I included the line:
wheel:x:10:sgallagh
I then configured my local SSSD using the id_provider=local to respond
with:
wheel:*:10:localuser,localuser2
I then ran `getent group wheel` against the newly-built glibc in
multiple situations and received the expected output as described
above:
* When SSSD was running.
* When SSSD was configured in nsswitch.conf but the daemon was not
running.
* When SSSD was configured in nsswitch.conf but nss_sss.so.2 was not
installed on the system.
* When the order of 'sss' and 'files' was reversed.
* All of the above with the [SUCCESS=merge] removed (to ensure no
regressions).
* All of the above with `getent group 10`.
* All of the above with `getent group` with and without
`enumerate=true` set in SSSD.
* All of the above with and without nscd enabled on the system.
2016-04-30 02:11:09 +00:00
|
|
|
|
/* This is a special-case. When [SUCCESS=merge] is in play,
|
|
|
|
|
_nss_next2() will skip to the next database. Due to the
|
|
|
|
|
implementation of that function, we can't know whether we're
|
|
|
|
|
in an enumeration or an individual lookup, which behaves
|
|
|
|
|
differently with regards to merging. We'll treat SUCCESS as
|
|
|
|
|
an indication to return the results here. */
|
|
|
|
|
if (status == NSS_STATUS_SUCCESS
|
|
|
|
|
&& nss_next_action (*nip, status) == NSS_ACTION_MERGE)
|
|
|
|
|
no_more = 1;
|
|
|
|
|
else
|
|
|
|
|
no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
|
|
|
|
|
status, 0);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
if (is_last_nip)
|
|
|
|
|
*last_nip = *nip;
|
|
|
|
|
|
|
|
|
|
if (! no_more)
|
|
|
|
|
{
|
|
|
|
|
/* Call the `setXXent' function. This wasn't done before. */
|
2002-11-10 11:06:36 +00:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
setent_function f;
|
|
|
|
|
void *ptr;
|
|
|
|
|
} sfct;
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
2007-10-30 00:48:09 +00:00
|
|
|
|
no_more = __nss_lookup (nip, setent_func_name, NULL, &sfct.ptr);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
|
|
|
|
|
if (! no_more)
|
|
|
|
|
{
|
|
|
|
|
if (stayopen_tmp)
|
2002-11-10 11:06:36 +00:00
|
|
|
|
status = DL_CALL_FCT (sfct.f, (*stayopen_tmp));
|
2000-12-08 22:39:54 +00:00
|
|
|
|
else
|
2002-11-10 11:06:36 +00:00
|
|
|
|
status = DL_CALL_FCT (sfct.f, (0));
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
status = NSS_STATUS_NOTFOUND;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (! no_more && status != NSS_STATUS_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 19:10:23 +00:00
|
|
|
|
__resolv_context_put (res_ctx);
|
|
|
|
|
|
2000-12-08 22:39:54 +00:00
|
|
|
|
*result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
|
2002-09-12 02:58:43 +00:00
|
|
|
|
return (status == NSS_STATUS_SUCCESS ? 0
|
|
|
|
|
: status != NSS_STATUS_TRYAGAIN ? ENOENT
|
|
|
|
|
/* h_errno functions only set errno if h_errno is NETDB_INTERNAL. */
|
|
|
|
|
: (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno
|
|
|
|
|
: EAGAIN);
|
2000-12-08 22:39:54 +00:00
|
|
|
|
}
|