ocserv: use getopt for command line parsing

The complexity of its command line options didn't require
the use of libopts, and by eliminating that dependency for cmd
parsing, we can chose another parser for config file parsing.

Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
This commit is contained in:
Nikos Mavrogiannopoulos
2018-01-13 18:46:51 +01:00
parent 6b7d09485e
commit 533677bd73
8 changed files with 88 additions and 1280 deletions

View File

@@ -36,16 +36,11 @@ ChangeLog:
.PHONY: ChangeLog
files-update:
cp -f $(builddir)/src/ocserv-args.c $(srcdir)/src/ocserv-args.c.in
cp -f $(builddir)/src/ocserv-args.h $(srcdir)/src/ocserv-args.h.in
files-compare:
@echo "*****************************************************************"
@echo "If the following step fails, the autogen .in files are out-of-date;"
@echo "run files-update to update them."
@echo "If the following step fails, run files-update"
@echo "*****************************************************************"
cmp $(srcdir)/src/ocserv-args.c.in $(builddir)/src/ocserv-args.c
cmp $(srcdir)/src/ocserv-args.h.in $(builddir)/src/ocserv-args.h
dist-hook: files-compare ChangeLog
mv ChangeLog $(distdir)
@@ -55,13 +50,5 @@ dist-hook: files-compare ChangeLog
@echo "check fails upgrade the included libopts."
@echo "*****************************************************************"
test "$(SKIP_LIBOPTS_CHECK)" = "1" || test "`autoopts-config libsrc|cut -d '-' -f 2|sed 's/.tar.gz//'`" = "`cat $(srcdir)/libopts/autoopts/options.h |grep OPTIONS_VERSION_STRING|cut -d '"' -f 2|sed 's/:/./g'`"
for dir in $(distdir)/src/;do \
( cd $$dir && for i in *args.def;do \
$(AUTOGEN) $$i; \
done && \
for i in *args.c *args.h;do \
mv $$i $$i.in; \
done ) \
done
.PHONY: files-update files-compare

View File

@@ -536,8 +536,6 @@ AC_SUBST([ROOTGID], [$gid])
LIBOPTS_CHECK([libopts])
if test "$NEED_LIBOPTS_DIR" = "true";then
dnl create libopts-generated files
AC_CONFIG_LINKS([src/ocserv-args.c:src/ocserv-args.c.in
src/ocserv-args.h:src/ocserv-args.h.in])
AC_SUBST([AUTOGEN], [:])
enable_local_libopts=yes
else

View File

