Initial commit

This is scdrand, a program that will connect to a running scdaemon
(the daemon used by GnuPG to access smart cards) and extract
random numbers generated by a smart card.
master
Damien Goutte-Gattat 2014-08-08 00:50:53 +02:00
commit 6b920141ba
13 changed files with 638 additions and 0 deletions

22
.gitignore vendored Normal file
View File

@ -0,0 +1,22 @@
# Autotools-generated files
Makefile.in
Makefile
autom4te.cache
config.h.in*
config.h
config.log
config.status
config
configure
stamp-*
# Autoconf macros files
*.m4
# Built files
src/*.o
src/.deps
src/scdrand
lib/*.o
lib/.deps
lib/lib*.a

3
Makefile.am Normal file
View File

@ -0,0 +1,3 @@
SUBDIRS = lib src
ACLOCAL_AMFLAGS = -I m4 --install

32
configure.ac Normal file
View File

@ -0,0 +1,32 @@
dnl Configure template for the scdrand package
AC_INIT([scdrand], [0.1.0],
[dgouttegattat@incenp.org])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_LIBOBJ_DIR([lib])
AC_USE_SYSTEM_EXTENSIONS
AM_INIT_AUTOMAKE([foreign])
dnl Check for development tools
AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_INSTALL
dnl Check for some non-ubiquitous
ICP_CHECK_NOTCH_FUNCS
dnl Output files
AC_CONFIG_FILES([Makefile lib/Makefile src/Makefile])
AC_OUTPUT
dnl Summary
echo "
${PACKAGE_NAME} version ${PACKAGE_VERSION}
Configuration complete
Prefix: '${prefix}'
Compiler: '${CC} ${CFLAGS} ${CPPFLAGS}'
"

5
lib/Makefile.am Normal file
View File

@ -0,0 +1,5 @@
noinst_LIBRARIES = libscdrand.a
libscdrand_a_SOURCES = err.compat.h compat.h xmem.c xmem.h
libscdrand_a_LIBADD = $(LIBOBJS)

108
lib/compat.h Normal file
View File

@ -0,0 +1,108 @@
/*
* compat - Incenp.org Notch Library: header for missing functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20110203_COMPAT_H
#define ICP20110203_COMPAT_H
#ifdef __cpluscplus
extern "C" {
#endif
#ifndef HAVE_ASPRINTF
#include <stdarg.h>
int
asprintf(char **, const char *, ...);
int
vasprintf(char **, const char *, va_list);
#endif /* ! HAVE_ASPRINTF */
#ifndef HAVE_GETDELIM
#include <stdio.h>
#include <stdlib.h>
ssize_t
getdelim(char **, size_t *, int, FILE *);
#define getline(l,n,f) getdelim((l), (n), '\n', (f))
#endif /* ! HAVE_GETDELIM */
#ifndef HAVE_GETSUBOPT
int
getsubopt(char **, char *const *, char **);
#endif /* ! HAVE_GETSUBOPT */
#ifndef HAVE_STRCHRNUL
char *
strchrnul(const char *, int);
#endif /* ! HAVE_STRCHRNUL */
#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
/*
* The GNU C library, combined with the GNU linker, automatically
* stores the program name into the global variable
* `program_invocation_short_name'. So `setprogname()' can be defined
* to nothing and `getprogname()' can be aliased to that variable.
*/
#include <errno.h>
#define setprogname(a)
#define getprogname() program_invocation_short_name
#else
/*
* If the above-mentioned variable is not available, check for the
* BSD functions `getprogname()' and `setprogname()', and provide our
* own implementation if they are missing.
*/
#ifndef HAVE_SETPROGNAME
void
setprogname(const char *);
#endif
#ifndef HAVE_GETPROGNAME
const char *
setprogname(void);
#endif
#endif /* ! HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME */
#ifdef __cplusplus
}
#endif
#endif /* !ICP20110203_COMPAT_H */

110
lib/err.c Normal file
View File

@ -0,0 +1,110 @@
/*
* err - Incenp.org Notch Library: (v)(err|warn)(x) replacement
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#define ERR_EXIT_ON_ERROR 0x01
#define ERR_PRINT_ERRNO 0x02
const char *getprogname(void);
static void
do_error(int opt, int eval, const char *fmt, va_list ap)
{
fprintf(stderr, "%s: ", getprogname());
if ( fmt )
vfprintf(stderr, fmt, ap);
if ( opt & ERR_PRINT_ERRNO )
fprintf(stderr, ": %s", strerror(errno));
fputc('\n', stderr);
if ( opt & ERR_EXIT_ON_ERROR )
exit(eval);
}
void
err(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
va_end(ap);
}
void
errx(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
va_end(ap);
}
void
verr(int eval, const char *fmt, va_list ap)
{
do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
}
void
verrx(int eval, const char *fmt, va_list ap)
{
do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
}
void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
va_end(ap);
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
do_error(0, 0, fmt, ap);
va_end(ap);
}
void
vwarn(const char *fmt, va_list ap)
{
do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
}
void
vwarnx(const char *fmt, va_list ap)
{
do_error(0, 0, fmt, ap);
}

48
lib/err.compat.h Normal file
View File

@ -0,0 +1,48 @@
/*
* err - Incenp.org Notch Library: (v)(err|warn)(x) replacement
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20110203_ERR_H
#define ICP20110203_ERR_H
#include <stdarg.h>
#ifdef __cpluscplus
extern "C" {
#endif
void err(int, const char *, ...);
void errx(int, const char *, ...);
void verr(int, const char *, va_list);
void verrx(int, const char *, va_list);
void warn(const char *, ...);
void warnx(const char *, ...);
void vwarn(const char *, va_list);
void vwarnx(const char *, va_list);
#ifdef __cplusplus
}
#endif
#endif /* !ICP20110203_ERR_H */

