Imported Upstream version 2.6.0

This commit is contained in:
arnaud.quette@free.fr 2011-01-26 10:35:08 +01:00
parent 26fb71b504
commit 459aaf9392
510 changed files with 40508 additions and 18859 deletions

View file

@ -10,12 +10,12 @@ endif
if WITH_SSL
AM_CFLAGS += $(LIBSSL_CFLAGS)
endif
LDADD = ../common/libcommon.a ../common/libparseconf.la $(NETLIBS)
LDADD = ../common/libcommon.la ../common/libparseconf.la $(NETLIBS)
if WITH_WRAP
LDADD += $(LIBWRAP_LDFLAGS)
LDADD += $(LIBWRAP_LIBS)
endif
if WITH_SSL
LDADD += $(LIBSSL_LDFLAGS)
LDADD += $(LIBSSL_LIBS)
endif
sbin_PROGRAMS = upsd

View file

@ -1,4 +1,4 @@
# Makefile.in generated by automake 1.11 from Makefile.am.
# Makefile.in generated by automake 1.11.1 from Makefile.am.
# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@ -39,19 +39,20 @@ host_triplet = @host@
target_triplet = @target@
@WITH_WRAP_TRUE@am__append_1 = $(LIBWRAP_CFLAGS)
@WITH_SSL_TRUE@am__append_2 = $(LIBSSL_CFLAGS)
@WITH_WRAP_TRUE@am__append_3 = $(LIBWRAP_LDFLAGS)
@WITH_SSL_TRUE@am__append_4 = $(LIBSSL_LDFLAGS)
@WITH_WRAP_TRUE@am__append_3 = $(LIBWRAP_LIBS)
@WITH_SSL_TRUE@am__append_4 = $(LIBSSL_LIBS)
sbin_PROGRAMS = upsd$(EXEEXT)
EXTRA_PROGRAMS = sockdebug$(EXEEXT)
subdir = server
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_create_stdint_h.m4 \
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_compare_version.m4 \
$(top_srcdir)/m4/ax_create_stdint_h.m4 \
$(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \
$(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \
$(top_srcdir)/m4/lt~obsolete.m4 \
$(top_srcdir)/m4/nut_arg_with.m4 \
$(top_srcdir)/m4/nut_check_ipv6.m4 \
$(top_srcdir)/m4/nut_check_asciidoc.m4 \
$(top_srcdir)/m4/nut_check_libgd.m4 \
$(top_srcdir)/m4/nut_check_libhal.m4 \
$(top_srcdir)/m4/nut_check_libneon.m4 \
@ -61,6 +62,7 @@ am__aclocal_m4_deps = $(top_srcdir)/m4/ax_create_stdint_h.m4 \
$(top_srcdir)/m4/nut_check_libusb.m4 \
$(top_srcdir)/m4/nut_check_libwrap.m4 \
$(top_srcdir)/m4/nut_check_os.m4 \
$(top_srcdir)/m4/nut_config_libhal.m4 \
$(top_srcdir)/m4/nut_report_feature.m4 \
$(top_srcdir)/m4/nut_type_socklen_t.m4 \
$(top_srcdir)/configure.in
@ -78,7 +80,7 @@ sockdebug_LDADD = $(LDADD)
am__DEPENDENCIES_1 =
@WITH_WRAP_TRUE@am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
@WITH_SSL_TRUE@am__DEPENDENCIES_3 = $(am__DEPENDENCIES_1)
sockdebug_DEPENDENCIES = ../common/libcommon.a \
sockdebug_DEPENDENCIES = ../common/libcommon.la \
../common/libparseconf.la $(am__DEPENDENCIES_1) \
$(am__DEPENDENCIES_2) $(am__DEPENDENCIES_3)
am_upsd_OBJECTS = upsd.$(OBJEXT) user.$(OBJEXT) conf.$(OBJEXT) \
@ -87,7 +89,7 @@ am_upsd_OBJECTS = upsd.$(OBJEXT) user.$(OBJEXT) conf.$(OBJEXT) \
netset.$(OBJEXT) netinstcmd.$(OBJEXT)
upsd_OBJECTS = $(am_upsd_OBJECTS)
upsd_LDADD = $(LDADD)
upsd_DEPENDENCIES = ../common/libcommon.a ../common/libparseconf.la \
upsd_DEPENDENCIES = ../common/libcommon.la ../common/libparseconf.la \
$(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) \
$(am__DEPENDENCIES_3)
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include
@ -108,9 +110,11 @@ DIST_SOURCES = $(sockdebug_SOURCES) $(upsd_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
A2X = @A2X@
ACLOCAL = @ACLOCAL@
AMTAR = @AMTAR@
AR = @AR@
ASCIIDOC = @ASCIIDOC@
AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@
AUTOMAKE = @AUTOMAKE@
@ -123,8 +127,10 @@ CONFPATH = @CONFPATH@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@
CYGPATH_W = @CYGPATH_W@
DBLATEX = @DBLATEX@
DEFS = @DEFS@
DEPDIR = @DEPDIR@
DOC_BUILD_LIST = @DOC_BUILD_LIST@
DRIVER_BUILD_LIST = @DRIVER_BUILD_LIST@
DRIVER_INSTALL_TARGET = @DRIVER_INSTALL_TARGET@
DRIVER_MAN_LIST = @DRIVER_MAN_LIST@
@ -141,7 +147,6 @@ HAL_CALLOUTS_PATH = @HAL_CALLOUTS_PATH@
HAL_DEVICE_MATCH_KEY = @HAL_DEVICE_MATCH_KEY@
HAL_FDI_PATH = @HAL_FDI_PATH@
HAL_USER = @HAL_USER@
HAVE_GLIB_2_14 = @HAVE_GLIB_2_14@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
@ -152,22 +157,22 @@ LDFLAGS = @LDFLAGS@
LIBGD_CFLAGS = @LIBGD_CFLAGS@
LIBGD_LDFLAGS = @LIBGD_LDFLAGS@
LIBHAL_CFLAGS = @LIBHAL_CFLAGS@
LIBHAL_LDFLAGS = @LIBHAL_LDFLAGS@
LIBHAL_LIBS = @LIBHAL_LIBS@
LIBNEON_CFLAGS = @LIBNEON_CFLAGS@
LIBNEON_LDFLAGS = @LIBNEON_LDFLAGS@
LIBNEON_LIBS = @LIBNEON_LIBS@
LIBNETSNMP_CFLAGS = @LIBNETSNMP_CFLAGS@
LIBNETSNMP_LDFLAGS = @LIBNETSNMP_LDFLAGS@
LIBNETSNMP_LIBS = @LIBNETSNMP_LIBS@
LIBOBJS = @LIBOBJS@
LIBPOWERMAN_CFLAGS = @LIBPOWERMAN_CFLAGS@
LIBPOWERMAN_LDFLAGS = @LIBPOWERMAN_LDFLAGS@
LIBPOWERMAN_LIBS = @LIBPOWERMAN_LIBS@
LIBS = @LIBS@
LIBSSL_CFLAGS = @LIBSSL_CFLAGS@
LIBSSL_LDFLAGS = @LIBSSL_LDFLAGS@
LIBSSL_LIBS = @LIBSSL_LIBS@
LIBTOOL = @LIBTOOL@
LIBUSB_CFLAGS = @LIBUSB_CFLAGS@
LIBUSB_LDFLAGS = @LIBUSB_LDFLAGS@
LIBUSB_LIBS = @LIBUSB_LIBS@
LIBWRAP_CFLAGS = @LIBWRAP_CFLAGS@
LIBWRAP_LDFLAGS = @LIBWRAP_LDFLAGS@
LIBWRAP_LIBS = @LIBWRAP_LIBS@
LIPO = @LIPO@
LN_S = @LN_S@
LTLIBOBJS = @LTLIBOBJS@
@ -268,7 +273,7 @@ udevdir = @udevdir@
# files. In any case, CFLAGS are only -I options, so there is no harm,
# but only add them if we really use the target.
AM_CFLAGS = -I$(top_srcdir)/include $(am__append_1) $(am__append_2)
LDADD = ../common/libcommon.a ../common/libparseconf.la $(NETLIBS) \
LDADD = ../common/libcommon.la ../common/libparseconf.la $(NETLIBS) \
$(am__append_3) $(am__append_4)
upsd_SOURCES = upsd.c user.c conf.c ssl.c sstate.c desc.c \
netget.c netmisc.c netlist.c netuser.c netset.c netinstcmd.c \

View file

@ -30,59 +30,33 @@
/* add another UPS for monitoring from ups.conf */
static void ups_create(const char *fn, const char *name, const char *desc)
{
upstype_t *temp, *last;
temp = last = firstups;
/* find end of linked list */
while (temp != NULL) {
last = temp;
upstype_t *temp;
for (temp = firstups; temp != NULL; temp = temp->next) {
if (!strcasecmp(temp->name, name)) {
upslogx(LOG_ERR, "UPS name [%s] is already in use!",
name);
upslogx(LOG_ERR, "UPS name [%s] is already in use!", name);
return;
}
temp = temp->next;
}
/* grab some memory and add the info */
temp = xmalloc(sizeof(upstype_t));
temp->name = xstrdup(name);
temp = xcalloc(1, sizeof(*temp));
temp->fn = xstrdup(fn);
temp->name = xstrdup(name);
if (desc)
if (desc) {
temp->desc = xstrdup(desc);
else
temp->desc = NULL;
}
temp->stale = 1;
temp->numlogins = 0;
temp->fsd = 0;
temp->retain = 1;
temp->next = NULL;
temp->dumpdone = 0;
temp->data_ok = 0;
temp->sock_fd = sstate_connect(temp);
/* preload this to the current time to avoid false staleness */
time(&temp->last_heard);
temp->last_ping = 0;
temp->last_connfail = 0;
temp->inforoot = NULL;
temp->cmdlist = NULL;
if (last == NULL)
firstups = temp;
else
last->next = temp;
temp->sock_fd = sstate_connect(temp);
temp->next = firstups;
firstups = temp;
num_ups++;
}
@ -272,48 +246,38 @@ void load_upsdconf(int reloading)
/* callback during parsing of ups.conf */
void do_upsconf_args(char *upsname, char *var, char *val)
{
ups_t *tmp, *last;
ups_t *temp;
/* no "global" stuff for us */
if (!upsname)
if (!upsname) {
return;
last = tmp = upstable;
while (tmp) {
last = tmp;
if (!strcmp(tmp->upsname, upsname)) {
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (!strcmp(var, "desc"))
tmp->desc = xstrdup(val);
return;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(ups_t));
tmp->upsname = xstrdup(upsname);
tmp->driver = NULL;
tmp->port = NULL;
tmp->desc = NULL;
tmp->next = NULL;
/* check if UPS is already listed */
for (temp = upstable; temp != NULL; temp = temp->next) {
if (!strcmp(temp->upsname, upsname)) {
break;
}
}
if (!strcmp(var, "driver"))
tmp->driver = xstrdup(val);
if (!strcmp(var, "port"))
tmp->port = xstrdup(val);
if (!strcmp(var, "desc"))
tmp->desc = xstrdup(val);
/* if not listed, create a new entry and prepend it to the list */
if (temp == NULL) {
temp = xcalloc(1, sizeof(*temp));
temp->upsname = xstrdup(upsname);
temp->next = upstable;
upstable = temp;
}
if (last)
last->next = tmp;
else
upstable = tmp;
if (!strcmp(var, "driver")) {
free(temp->driver);
temp->driver = xstrdup(val);
} else if (!strcmp(var, "port")) {
free(temp->port);
temp->port = xstrdup(val);
} else if (!strcmp(var, "desc")) {
free(temp->desc);
temp->desc = xstrdup(val);
}
}
/* add valid UPSes from ups.conf to the internal structures */

View file

@ -26,17 +26,17 @@
extern const char *datapath;
struct dlist_t {
typedef struct dlist_s {
char *name;
char *desc;
struct dlist_t *next;
};
struct dlist_s *next;
} dlist_t;
static struct dlist_t *cmd_list = NULL, *var_list = NULL;
static dlist_t *cmd_list = NULL, *var_list = NULL;
static void list_free(struct dlist_t *ptr)
static void list_free(dlist_t *ptr)
{
struct dlist_t *next;
dlist_t *next;
while (ptr) {
next = ptr->next;
@ -49,51 +49,39 @@ static void list_free(struct dlist_t *ptr)
}
}
static const char *list_get(const struct dlist_t *list, const char *name)
static const char *list_get(const dlist_t *list, const char *name)
{
const struct dlist_t *tmp;
const dlist_t *temp;
tmp = list;
for (temp = list; temp != NULL; temp = temp->next) {
while (tmp) {
if (!strcasecmp(tmp->name, name))
return tmp->desc;
tmp = tmp->next;
if (!strcasecmp(temp->name, name)) {
return temp->desc;
}
}
return NULL;
}
static void desc_add(struct dlist_t **list, const char *name, const char *desc)
static void desc_add(dlist_t **list, const char *name, const char *desc)
{
struct dlist_t *tmp, *last;
dlist_t *temp;
tmp = last = *list;
while (tmp) {
last = tmp;
/* replace duplicates */
if (!strcasecmp(tmp->name, name)) {
free(tmp->desc);
tmp->desc = xstrdup(desc);
return;
for (temp = *list; temp != NULL; temp = temp->next) {
if (!strcasecmp(temp->name, name)) {
break;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(struct dlist_t));
if (temp == NULL) {
temp = xcalloc(1, sizeof(*temp));
temp->name = xstrdup(name);
temp->next = *list;
*list = temp;
}
tmp->name = xstrdup(name);
tmp->desc = xstrdup(desc);
tmp->next = NULL;
if (last)
last->next = tmp;
else
*list = tmp;
free(temp->desc);
temp->desc = xstrdup(desc);
}
static void desc_file_err(const char *errmsg)
@ -121,14 +109,13 @@ void desc_load(void)
while (pconf_file_next(&ctx)) {
if (pconf_parse_error(&ctx)) {
upslogx(LOG_ERR, "Parse error: %s:%d: %s",
fn, ctx.linenum, ctx.errmsg);
upslogx(LOG_ERR, "Parse error: %s:%d: %s", fn, ctx.linenum, ctx.errmsg);
continue;
}
if (ctx.numargs < 3)
if (ctx.numargs < 3) {
continue;
}
if (!strcmp(ctx.arglist[0], "CMDDESC")) {
desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);

View file

@ -29,14 +29,11 @@
#define FLAG_USER 0x0001 /* username and password must be set */
struct netcmds_t {
struct {
const char *name;
void (*func)(ctype_t *client, int numargs, const char **arg);
int flags;
};
struct netcmds_t netcmds[] =
{
} netcmds[] = {
{ "VER", net_ver, 0 },
{ "HELP", net_help, 0 },
{ "STARTTLS", net_starttls, 0 },

View file

@ -120,7 +120,7 @@ static void get_type(ctype_t *client, const char *upsname, const char *var)
{
char buf[SMALLBUF];
const upstype_t *ups;
const struct st_tree_t *node;
const st_tree_t *node;
ups = get_ups_ptr(upsname);

View file

@ -33,7 +33,7 @@ static void send_instcmd(ctype_t *client, const char *upsname,
{
int found;
upstype_t *ups;
const struct cmdlist_t *ctmp;
const cmdlist_t *ctmp;
char sockcmd[SMALLBUF];
ups = get_ups_ptr(upsname);

View file

@ -28,7 +28,7 @@
extern upstype_t *firstups; /* for list_ups */
static int tree_dump(struct st_tree_t *node, ctype_t *client, const char *ups,
static int tree_dump(st_tree_t *node, ctype_t *client, const char *ups,
int rw, int fsd)
{
int ret;
@ -127,7 +127,7 @@ static void list_var(ctype_t *client, const char *upsname)
static void list_cmd(ctype_t *client, const char *upsname)
{
const upstype_t *ups;
struct cmdlist_t *ctmp;
cmdlist_t *ctmp;
ups = get_ups_ptr(upsname);
@ -153,8 +153,8 @@ static void list_cmd(ctype_t *client, const char *upsname)
static void list_enum(ctype_t *client, const char *upsname, const char *var)
{
const upstype_t *ups;
const struct st_tree_t *node;
const struct enum_t *etmp;
const st_tree_t *node;
const enum_t *etmp;
ups = get_ups_ptr(upsname);

View file

@ -32,7 +32,7 @@ static void set_var(ctype_t *client, const char *upsname, const char *var,
{
upstype_t *ups;
const char *val;
const struct enum_t *etmp;
const enum_t *etmp;
char cmd[SMALLBUF], esc[SMALLBUF];
ups = get_ups_ptr(upsname);

View file

@ -118,10 +118,11 @@ static void read_sock(int fd)
int main(int argc, char **argv)
{
const char *prog = xbasename(argv[0]);
int ret, sockfd;
if (argc != 2) {
fprintf(stderr, "usage: %s <socket name>\n", argv[0]);
fprintf(stderr, "usage: %s <socket name>\n", prog);
fprintf(stderr, " %s /var/state/ups/apcsmart-ttyS1.newsock\n",
argv[0]);
exit(EXIT_SUCCESS);

View file

@ -115,42 +115,52 @@ void net_starttls(ctype_t *client, int numarg, const char **arg)
void ssl_init(void)
{
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
const SSL_METHOD *ssl_method;
#else
SSL_METHOD *ssl_method;
#endif
if (!certfile) {
return;
}
check_perms(certfile);
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
SSL_library_init();
if ((ssl_ctx = SSL_CTX_new(TLSv1_server_method())) == NULL) {
fatal_with_errno(EXIT_FAILURE, "SSL_CTX_new");
if ((ssl_method = TLSv1_server_method()) == NULL) {
ssl_debug();
fatalx(EXIT_FAILURE, "TLSv1_server_method failed");
}
if (SSL_CTX_use_RSAPrivateKey_file(ssl_ctx, certfile, SSL_FILETYPE_PEM) != 1) {
if ((ssl_ctx = SSL_CTX_new(ssl_method)) == NULL) {
ssl_debug();
upslogx(LOG_ERR, "SSL_CTX_use_RSAPrivateKey_file(%s) failed", certfile);
return;
fatalx(EXIT_FAILURE, "SSL_CTX_new failed");
}
if (SSL_CTX_use_certificate_chain_file(ssl_ctx, certfile) != 1) {
upslogx(LOG_ERR, "SSL_CTX_use_certificate_chain_file(%s) failed", certfile);
return;
ssl_debug();
fatalx(EXIT_FAILURE, "SSL_CTX_use_certificate_chain_file(%s) failed", certfile);
}
if (SSL_CTX_use_PrivateKey_file(ssl_ctx, certfile, SSL_FILETYPE_PEM) != 1) {
ssl_debug();
fatalx(EXIT_FAILURE, "SSL_CTX_use_PrivateKey_file(%s) failed", certfile);
}
if (SSL_CTX_check_private_key(ssl_ctx) != 1) {
upslogx(LOG_ERR, "SSL_CTX_check_private_key(%s) failed", certfile);
return;
ssl_debug();
fatalx(EXIT_FAILURE, "SSL_CTX_check_private_key(%s) failed", certfile);
}
if (SSL_CTX_set_cipher_list(ssl_ctx, "HIGH:@STRENGTH") != 1) {
ssl_debug();
fatalx(EXIT_FAILURE, "SSL_CTX_set_cipher_list failed");
}
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, NULL);
if (SSL_CTX_set_cipher_list(ssl_ctx, "HIGH:@STRENGTH") != 1) {
fatalx(EXIT_FAILURE, "SSL_CTX_set_cipher_list");
}
ssl_initialized = 1;
}

View file

@ -19,6 +19,13 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common.h"
#include "timehead.h"
#include "sstate.h"
#include "upstype.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
@ -27,14 +34,6 @@
#include <sys/socket.h>
#include <sys/un.h>
#include "common.h"
#include "timehead.h"
#include "upstype.h"
#include "sstate.h"
#include "state.h"
static int parse_args(upstype_t *ups, int numargs, char **arg)
{
if (numargs < 1)
@ -297,12 +296,12 @@ int sstate_getaux(const upstype_t *ups, const char *var)
return state_getaux(ups->inforoot, var);
}
const struct enum_t *sstate_getenumlist(const upstype_t *ups, const char *var)
const enum_t *sstate_getenumlist(const upstype_t *ups, const char *var)
{
return state_getenumlist(ups->inforoot, var);
}
const struct cmdlist_t *sstate_getcmdlist(const upstype_t *ups)
const cmdlist_t *sstate_getcmdlist(const upstype_t *ups)
{
return ups->cmdlist;
}
@ -376,7 +375,7 @@ int sstate_sendline(upstype_t *ups, const char *buf)
return 0; /* failed */
}
const struct st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname)
const st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname)
{
return state_tree_find(ups->inforoot, varname);
}

View file

@ -19,6 +19,10 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SSTATE_H_SEEN
#define SSTATE_H_SEEN
#include "state.h"
#include "upstype.h"
#define SS_CONNFAIL_INT 300 /* complain about a dead driver every 5 mins */
@ -30,8 +34,8 @@ void sstate_readline(upstype_t *ups);
const char *sstate_getinfo(const upstype_t *ups, const char *var);
int sstate_getflags(const upstype_t *ups, const char *var);
int sstate_getaux(const upstype_t *ups, const char *var);
const struct enum_t *sstate_getenumlist(const upstype_t *ups, const char *var);
const struct cmdlist_t *sstate_getcmdlist(const upstype_t *ups);
const enum_t *sstate_getenumlist(const upstype_t *ups, const char *var);
const cmdlist_t *sstate_getcmdlist(const upstype_t *ups);
void sstate_makeinfolist(const upstype_t *ups, char *buf, size_t bufsize);
void sstate_makerwlist(const upstype_t *ups, char *buf, size_t bufsize);
void sstate_makeinstcmdlist_t(const upstype_t *ups, char *buf, size_t bufsize);
@ -39,5 +43,6 @@ int sstate_dead(upstype_t *ups, int maxage);
void sstate_infofree(upstype_t *ups);
void sstate_cmdfree(upstype_t *ups);
int sstate_sendline(upstype_t *ups, const char *buf);
const struct st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname);
const st_tree_t *sstate_getnode(const upstype_t *ups, const char *varname);
#endif /* SSTATE_H_SEEN */

View file

@ -70,9 +70,7 @@ static ctype_t *firstclient = NULL;
/* default is to listen on all local interfaces */
static stype_t *firstaddr = NULL;
#ifdef HAVE_IPV6
static int opt_af = AF_UNSPEC;
#endif
typedef enum {
DRIVER = 1,
@ -95,7 +93,6 @@ static char pidfn[SMALLBUF];
/* set by signal handlers */
static int reload_flag = 0, exit_flag = 0;
#ifdef HAVE_IPV6
static const char *inet_ntopW (struct sockaddr_storage *s)
{
static char str[40];
@ -111,7 +108,6 @@ static const char *inet_ntopW (struct sockaddr_storage *s)
return NULL;
}
}
#endif
/* return a pointer to the named ups if possible */
upstype_t *get_ups_ptr(const char *name)
@ -181,59 +177,6 @@ void listen_add(const char *addr, const char *port)
/* create a listening socket for tcp connections */
static void setuptcp(stype_t *server)
{
#ifndef HAVE_IPV6
struct hostent *host;
struct sockaddr_in sockin;
int res, one = 1;
host = gethostbyname(server->addr);
if (!host) {
struct in_addr listenaddr;
if (!inet_aton(server->addr, &listenaddr)) {
fatal_with_errno(EXIT_FAILURE, "inet_aton");
}
host = gethostbyaddr(&listenaddr, sizeof(listenaddr), AF_INET);
if (!host) {
fatal_with_errno(EXIT_FAILURE, "gethostbyaddr");
}
}
if ((server->sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fatal_with_errno(EXIT_FAILURE, "socket");
}
res = setsockopt(server->sock_fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
if (res != 0) {
fatal_with_errno(EXIT_FAILURE, "setsockopt(SO_REUSEADDR)");
}
memset(&sockin, '\0', sizeof(sockin));
sockin.sin_family = AF_INET;
sockin.sin_port = htons(atoi(server->port));
memcpy(&sockin.sin_addr, host->h_addr, host->h_length);
if (bind(server->sock_fd, (struct sockaddr *) &sockin, sizeof(sockin)) == -1) {
fatal_with_errno(EXIT_FAILURE, "Can't bind TCP port %s", server->port);
}
if ((res = fcntl(server->sock_fd, F_GETFL, 0)) == -1) {
fatal_with_errno(EXIT_FAILURE, "fcntl(get)");
}
if (fcntl(server->sock_fd, F_SETFL, res | O_NDELAY) == -1) {
fatal_with_errno(EXIT_FAILURE, "fcntl(set)");
}
if (listen(server->sock_fd, 16)) {
fatal_with_errno(EXIT_FAILURE, "listen");
}
#else
struct addrinfo hints, *res, *ai;
int v = 0, one = 1;
@ -290,7 +233,6 @@ static void setuptcp(stype_t *server)
}
freeaddrinfo(res);
#endif
/* don't fail silently */
if (server->sock_fd < 0) {
@ -468,7 +410,8 @@ static void check_command(int cmdnum, ctype_t *client, int numarg,
}
#ifdef HAVE_WRAP
request_init(&req, RQ_DAEMON, progname, RQ_CLIENT_ADDR, client->addr, RQ_USER, client->username, 0);
request_init(&req, RQ_DAEMON, progname, RQ_FILE, client->sock_fd, RQ_USER, client->username, 0);
fromhost(&req);
if (!hosts_access(&req)) {
/* tcp-wrappers says access should be denied */
@ -508,11 +451,7 @@ static void parse_net(ctype_t *client)
/* answer incoming tcp connections */
static void client_connect(stype_t *server)
{
#ifndef HAVE_IPV6
struct sockaddr_in csock;
#else
struct sockaddr_storage csock;
#endif
socklen_t clen;
int fd;
ctype_t *client;
@ -530,11 +469,7 @@ static void client_connect(stype_t *server)
time(&client->last_heard);
#ifndef HAVE_IPV6
client->addr = xstrdup(inet_ntoa(csock.sin_addr));
#else
client->addr = xstrdup(inet_ntopW(&csock));
#endif
pconf_init(&client->ctx, NULL);
@ -610,7 +545,6 @@ void server_load(void)
/* default behaviour if no LISTEN addres has been specified */
if (!firstaddr) {
#ifdef HAVE_IPV6
if (opt_af != AF_INET) {
listen_add("::1", string_const(PORT));
}
@ -618,9 +552,6 @@ void server_load(void)
if (opt_af != AF_INET6) {
listen_add("127.0.0.1", string_const(PORT));
}
#else
listen_add("127.0.0.1", string_const(PORT));
#endif
}
for (server = firstaddr; server; server = server->next) {
@ -879,12 +810,11 @@ static void help(const char *progname)
printf(" -D raise debugging level\n");
printf(" -h display this help\n");
printf(" -r <dir> chroots to <dir>\n");
printf(" -q raise log level threshold\n");
printf(" -u <user> switch to <user> (if started as root)\n");
printf(" -V display the version of this software\n");
#ifdef HAVE_IPV6
printf(" -4 IPv4 only\n");
printf(" -6 IPv6 only\n");
#endif
exit(EXIT_SUCCESS);
}
@ -943,24 +873,21 @@ int main(int argc, char **argv)
{
int i, cmd = 0;
char *chroot_path = NULL;
const char *user = NULL;
const char *user = RUN_AS_USER;
struct passwd *new_uid = NULL;
progname = xbasename(argv[0]);
/* pick up a default from configure --with-user */
user = RUN_AS_USER;
/* yes, xstrdup - the conf handlers call free on this later */
statepath = xstrdup(dflt_statepath());
datapath = xstrdup(DATADIR);
/* set up some things for later */
snprintf(pidfn, sizeof(pidfn), "%s/upsd.pid", altpidpath());
snprintf(pidfn, sizeof(pidfn), "%s/%s.pid", altpidpath(), progname);
printf("Network UPS Tools upsd %s\n", UPS_VERSION);
printf("Network UPS Tools %s %s\n", progname, UPS_VERSION);
while ((i = getopt(argc, argv, "+h46p:r:i:fu:Vc:D")) != -1) {
while ((i = getopt(argc, argv, "+h46p:qr:i:fu:Vc:D")) != -1) {
switch (i) {
case 'h':
help(progname);
@ -970,6 +897,9 @@ int main(int argc, char **argv)
fatalx(EXIT_FAILURE, "Specifying a listening addresses with '-i <address>' and '-p <port>'\n"
"is deprecated. Use 'LISTEN <address> [<port>]' in 'upsd.conf' instead.\n"
"See 'man 8 upsd.conf' for more information.");
case 'q':
nut_log_level++;
break;
case 'r':
chroot_path = optarg;
break;
@ -977,7 +907,6 @@ int main(int argc, char **argv)
user = optarg;
break;
case 'V':
/* do nothing - we already printed the banner */
exit(EXIT_SUCCESS);
@ -996,15 +925,13 @@ int main(int argc, char **argv)
nut_debug_level++;
break;
#ifdef HAVE_IPV6
case '4':
case '4':
opt_af = AF_INET;
break;
case '6':
case '6':
opt_af = AF_INET6;
break;
#endif
default:
help(progname);
@ -1028,7 +955,7 @@ int main(int argc, char **argv)
setup_signals();
open_syslog("upsd");
open_syslog(progname);
/* send logging to the syslog pre-background for later use */
syslogbit_set();
@ -1049,6 +976,9 @@ int main(int argc, char **argv)
/* start server */
server_load();
/* initialize SSL before we drop privileges (we may not be able to read the keyfile as non-root) */
ssl_init();
become_user(new_uid);
if (chdir(statepath)) {
@ -1067,8 +997,6 @@ int main(int argc, char **argv)
fatalx(EXIT_FAILURE, "Fatal error: at least one UPS must be defined in ups.conf");
}
ssl_init();
/* try to bring in the var/cmd descriptions */
desc_load();
@ -1089,3 +1017,4 @@ int main(int argc, char **argv)
upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
return EXIT_SUCCESS;
}

View file

@ -38,8 +38,8 @@ typedef struct upstype_s {
time_t last_ping;
time_t last_connfail;
PCONF_CTX_t sock_ctx;
struct st_tree_t *inforoot;
struct cmdlist_t *cmdlist;
struct st_tree_s *inforoot;
struct cmdlist_s *cmdlist;
int numlogins;
int fsd; /* forced shutdown in effect? */