Store the leasefile in the state directory by default, since a state

directory is now the normal mode of operation because of RFC4361.
This commit is contained in:
Nicholas J. Kain 2014-03-19 04:12:24 -04:00
parent b1e1ccf7c3
commit e188658c4c
6 changed files with 52 additions and 50 deletions

View File

@ -42,36 +42,9 @@
#include "io.h" #include "io.h"
#include "ndhc.h" #include "ndhc.h"
static char clientid_path[PATH_MAX] = "/etc/ndhc";
void set_clientid_path(char *df)
{
strnkcpy(clientid_path, df, sizeof clientid_path);
}
static void fail_if_clientid_path_dne(void)
{
if (strlen(clientid_path) == 0) {
log_error("clientid path is empty; it must be specified");
exit(EXIT_FAILURE);
}
struct stat st;
if (stat(clientid_path, &st) < 0) {
log_error("failed to stat clientid path '%s': %s",
clientid_path, strerror(errno));
exit(EXIT_FAILURE);
}
if (!S_ISDIR(st.st_mode)) {
log_error("clientid path '%s' does not specify a directory",
clientid_path);
exit(EXIT_FAILURE);
}
}
static void get_duid_path(char *duidfile, size_t dlen) static void get_duid_path(char *duidfile, size_t dlen)
{ {
fail_if_clientid_path_dne(); int splen = snprintf(duidfile, sizeof dlen, "%s/DUID", state_dir);
int splen = snprintf(duidfile, sizeof dlen, "%s/DUID", clientid_path);
if (splen < 0) { if (splen < 0) {
log_line("%s: snprintf failed; return=%d", __func__, splen); log_line("%s: snprintf failed; return=%d", __func__, splen);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -86,7 +59,6 @@ static void get_duid_path(char *duidfile, size_t dlen)
static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr, static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr,
size_t hwaddrlen) size_t hwaddrlen)
{ {
fail_if_clientid_path_dne();
if (hwaddrlen != 6) { if (hwaddrlen != 6) {
log_line("%s: Hardware address length=%u != 6 bytes", log_line("%s: Hardware address length=%u != 6 bytes",
__func__, hwaddrlen); __func__, hwaddrlen);
@ -95,7 +67,7 @@ static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr,
int splen = snprintf int splen = snprintf
(iaidfile, sizeof ilen, (iaidfile, sizeof ilen,
"%s/IAID-%.2hhx:%.2hhx:%.2hhx:%.2hhx:%.2hhx:%.2hhx", "%s/IAID-%.2hhx:%.2hhx:%.2hhx:%.2hhx:%.2hhx:%.2hhx",
clientid_path, hwaddr[0], hwaddr[1], hwaddr[2], state_dir, hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5]); hwaddr[3], hwaddr[4], hwaddr[5]);
if (splen < 0) { if (splen < 0) {
log_line("%s: snprintf failed; return=%d", __func__, splen); log_line("%s: snprintf failed; return=%d", __func__, splen);

View File

@ -30,7 +30,6 @@
#include "ndhc.h" #include "ndhc.h"
void set_clientid_path(char *df);
void get_clientid(struct client_state_t *cs, struct client_config_t *cc); void get_clientid(struct client_state_t *cs, struct client_config_t *cc);
#endif /* NJK_NDHC_DUIAID_H_ */ #endif /* NJK_NDHC_DUIAID_H_ */

View File

@ -27,6 +27,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
@ -37,27 +38,39 @@
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include "leasefile.h" #include "leasefile.h"
#include "ndhc.h"
#include "log.h" #include "log.h"
#include "strl.h" #include "strl.h"
#include "io.h" #include "io.h"
#include "defines.h" #include "defines.h"
static char leasefile[PATH_MAX] = "\0";
static int leasefilefd = -1; static int leasefilefd = -1;
void set_leasefile(char *lf) static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname)
{ {
strnkcpy(leasefile, lf, sizeof leasefile); int splen = snprintf(leasefile, sizeof dlen, "%s/LEASE-%s",
state_dir, ifname);
if (splen < 0) {
log_line("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, splen);
exit(EXIT_FAILURE);
}
if ((size_t)splen >= sizeof dlen) {
log_line("%s: (%s) snprintf dest buffer too small %d >= %u",
client_config.interface, __func__, splen, sizeof dlen);
exit(EXIT_FAILURE);
}
} }
void open_leasefile(void) void open_leasefile(void)
{ {
if (strlen(leasefile) > 0) { char leasefile[PATH_MAX];
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644); get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
if (leasefilefd < 0) { leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
log_line("Failed to create lease file (%s).", leasefile); if (leasefilefd < 0) {
exit(EXIT_FAILURE); log_line("%s: Failed to create lease file '%s': %s",
} client_config.interface, leasefile, strerror(errno));
exit(EXIT_FAILURE);
} }
} }
@ -76,13 +89,15 @@ void write_leasefile(struct in_addr ipnum)
case -1: case -1:
if (errno == EINTR) if (errno == EINTR)
goto retry_trunc; goto retry_trunc;
log_warning("Failed to truncate lease file."); log_warning("%s: Failed to truncate lease file: %s",
client_config.interface, strerror(errno));
return; return;
} }
lseek(leasefilefd, 0, SEEK_SET); lseek(leasefilefd, 0, SEEK_SET);
ret = safe_write(leasefilefd, ip, strlen(ip)); ret = safe_write(leasefilefd, ip, strlen(ip));
if (ret == -1) if (ret == -1)
log_warning("Failed to write ip to lease file."); log_warning("%s: Failed to write ip to lease file.",
client_config.interface);
else else
fsync(leasefilefd); fsync(leasefilefd);
} }

View File

@ -29,7 +29,6 @@
#ifndef NJK_NDHC_LEASEFILE_H_ #ifndef NJK_NDHC_LEASEFILE_H_
#define NJK_NDHC_LEASEFILE_H_ #define NJK_NDHC_LEASEFILE_H_
void set_leasefile(char *lf);
void open_leasefile(void); void open_leasefile(void);
void write_leasefile(struct in_addr ipnum); void write_leasefile(struct in_addr ipnum);

View File

@ -107,7 +107,6 @@ static void show_usage(void)
" immediately negotiated.\n" " immediately negotiated.\n"
" -p, --pidfile=FILE File where the ndhc pid will be written\n" " -p, --pidfile=FILE File where the ndhc pid will be written\n"
" -P, --ifch-pidfile=FILE File where the ndhc-ifch pid will be written\n" " -P, --ifch-pidfile=FILE File where the ndhc-ifch pid will be written\n"
" -l, --leasefile=FILE File to which the lease IP will be written\n"
" -i, --interface=INTERFACE Interface to use (default: eth0)\n" " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
" -n, --now Exit with failure if lease cannot be\n" " -n, --now Exit with failure if lease cannot be\n"
" immediately negotiated.\n" " immediately negotiated.\n"
@ -228,6 +227,25 @@ static int get_clientid_string(char *str, size_t slen)
return 1; return 1;
} }
static void fail_if_state_dir_dne(void)
{
if (strlen(state_dir) == 0) {
log_error("state_dir path is empty; it must be specified");
exit(EXIT_FAILURE);
}
struct stat st;
if (stat(state_dir, &st) < 0) {
log_error("failed to stat state_dir path '%s': %s",
state_dir, strerror(errno));
exit(EXIT_FAILURE);
}
if (!S_ISDIR(st.st_mode)) {
log_error("state_dir path '%s' does not specify a directory",
state_dir);
exit(EXIT_FAILURE);
}
}
static void handle_ifch_message(void) static void handle_ifch_message(void)
{ {
char c; char c;
@ -321,6 +339,7 @@ jumpstart:
} }
} }
char state_dir[MAX_PATH_LENGTH] = "/etc/ndhc";
char chroot_dir[MAX_PATH_LENGTH] = ""; char chroot_dir[MAX_PATH_LENGTH] = "";
char resolv_conf_d[MAX_PATH_LENGTH] = ""; char resolv_conf_d[MAX_PATH_LENGTH] = "";
static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT; static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
@ -408,7 +427,6 @@ int main(int argc, char **argv)
{"background", no_argument, 0, 'b'}, {"background", no_argument, 0, 'b'},
{"pidfile", required_argument, 0, 'p'}, {"pidfile", required_argument, 0, 'p'},
{"ifch-pidfile", required_argument, 0, 'P'}, {"ifch-pidfile", required_argument, 0, 'P'},
{"leasefile", required_argument, 0, 'l'},
{"hostname", required_argument, 0, 'h'}, {"hostname", required_argument, 0, 'h'},
{"interface", required_argument, 0, 'i'}, {"interface", required_argument, 0, 'i'},
{"now", no_argument, 0, 'n'}, {"now", no_argument, 0, 'n'},
@ -435,7 +453,7 @@ int main(int argc, char **argv)
while (1) { while (1) {
int c; int c;
c = getopt_long(argc, argv, "c:fbp:P:l:h:i:nqr:V:u:U:C:s:Sdw:W:m:M:t:R:Hv?", c = getopt_long(argc, argv, "c:fbp:P:h:i:nqr:V:u:U:C:s:Sdw:W:m:M:t:R:Hv?",
arg_options, NULL); arg_options, NULL);
if (c == -1) break; if (c == -1) break;
@ -457,9 +475,6 @@ int main(int argc, char **argv)
case 'P': case 'P':
strnkcpy(pidfile_ifch, optarg, sizeof pidfile_ifch); strnkcpy(pidfile_ifch, optarg, sizeof pidfile_ifch);
break; break;
case 'l':
set_leasefile(optarg);
break;
case 'h': case 'h':
strnkcpy(client_config.hostname, optarg, strnkcpy(client_config.hostname, optarg,
sizeof client_config.hostname); sizeof client_config.hostname);
@ -515,7 +530,7 @@ int main(int argc, char **argv)
strnkcpy(chroot_dir, optarg, sizeof chroot_dir); strnkcpy(chroot_dir, optarg, sizeof chroot_dir);
break; break;
case 's': case 's':
set_clientid_path(optarg); strnkcpy(state_dir, optarg, sizeof state_dir);
break; break;
case 'S': case 'S':
seccomp_enforce = true; seccomp_enforce = true;
@ -610,6 +625,7 @@ int main(int argc, char **argv)
suicide("FATAL - I need to be started as root."); suicide("FATAL - I need to be started as root.");
if (!strncmp(chroot_dir, "", sizeof chroot_dir)) if (!strncmp(chroot_dir, "", sizeof chroot_dir))
suicide("FATAL - No chroot path specified. Refusing to run."); suicide("FATAL - No chroot path specified. Refusing to run.");
fail_if_state_dir_dne();
if (nl_getifdata() < 0) { if (nl_getifdata() < 0) {
log_line("FATAL - failed to get interface MAC or index"); log_line("FATAL - failed to get interface MAC or index");

View File

@ -71,6 +71,7 @@ extern int pToIfchR;
extern int pToIfchW; extern int pToIfchW;
extern int pToNdhcR; extern int pToNdhcR;
extern int pToNdhcW; extern int pToNdhcW;
extern char state_dir[MAX_PATH_LENGTH];
extern char chroot_dir[MAX_PATH_LENGTH]; extern char chroot_dir[MAX_PATH_LENGTH];
extern char resolv_conf_d[MAX_PATH_LENGTH]; extern char resolv_conf_d[MAX_PATH_LENGTH];