31
lib/progname.c Normal file
View File

@ -0,0 +1,31 @@
/*
* progname - Incenp.org Notch Library: (set/get)progname replacement
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
static const char *progname;
void
setprogname(const char *name)
{
progname = name;
}
const char *
getprogname(void)
{
return progname;
}

114
lib/xmem.c Normal file
View File

@ -0,0 +1,114 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <xmem.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
void (*xmem_error)(const char *, unsigned int) = NULL;
#define mem_error(f, l) \
do { \
if ( xmem_error ) \
(*xmem_error)((f), (l)); \
err(EXIT_FAILURE, "%s, %d", (f), (l)); \
} while ( 0 )
void *
do_malloc(size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = malloc(s)) && s )
mem_error(file, line);
return p;
}
void *
do_calloc(size_t n, size_t s, const char *file, unsigned int line)
{
void *p;
if ( ! (p = calloc(n, s)) && n && s )
mem_error(file, line);
return p;
}
void *
do_realloc(void *p, size_t s, const char *file, unsigned int line)
{
void *np;
if ( ! (np = realloc(p, s)) && s )
mem_error(file, line);
return np;
}
char *
do_strdup(const char *s, const char *file, unsigned int line)
{
char *dup;
if ( ! (dup = strdup(s)) && s )
mem_error(file, line);
return dup;
}
int
do_asprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
...)
{
int ret;
va_list ap;
va_start(ap, line);
ret = vasprintf(s, fmt, ap);
va_end(ap);
if ( ret == -1 )
mem_error(file, line);
return ret;
}
int
do_vasprintf(char **s,
const char *fmt,
const char *file,
unsigned int line,
va_list ap)
{
int ret;
if ( (ret = vasprintf(s, fmt, ap)) == -1 )
mem_error(file, line);
return ret;
}

60
lib/xmem.h Normal file
View File

@ -0,0 +1,60 @@
/*
* xmem - Incenp.org Notch Library: die-on-error memory functions
* Copyright (C) 2011 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20110203_XMEM_H
#define ICP20110203_XMEM_H
#include <stdlib.h>
#include <stdarg.h>
#ifdef __cpluscplus
extern "C" {
#endif
extern void (*xmem_error)(const char *, unsigned int);
void *
do_malloc(size_t, const char *, unsigned int);
void *
do_calloc(size_t, size_t, const char *, unsigned int);
void *
do_realloc(void *, size_t, const char *, unsigned int);
char *
do_strdup(const char *, const char *, unsigned int);
int
do_asprintf(char **, const char *, const char *, unsigned int, ...);
int
do_vasprintf(char **, const char *, const char *, unsigned int, va_list);
#define xmalloc(s) do_malloc(s, __FILE__, __LINE__)
#define xcalloc(n,s) do_calloc(n, s, __FILE__, __LINE__)
#define xrealloc(p,s) do_realloc(p, s, __FILE__, __LINE__)
#define xstrdup(s) do_strdup(s, __FILE__, __LINE__)
#define xasprintf(s,f,...) do_asprintf(s, f, __FILE__, __LINE__, __VA_ARGS__)
#define xvasprintf(s,f,v) do_vasprintf(s, f, __FILE__, __LINE__, v)
#ifdef __cplusplus
}
#endif
#endif /* !ICP20110203_XMEM_H */

16
m4/notch.m4 Normal file
View File

@ -0,0 +1,16 @@
dnl ICP_CHECK_NOTCH_FUNCS
dnl Check for various functions that may not be present
dnl everywhere, and for which we provide a replacement.
dnl
AC_DEFUN([ICP_CHECK_NOTCH_FUNCS],[
AC_CHECK_HEADERS([err.h], [], [AC_CONFIG_LINKS([err.h:lib/err.compat.h])])
AC_REPLACE_FUNCS([err])
AC_CHECK_DECLS([program_invocation_short_name], [],
[AC_CHECK_FUNCS([getprogname setprogname], [],
[AC_LIBOBJ([progname])])],
[#include <errno.h>
])
AC_CHECK_DECLS([P_tmpdir])
AH_BOTTOM([#include <compat.h>
])
])

7
src/Makefile.am Normal file
View File

@ -0,0 +1,7 @@
bin_PROGRAMS = scdrand
scdrand_SOURCES = scdrand.c
AM_CPPFLAGS = -I$(top_srcdir)/lib
AM_LDFLAGS = -L$(top_builddir)/lib
LDADD = -lscdrand

82
src/scdrand.c Normal file
View File

@ -0,0 +1,82 @@
/*
* scdrand - Extract random numbers from a smart card
* Copyright (C) 2014 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <err.h>
static void
usage(int status)
{
puts("Usage: scdrand\n");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
}
static void
info(void)
{
printf("\
scdrand %s\n\
Copyright (C) 2014 Damien Goutte-Gattat\n\
\n\
This program is released under the GNU General Public License.\n\
", VERSION);
exit(EXIT_SUCCESS);
}
int
main(int argc, char **argv)
{
int c;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
while ( (c = getopt_long(argc, argv, "hv", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
}
}
return EXIT_SUCCESS;
}