dhcpcd/dhcpcd.c

590 lines
15 KiB
C
Raw Normal View History

2006-11-28 04:23:22 +08:00
/*
* dhcpcd - DHCP client daemon
* Copyright 2006-2007 Roy Marples <roy@marples.name>
2006-11-28 04:23:22 +08:00
*
* Distributed under the terms of the GNU General Public License v2
2006-11-28 04:23:22 +08:00
*/
/* We need to define this to get kill on GNU systems */
#ifdef __linux__
#define _BSD_SOURCE
#define _POSIX_SOURCE
#endif
#include <sys/file.h>
#include <sys/types.h>
2006-11-28 04:23:22 +08:00
#include <sys/stat.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <paths.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2007-05-11 00:08:49 +08:00
#include "config.h"
2006-11-28 04:23:22 +08:00
#include "client.h"
#include "dhcpcd.h"
#include "dhcp.h"
#include "interface.h"
#include "logger.h"
#include "version.h"
static int doversion = 0;
static int dohelp = 0;
#define EXTRA_OPTS
static const struct option longopts[] = {
{"arp", no_argument, NULL, 'a'},
{"script", required_argument, NULL, 'c'},
{"debug", no_argument, NULL, 'd'},
{"hostname", optional_argument, NULL, 'h'},
{"classid", optional_argument, NULL, 'i'},
{"release", no_argument, NULL, 'k'},
{"leasetime", required_argument, NULL, 'l'},
{"metric", required_argument, NULL, 'm'},
{"renew", no_argument, NULL, 'n'},
{"persistent", no_argument, NULL, 'p'},
{"inform", optional_argument, NULL, 's'},
{"request", optional_argument, NULL, 'r'},
{"timeout", required_argument, NULL, 't'},
{"userclass", required_argument, NULL, 'u'},
{"exit", no_argument, NULL, 'x'},
{"lastlease", no_argument, NULL, 'E'},
{"fqdn", required_argument, NULL, 'F'},
{"nogateway", no_argument, NULL, 'G'},
{"sethostname", no_argument, NULL, 'H'},
{"clientid", optional_argument, NULL, 'I'},
{"noipv4ll", no_argument, NULL, 'L'},
{"nomtu", no_argument, NULL, 'M'},
{"nontp", no_argument, NULL, 'N'},
{"nodns", no_argument, NULL, 'R'},
{"test", no_argument, NULL, 'T'},
{"nonis", no_argument, NULL, 'Y'},
{"help", no_argument, &dohelp, 1},
{"version", no_argument, &doversion, 1},
#ifdef THERE_IS_NO_FORK
{"daemonised", no_argument, NULL, 'f'},
{"skiproutes", required_argument, NULL, 'g'},
#endif
{NULL, 0, NULL, 0}
};
2006-11-28 04:23:22 +08:00
#define STRINGINT(_string, _int) { \
2007-04-11 21:18:33 +08:00
char *_tmp; \
long _number = strtol (_string, &_tmp, 0); \
errno = 0; \
if ((errno != 0 && _number == 0) || _string == _tmp || \
(errno == ERANGE && (_number == LONG_MAX || _number == LONG_MIN))) \
{ \
logger (LOG_ERR, "`%s' out of range", _string);; \
exit (EXIT_FAILURE); \
} \
else \
_int = (int) _number; \
2006-11-28 04:23:22 +08:00
}
#ifdef THERE_IS_NO_FORK
char dhcpcd[PATH_MAX];
char **dhcpcd_argv = NULL;
int dhcpcd_argc = 0;
char *dhcpcd_skiproutes = NULL;
#undef EXTRA_OPTS
#define EXTRA_OPTS "fg:"
#endif
static pid_t read_pid (const char *pidfile)
2006-11-28 04:23:22 +08:00
{
2007-04-11 21:18:33 +08:00
FILE *fp;
pid_t pid = 0;
2006-11-28 04:23:22 +08:00
2007-04-11 21:18:33 +08:00
if ((fp = fopen (pidfile, "r")) == NULL) {
errno = ENOENT;
return 0;
}
2006-11-28 04:23:22 +08:00
2007-04-11 21:18:33 +08:00
fscanf (fp, "%d", &pid);
fclose (fp);
2006-11-28 04:23:22 +08:00
2007-04-11 21:18:33 +08:00
return pid;
2006-11-28 04:23:22 +08:00
}
static void usage ()
{
printf ("usage: "PACKAGE" [-adknpEGHMNRTY] [-c script] [-h hostame] [-i classID]\n"
" [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n"
" [-t timeout] [-u userclass] [-F none | ptr | both]\n"
" [-I clientID] <interface>\n");
2006-11-28 04:23:22 +08:00
}
int main(int argc, char **argv)
{
options_t *options;
2007-04-11 21:18:33 +08:00
int userclasses = 0;
2007-05-14 21:34:36 +08:00
int opt;
2007-04-11 21:18:33 +08:00
int option_index = 0;
char *prefix;
2007-04-11 21:18:33 +08:00
pid_t pid;
int debug = 0;
int i;
int pidfd = -1;
int sig = 0;
2007-04-11 21:18:33 +08:00
/* Close any un-needed fd's */
for (i = getdtablesize() - 1; i >= 3; --i)
close (i);
2007-04-11 21:18:33 +08:00
openlog (PACKAGE, LOG_PID, LOG_LOCAL0);
options = xmalloc (sizeof (options_t));
memset (options, 0, sizeof (options_t));
options->script = (char *) DEFAULT_SCRIPT;
snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION);
options->classid_len = strlen (options->classid);
options->doarp = true;
options->dodns = true;
options->domtu = true;
options->donis = true;
options->dontp = true;
options->dogateway = true;
options->daemonise = true;
options->doinform = false;
options->doipv4ll = true;
options->timeout = DEFAULT_TIMEOUT;
gethostname (options->hostname, sizeof (options->hostname));
if (strcmp (options->hostname, "(none)") == 0 ||
strcmp (options->hostname, "localhost") == 0)
memset (options->hostname, 0, sizeof (options->hostname));
/* Don't set any optional arguments here so we retain POSIX
* compatibility with getopt */
while ((opt = getopt_long(argc, argv, EXTRA_OPTS
"c:dh:i:kl:m:npr:s:t:u:xAEF:GHI:LMNRTY",
2007-05-14 21:34:36 +08:00
longopts, &option_index)) != -1)
{
2007-05-14 21:34:36 +08:00
switch (opt) {
2007-04-11 21:18:33 +08:00
case 0:
if (longopts[option_index].flag)
break;
logger (LOG_ERR, "option `%s' should set a flag",
longopts[option_index].name);
exit (EXIT_FAILURE);
break;
case 'c':
options->script = optarg;
2007-04-11 21:18:33 +08:00
break;
case 'd':
debug++;
switch (debug) {
case 1:
setloglevel (LOG_DEBUG);
break;
case 2:
options->daemonise = false;
2007-04-11 21:18:33 +08:00
break;
}
break;
#ifdef THERE_IS_NO_FORK
case 'f':
options->daemonised = true;
close_fds ();
break;
case 'g':
dhcpcd_skiproutes = xstrdup (optarg);
break;
#endif
2007-04-11 21:18:33 +08:00
case 'h':
if (! optarg)
memset (options->hostname, 0, sizeof (options->hostname));
else if (strlen (optarg) > MAXHOSTNAMELEN) {
logger (LOG_ERR, "`%s' too long for HostName string, max is %d",
optarg, MAXHOSTNAMELEN);
2007-04-11 21:18:33 +08:00
exit (EXIT_FAILURE);
} else
strlcpy (options->hostname, optarg, sizeof (options->hostname));
2007-04-11 21:18:33 +08:00
break;
case 'i':
if (! optarg) {
memset (options->classid, 0, sizeof (options->classid));
options->classid_len = 0;
} else if (strlen (optarg) > CLASS_ID_MAX_LEN) {
2007-04-11 21:18:33 +08:00
logger (LOG_ERR, "`%s' too long for ClassID string, max is %d",
optarg, CLASS_ID_MAX_LEN);
exit (EXIT_FAILURE);
} else
options->classid_len = strlcpy (options->classid, optarg,
sizeof (options->classid));
2007-04-11 21:18:33 +08:00
break;
case 'k':
sig = SIGHUP;
2007-04-11 21:18:33 +08:00
break;
case 'l':
STRINGINT (optarg, options->leasetime);
if (options->leasetime <= 0) {
2007-04-11 21:18:33 +08:00
logger (LOG_ERR, "leasetime must be a positive value");
exit (EXIT_FAILURE);
}
break;
case 'm':
STRINGINT (optarg, options->metric);
2007-04-11 21:18:33 +08:00
break;
case 'n':
sig = SIGALRM;
2007-04-11 21:18:33 +08:00
break;
case 'p':
options->persistent = true;
2007-04-11 21:18:33 +08:00
break;
case 's':
options->doinform = true;
options->doarp = false;
if (! optarg || strlen (optarg) == 0) {
options->request_address.s_addr = 0;
break;
} else {
char *slash = strchr (optarg, '/');
if (slash) {
int cidr;
/* nullify the slash, so the -r option can read the
* address */
*slash++ = '\0';
if (sscanf (slash, "%d", &cidr) != 1 ||
inet_cidrtoaddr (cidr, &options->request_netmask) != 0) {
logger (LOG_ERR, "`%s' is not a valid CIDR", slash);
exit (EXIT_FAILURE);
}
}
/* fall through */
}
case 'r':
if (! options->doinform)
options->dorequest = true;
if (strlen (optarg) > 0 &&
! inet_aton (optarg, &options->request_address))
{
2007-04-11 21:18:33 +08:00
logger (LOG_ERR, "`%s' is not a valid IP address", optarg);
exit (EXIT_FAILURE);
}
break;
case 't':
STRINGINT (optarg, options->timeout);
if (options->timeout < 0) {
2007-04-11 21:18:33 +08:00
logger (LOG_ERR, "timeout must be a positive value");
exit (EXIT_FAILURE);
}
break;
case 'u':
{
int offset = 0;
for (i = 0; i < userclasses; i++)
offset += (int) options->userclass[offset] + 1;
2007-04-11 21:18:33 +08:00
if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) {
logger (LOG_ERR, "userclass overrun, max is %d",
USERCLASS_MAX_LEN);
exit (EXIT_FAILURE);
}
userclasses++;
memcpy (options->userclass + offset + 1 , optarg, strlen (optarg));
options->userclass[offset] = strlen (optarg);
options->userclass_len += (strlen (optarg)) + 1;
2007-04-11 21:18:33 +08:00
}
break;
case 'x':
sig = SIGTERM;
break;
case 'A':
#ifndef ENABLE_ARP
logger (LOG_ERR, "arp support not compiled into dhcpcd");
exit (EXIT_FAILURE);
#endif
options->doarp = false;
break;
case 'E':
#ifndef ENABLE_INFO
logger (LOG_ERR, "info support not compiled into dhcpcd");
exit (EXIT_FAILURE);
#endif
options->dolastlease = true;
break;
2007-04-11 21:18:33 +08:00
case 'F':
if (strncmp (optarg, "none", strlen (optarg)) == 0)
options->fqdn = FQDN_NONE;
else if (strncmp (optarg, "ptr", strlen (optarg)) == 0)
options->fqdn = FQDN_PTR;
else if (strncmp (optarg, "both", strlen (optarg)) == 0)
options->fqdn = FQDN_BOTH;
else {
logger (LOG_ERR, "invalid value `%s' for FQDN", optarg);
exit (EXIT_FAILURE);
}
2007-04-11 21:18:33 +08:00
break;
case 'G':
options->dogateway = false;
2007-04-11 21:18:33 +08:00
break;
case 'H':
options->dohostname++;
2007-04-11 21:18:33 +08:00
break;
case 'I':
if (optarg) {
if (strlen (optarg) > CLIENT_ID_MAX_LEN) {
logger (LOG_ERR, "`%s' is too long for ClientID, max is %d",
optarg, CLIENT_ID_MAX_LEN);
exit (EXIT_FAILURE);
}
options->clientid_len = strlcpy (options->clientid, optarg,
sizeof (options->clientid));
/* empty string disabled duid */
if (options->clientid_len == 0)
options->clientid_len = -1;
} else {
memset (options->clientid, 0, sizeof (options->clientid));
options->clientid_len = -1;
}
2007-04-11 21:18:33 +08:00
break;
case 'L':
options->doipv4ll = false;
break;
2007-04-11 21:18:33 +08:00
case 'M':
options->domtu = false;
2007-04-11 21:18:33 +08:00
break;
case 'N':
options->dontp = false;
2007-04-11 21:18:33 +08:00
break;
case 'R':
options->dodns = false;
2007-04-11 21:18:33 +08:00
break;
case 'T':
#ifndef ENABLE_INFO
logger (LOG_ERR, "info support not compiled into dhcpcd");
exit (EXIT_FAILURE);
#endif
options->test = true;
options->persistent = true;
break;
2007-04-11 21:18:33 +08:00
case 'Y':
options->donis = false;
2007-04-11 21:18:33 +08:00
break;
case '?':
usage ();
exit (EXIT_FAILURE);
default:
usage ();
exit (EXIT_FAILURE);
}
}
if (doversion) {
2007-04-11 21:18:33 +08:00
printf (""PACKAGE" "VERSION"\n");
printf ("Compile time options:"
#ifdef ENABLE_ARP
" ARP"
#endif
#ifdef ENABLE_DUID
" DUID"
#endif
#ifdef ENABLE_INFO
" INFO"
#endif
#ifdef ENABLE_INFO_COMPAT
" INFO_COMPAT"
#endif
#ifdef ENABLE_IPV4LL
" IPV4LL"
#endif
#ifdef ENABLE_NIS
" NIS"
#endif
#ifdef ENABLE_NTP
" NTP"
#endif
2007-11-08 18:37:52 +08:00
#ifdef ENABLE_ORC
" ORC"
#elif ENABLE_RC
" RC"
#endif
#ifdef ENABLE_RESOLVCONF
" RESOLVCONF"
#endif
#ifdef THERE_IS_NO_FORK
" THERE_IS_NO_FORK"
#endif
"\n");
}
2007-04-11 21:18:33 +08:00
if (dohelp)
usage ();
#ifdef THERE_IS_NO_FORK
dhcpcd_argv = argv;
dhcpcd_argc = argc;
/* We need the full path to the dhcpcd */
if (*argv[0] == '/')
strlcpy (dhcpcd, argv[0], sizeof (dhcpcd));
else {
char pwd[PATH_MAX];
if (! getcwd (pwd, PATH_MAX)) {
logger (LOG_ERR, "getcwd: %s", strerror (errno));
exit (EXIT_FAILURE);
}
snprintf (dhcpcd, sizeof (dhcpcd), "%s/%s", pwd, argv[0]);
}
#endif
2007-04-11 21:18:33 +08:00
if (optind < argc) {
if (strlen (argv[optind]) > IF_NAMESIZE) {
logger (LOG_ERR, "`%s' is too long for an interface name (max=%d)",
argv[optind], IF_NAMESIZE);
exit (EXIT_FAILURE);
}
strlcpy (options->interface, argv[optind],
sizeof (options->interface));
2007-04-11 21:18:33 +08:00
} else {
/* If only version was requested then exit now */
if (doversion || dohelp)
exit (EXIT_SUCCESS);
logger (LOG_ERR, "no interface specified");
exit (EXIT_FAILURE);
}
if (strchr (options->hostname, '.')) {
if (options->fqdn == FQDN_DISABLE)
options->fqdn = FQDN_BOTH;
} else
options->fqdn = FQDN_DISABLE;
if (options->request_address.s_addr == 0 && options->doinform) {
if ((options->request_address.s_addr = get_address (options->interface)) != 0)
options->keep_address = true;
}
if (IN_LINKLOCAL (options->request_address.s_addr)) {
logger (LOG_ERR, "you are not allowed to request a link local address");
exit (EXIT_FAILURE);
}
2007-04-11 21:18:33 +08:00
if (geteuid ()) {
logger (LOG_ERR, "you need to be root to run "PACKAGE);
exit (EXIT_FAILURE);
}
prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3));
snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface);
2007-04-11 21:18:33 +08:00
setlogprefix (prefix);
snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE,
options->interface);
free (prefix);
2007-04-11 21:18:33 +08:00
2007-06-26 20:02:26 +08:00
chdir ("/");
2007-04-11 21:18:33 +08:00
umask (022);
2007-04-11 21:18:33 +08:00
if (mkdir (CONFIGDIR, S_IRUSR |S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP
| S_IROTH | S_IXOTH) && errno != EEXIST )
{
logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", CONFIGDIR, strerror (errno));
exit (EXIT_FAILURE);
}
if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
| S_IROTH | S_IXOTH) && errno != EEXIST )
{
logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno));
exit (EXIT_FAILURE);
}
if (options->test) {
if (options->dorequest || options->doinform) {
logger (LOG_ERR, "cannot test with --inform or --request");
exit (EXIT_FAILURE);
}
2007-04-11 21:18:33 +08:00
if (options->dolastlease) {
logger (LOG_ERR, "cannot test with --lastlease");
exit (EXIT_FAILURE);
}
if (sig != 0) {
logger (LOG_ERR, "cannot test with --release or --renew");
exit (EXIT_FAILURE);
}
}
if (sig != 0) {
int killed = -1;
pid = read_pid (options->pidfile);
if (pid != 0)
logger (LOG_INFO, "sending signal %d to pid %d", sig, pid);
if (! pid || (killed = kill (pid, sig)))
logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, ""PACKAGE" not running");
if (pid != 0 && (sig != SIGALRM || killed != 0))
unlink (options->pidfile);
if (killed == 0)
exit (EXIT_SUCCESS);
if (sig != SIGALRM)
exit (EXIT_FAILURE);
}
2007-04-11 21:18:33 +08:00
if (! options->test && ! options->daemonised) {
if ((pid = read_pid (options->pidfile)) > 0 && kill (pid, 0) == 0) {
logger (LOG_ERR, ""PACKAGE" already running on pid %d (%s)",
pid, options->pidfile);
exit (EXIT_FAILURE);
}
pidfd = open (options->pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
if (pidfd == -1) {
logger (LOG_ERR, "open `%s': %s", options->pidfile, strerror (errno));
exit (EXIT_FAILURE);
}
/* Lock the file so that only one instance of dhcpcd runs on an interface */
if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) {
logger (LOG_ERR, "flock `%s': %s", options->pidfile, strerror (errno));
exit (EXIT_FAILURE);
}
/* dhcpcd.sh should not interhit this fd */
2007-07-18 19:35:50 +08:00
if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 ||
fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
logger (LOG_ERR, "fcntl: %s", strerror (errno));
writepid (pidfd, getpid ());
logger (LOG_INFO, PACKAGE " " VERSION " starting");
}
/* Seed random */
srandomdev ();
i = EXIT_FAILURE;
if (dhcp_run (options, &pidfd) == 0)
i = EXIT_SUCCESS;
/* If we didn't daemonise then we need to punt the pidfile now */
if (pidfd > -1) {
close (pidfd);
unlink (options->pidfile);
}
free (options);
#ifdef THERE_IS_NO_FORK
/* There may have been an error before the dhcp_run function
* clears this, so just do it here to be safe */
free (dhcpcd_skiproutes);
#endif
logger (LOG_INFO, "exiting");
exit (i);
2006-11-28 04:23:22 +08:00
}