#include <config.h>
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IN6_H
#include <netinet/in6.h>
#endif
#ifdef HAVE_NETINET6_IN6_H
#include <netinet6/in6.h>
#endif
#ifdef HAVE_RPCSVC_YPCLNT_H
#include <rpcsvc/ypclnt.h>
#endif
#include "roken.h"
int __check_rhosts_file = 1;
char *__rcmd_errstr = 0;
static
int
__icheckhost(unsigned raddr, const char *lhost)
{
struct hostent *hp;
u_long laddr;
char **pp;
if (isdigit((unsigned char)*lhost)
&& (long)(laddr = inet_addr(lhost)) != -1)
return (raddr == laddr);
if ((hp = gethostbyname(lhost)) == NULL)
return (0);
for (pp = hp->h_addr_list; *pp; ++pp)
if (memcmp(&raddr, *pp, sizeof(u_long)) == 0)
return (1);
return (0);
}
static
int
__ivaliduser(FILE *hostf, unsigned raddr, const char *luser,
const char *ruser)
{
char *user, *p;
int ch;
char buf[MaxHostNameLen + 128];
char hname[MaxHostNameLen];
struct hostent *hp;
int userok = 0, hostok = 0;
#ifdef HAVE_YP_GET_DEFAULT_DOMAIN
char *ypdomain;
if (yp_get_default_domain(&ypdomain))
ypdomain = NULL;
#else
#define ypdomain NULL
#endif
if ((hp = gethostbyaddr((char *)&raddr,
sizeof(u_long),
AF_INET)) == NULL)
return (-1);
strlcpy(hname, hp->h_name, sizeof(hname));
while (fgets(buf, sizeof(buf), hostf)) {
p = buf;
if (strchr(p, '\n') == NULL) {
while ((ch = getc(hostf)) != '\n' && ch != EOF);
continue;
}
if (*p == '\n' || *p == '#') {
continue;
}
while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
if (isupper((unsigned char)*p))
*p = tolower((unsigned char)*p);
p++;
}
if (*p == ' ' || *p == '\t') {
*p++ = '\0';
while (*p == ' ' || *p == '\t')
p++;
user = p;
while (*p != '\n' && *p != ' ' &&
*p != '\t' && *p != '\0')
p++;
} else
user = p;
*p = '\0';
switch(buf[0]) {
case '+':
if (!buf[1]) {
hostok = 1;
break;
}
if (buf[1] == '@')
hostok = innetgr((char *)&buf[2],
(char *)&hname, NULL, ypdomain);
else
hostok = __icheckhost(raddr,(char *)&buf[1]);
break;
case '-':
if (buf[1] == '@') {
if (innetgr((char *)&buf[2],
(char *)&hname, NULL, ypdomain))
return(-1);
} else {
if (__icheckhost(raddr,(char *)&buf[1]))
return(-1);
}
break;
default:
hostok = __icheckhost(raddr, buf);
break;
}
switch(*user) {
case '+':
if (!*(user+1)) {
userok = 1;
break;
}
if (*(user+1) == '@')
userok = innetgr(user+2, NULL, (char *)ruser,
ypdomain);
else
userok = !(strcmp(ruser, user+1));
break;
case '-':
if (hostok) {
if (!*(user+1))
return(-1);
if (*(user+1) == '@') {
if (innetgr(user+2, NULL,
(char *)ruser, ypdomain))
return(-1);
} else {
if (!strcmp(ruser, user+1))
return(-1);
}
}
break;
default:
if (hostok)
userok = !(strcmp(ruser,*user ? user : luser));
break;
}
if (hostok && userok)
return(0);
}
return (-1);
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
iruserok(unsigned raddr, int superuser, const char *ruser, const char *luser)
{
char *cp;
struct stat sbuf;
struct passwd *pwd;
FILE *hostf;
uid_t uid;
int first;
char pbuf[MaxPathLen];
first = 1;
hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
again:
if (hostf) {
if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
fclose(hostf);
return (0);
}
fclose(hostf);
}
if (first == 1 && (__check_rhosts_file || superuser)) {
first = 0;
if ((pwd = k_getpwnam((char*)luser)) == NULL)
return (-1);
snprintf (pbuf, sizeof(pbuf), "%s/.rhosts", pwd->pw_dir);
uid = geteuid();
if (seteuid(pwd->pw_uid) < 0)
return (-1);
hostf = fopen(pbuf, "r");
seteuid(uid);
if (hostf == NULL)
return (-1);
cp = NULL;
if (lstat(pbuf, &sbuf) < 0)
cp = ".rhosts lstat failed";
else if (!S_ISREG(sbuf.st_mode))
cp = ".rhosts not regular file";
else if (fstat(fileno(hostf), &sbuf) < 0)
cp = ".rhosts fstat failed";
else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
cp = "bad .rhosts owner";
else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
cp = ".rhosts writeable by other than owner";
if (cp) {
__rcmd_errstr = cp;
fclose(hostf);
return (-1);
}
goto again;
}
return (-1);
}