#include <sys/types.h>
#include <nsswitch.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include "ns_sldap.h"
#include <nss_dbdefs.h>
#include <nsswitch.h>
#include <pwd.h>
#include <shadow.h>
#include <rpcsvc/nis.h>
#include "passwdutil.h"
int
name_to_int(char *rep_name)
{
int result = REP_ERANGE;
if (strcmp(rep_name, "files") == 0)
result = REP_FILES;
else if (strcmp(rep_name, "nis") == 0)
result = REP_NIS;
else if (strcmp(rep_name, "ldap") == 0)
result = REP_LDAP;
else if (strcmp(rep_name, "compat") == 0) {
struct __nsw_switchconfig *cfg;
enum __nsw_parse_err pserr;
cfg = __nsw_getconfig("passwd_compat", &pserr);
if (cfg == NULL) {
result = REP_FILES | REP_NIS;
} else {
if (strcmp(cfg->lookups->service_name, "ldap") == 0)
result = REP_FILES | REP_LDAP;
else
result = REP_ERANGE;
(void) __nsw_freeconfig(cfg);
}
}
return (result);
}
int
get_compat_mode(void)
{
struct __nsw_switchconfig *cfg;
enum __nsw_parse_err pserr;
int result = REP_COMPAT_NIS;
if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) {
if (strcmp(cfg->lookups->service_name, "ldap") == 0)
result = REP_COMPAT_LDAP;
}
(void) __nsw_freeconfig(cfg);
return (result);
}
int
get_ns(pwu_repository_t *rep, int accesstype)
{
struct __nsw_switchconfig *conf = NULL;
enum __nsw_parse_err pserr;
struct __nsw_lookup *lkp;
struct __nsw_lookup *lkp2;
struct __nsw_lookup *lkp3;
struct __nsw_lookup *lkpn;
int result = REP_NOREP;
if (rep != PWU_DEFAULT_REP) {
result = name_to_int(rep->type);
return (result);
}
conf = __nsw_getconfig("passwd", &pserr);
if (conf == NULL) {
syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for "
"passwd not found.");
result = REP_FILES | REP_NIS;
return (result);
}
lkp = conf->lookups;
if (conf->num_lookups == 1) {
if (strcmp(lkp->service_name, "files") == 0) {
result = name_to_int(lkp->service_name);
} else if (strcmp(lkp->service_name, "compat") == 0) {
if (accesstype == PWU_READ)
result = REP_NSS | get_compat_mode();
else
result = name_to_int(lkp->service_name);
} else
result = REP_NSS;
} else if (conf->num_lookups == 2) {
lkp2 = lkp->next;
if (strcmp(lkp->service_name, "files") == 0) {
result = REP_FILES;
if (strcmp(lkp2->service_name, "ldap") == 0)
result |= REP_LDAP;
else if (strcmp(lkp2->service_name, "nis") == 0)
result |= REP_NIS;
else if (strcmp(lkp2->service_name, "ad") != 0)
result = REP_NSS;
} else {
result = REP_NSS;
}
} else if (conf->num_lookups == 3) {
lkp2 = lkp->next;
lkp3 = lkp2->next;
if (strcmp(lkp2->service_name, "ad") == 0)
lkpn = lkp3;
else if (strcmp(lkp3->service_name, "ad") == 0)
lkpn = lkp2;
else
lkpn = NULL;
if (strcmp(lkp->service_name, "files") == 0 &&
lkpn != NULL) {
result = REP_FILES;
if (strcmp(lkpn->service_name, "ldap") == 0)
result |= REP_LDAP;
else if (strcmp(lkpn->service_name, "nis") == 0)
result |= REP_NIS;
else
result = REP_NSS;
} else {
result = REP_NSS;
}
} else {
result = REP_NSS;
}
(void) __nsw_freeconfig(conf);
return (result);
}
static void
nss_ldap_passwd(p)
nss_db_params_t *p;
{
p->name = NSS_DBNAM_PASSWD;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = "ldap";
}
static void
nss_ldap_shadow(p)
nss_db_params_t *p;
{
p->name = NSS_DBNAM_SHADOW;
p->config_name = NSS_DBNAM_PASSWD;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = "ldap";
}
#ifdef PAM_NIS
static void
nss_nis_passwd(p)
nss_db_params_t *p;
{
p->name = NSS_DBNAM_PASSWD;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = "nis";
}
static void
nss_nis_shadow(p)
nss_db_params_t *p;
{
p->name = NSS_DBNAM_SHADOW;
p->config_name = NSS_DBNAM_PASSWD;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = "nis";
}
#endif
static char *
gettok(nextpp)
char **nextpp;
{
char *p = *nextpp;
char *q = p;
char c;
if (p == 0) {
return (0);
}
while ((c = *q) != '\0' && c != ':') {
q++;
}
if (c == '\0') {
*nextpp = 0;
} else {
*q++ = '\0';
*nextpp = q;
}
return (p);
}
static int
str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
struct passwd *passwd = (struct passwd *)ent;
char *p, *next;
int black_magic;
if (lenstr + 1 > buflen) {
return (NSS_STR_PARSE_ERANGE);
}
(void) memcpy(buffer, instr, lenstr);
buffer[lenstr] = '\0';
next = buffer;
passwd->pw_name = p = gettok(&next);
if (*p == '\0') {
return (NSS_STR_PARSE_PARSE);
}
black_magic = (*p == '+' || *p == '-');
if (black_magic) {
passwd->pw_uid = UID_NOBODY;
passwd->pw_gid = GID_NOBODY;
passwd->pw_passwd = "";
passwd->pw_age = "";
passwd->pw_comment = "";
passwd->pw_gecos = "";
passwd->pw_dir = "";
passwd->pw_shell = "";
}
passwd->pw_passwd = p = gettok(&next);
if (p == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
for (; *p != '\0'; p++) {
if (*p == ',') {
*p++ = '\0';
break;
}
}
passwd->pw_age = p;
p = next;
if (p == 0 || *p == '\0') {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
if (!black_magic) {
passwd->pw_uid = strtol(p, &next, 10);
if (next == p) {
return (NSS_STR_PARSE_PARSE);
}
if (passwd->pw_uid > MAXUID)
passwd->pw_uid = UID_NOBODY;
}
if (*next++ != ':') {
if (black_magic)
p = gettok(&next);
else
return (NSS_STR_PARSE_PARSE);
}
p = next;
if (p == 0 || *p == '\0') {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
if (!black_magic) {
passwd->pw_gid = strtol(p, &next, 10);
if (next == p) {
return (NSS_STR_PARSE_PARSE);
}
if (passwd->pw_gid > MAXUID)
passwd->pw_gid = GID_NOBODY;
}
if (*next++ != ':') {
if (black_magic)
p = gettok(&next);
else
return (NSS_STR_PARSE_PARSE);
}
passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
if (p == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
passwd->pw_dir = p = gettok(&next);
if (p == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
passwd->pw_shell = p = gettok(&next);
if (p == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
if (next == 0) {
return (NSS_STR_PARSE_SUCCESS);
}
return (NSS_STR_PARSE_PARSE);
}
typedef const char *constp;
static int
getfield(nextp, limit, uns, valp)
constp *nextp;
constp limit;
int uns;
void *valp;
{
constp p = *nextp;
char *endfield;
char numbuf[12];
int len;
long x;
unsigned long ux;
if (p == 0 || p >= limit) {
return (0);
}
if (*p == ':') {
p++;
*nextp = p;
return (p < limit);
}
if ((len = limit - p) > sizeof (numbuf) - 1) {
len = sizeof (numbuf) - 1;
}
if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
if (len != limit - p) {
return (0);
}
numbuf[len] = ':';
p = limit;
} else {
p += (endfield - numbuf);
}
if (uns) {
ux = strtoul(numbuf, &endfield, 10);
if (*endfield != ':') {
return (0);
}
*((unsigned int *)valp) = (unsigned int)ux;
} else {
x = strtol(numbuf, &endfield, 10);
if (*endfield != ':') {
return (0);
}
*((int *)valp) = (int)x;
}
*nextp = p;
return (p < limit);
}
int
str2spwd(instr, lenstr, ent, buffer, buflen)
const char *instr;
int lenstr;
void *ent;
char *buffer;
int buflen;
{
struct spwd *shadow = (struct spwd *)ent;
const char *p = instr, *limit;
char *bufp;
int lencopy, black_magic;
limit = p + lenstr;
if ((p = memchr(instr, ':', lenstr)) == 0 ||
++p >= limit ||
(p = memchr(p, ':', limit - p)) == 0) {
lencopy = lenstr;
p = 0;
} else {
lencopy = p - instr;
p++;
}
if (lencopy + 1 > buflen) {
return (NSS_STR_PARSE_ERANGE);
}
(void) memcpy(buffer, instr, lencopy);
buffer[lencopy] = 0;
black_magic = (*instr == '+' || *instr == '-');
shadow->sp_namp = bufp = buffer;
shadow->sp_pwdp = 0;
shadow->sp_lstchg = -1;
shadow->sp_min = -1;
shadow->sp_max = -1;
shadow->sp_warn = -1;
shadow->sp_inact = -1;
shadow->sp_expire = -1;
shadow->sp_flag = 0;
if ((bufp = strchr(bufp, ':')) == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
*bufp++ = '\0';
shadow->sp_pwdp = bufp;
if (instr == 0) {
if ((bufp = strchr(bufp, ':')) == 0) {
if (black_magic)
return (NSS_STR_PARSE_SUCCESS);
else
return (NSS_STR_PARSE_PARSE);
}
*bufp++ = '\0';
p = bufp;
}
if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 0, &shadow->sp_min))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 0, &shadow->sp_max))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 0, &shadow->sp_warn))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 0, &shadow->sp_inact))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 0, &shadow->sp_expire))
return (NSS_STR_PARSE_SUCCESS);
if (!getfield(&p, limit, 1, &shadow->sp_flag))
return (NSS_STR_PARSE_SUCCESS);
if (p != limit) {
return (NSS_STR_PARSE_PARSE);
}
return (NSS_STR_PARSE_SUCCESS);
}
static nss_XbyY_buf_t *buffer;
static DEFINE_NSS_DB_ROOT(db_root);
#define GETBUF() \
NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD)
#pragma fini(endutilpwent)
static void
endutilpwent(void)
{
NSS_XbyY_FREE(&buffer);
nss_delete(&db_root);
}
struct passwd *
getpwnam_from(const char *name, pwu_repository_t *rep, int reptype)
{
nss_XbyY_buf_t *b = GETBUF();
nss_XbyY_args_t arg;
if (b == 0)
return (0);
NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
arg.key.name = name;
switch (reptype) {
case REP_LDAP:
(void) nss_search(&db_root, nss_ldap_passwd,
NSS_DBOP_PASSWD_BYNAME, &arg);
break;
#ifdef PAM_NIS
case REP_NIS:
(void) nss_search(&db_root, nss_nis_passwd,
NSS_DBOP_PASSWD_BYNAME, &arg);
break;
#endif
default:
return (NULL);
}
return (struct passwd *)NSS_XbyY_FINI(&arg);
}
struct passwd *
getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype)
{
nss_XbyY_buf_t *b = GETBUF();
nss_XbyY_args_t arg;
if (b == 0)
return (0);
NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
arg.key.uid = uid;
switch (reptype) {
case REP_LDAP:
(void) nss_search(&db_root, nss_ldap_passwd,
NSS_DBOP_PASSWD_BYUID, &arg);
break;
#ifdef PAM_NIS
case REP_NIS:
(void) nss_search(&db_root, nss_nis_passwd,
NSS_DBOP_PASSWD_BYUID, &arg);
break;
#endif
default:
return (NULL);
}
return (struct passwd *)NSS_XbyY_FINI(&arg);
}
static nss_XbyY_buf_t *spbuf;
static DEFINE_NSS_DB_ROOT(spdb_root);
#define GETSPBUF() \
NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW)
#pragma fini(endutilspent)
static void
endutilspent(void)
{
NSS_XbyY_FREE(&spbuf);
nss_delete(&spdb_root);
}
struct spwd *
getspnam_from(const char *name, pwu_repository_t *rep, int reptype)
{
nss_XbyY_buf_t *b = GETSPBUF();
nss_XbyY_args_t arg;
if (b == 0)
return (0);
NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd);
arg.key.name = name;
switch (reptype) {
case REP_LDAP:
(void) nss_search(&spdb_root, nss_ldap_shadow,
NSS_DBOP_SHADOW_BYNAME, &arg);
break;
#ifdef PAM_NIS
case REP_NIS:
(void) nss_search(&spdb_root, nss_nis_shadow,
NSS_DBOP_SHADOW_BYNAME, &arg);
break;
#endif
default:
return (NULL);
}
return (struct spwd *)NSS_XbyY_FINI(&arg);
}