nfsmapid_server.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Door server routines for nfsmapid daemon
* Translate NFSv4 users and groups between numeric and string values
*/
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <signal.h>
#include <libintl.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <memory.h>
#include <pwd.h>
#include <grp.h>
#include <door.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <deflt.h>
#include <nfs/nfsid_map.h>
#include "nfsmapid_resolv.h"
/*
* We cannot use the backend nscd as it may make syscalls that may
* cause further nfsmapid upcalls introducing deadlock.
* Use the internal uncached versions of get*_r.
*/
char *, int);
char *, int);
/*
* is timestamp a == b?
*/
#define TIMESTRUC_EQ(a, b) \
/*
* domain*: describe nfsmapid domain currently in use
* dns_* : describe nfsmapid domain speficied by /etc/resolv.conf
*
* vars listed above (between nfsmapid service threads).
*
* Each nfsmapid thread holds the rdlock and stats the config files.
* If the mtime is different, then they get the writelock and update
* the cached info.
*
* to look at resolv.conf.
*/
timestruc_t nfs_mtime = {0};
uint32_t nfs_domain_len = 0;
timestruc_t dns_mtime = {0};
uint32_t dns_domain_len = 0;
uint32_t cur_domain_len = 0;
timestruc_t zapped_mtime = {0};
#define ZAP_DOMAIN(which) { \
which##_domain_len = 0; \
}
/*
* Diags
*/
#define DIAG_FILE "/var/run/nfs4_domain"
extern size_t pwd_buflen;
extern size_t grp_buflen;
extern thread_t sig_thread;
/*
* Prototypes
*/
extern void check_domain(int);
extern void idmap_kcall(int);
extern int standard_domain_str(const char *);
extern int _nfssys(int, void *);
static int valid_domain(const char *);
static int validate_id_str(const char *);
static int get_mtime(char *, timestruc_t *);
static void get_nfs_domain(void);
static void get_dns_domain(void);
static int extract_domain(char *, char **, char **);
extern void update_diag_file(char *);
static void
{
char *pwd_buf;
char *user;
char *domain;
goto done;
}
long id;
/*
* Invalid "user@dns_domain" string. Still, the user
* part might be an encoded uid, so do a final check.
* Remember, domain part of string was not set since
* not a valid string.
*/
if (!validate_id_str(user)) {
goto done;
}
/*
* Since atoi() does not return proper errors for
* invalid translation, use strtol() instead.
*/
errno = 0;
goto done;
}
goto done;
}
/*
* String properly constructed. Now we check for domain and
* group validity. Note that we only look at the domain iff
* the local domain is configured.
*/
goto done;
}
else {
/*
* Not a valid user
*/
}
goto done;
}
/*
* Valid user entry
*/
done:
}
/* ARGSUSED1 */
static void
{
int pwd_len;
char *pwd_buf;
char *pw_str;
char *at_str;
/*
* Negative uid or greater than UID_MAX
*/
goto done;
}
/*
* Make local copy of domain for further manipuation
*/
(void) rw_rdlock(&domain_cfg_lock);
if (CUR_DOMAIN_NULL()) {
dom_str_len = 0;
dom_str[0] = '\0';
} else {
}
(void) rw_unlock(&domain_cfg_lock);
/*
* We want to encode the uid into a literal string... :
*
* - upon failure to allocate space from the heap
* - if there is no current domain configured
* - if there is no such uid in the passwd DB's
*/
/*
* If we could not allocate from the heap, try
* allocating from the stack as a last resort.
*/
goto done;
}
/*
* Constructing literal string without '@' so that
* we'll know that it's not a user, but rather a
* uid encoded string. Can't overflow because we
* already checked UID_MAX.
*/
at_str_len = dom_str_len = 0;
at_str = "";
dom_str[0] = '\0';
} else {
/*
* Otherwise, we construct the "user@domain" string
*/
at_str = "@";
at_str_len = 1;
}
goto done;
}
/* LINTED format argument to sprintf */
done:
/*
* There is a chance that the door_return will fail because the
* resulting string is too large, try to indicate that if possible
*/
if (door_return((char *)resp,
NULL, 0);
}
}
static void
{
char *grp_buf;
char *group;
char *domain;
goto done;
}
long id;
/*
* Invalid "group@dns_domain" string. Still, the
* group part might be an encoded gid, so do a
* final check. Remember, domain part of string
* was not set since not a valid string.
*/
if (!validate_id_str(group)) {
goto done;
}
/*
* Since atoi() does not return proper errors for
* invalid translation, use strtol() instead.
*/
errno = 0;
goto done;
}
goto done;
}
/*
* String properly constructed. Now we check for domain and
* group validity. Note that we only look at the domain iff
* the local domain is configured.
*/
goto done;
}
else {
/*
* Not a valid group
*/
}
goto done;
}
/*
* Valid group entry
*/
done:
}
/* ARGSUSED1 */
static void
{
char *grp_buf;
char *gr_str;
char *at_str;
/*
* Negative gid or greater than UID_MAX
*/
goto done;
}
/*
* Make local copy of domain for further manipuation
*/
(void) rw_rdlock(&domain_cfg_lock);
if (CUR_DOMAIN_NULL()) {
dom_str_len = 0;
dom_str[0] = '\0';
} else {
}
(void) rw_unlock(&domain_cfg_lock);
/*
* We want to encode the gid into a literal string... :
*
* - upon failure to allocate space from the heap
* - if there is no current domain configured
* - if there is no such gid in the group DB's
*/
/*
* If we could not allocate from the heap, try
* allocating from the stack as a last resort.
*/
goto done;
}
/*
* Constructing literal string without '@' so that
* we'll know that it's not a group, but rather a
* gid encoded string. Can't overflow because we
* already checked UID_MAX.
*/
at_str_len = dom_str_len = 0;
at_str = "";
dom_str[0] = '\0';
} else {
/*
* Otherwise, we construct the "group@domain" string
*/
at_str = "@";
at_str_len = 1;
}
goto done;
}
/* LINTED format argument to sprintf */
done:
/*
* There is a chance that the door_return will fail because the
* resulting string is too large, try to indicate that if possible
*/
if (door_return((char *)resp,
NULL, 0);
}
}
/* ARGSUSED */
void
{
/*
* Make sure we have a valid argument
*/
NULL, 0);
return;
}
/* LINTED pointer cast */
case NFSMAPID_STR_UID:
return;
case NFSMAPID_UID_STR:
return;
case NFSMAPID_STR_GID:
return;
case NFSMAPID_GID_STR:
return;
default:
break;
}
}
static int
{
/*
* Caller must insure that the string is valid
*/
return (0);
*(*dpp)++ = '\0';
return (1);
}
static int
valid_domain(const char *dom)
{
const char *whoami = "valid_domain";
if (!standard_domain_str(dom)) {
return (0);
}
(void) rw_rdlock(&domain_cfg_lock);
(void) rw_unlock(&domain_cfg_lock);
return (1);
}
(void) rw_unlock(&domain_cfg_lock);
return (0);
}
static int
validate_id_str(const char *id)
{
while (*id) {
return (0);
}
return (1);
}
static int
{
int err;
return (err);
return (0);
}
static void
get_nfs_domain(void)
{
const char *whoami = "get_nfs_domain";
char *ndomain;
/*
* If we can't get stats for the config file, then
* zap the NFS domain info. If mtime hasn't changed,
* then there's no work to do, so just return.
*/
return;
}
return;
/*
* Note: defread() returns a ptr to TSD.
*/
/* close default file */
/*
* NFSMAPID_DOMAIN was set so its time for validation.
* If its okay, then update NFS domain and return. If not,
* complain about invalid domain.
*/
if (ndomain) {
if (standard_domain_str(ndomain)) {
return;
}
"Check configuration file and restart daemon."),
}
}
/*
* So the NFS config file changed but it couldn't be opened or
* it didn't specify NFSMAPID_DOMAIN or it specified an invalid
* NFSMAPID_DOMAIN. Time to zap current NFS domain info.
*/
}
static void
get_dns_domain(void)
{
#ifdef DEBUG
const char *whoami = "get_dns_domain";
#endif
timestruc_t ntime = {0};
/*
* If we can't get stats for the config file, then
* zap the DNS domain info. If mtime hasn't changed,
* then there's no work to do, so just return.
*/
errno = 0;
switch (errno) {
case ENOENT:
/*
* The resolver defaults to obtaining the
* domain off of the NIS domainname(1M) if
* /etc/resolv.conf does not exist, so we
* move forward.
*/
break;
default:
return;
}
return;
}
/*
* Re-initialize resolver to zap DNS domain from previous
* resolv_init() calls.
*/
(void) resolv_init();
/*
* Update cached DNS domain. No need for validation since
* domain comes from resolver. If resolver doesn't return the
* domain, then zap the DNS domain. This shouldn't ever happen,
* and if it does, the machine has bigger problems (so no need
* to generating a message that says DNS appears to be broken).
*/
(void) rw_rdlock(&dns_data_lock);
if (sysdns_domain[0] != '\0') {
(void) rw_unlock(&dns_data_lock);
return;
}
(void) rw_unlock(&dns_data_lock);
}
void
idmap_kcall(int did)
{
struct nfsidmap_args args;
if (did >= 0) {
} else {
}
}
/*
* Get the current NFS domain.
*
* otherwise, the DNS domain is used.
*/
void
check_domain(int flush)
{
const char *whoami = "check_domain";
char *new_domain;
int new_dlen = 0;
static int setup_done = 0;
if (nfs_domain_len != 0) {
goto dname_chkd;
}
/*
* If called in response to a SIGHUP,
* reset any cached DNS TXT RR state.
*/
if (dns_txt_domain_len != 0) {
} else {
/*
* We're either here because:
*
* . No suitable DNS TXT resource record exists
* . DNS server is not responding to requests
*
* in either case, we want to default to using the
* system configured DNS domain. If this fails, then
* dns_domain will be empty and dns_domain_len will
* be 0.
*/
}
/*
* Update cur_domain if new_domain is different. Set flush
* to guarantee that kernel idmapping caches are flushed.
*/
(void) rw_wrlock(&domain_cfg_lock);
(void) rw_unlock(&domain_cfg_lock);
flush = 1;
}
/*
* Restart the signal handler thread if we're still setting up
*/
if (!setup_done) {
setup_done = 1;
if (thr_continue(sig_thread)) {
"handler thread could not be restarted."), whoami);
exit(6);
}
/*
* We force bail here so we don't end up flushing kernel
* caches until we _know_ we're up.
*/
return;
}
/*
* If caller requested flush or if domain has changed, then
* flush kernel idmapping caches.
*/
if (flush)
idmap_kcall(-1);
}
/*
* Based on the recommendations from
* RFC1033 DOMAIN ADMINISTRATORS OPERATIONS GUIDE
* RFC1035 DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION
* check if a given domain name string is valid.
*/
int
standard_domain_str(const char *ds)
{
int i;
return (0);
}
if (i == NS_MAXCDNAME)
return (0);
return (1);
}
/*
* Need to be able to open the DIAG_FILE before nfsmapid(1m)
* releases it's root priviledges. The DIAG_FILE then remains
* open for the duration of this nfsmapid instance via n4_fp.
*/
void
{
static int msg_done = 0;
return;
if (msg_done)
return;
"daemon.debug for more info", DIAG_FILE);
msg_done = 1;
}
/*
* When a new domain name is configured, save to DIAG_FILE
* and log to syslog, with LOG_DEBUG level (if configured).
*/
void
update_diag_file(char *new)
{
}