#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <rpcsvc/nlm_prot.h>
#include <sys/utsname.h>
#include <nfs/nfs.h>
#include "nfs_subr.h"
#include <errno.h>
#include <deflt.h>
#include <rpcsvc/daemon_utils.h>
#include "smfcfg.h"
#include <nfs/nfssys.h>
extern int _nfssys(enum nfssys_op, void *);
int
remote_lock(char *fshost, caddr_t fh)
{
nlm_testargs rlm_args;
nlm_res rlm_res;
struct timeval timeout = { 5, 0};
CLIENT *cl;
enum clnt_stat rpc_stat;
struct utsname myid;
(void) memset((char *)&rlm_args, 0, sizeof (nlm_testargs));
(void) memset((char *)&rlm_res, 0, sizeof (nlm_res));
if (uname(&myid) == -1) {
rlm_args.alock.caller_name = fshost;
} else {
rlm_args.alock.caller_name = myid.nodename;
}
rlm_args.alock.fh.n_len = sizeof (fhandle_t);
rlm_args.alock.fh.n_bytes = fh;
cl = clnt_create(fshost, NLM_PROG, NLM_VERS, "datagram_v");
if (cl == NULL)
return (0);
rpc_stat = clnt_call(cl, NLM_GRANTED,
xdr_nlm_testargs, (caddr_t)&rlm_args,
xdr_nlm_res, (caddr_t)&rlm_res, timeout);
clnt_destroy(cl);
return (rpc_stat == RPC_CANTDECODEARGS);
}
#define fromhex(c) ((c >= '0' && c <= '9') ? (c - '0') : \
((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
void
URLparse(char *str)
{
char *p, *q;
p = q = str;
while (*p) {
*q = *p;
if (*p++ == '%') {
if (*p) {
*q = fromhex(*p) * 16;
p++;
if (*p) {
*q += fromhex(*p);
p++;
}
}
}
q++;
}
*q = '\0';
}
int
convert_special(char **specialp, char *host, char *oldpath, char *newpath,
char *cur_special)
{
char *url;
char *newspec;
char *p;
char *p1, *p2;
url = malloc(strlen("nfs:") + strlen(oldpath) + 1);
if (url == NULL)
return (-1);
strcpy(url, "nfs:");
strcat(url, oldpath);
if (*specialp == NULL) {
newspec = *specialp = strdup(cur_special);
if (newspec == NULL) {
free(url);
return (-1);
}
} else {
newspec = *specialp;
}
p = strstr(newspec, url);
if (p == NULL) {
free(url);
return (-1);
}
p1 = p;
p2 = host;
for (;;) {
if (*p1 == '\0') {
free(url);
free(*specialp);
*specialp = NULL;
return (-1);
}
if (*p2 == '\0') {
break;
}
*p1 = *p2;
p1++;
p2++;
}
*p1 = ':';
p1++;
p2 = newpath;
for (;;) {
if (*p1 == '\0') {
free(url);
free(*specialp);
*specialp = NULL;
return (-1);
}
if (*p2 == '\0') {
break;
}
*p1 = *p2;
p1++;
p2++;
}
p2 = p + strlen(url);
for (;;) {
if (*p1 == '\0') {
free(url);
free(*specialp);
*specialp = NULL;
return (-1);
}
if (*p2 == '\0') {
break;
}
*p1 = *p2;
p1++;
p2++;
}
*p1 = '\0';
free(url);
return (0);
}
#define AUTOFS_MOUNT_TIMEOUT 600
void
set_nfsv4_ephemeral_mount_to(void)
{
char valbuf[6];
int bufsz = sizeof (valbuf);
uint_t mount_to = AUTOFS_MOUNT_TIMEOUT;
if (autofs_smf_get_prop("timeout", valbuf, DEFAULT_INSTANCE,
SCF_TYPE_INTEGER, AUTOMOUNTD, &bufsz) == SA_OK) {
const char *errstr;
uint_t val = strtonum(valbuf, 0, UINT_MAX, &errstr);
if (errstr == NULL)
mount_to = val;
}
(void) _nfssys(NFS4_EPHEMERAL_MOUNT_TO, &mount_to);
}