#include <rpcsvc/bootparam_prot.h>
#include <netdb.h>
#include <nlist.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <nsswitch.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#define KERNEL
#include <sys/stream.h>
#include <net/route.h>
#undef KERNEL
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include "bootparam_private.h"
#define LINESIZE 1024
extern int getdomainname(char *, int);
extern int bootparams_getbyname(char *, char *, int);
static char *wildcard = "*";
static char domainkey[] = "domain=";
static void getf_printres(bp_getfile_res *);
static void copydomain(char *, char *, int);
bp_whoami_res *
bootparamproc_whoami_1(bp_whoami_arg *argp, CLIENT *cl)
{
static bp_whoami_res res;
struct in_addr clnt_addr;
struct in_addr route_addr;
struct hostent *hp;
static char clnt_entry[LINESIZE];
static char domain[MAX_MACHINE_NAME];
char *cp;
if (argp->client_address.address_type != IP_ADDR_TYPE) {
if (debug) {
msgout("Whoami failed: unknown address type %d",
argp->client_address.address_type);
}
return (NULL);
}
(void) memcpy(&clnt_addr, &argp->client_address.bp_address_u.ip_addr,
sizeof (clnt_addr));
hp = gethostbyaddr((char *)&clnt_addr, sizeof (clnt_addr), AF_INET);
if (hp == NULL) {
if (debug) {
msgout("Whoami failed: gethostbyaddr for %s.",
inet_ntoa(clnt_addr));
}
return (NULL);
}
if ((bootparams_getbyname(hp->h_name, clnt_entry,
sizeof (clnt_entry)) != __NSW_SUCCESS) &&
(bootparams_getbyname(wildcard, clnt_entry,
sizeof (clnt_entry)) != __NSW_SUCCESS))
return (NULL);
res.client_name = hp->h_name;
if (cp = strstr(clnt_entry, domainkey)) {
copydomain(cp + sizeof (domainkey) - 1, domain,
sizeof (domain));
} else {
if ((bootparams_getbyname(wildcard, clnt_entry,
sizeof (clnt_entry)) == __NSW_SUCCESS) &&
(cp = strstr(clnt_entry, domainkey))) {
copydomain(cp + sizeof (domainkey) - 1, domain,
sizeof (domain));
} else {
(void) getdomainname(domain, sizeof (domain));
}
}
res.domain_name = domain;
res.router_address.address_type = IP_ADDR_TYPE;
route_addr.s_addr = get_ip_route(clnt_addr);
(void) memcpy(&res.router_address.bp_address_u.ip_addr,
&route_addr,
sizeof (res.router_address.bp_address_u.ip_addr));
if (debug) {
struct in_addr in;
(void) memcpy(&in.s_addr,
&res.router_address.bp_address_u.ip_addr,
sizeof (in.s_addr));
msgout("Whoami returning name = %s, router address = %s",
res.client_name,
inet_ntoa(in));
}
return (&res);
}
bp_getfile_res *
bootparamproc_getfile_1(bp_getfile_arg *argp, CLIENT *cl)
{
static bp_getfile_res res;
static char clnt_entry[LINESIZE];
struct hostent *hp;
char *cp;
char filekey[LINESIZE];
char *server_hostname;
char *path_on_server;
int do_wildcard = 0;
static char *zero_len_string = "";
(void) strncpy(filekey, argp->file_id, sizeof (filekey) - 2);
filekey[sizeof (filekey) - 2] = '\0';
(void) strcat(filekey, "=");
if (bootparams_getbyname(argp->client_name, clnt_entry,
sizeof (clnt_entry)) == __NSW_SUCCESS) {
cp = strstr(clnt_entry, filekey);
if (cp == NULL)
do_wildcard++;
} else
do_wildcard++;
if (do_wildcard) {
if (bootparams_getbyname(wildcard, clnt_entry,
sizeof (clnt_entry)) != __NSW_SUCCESS)
return (NULL);
cp = strstr(clnt_entry, filekey);
if (cp == NULL)
return (NULL);
}
cp = strchr(cp, '=');
if (cp == NULL)
return (NULL);
cp++;
if (*cp == '\0')
return (NULL);
server_hostname = cp;
cp = strchr(server_hostname, ':');
if (cp == NULL)
return (NULL);
*cp = '\0';
cp++;
path_on_server = strtok(cp, "\t\n ");
if (path_on_server == NULL)
path_on_server = zero_len_string;
res.server_name = server_hostname;
res.server_path = path_on_server;
if (*res.server_name == 0) {
res.server_address.address_type = IP_ADDR_TYPE;
(void) memset(&res.server_address.bp_address_u.ip_addr, 0,
sizeof (res.server_address.bp_address_u.ip_addr));
} else {
in_addr_t addr;
if ((hp = gethostbyname(server_hostname)) != NULL) {
addr = find_best_server_int(hp->h_addr_list,
argp->client_name);
} else {
addr = inet_addr(server_hostname);
if (addr == INADDR_BROADCAST) {
if (debug) {
msgout("getfile_1: gethostbyname(%s) "
"failed", res.server_name);
}
return (NULL);
}
}
res.server_address.address_type = IP_ADDR_TYPE;
(void) memcpy(&res.server_address.bp_address_u.ip_addr,
&addr, sizeof (res.server_address.bp_address_u.ip_addr));
}
if (debug) {
getf_printres(&res);
}
return (&res);
}
void
getf_printres(bp_getfile_res *res)
{
struct in_addr in;
(void) memcpy(&in.s_addr, &res->server_address.bp_address_u.ip_addr,
sizeof (in.s_addr));
msgout("getfile_1: file is \"%s\" %s \"%s\"",
res->server_name,
inet_ntoa(in),
res->server_path);
}
void
copydomain(char *source, char *target, int len)
{
int n; ;
len--;
if (source)
for (n = 0; *source != '\0' && n < len; n++)
if (isspace((int)*source))
break;
else
*target++ = *source++;
*target = '\0';
}