@@ -8,24 +8,18 @@ AM_CPPFLAGS += -I$(srcdir)/../gl/ -I$(builddir)/../gl/ \
$(LIBTALLOC_CFLAGS) $(LIBDBUS_CFLAGS) $(LIBOATH_CFLAGS) \
$(LIBKRB5_CFLAGS) $(LIBTASN1_CFLAGS) $(RADCLI_CFLAGS) $(SRC_CFLAGS)
BUILT_SOURCES = ocserv-args.c ocserv-args.h ipc.pb-c.c ipc.pb-c.h \
BUILT_SOURCES = ipc.pb-c.c ipc.pb-c.h \
http-heads.h kkdcp_asn1_tab.c ctl.pb-c.c ctl.pb-c.h
EXTRA_DIST = version.inc.in version.inc ocserv-args.c.in ocserv-args.h.in \
ipc.proto ctl.proto http-heads.gperf common.mk ocserv-fw occtl/occtl-args.def
EXTRA_DIST = version.inc.in version.inc \
ipc.proto ctl.proto http-heads.gperf common.mk ocserv-fw occtl/occtl-args.def \
ocpasswd/ocpasswd-args.def ocserv-args.def
sbin_PROGRAMS = ocserv
bin_SCRIPTS = ocserv-fw
noinst_LIBRARIES = libcmd-ocserv.a libipc.a
libcmd_ocserv_a_SOURCES = ocserv-args.def ocserv-args.c ocserv-args.h
ocserv-args.stamp: $(srcdir)/ocserv-args.def $(builddir)/version.inc
$(AUTOGEN) $<
touch $@
ocserv-args.h: ocserv-args.stamp
ocserv-args.c: ocserv-args.stamp
noinst_LIBRARIES = libipc.a
# Authentication module sources
AUTH_SOURCES=auth/pam.c auth/pam.h auth/plain.c auth/plain.h auth/radius.c auth/radius.h \
@@ -66,7 +60,7 @@ if LOCAL_HTTP_PARSER
HTTP_PARSER_SOURCES = http-parser/http_parser.c http-parser/http_parser.h
endif
ocserv_LDADD = ../gl/libgnu.a $(NEEDED_LIBOPTS) libcmd-ocserv.a libccan.a libcommon.a
ocserv_LDADD = ../gl/libgnu.a $(NEEDED_LIBOPTS) libccan.a libcommon.a
ocserv_LDADD += $(LIBGNUTLS_LIBS) $(PAM_LIBS) $(LIBUTIL) \
$(LIBSECCOMP) $(LIBWRAP) $(LIBCRYPT) $(NEEDED_HTTP_PARSER_LIBS) \
$(NEEDED_LIBPROTOBUF_LIBS) $(LIBSYSTEMD) $(LIBTALLOC_LIBS) \
@@ -115,8 +109,6 @@ occtl_occtl_LDADD = ../gl/libgnu.a libcommon.a $(LIBREADLINE_LIBS) \
# Ocpasswd
EXTRA_DIST += ocpasswd/ocpasswd-args.def
bin_PROGRAMS += ocpasswd/ocpasswd
ocpasswd_ocpasswd_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/ocpasswd

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013, 2014, 2015 Nikos Mavrogiannopoulos
* Copyright (C) 2013-2017 Nikos Mavrogiannopoulos
* Copyright (C) 2014, 2015 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
@@ -26,7 +26,6 @@
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <ocserv-args.h>
#include <autoopts/options.h>
#include <limits.h>
#include <common.h>
@@ -54,9 +53,13 @@
#include <occtl/ctl.h>
#include "common-config.h"
#include <getopt.h>
#define OLD_DEFAULT_CFG_FILE "/etc/ocserv.conf"
#define DEFAULT_CFG_FILE "/etc/ocserv/ocserv.conf"
static void print_version(void);
static char pid_file[_POSIX_PATH_MAX] = "";
static char cfg_file[_POSIX_PATH_MAX] = DEFAULT_CFG_FILE;
@@ -1221,28 +1224,88 @@ static void check_cfg(struct perm_cfg_st *perm_config)
perm_config->config->priorities = talloc_strdup(perm_config->config, "NORMAL:%SERVER_PRECEDENCE:%COMPAT");
}
static const struct option long_options[] = {
{"debug", 1, 0, 'd'},
{"config", 1, 0, 'c'},
{"pid-file", 0, 0, 'p'},
{"test-config", 0, 0, 't'},
{"foreground", 0, 0, 'f'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'v'},
{NULL, 0, 0, 0}
};
static
void usage(void)
{
fprintf(stderr, "ocserv - OpenConnect VPN server\n");
fprintf(stderr, "Usage: ocserv [ -<flag> [<val>] | --<name>[{=| }<val>] ]...\n\n");
fprintf(stderr, " -f, --foreground Do not fork into background\n");
fprintf(stderr, " -d, --debug=num Enable verbose network debugging information\n");
fprintf(stderr, " - it must be in the range:\n");
fprintf(stderr, " 0 to 9999\n");
fprintf(stderr, " -c, --config=file Configuration file for the server\n");
fprintf(stderr, " - file must exist\n");
fprintf(stderr, " -t, --test-config Test the provided configuration file\n");
fprintf(stderr, " -p, --pid-file=file Specify pid file for the server\n");
fprintf(stderr, " -v, --version output version information and exit\n");
fprintf(stderr, " -h, --help display extended usage information and exit\n\n");
fprintf(stderr, "Openconnect VPN server (ocserv) is a VPN server compatible with the\n");
fprintf(stderr, "openconnect VPN client. It follows the TLS and DTLS-based AnyConnect VPN\n");
fprintf(stderr, "protocol which is used by several CISCO routers.\n\n");
fprintf(stderr, "Please send bug reports to: "PACKAGE_BUGREPORT"\n");
}
int cmd_parser (void *pool, int argc, char **argv, struct perm_cfg_st** config)
{
unsigned test_only = 0;
int c;
*config = talloc_zero(pool, struct perm_cfg_st);
if (*config == NULL)
exit(1);
optionProcess( &ocservOptions, argc, argv);
while (1) {
c = getopt_long(argc, argv, "d:c:p:ftvh", long_options, NULL);
if (c == -1)
break;
if (HAVE_OPT(FOREGROUND))
(*config)->foreground = 1;
if (HAVE_OPT(PID_FILE)) {
strlcpy(pid_file, OPT_ARG(PID_FILE), sizeof(pid_file));
switch(c) {
case 'f':
(*config)->foreground = 1;
break;
case 'p':
strlcpy(pid_file, optarg, sizeof(pid_file));
break;
case 'c':
strlcpy(cfg_file, optarg, sizeof(cfg_file));
break;
case 'd':
(*config)->debug = atoi(optarg);
break;
case 't':
test_only = 1;
break;
case 'h':
usage();
exit(0);
case 'v':
print_version();
exit(0);
}
}
if (HAVE_OPT(DEBUG))
(*config)->debug = OPT_VALUE_DEBUG;
if (optind != argc) {
fprintf(stderr, ERRSTR"no additional command line options are allowed\n\n");
exit(1);
}
if (HAVE_OPT(CONFIG)) {
strlcpy(cfg_file, OPT_ARG(CONFIG), sizeof(cfg_file));
} else if (access(cfg_file, R_OK) != 0) {
fprintf(stderr, "%s -c [config]\nUse %s --help for more information.\n", argv[0], argv[0]);
if (access(cfg_file, R_OK) != 0) {
fprintf(stderr, ERRSTR"cannot access config file: %s\n", cfg_file);
fprintf(stderr, "Usage: %s -c [config]\nUse %s --help for more information.\n", argv[0], argv[0]);
exit(1);
}
@@ -1250,7 +1313,7 @@ int cmd_parser (void *pool, int argc, char **argv, struct perm_cfg_st** config)
check_cfg(*config);
if (HAVE_OPT(TEST_CONFIG))
if (test_only)
exit(0);
return 0;
@@ -1307,11 +1370,11 @@ static void append(const char *option)
fprintf(stderr, "%s", option);
}
void print_version(tOptions *opts, tOptDesc *desc)
static void print_version(void)
{
const char *p;
fputs(OCSERV_FULL_VERSION, stderr);
fputs(PACKAGE_STRING, stderr);
fprintf(stderr, "\n\nCompiled with: ");
#ifdef HAVE_LIBSECCOMP
append("seccomp");
@@ -1345,8 +1408,6 @@ void print_version(tOptions *opts, tOptDesc *desc)
} else {
fprintf(stderr, "GnuTLS version: %s\n", p);
}
exit(0);
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,217 +0,0 @@
/* -*- buffer-read-only: t -*- vi: set ro:
*
* DO NOT EDIT THIS FILE (ocserv-args.h)
*
* It has been AutoGen-ed
* From the definitions ocserv-args.def
* and the template file options
*
* Generated from AutoOpts 41:1:16 templates.
*
* AutoOpts is a copyrighted work. This header file is not encumbered
* by AutoOpts licensing, but is provided under the licensing terms chosen
* by the ocserv author or copyright holder. AutoOpts is
* licensed under the terms of the LGPL. The redistributable library
* (``libopts'') is licensed under the terms of either the LGPL or, at the
* users discretion, the BSD license. See the AutoOpts and/or libopts sources
* for details.
*
* The ocserv program is copyrighted and licensed
* under the following terms:
*
* Copyright (C) 2013-2017 Nikos Mavrogiannopoulos, all rights reserved.
* This is free software. It is licensed for use, modification and
* redistribution under the terms of the GNU General Public License,
* version 2 <http://gnu.org/licenses/gpl.html>
*
* ocserv is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License,
* as published by the Free Software Foundation.
*
* ocserv is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* version 2, along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file contains the programmatic interface to the Automated
* Options generated for the ocserv program.
* These macros are documented in the AutoGen info file in the
* "AutoOpts" chapter. Please refer to that doc for usage help.
*/
#ifndef AUTOOPTS_OCSERV_ARGS_H_GUARD
#define AUTOOPTS_OCSERV_ARGS_H_GUARD 1
#include "config.h"
#include <autoopts/options.h>
/**
* Ensure that the library used for compiling this generated header is at
* least as new as the version current when the header template was released
* (not counting patch version increments). Also ensure that the oldest
* tolerable version is at least as old as what was current when the header
* template was released.
*/
#define AO_TEMPLATE_VERSION 167937
#if (AO_TEMPLATE_VERSION < OPTIONS_MINIMUM_VERSION) \
|| (AO_TEMPLATE_VERSION > OPTIONS_STRUCT_VERSION)
# error option template version mismatches autoopts/options.h header
Choke Me.
#endif
/**
* Enumeration of each option type for ocserv
*/
typedef enum {
INDEX_OPT_FOREGROUND = 0,
INDEX_OPT_DEBUG = 1,
INDEX_OPT_CONFIG = 2,
INDEX_OPT_TEST_CONFIG = 3,
INDEX_OPT_PID_FILE = 4,
INDEX_OPT_VERSION = 5,
INDEX_OPT_HELP = 6,
INDEX_OPT_MORE_HELP = 7
} teOptIndex;
/** count of all options for ocserv */
#define OPTION_CT 8
/** ocserv version */
#define OCSERV_VERSION "0.11.10"
/** Full ocserv version text */
#define OCSERV_FULL_VERSION "ocserv 0.11.10"
/**
* Interface defines for all options. Replace "n" with the UPPER_CASED
* option name (as in the teOptIndex enumeration above).
* e.g. HAVE_OPT(FOREGROUND)
*/
#define DESC(n) (ocservOptions.pOptDesc[INDEX_OPT_## n])
/** 'true' if an option has been specified in any way */
#define HAVE_OPT(n) (! UNUSED_OPT(& DESC(n)))
/** The string argument to an option. The argument type must be \"string\". */
#define OPT_ARG(n) (DESC(n).optArg.argString)
/** Mask the option state revealing how an option was specified.
* It will be one and only one of \a OPTST_SET, \a OPTST_PRESET,
* \a OPTST_DEFINED, \a OPTST_RESET or zero.
*/
#define STATE_OPT(n) (DESC(n).fOptState & OPTST_SET_MASK)
/** Count of option's occurrances *on the command line*. */
#define COUNT_OPT(n) (DESC(n).optOccCt)
/** mask of \a OPTST_SET and \a OPTST_DEFINED. */
#define ISSEL_OPT(n) (SELECTED_OPT(&DESC(n)))
/** 'true' if \a HAVE_OPT would yield 'false'. */
#define ISUNUSED_OPT(n) (UNUSED_OPT(& DESC(n)))
/** 'true' if OPTST_DISABLED bit not set. */
#define ENABLED_OPT(n) (! DISABLED_OPT(& DESC(n)))
/** number of stacked option arguments.
* Valid only for stacked option arguments. */
#define STACKCT_OPT(n) (((tArgList*)(DESC(n).optCookie))->useCt)
/** stacked argument vector.
* Valid only for stacked option arguments. */
#define STACKLST_OPT(n) (((tArgList*)(DESC(n).optCookie))->apzArgs)
/** Reset an option. */
#define CLEAR_OPT(n) STMTS( \
DESC(n).fOptState &= OPTST_PERSISTENT_MASK; \
if ((DESC(n).fOptState & OPTST_INITENABLED) == 0) \
DESC(n).fOptState |= OPTST_DISABLED; \
DESC(n).optCookie = NULL )
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Enumeration of ocserv exit codes
*/
typedef enum {
OCSERV_EXIT_SUCCESS = 0,
OCSERV_EXIT_FAILURE = 1,
OCSERV_EXIT_USAGE_ERROR = 64,
OCSERV_EXIT_LIBOPTS_FAILURE = 70
} ocserv_exit_code_t;
/**
* Interface defines for specific options.
* @{
*/
#define VALUE_OPT_FOREGROUND 'f'
#define VALUE_OPT_DEBUG 'd'
#define OPT_VALUE_DEBUG (DESC(DEBUG).optArg.argInt)
#define VALUE_OPT_CONFIG 'c'
#define VALUE_OPT_TEST_CONFIG 't'
#define VALUE_OPT_PID_FILE 'p'
/** option flag (value) for help-value option */
#define VALUE_OPT_HELP 'h'
/** option flag (value) for more-help-value option */
#define VALUE_OPT_MORE_HELP '!'
/** option flag (value) for version-value option */
#define VALUE_OPT_VERSION 'v'
/*
* Interface defines not associated with particular options
*/
#define ERRSKIP_OPTERR STMTS(ocservOptions.fOptSet &= ~OPTPROC_ERRSTOP)
#define ERRSTOP_OPTERR STMTS(ocservOptions.fOptSet |= OPTPROC_ERRSTOP)
#define RESTART_OPT(n) STMTS( \
ocservOptions.curOptIdx = (n); \
ocservOptions.pzCurOpt = NULL )
#define START_OPT RESTART_OPT(1)
#define USAGE(c) (*ocservOptions.pUsageProc)(&ocservOptions, c)
#ifdef __cplusplus
extern "C" {
#endif
/* * * * * *
*
* Declare the ocserv option descriptor.
*/
extern tOptions ocservOptions;
#if defined(ENABLE_NLS)
# ifndef _
# include <stdio.h>
# ifndef HAVE_GETTEXT
extern char * gettext(char const *);
# else
# include <libintl.h>
# endif
# ifndef ATTRIBUTE_FORMAT_ARG
# define ATTRIBUTE_FORMAT_ARG(_a)
# endif
static inline char* aoGetsText(char const* pz) ATTRIBUTE_FORMAT_ARG(1);
static inline char* aoGetsText(char const* pz) {
if (pz == NULL) return NULL;
return (char*)gettext(pz);
}
# define _(s) aoGetsText(s)
# endif /* _() */
# define OPT_NO_XLAT_CFG_NAMES STMTS(ocservOptions.fOptSet |= \
OPTPROC_NXLAT_OPT_CFG;)
# define OPT_NO_XLAT_OPT_NAMES STMTS(ocservOptions.fOptSet |= \
OPTPROC_NXLAT_OPT|OPTPROC_NXLAT_OPT_CFG;)
# define OPT_XLAT_CFG_NAMES STMTS(ocservOptions.fOptSet &= \
~(OPTPROC_NXLAT_OPT|OPTPROC_NXLAT_OPT_CFG);)
# define OPT_XLAT_OPT_NAMES STMTS(ocservOptions.fOptSet &= \
~OPTPROC_NXLAT_OPT;)
#else /* ENABLE_NLS */
# define OPT_NO_XLAT_CFG_NAMES
# define OPT_NO_XLAT_OPT_NAMES
# define OPT_XLAT_CFG_NAMES
# define OPT_XLAT_OPT_NAMES
# ifndef _
# define _(_s) _s
# endif
#endif /* ENABLE_NLS */
#ifdef __cplusplus
}
#endif
#endif /* AUTOOPTS_OCSERV_ARGS_H_GUARD */
/* ocserv-args.h ends here */

View File

@@ -26,7 +26,6 @@
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <ocserv-args.h>
#include <autoopts/options.h>
#include <limits.h>
#include <common.h>

View File

@@ -25,7 +25,6 @@
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <ocserv-args.h>
#include <autoopts/options.h>
#include <limits.h>
#include <common.h>