1
0
This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
TP-Link_Archer-XR500v/EN7526G_3.18Kernel_SDK/apps/public/wide-dhcpv6-20080615/dhcp6c_script.c
2024-07-22 01:58:46 -03:00

1144 lines
30 KiB
C
Executable File

/* $KAME: dhcp6c_script.c,v 1.11 2004/11/28 10:48:38 jinmei Exp $ */
/*
* Copyright (C) 2003 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <sys/stat.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include "dhcp6.h"
#include "config.h"
#include "common.h"
#if defined(TCSUPPORT_CT)
static char sipserver_str[] = "new_sip_servers";
static char sipname_str[] = "new_sip_name";
static char dnsserver_str[] = "new_domain_name_servers";
static char dnsname_str[] = "new_domain_name";
static char ntpserver_str[] = "new_ntp_servers";
static char nisserver_str[] = "new_nis_servers";
static char nisname_str[] = "new_nis_name";
static char nispserver_str[] = "new_nisp_servers";
static char nispname_str[] = "new_nisp_name";
static char bcmcsserver_str[] = "new_bcmcs_servers";
static char bcmcsname_str[] = "new_bcmcs_name";
/*add for output interface name, by jlliu*/
static char ifname_str[] = "interface";
#if defined(TCSUPPORT_CT_DSLITE)
static char dsliteaddr_str[] = "dslite_address";
static char dslitename_str[] = "dslite_name";
#endif
static char buf[BUFSIZ];
static char *iapd2str __P((int, struct dhcp6_listval *));
static char *iana2str __P((int, struct dhcp6_listval *));
int
client6_script(scriptpath, ifname, state, optinfo)
char *scriptpath;
char *ifname;
int state;
struct dhcp6_optinfo *optinfo;
{
int i = 0, j, dnsservers, ntpservers, dnsnamelen, envc, elen, ret = 0;
int sipservers, sipnamelen;
int nisservers, nisnamelen;
int nispservers, nispnamelen;
int bcmcsservers, bcmcsnamelen;
int ianas, ianalen;
int iapds, iapdlen;
char **envp, *s;
char reason[] = "REASON=NBI";
struct dhcp6_listval *v;
struct dhcp6_listval *iaaddr;
pid_t pid, wpid;
#if defined(TCSUPPORT_CT_DSLITE)
int dsliteaddrs = 0, dsliteaddrlen = 0;
int dslitenames = 0, dslitenamelen = 0;
#endif
/* if a script is not specified, do nothing */
if (scriptpath == NULL || strlen(scriptpath) == 0)
return -1;
/* initialize counters */
dnsservers = 0;
ntpservers = 0;
dnsnamelen = 0;
sipservers = 0;
sipnamelen = 0;
nisservers = 0;
nisnamelen = 0;
nispservers = 0;
nispnamelen = 0;
bcmcsservers = 0;
bcmcsnamelen = 0;
ianas = 0;
ianalen = 0;
iapds = 0;
iapdlen = 0;
envc = 2; /* we at least include the reason and the terminator */
if(optinfo != NULL){
/* count the number of variables */
for (v = TAILQ_FIRST(&optinfo->dns_list); v; v = TAILQ_NEXT(v, link))
dnsservers++;
envc += dnsservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->dnsname_list); v;
v = TAILQ_NEXT(v, link)) {
dnsnamelen += v->val_vbuf.dv_len;
}
envc += dnsnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->ntp_list); v; v = TAILQ_NEXT(v, link))
ntpservers++;
envc += ntpservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->sip_list); v; v = TAILQ_NEXT(v, link))
sipservers++;
envc += sipservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->sipname_list); v;
v = TAILQ_NEXT(v, link)) {
sipnamelen += v->val_vbuf.dv_len;
}
envc += sipnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nis_list); v; v = TAILQ_NEXT(v, link))
nisservers++;
envc += nisservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nisname_list); v;
v = TAILQ_NEXT(v, link)) {
nisnamelen += v->val_vbuf.dv_len;
}
envc += nisnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nisp_list); v; v = TAILQ_NEXT(v, link))
nispservers++;
envc += nispservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nispname_list); v;
v = TAILQ_NEXT(v, link)) {
nispnamelen += v->val_vbuf.dv_len;
}
envc += nispnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->bcmcs_list); v; v = TAILQ_NEXT(v, link))
bcmcsservers++;
envc += bcmcsservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->bcmcsname_list); v;
v = TAILQ_NEXT(v, link)) {
bcmcsnamelen += v->val_vbuf.dv_len;
}
envc += bcmcsnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->iapd_list); v; v = TAILQ_NEXT(v, link))
iapds++;
envc += iapds;
for (v = TAILQ_FIRST(&optinfo->iana_list); v; v = TAILQ_NEXT(v, link))
ianas++;
envc += ianas;
envc +=1; /*for interface name*/
#if defined(TCSUPPORT_CT_DSLITE)
for (v = TAILQ_FIRST(&optinfo->dsliteaddr_list); v; v = TAILQ_NEXT(v, link))
dsliteaddrs++;
envc += dsliteaddrs ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->dslitename_list); v; v = TAILQ_NEXT(v, link)) {
dslitenamelen += v->val_vbuf.dv_len;
dprintf(LOG_NOTICE, FNAME,
"get dslite name");
}
envc += dslitenamelen ? 1 : 0;
#endif
/* allocate an environments array */
if ((envp = malloc(sizeof (char *) * envc)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate environment buffer");
return -1;
}
memset(envp, 0, sizeof (char *) * envc);
/*
* Copy the parameters as environment variables
*/
i = 0;
/* reason */
if ((envp[i++] = strdup(reason)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate reason strings");
ret = -1;
goto clean;
}
/* "var=addr1 addr2 ... addrN" + null char for termination */
if (dnsservers) {
elen = sizeof (dnsserver_str) +
(INET6_ADDRSTRLEN + 1) * dnsservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dnsserver_str);
for (v = TAILQ_FIRST(&optinfo->dns_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (ntpservers) {
elen = sizeof (ntpserver_str) +
(INET6_ADDRSTRLEN + 1) * ntpservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NTP servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", ntpserver_str);
for (v = TAILQ_FIRST(&optinfo->ntp_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (dnsnamelen) {
elen = sizeof (dnsname_str) + dnsnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dnsname_str);
for (v = TAILQ_FIRST(&optinfo->dnsname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (sipservers) {
elen = sizeof (sipserver_str) +
(INET6_ADDRSTRLEN + 1) * sipservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for SIP servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", sipserver_str);
for (v = TAILQ_FIRST(&optinfo->sip_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (sipnamelen) {
elen = sizeof (sipname_str) + sipnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for SIP domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", sipname_str);
for (v = TAILQ_FIRST(&optinfo->sipname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (nisservers) {
elen = sizeof (nisserver_str) +
(INET6_ADDRSTRLEN + 1) * nisservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nisserver_str);
for (v = TAILQ_FIRST(&optinfo->nis_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (nisnamelen) {
elen = sizeof (nisname_str) + nisnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nisname_str);
for (v = TAILQ_FIRST(&optinfo->nisname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (nispservers) {
elen = sizeof (nispserver_str) +
(INET6_ADDRSTRLEN + 1) * nispservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS+ servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nispserver_str);
for (v = TAILQ_FIRST(&optinfo->nisp_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (nispnamelen) {
elen = sizeof (nispname_str) + nispnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS+ domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nispname_str);
for (v = TAILQ_FIRST(&optinfo->nispname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (bcmcsservers) {
elen = sizeof (bcmcsserver_str) +
(INET6_ADDRSTRLEN + 1) * bcmcsservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for BCMC servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", bcmcsserver_str);
for (v = TAILQ_FIRST(&optinfo->bcmcs_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (bcmcsnamelen) {
elen = sizeof (bcmcsname_str) + bcmcsnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for BCMC domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", bcmcsname_str);
for (v = TAILQ_FIRST(&optinfo->bcmcsname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
/* IAs */
j = 0;
for (v = TAILQ_FIRST(&optinfo->iapd_list); v;
v = TAILQ_NEXT(v, link)) {
if ((s = envp[i++] = iapd2str(j++, v)) == NULL) {
ret = -1;
goto clean;
}
}
j = 0;
for (v = TAILQ_FIRST(&optinfo->iana_list); v;
v = TAILQ_NEXT(v, link)) {
if ((s = envp[i++] = iana2str(j++, v)) == NULL) {
ret = -1;
goto clean;
}
}
#if defined(TCSUPPORT_CT_DSLITE)
if (dsliteaddrs) {
elen = sizeof (dsliteaddr_str) +
(INET6_ADDRSTRLEN + 1) * dsliteaddrs + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for dslite address");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dsliteaddr_str);
for (v = TAILQ_FIRST(&optinfo->dsliteaddr_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (dslitenamelen) {
elen = sizeof (dslitename_str) + dslitenamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dslitename_str);
for (v = TAILQ_FIRST(&optinfo->dslitename_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
#endif
}
else{
envc +=1; /*for interface name*/
dprintf(LOG_ERR, FNAME, "script Step 0,envc:%d", envc);
/* allocate an environments array */
if ((envp = malloc(sizeof (char *) * envc)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate environment buffer");
return -1;
}
memset(envp, 0, sizeof (char *) * envc);
/*
* Copy the parameters as environment variables
*/
i = 0;
/* reason */
if ((envp[i++] = strdup(reason)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate reason strings");
ret = -1;
goto clean;
}
}
if(ifname == NULL || strlen(ifname) == 0){
dprintf(LOG_ERR, FNAME, "failed to get interface name!");
goto clean;
}
elen = sizeof (ifname_str) + 1 + strlen(ifname) + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate string for interface name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=%s", ifname_str, ifname);
/* launch the script */
pid = fork();
if (pid < 0) {
dprintf(LOG_ERR, FNAME, "failed to fork: %s", strerror(errno));
ret = -1;
goto clean;
} else if (pid) {
int wstatus;
do {
wpid = wait(&wstatus);
} while (wpid != pid && wpid > 0);
if (wpid < 0)
dprintf(LOG_ERR, FNAME, "wait: %s", strerror(errno));
else {
dprintf(LOG_DEBUG, FNAME,
"script \"%s\" terminated", scriptpath);
}
} else {
char *argv[3];
char str_state[10] = {0};
int fd;
argv[0] = scriptpath;
if(DHCP6S_RELEASE == state){
strcpy(str_state, "release");
}
else{
strcpy(str_state, "renew");
}
argv[1] = str_state;
argv[2] = NULL;
if (safefile(scriptpath)) {
dprintf(LOG_ERR, FNAME,
"script \"%s\" cannot be executed safely",
scriptpath);
//exit(1);
}
if (foreground == 0 && (fd = open("/dev/null", O_RDWR)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
}
execve(scriptpath, argv, envp);
dprintf(LOG_ERR, FNAME, "child: exec failed: %s",
strerror(errno));
exit(0);
}
clean:
for (i = 0; i < envc; i++)
free(envp[i]);
free(envp);
return ret;
}
static char *
iapd2str(num, iav)
int num;
struct dhcp6_listval *iav;
{
struct dhcp6_listval *siav;
char *s, *r, *comma;
s = buf;
memset(s, 0, BUFSIZ);
snprintf(s, BUFSIZ, "iapd_%d=", num);
comma = "";
for (siav = TAILQ_FIRST(&iav->sublist); siav;
siav = TAILQ_NEXT(siav, link)) {
switch (siav->type) {
case DHCP6_LISTVAL_PREFIX6:
snprintf(s + strlen(s), BUFSIZ - strlen(s),
"%s%s/%d", comma,
in6addr2str(&siav->val_prefix6.addr, 0),
siav->val_prefix6.plen);
comma = ",";
break;
case DHCP6_LISTVAL_STCODE:
#if 0
snprintf(s + strlen(s), BUFSIZ - strlen(s),
"%s#%d", comma, siav->val_num16);
comma = ",";
#endif
break;
default:
dprintf(LOG_ERR, FNAME, "impossible subopt");
}
}
if ((r = strdup(s)) == NULL)
dprintf(LOG_ERR, FNAME, "failed to allocate iapd_%d", num);
dprintf(LOG_ERR, FNAME, "\n%s\n", r);
return r;
}
static char *
iana2str(num, iav)
int num;
struct dhcp6_listval *iav;
{
struct dhcp6_listval *siav;
char *s, *r, *comma;
s = buf;
memset(s, 0, BUFSIZ);
snprintf(s, BUFSIZ, "iana_%d=", num);
comma = "";
for (siav = TAILQ_FIRST(&iav->sublist); siav;
siav = TAILQ_NEXT(siav, link)) {
switch (siav->type) {
case DHCP6_LISTVAL_STATEFULADDR6:
snprintf(s + strlen(s), BUFSIZ - strlen(s),
"%s%s", comma,
in6addr2str(&siav->val_statefuladdr6.addr, 0));
comma = ",";
break;
case DHCP6_LISTVAL_STCODE:
#if 0
snprintf(s + strlen(s), BUFSIZ - strlen(s),
"%s#%d", comma, siav->val_num16);
comma = ",";
#endif
break;
default:
dprintf(LOG_ERR, FNAME, "impossible subopt");
}
}
if ((r = strdup(s)) == NULL)
dprintf(LOG_ERR, FNAME, "failed to allocate iana_%d", num);
dprintf(LOG_ERR, FNAME, "\n%s\n", r);
return r;
}
#else
static char sipserver_str[] = "new_sip_servers";
static char sipname_str[] = "new_sip_name";
static char dnsserver_str[] = "new_domain_name_servers";
static char dnsname_str[] = "new_domain_name";
static char ntpserver_str[] = "new_ntp_servers";
static char nisserver_str[] = "new_nis_servers";
static char nisname_str[] = "new_nis_name";
static char nispserver_str[] = "new_nisp_servers";
static char nispname_str[] = "new_nisp_name";
static char bcmcsserver_str[] = "new_bcmcs_servers";
static char bcmcsname_str[] = "new_bcmcs_name";
#if defined(TCSUPPORT_CT_DHCP6_OPTION)
static char vendoropts_str[] = "vendor_opts";
#endif
#ifdef TCSUPPORT_DSLITE
static char dsliteaddr_str[] = "dslite_address";
static char dslitename_str[] = "dslite_name";
#endif
int
client6_script(scriptpath, infopath, state, optinfo)
char *scriptpath;
char *infopath;
int state;
struct dhcp6_optinfo *optinfo;
{
int i, dnsservers, ntpservers, dnsnamelen, envc, elen, ret = 0;
int sipservers, sipnamelen;
int nisservers, nisnamelen;
int nispservers, nispnamelen;
int bcmcsservers, bcmcsnamelen;
#if defined(TCSUPPORT_CT_DHCP6_OPTION)
int vendoropts = 0, vendoroptslen = 0;
#endif
#ifdef TCSUPPORT_DSLITE
int dsliteaddrs = 0, dsliteaddrlen = 0;
int dslitenames = 0, dslitenamelen = 0;
#endif
char **envp, *s;
char reason[] = "REASON=NBI";
struct dhcp6_listval *v;
pid_t pid, wpid;
/* if a script is not specified, do nothing */
if (scriptpath == NULL || strlen(scriptpath) == 0)
return -1;
/* initialize counters */
dnsservers = 0;
ntpservers = 0;
dnsnamelen = 0;
sipservers = 0;
sipnamelen = 0;
nisservers = 0;
nisnamelen = 0;
nispservers = 0;
nispnamelen = 0;
bcmcsservers = 0;
bcmcsnamelen = 0;
envc = 2; /* we at least include the reason and the terminator */
/* count the number of variables */
for (v = TAILQ_FIRST(&optinfo->dns_list); v; v = TAILQ_NEXT(v, link))
dnsservers++;
envc += dnsservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->dnsname_list); v;
v = TAILQ_NEXT(v, link)) {
dnsnamelen += v->val_vbuf.dv_len;
}
envc += dnsnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->ntp_list); v; v = TAILQ_NEXT(v, link))
ntpservers++;
envc += ntpservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->sip_list); v; v = TAILQ_NEXT(v, link))
sipservers++;
envc += sipservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->sipname_list); v;
v = TAILQ_NEXT(v, link)) {
sipnamelen += v->val_vbuf.dv_len;
}
envc += sipnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nis_list); v; v = TAILQ_NEXT(v, link))
nisservers++;
envc += nisservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nisname_list); v;
v = TAILQ_NEXT(v, link)) {
nisnamelen += v->val_vbuf.dv_len;
}
envc += nisnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nisp_list); v; v = TAILQ_NEXT(v, link))
nispservers++;
envc += nispservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->nispname_list); v;
v = TAILQ_NEXT(v, link)) {
nispnamelen += v->val_vbuf.dv_len;
}
envc += nispnamelen ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->bcmcs_list); v; v = TAILQ_NEXT(v, link))
bcmcsservers++;
envc += bcmcsservers ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->bcmcsname_list); v;
v = TAILQ_NEXT(v, link)) {
bcmcsnamelen += v->val_vbuf.dv_len;
}
envc += bcmcsnamelen ? 1 : 0;
#if defined(TCSUPPORT_CT_DHCP6_OPTION)
for (v = TAILQ_FIRST(&optinfo->option17_list); v; v = TAILQ_NEXT(v, link)) {
vendoroptslen += v->val_vbuf.dv_len;
dprintf(LOG_NOTICE, FNAME,
"get vendor opts");
}
envc += vendoroptslen ? 1 : 0;
#endif
#ifdef TCSUPPORT_DSLITE
for (v = TAILQ_FIRST(&optinfo->dsliteaddr_list); v; v = TAILQ_NEXT(v, link))
dsliteaddrs++;
envc += dsliteaddrs ? 1 : 0;
for (v = TAILQ_FIRST(&optinfo->dslitename_list); v; v = TAILQ_NEXT(v, link)) {
dslitenamelen += v->val_vbuf.dv_len;
dprintf(LOG_NOTICE, FNAME,
"get dslite name");
}
envc += dslitenamelen ? 1 : 0;
#endif
/* allocate an environments array */
if ((envp = malloc(sizeof (char *) * envc)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate environment buffer");
return -1;
}
memset(envp, 0, sizeof (char *) * envc);
/*
* Copy the parameters as environment variables
*/
i = 0;
/* reason */
if ((envp[i++] = strdup(reason)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate reason strings");
ret = -1;
goto clean;
}
/* "var=addr1 addr2 ... addrN" + null char for termination */
if (dnsservers) {
elen = sizeof (dnsserver_str) +
(INET6_ADDRSTRLEN + 1) * dnsservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dnsserver_str);
for (v = TAILQ_FIRST(&optinfo->dns_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (ntpservers) {
elen = sizeof (ntpserver_str) +
(INET6_ADDRSTRLEN + 1) * ntpservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NTP servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", ntpserver_str);
for (v = TAILQ_FIRST(&optinfo->ntp_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (dnsnamelen) {
elen = sizeof (dnsname_str) + dnsnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dnsname_str);
for (v = TAILQ_FIRST(&optinfo->dnsname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (sipservers) {
elen = sizeof (sipserver_str) +
(INET6_ADDRSTRLEN + 1) * sipservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for SIP servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", sipserver_str);
for (v = TAILQ_FIRST(&optinfo->sip_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (sipnamelen) {
elen = sizeof (sipname_str) + sipnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for SIP domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", sipname_str);
for (v = TAILQ_FIRST(&optinfo->sipname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (nisservers) {
elen = sizeof (nisserver_str) +
(INET6_ADDRSTRLEN + 1) * nisservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nisserver_str);
for (v = TAILQ_FIRST(&optinfo->nis_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (nisnamelen) {
elen = sizeof (nisname_str) + nisnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nisname_str);
for (v = TAILQ_FIRST(&optinfo->nisname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (nispservers) {
elen = sizeof (nispserver_str) +
(INET6_ADDRSTRLEN + 1) * nispservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS+ servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nispserver_str);
for (v = TAILQ_FIRST(&optinfo->nisp_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (nispnamelen) {
elen = sizeof (nispname_str) + nispnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for NIS+ domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", nispname_str);
for (v = TAILQ_FIRST(&optinfo->nispname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
if (bcmcsservers) {
elen = sizeof (bcmcsserver_str) +
(INET6_ADDRSTRLEN + 1) * bcmcsservers + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for BCMC servers");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", bcmcsserver_str);
for (v = TAILQ_FIRST(&optinfo->bcmcs_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (bcmcsnamelen) {
elen = sizeof (bcmcsname_str) + bcmcsnamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for BCMC domain name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", bcmcsname_str);
for (v = TAILQ_FIRST(&optinfo->bcmcsname_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
#if defined(TCSUPPORT_CT_DHCP6_OPTION)
if (vendoroptslen) {
elen = sizeof (vendoropts_str) + vendoroptslen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for vendor opts");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", vendoropts_str);
for (v = TAILQ_FIRST(&optinfo->option17_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
#endif
#ifdef TCSUPPORT_DSLITE
if (dsliteaddrs) {
elen = sizeof (dsliteaddr_str) +
(INET6_ADDRSTRLEN + 1) * dsliteaddrs + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for dslite address");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dsliteaddr_str);
for (v = TAILQ_FIRST(&optinfo->dsliteaddr_list); v;
v = TAILQ_NEXT(v, link)) {
char *addr;
addr = in6addr2str(&v->val_addr6, 0);
strlcat(s, addr, elen);
strlcat(s, " ", elen);
}
}
if (dslitenamelen) {
elen = sizeof (dslitename_str) + dslitenamelen + 1;
if ((s = envp[i++] = malloc(elen)) == NULL) {
dprintf(LOG_NOTICE, FNAME,
"failed to allocate strings for DNS name");
ret = -1;
goto clean;
}
memset(s, 0, elen);
snprintf(s, elen, "%s=", dslitename_str);
for (v = TAILQ_FIRST(&optinfo->dslitename_list); v;
v = TAILQ_NEXT(v, link)) {
strlcat(s, v->val_vbuf.dv_buf, elen);
strlcat(s, " ", elen);
}
}
#endif
/* launch the script */
pid = fork();
if (pid < 0) {
dprintf(LOG_ERR, FNAME, "failed to fork: %s", strerror(errno));
ret = -1;
goto clean;
} else if (pid) {
int wstatus;
do {
wpid = wait(&wstatus);
} while (wpid != pid && wpid > 0);
if (wpid < 0)
dprintf(LOG_ERR, FNAME, "wait: %s", strerror(errno));
else {
dprintf(LOG_DEBUG, FNAME,
"script \"%s\" terminated", scriptpath);
}
} else {
char *argv[2];
int fd;
argv[0] = scriptpath;
argv[1] = infopath;
argv[2] = NULL;
if (safefile(scriptpath)) {
dprintf(LOG_ERR, FNAME,
"script \"%s\" cannot be executed safely",
scriptpath);
//exit(1);
}
if (foreground == 0 && (fd = open("/dev/null", O_RDWR)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
}
execve(scriptpath, argv, envp);
dprintf(LOG_ERR, FNAME, "child: exec failed: %s",
strerror(errno));
exit(0);
}
clean:
for (i = 0; i < envc; i++)
free(envp[i]);
free(envp);
return ret;
}
#endif