1
0
mirror of https://github.com/kakwa/uts-server synced 2024-06-11 13:29:53 +02:00

reformat source code

This commit is contained in:
kakwa 2016-08-23 19:38:07 +02:00
parent 4e8693da48
commit 291f58bd7e
6 changed files with 343 additions and 362 deletions

View File

@ -18,8 +18,8 @@ extern "C" {
#include <stdio.h> #include <stdio.h>
/* Typedef for prototype of handler function. */ /* Typedef for prototype of handler function. */
typedef int (*ini_handler)(void* user, const char* section, typedef int (*ini_handler)(void *user, const char *section, const char *name,
const char* name, const char* value); const char *value);
/* Typedef for prototype of fgets-style reader function. */ /* Typedef for prototype of fgets-style reader function. */
typedef char *(*ini_reader)(char *str, int num, void *stream); typedef char *(*ini_reader)(char *str, int num, void *stream);

View File

@ -11,34 +11,31 @@
const char *argp_program_version = UTS_VERSION; const char *argp_program_version = UTS_VERSION;
const char *argp_program_bug_address = "Pierre-Francois Carpentier <carpentier.pf@gmail.com>"; const char *argp_program_bug_address =
"Pierre-Francois Carpentier <carpentier.pf@gmail.com>";
static char doc[] = "\nUTS micro timestamp server (RFC 3161)"; static char doc[] = "\nUTS micro timestamp server (RFC 3161)";
static struct argp_option options[] = { static struct argp_option options[] = {
{"conffile", 'c', "CONFFILE", 0, "Path to configuration file"}, {"conffile", 'c', "CONFFILE", 0, "Path to configuration file"},
{"daemonize", 'd', 0, 0, "Launch as a daemon"}, {"daemonize", 'd', 0, 0, "Launch as a daemon"},
{ 0 } {0}};
};
/* A description of the arguments we accept. */ /* A description of the arguments we accept. */
static char args_doc[] = "-c CONFFILE -d"; static char args_doc[] = "-c CONFFILE -d";
struct arguments struct arguments {
{
char *args[2]; /* arg1 & arg2 */ char *args[2]; /* arg1 & arg2 */
int daemonize; int daemonize;
char *conffile; char *conffile;
}; };
static error_t parse_opt (int key, char *arg, struct argp_state *state) static error_t parse_opt(int key, char *arg, struct argp_state *state) {
{
/* Get the input argument from argp_parse, which we /* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */ know is a pointer to our arguments structure. */
struct arguments *arguments = (struct arguments *)state->input; struct arguments *arguments = (struct arguments *)state->input;
switch (key) switch (key) {
{
case 'd': case 'd':
arguments->daemonize = 1; arguments->daemonize = 1;
break; break;
@ -54,10 +51,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
/* Our argp parser. */ /* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc}; static struct argp argp = {options, parse_opt, args_doc, doc};
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
struct arguments args; struct arguments args;
args.conffile = NULL; args.conffile = NULL;
args.daemonize = 0; args.daemonize = 0;
@ -66,8 +60,7 @@ int main(int argc, char **argv)
if (args.daemonize) if (args.daemonize)
skeleton_daemon(); skeleton_daemon();
while (1) while (1) {
{
// TODO: Insert daemon code here. // TODO: Insert daemon code here.
http_server_start(); http_server_start();
syslog(LOG_NOTICE, "First daemon started."); syslog(LOG_NOTICE, "First daemon started.");

View File

@ -25,8 +25,7 @@ https://github.com/benhoyt/inih
#define MAX_NAME 50 #define MAX_NAME 50
/* Strip whitespace chars off end of given string, in place. Return s. */ /* Strip whitespace chars off end of given string, in place. Return s. */
static char* rstrip(char* s) static char *rstrip(char *s) {
{
char *p = s + strlen(s); char *p = s + strlen(s);
while (p > s && isspace((unsigned char)(*--p))) while (p > s && isspace((unsigned char)(*--p)))
*p = '\0'; *p = '\0';
@ -34,8 +33,7 @@ static char* rstrip(char* s)
} }
/* Return pointer to first non-whitespace char in given string. */ /* Return pointer to first non-whitespace char in given string. */
static char* lskip(const char* s) static char *lskip(const char *s) {
{
while (*s && isspace((unsigned char)(*s))) while (*s && isspace((unsigned char)(*s)))
s++; s++;
return (char *)s; return (char *)s;
@ -44,8 +42,7 @@ static char* lskip(const char* s)
/* Return pointer to first char (of chars) or inline comment in given string, /* Return pointer to first char (of chars) or inline comment in given string,
or pointer to null at end of string if neither found. Inline comment must or pointer to null at end of string if neither found. Inline comment must
be prefixed by a whitespace character to register as a comment. */ be prefixed by a whitespace character to register as a comment. */
static char* find_chars_or_comment(const char* s, const char* chars) static char *find_chars_or_comment(const char *s, const char *chars) {
{
#if INI_ALLOW_INLINE_COMMENTS #if INI_ALLOW_INLINE_COMMENTS
int was_space = 0; int was_space = 0;
while (*s && (!chars || !strchr(chars, *s)) && while (*s && (!chars || !strchr(chars, *s)) &&
@ -62,8 +59,7 @@ static char* find_chars_or_comment(const char* s, const char* chars)
} }
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */ /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
static char* strncpy0(char* dest, const char* src, size_t size) static char *strncpy0(char *dest, const char *src, size_t size) {
{
strncpy(dest, src, size); strncpy(dest, src, size);
dest[size - 1] = '\0'; dest[size - 1] = '\0';
return dest; return dest;
@ -71,8 +67,7 @@ static char* strncpy0(char* dest, const char* src, size_t size)
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler, int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
void* user) void *user) {
{
/* Uses a fair bit of stack (use heap instead if you need to) */ /* Uses a fair bit of stack (use heap instead if you need to) */
#if INI_USE_STACK #if INI_USE_STACK
char line[INI_MAX_LINE]; char line[INI_MAX_LINE];
@ -129,13 +124,11 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
*end = '\0'; *end = '\0';
strncpy0(section, start + 1, sizeof(section)); strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0'; *prev_name = '\0';
} } else if (!error) {
else if (!error) {
/* No ']' found on section line */ /* No ']' found on section line */
error = lineno; error = lineno;
} }
} } else if (*start) {
else if (*start) {
/* Not a comment, must be a name[=:]value pair */ /* Not a comment, must be a name[=:]value pair */
end = find_chars_or_comment(start, "=:"); end = find_chars_or_comment(start, "=:");
if (*end == '=' || *end == ':') { if (*end == '=' || *end == ':') {
@ -153,8 +146,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
strncpy0(prev_name, name, sizeof(prev_name)); strncpy0(prev_name, name, sizeof(prev_name));
if (!handler(user, section, name, value) && !error) if (!handler(user, section, name, value) && !error)
error = lineno; error = lineno;
} } else if (!error) {
else if (!error) {
/* No '=' or ':' found on name[=:]value line */ /* No '=' or ':' found on name[=:]value line */
error = lineno; error = lineno;
} }
@ -174,14 +166,12 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse_file(FILE* file, ini_handler handler, void* user) int ini_parse_file(FILE *file, ini_handler handler, void *user) {
{
return ini_parse_stream((ini_reader)fgets, file, handler, user); return ini_parse_stream((ini_reader)fgets, file, handler, user);
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse(const char* filename, ini_handler handler, void* user) int ini_parse(const char *filename, ini_handler handler, void *user) {
{
FILE *file; FILE *file;
int error; int error;

View File

@ -28,13 +28,13 @@ static CONF *load_config_file(const char *configfile);
static int reply_command(CONF *conf, char *section, char *engine, static int reply_command(CONF *conf, char *section, char *engine,
char *queryfile, char *passin, char *inkey, char *queryfile, char *passin, char *inkey,
const EVP_MD *md, char *signer, char *chain, const EVP_MD *md, char *signer, char *chain,
const char *policy, char *in, int token_in, const char *policy, char *in, int token_in, char *out,
char *out, int token_out, int text); int token_out, int text);
static TS_RESP *read_PKCS7(BIO *in_bio); static TS_RESP *read_PKCS7(BIO *in_bio);
static TS_RESP *create_response(CONF *conf, const char *section, char *engine, static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
char *queryfile, char *passin, char *queryfile, char *passin, char *inkey,
char *inkey, const EVP_MD *md, char *signer, const EVP_MD *md, char *signer, char *chain,
char *chain, const char *policy); const char *policy);
static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data); static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
static ASN1_INTEGER *next_serial(const char *serialfile); static ASN1_INTEGER *next_serial(const char *serialfile);
static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial); static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
@ -44,7 +44,6 @@ static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
#define FORMAT_TEXT (1 | B_FORMAT_TEXT) /* Generic text */ #define FORMAT_TEXT (1 | B_FORMAT_TEXT) /* Generic text */
#define FORMAT_ASN1 4 /* ASN.1/DER */ #define FORMAT_ASN1 4 /* ASN.1/DER */
/* /*
int ts_http_respond(short event, ad_conn_t *conn, void *userdata) { int ts_http_respond(short event, ad_conn_t *conn, void *userdata) {
if (event & AD_EVENT_READ) { if (event & AD_EVENT_READ) {
@ -67,10 +66,8 @@ static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
} }
*/ */
// This function will be called by civetweb on every new request. // This function will be called by civetweb on every new request.
static int begin_request_handler(struct mg_connection *conn) static int begin_request_handler(struct mg_connection *conn) {
{
const struct mg_request_info *request_info = mg_get_request_info(conn); const struct mg_request_info *request_info = mg_get_request_info(conn);
char content[100]; char content[100];
@ -100,7 +97,8 @@ int http_server_start() {
// List of options. Last element must be NULL. // List of options. Last element must be NULL.
const char *options[] = {"listening_ports", "8080", NULL}; const char *options[] = {"listening_ports", "8080", NULL};
// Prepare callbacks structure. We have only one callback, the rest are NULL. // Prepare callbacks structure. We have only one callback, the rest are
// NULL.
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));
callbacks.begin_request = begin_request_handler; callbacks.begin_request = begin_request_handler;
@ -121,8 +119,7 @@ int http_server_start() {
* Configuration file-related function definitions. * Configuration file-related function definitions.
*/ */
static ASN1_OBJECT *txt2obj(const char *oid) static ASN1_OBJECT *txt2obj(const char *oid) {
{
ASN1_OBJECT *oid_obj = NULL; ASN1_OBJECT *oid_obj = NULL;
if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL) if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
@ -138,7 +135,8 @@ static ASN1_OBJECT *txt2obj(const char *oid)
// if (conf != NULL) { // if (conf != NULL) {
// const char *p; // const char *p;
// //
//// BIO_printf(bio_err, "Using configuration from %s\n", configfile); //// BIO_printf(bio_err, "Using configuration from %s\n",
///configfile);
// p = NCONF_get_string(conf, NULL, ENV_OID_FILE); // p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
// if (p != NULL) { // if (p != NULL) {
// BIO *oid_bio = BIO_new_file(p, "r"); // BIO *oid_bio = BIO_new_file(p, "r");
@ -163,9 +161,8 @@ static ASN1_OBJECT *txt2obj(const char *oid)
static int reply_command(CONF *conf, char *section, char *engine, static int reply_command(CONF *conf, char *section, char *engine,
char *queryfile, char *passin, char *inkey, char *queryfile, char *passin, char *inkey,
const EVP_MD *md, char *signer, char *chain, const EVP_MD *md, char *signer, char *chain,
const char *policy, char *in, int token_in, const char *policy, char *in, int token_in, char *out,
char *out, int token_out, int text) int token_out, int text) {
{
int ret = 0; int ret = 0;
TS_RESP *response = NULL; TS_RESP *response = NULL;
BIO *in_bio = NULL; BIO *in_bio = NULL;
@ -184,19 +181,22 @@ static int reply_command(CONF *conf, char *section, char *engine,
response = d2i_TS_RESP_bio(in_bio, NULL); response = d2i_TS_RESP_bio(in_bio, NULL);
} }
} else { } else {
response = create_response(conf, section, engine, queryfile, response = create_response(conf, section, engine, queryfile, passin,
passin, inkey, md, signer, chain, policy); inkey, md, signer, chain, policy);
// if (response) // if (response)
// BIO_printf(bio_err, "Response has been generated.\n"); // BIO_printf(bio_err, "Response has been
//generated.\n");
// else // else
// BIO_printf(bio_err, "Response is not generated.\n"); // BIO_printf(bio_err, "Response is not
//generated.\n");
} }
if (response == NULL) if (response == NULL)
goto end; goto end;
/* Write response. */ /* Write response. */
if (text) { if (text) {
// if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL) // if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) ==
//NULL)
// goto end; // goto end;
if (token_out) { if (token_out) {
TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response); TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
@ -207,7 +207,8 @@ static int reply_command(CONF *conf, char *section, char *engine,
goto end; goto end;
} }
} else { } else {
// if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL) // if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) ==
//NULL)
// goto end; // goto end;
if (token_out) { if (token_out) {
PKCS7 *token = TS_RESP_get_token(response); PKCS7 *token = TS_RESP_get_token(response);
@ -233,8 +234,7 @@ end:
} }
/* Reads a PKCS7 token and adds default 'granted' status info to it. */ /* Reads a PKCS7 token and adds default 'granted' status info to it. */
static TS_RESP *read_PKCS7(BIO *in_bio) static TS_RESP *read_PKCS7(BIO *in_bio) {
{
int ret = 0; int ret = 0;
PKCS7 *token = NULL; PKCS7 *token = NULL;
TS_TST_INFO *tst_info = NULL; TS_TST_INFO *tst_info = NULL;
@ -270,10 +270,9 @@ end:
} }
static TS_RESP *create_response(CONF *conf, const char *section, char *engine, static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
char *queryfile, char *passin, char *queryfile, char *passin, char *inkey,
char *inkey, const EVP_MD *md, char *signer, const EVP_MD *md, char *signer, char *chain,
char *chain, const char *policy) const char *policy) {
{
int ret = 0; int ret = 0;
TS_RESP *response = NULL; TS_RESP *response = NULL;
BIO *query_bio = NULL; BIO *query_bio = NULL;
@ -335,8 +334,7 @@ end:
return response; return response;
} }
static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data) static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data) {
{
const char *serial_file = (const char *)data; const char *serial_file = (const char *)data;
ASN1_INTEGER *serial = next_serial(serial_file); ASN1_INTEGER *serial = next_serial(serial_file);
@ -351,8 +349,7 @@ static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
return serial; return serial;
} }
static ASN1_INTEGER *next_serial(const char *serialfile) static ASN1_INTEGER *next_serial(const char *serialfile) {
{
int ret = 0; int ret = 0;
BIO *in = NULL; BIO *in = NULL;
ASN1_INTEGER *serial = NULL; ASN1_INTEGER *serial = NULL;
@ -363,14 +360,17 @@ static ASN1_INTEGER *next_serial(const char *serialfile)
if ((in = BIO_new_file(serialfile, "r")) == NULL) { if ((in = BIO_new_file(serialfile, "r")) == NULL) {
ERR_clear_error(); ERR_clear_error();
// BIO_printf(bio_err, "Warning: could not open file %s for " // BIO_printf(bio_err, "Warning: could not open file %s for
// "reading, using serial number: 1\n", serialfile); //"
// "reading, using serial number: 1\n",
//serialfile);
if (!ASN1_INTEGER_set(serial, 1)) if (!ASN1_INTEGER_set(serial, 1))
goto err; goto err;
} else { } else {
char buf[1024]; char buf[1024];
if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) { if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
// BIO_printf(bio_err, "unable to load number from %s\n", // BIO_printf(bio_err, "unable to load number from
//%s\n",
// serialfile); // serialfile);
goto err; goto err;
} }
@ -395,8 +395,7 @@ err:
return serial; return serial;
} }
static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial) static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial) {
{
int ret = 0; int ret = 0;
BIO *out = NULL; BIO *out = NULL;
@ -409,7 +408,8 @@ static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
ret = 1; ret = 1;
err: err:
if (!ret) if (!ret)
// BIO_printf(bio_err, "could not save serial number to %s\n", // BIO_printf(bio_err, "could not save serial number to
//%s\n",
// serialfile); // serialfile);
BIO_free_all(out); BIO_free_all(out);
return ret; return ret;

View File

@ -7,8 +7,7 @@
#include <syslog.h> #include <syslog.h>
#include "utils.h" #include "utils.h"
void skeleton_daemon() void skeleton_daemon() {
{
pid_t pid; pid_t pid;
/* Fork off the parent process */ /* Fork off the parent process */
@ -51,8 +50,7 @@ void skeleton_daemon()
/* Close all open file descriptors */ /* Close all open file descriptors */
int x; int x;
for (x = sysconf(_SC_OPEN_MAX); x>0; x--) for (x = sysconf(_SC_OPEN_MAX); x > 0; x--) {
{
close(x); close(x);
} }