2003-12-16 11:32:52 +00:00
|
|
|
/* watchgnupg.c - Socket server for GnuPG logs
|
2011-02-03 16:50:01 +01:00
|
|
|
* Copyright (C) 2003, 2004, 2010 Free Software Foundation, Inc.
|
2020-01-20 16:50:56 +01:00
|
|
|
* Copyright (C) 2003, 2004, 2010, 2020 g10 Code GmbH
|
2003-12-16 11:32:52 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-12-16 11:32:52 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-12-16 11:32:52 +00:00
|
|
|
*/
|
|
|
|
|
2004-02-12 10:02:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2003-12-16 11:32:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2020-01-20 16:50:56 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2010-08-09 15:40:29 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2004-01-16 22:38:58 +00:00
|
|
|
#include <fcntl.h>
|
2003-12-16 16:31:42 +00:00
|
|
|
#include <time.h>
|
2014-12-01 15:55:28 +01:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
#endif
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
#define PGM "watchgnupg"
|
|
|
|
|
2009-05-19 22:39:45 +00:00
|
|
|
/* Allow for a standalone build on most systems. */
|
2004-02-12 10:02:22 +00:00
|
|
|
#ifdef VERSION
|
2013-11-18 14:09:47 +01:00
|
|
|
#define MYVERSION_LINE PGM " ("GNUPG_NAME") " VERSION
|
2004-02-12 10:02:22 +00:00
|
|
|
#define BUGREPORT_LINE "\nReport bugs to <bug-gnupg@gnu.org>.\n"
|
|
|
|
#else
|
2010-08-09 15:40:29 +00:00
|
|
|
#define MYVERSION_LINE PGM " (standalone build) " __DATE__
|
2004-02-12 10:02:22 +00:00
|
|
|
#define BUGREPORT_LINE ""
|
|
|
|
#endif
|
2009-05-19 22:39:45 +00:00
|
|
|
#if !defined(SUN_LEN) || !defined(PF_LOCAL) || !defined(AF_LOCAL)
|
2015-04-24 16:10:15 +02:00
|
|
|
#define GNUPG_COMMON_NEED_AFLOCAL
|
2012-08-24 09:29:56 +02:00
|
|
|
#include "../common/mischelp.h"
|
2005-05-31 20:03:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-20 16:50:56 +01:00
|
|
|
#ifndef GNUPG_DEF_COPYRIGHT_LINE
|
|
|
|
#define GNUPG_DEF_COPYRIGHT_LINE "Copyright (C) 2020 g10 Code GmbH"
|
|
|
|
#endif
|
|
|
|
|
2005-05-31 20:03:04 +00:00
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
static int verbose;
|
2010-10-06 11:29:10 +00:00
|
|
|
static int time_only;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
die (const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
|
|
|
fflush (stdout);
|
|
|
|
fprintf (stderr, "%s: ", PGM);
|
|
|
|
|
|
|
|
va_start (arg_ptr, format);
|
|
|
|
vfprintf (stderr, format, arg_ptr);
|
|
|
|
va_end (arg_ptr);
|
|
|
|
putc ('\n', stderr);
|
|
|
|
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
static void
|
|
|
|
err (const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
fflush (stdout);
|
|
|
|
fprintf (stderr, "%s: ", PGM);
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
va_start (arg_ptr, format);
|
|
|
|
vfprintf (stderr, format, arg_ptr);
|
|
|
|
va_end (arg_ptr);
|
|
|
|
putc ('\n', stderr);
|
|
|
|
}
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
static void *
|
|
|
|
xmalloc (size_t n)
|
|
|
|
{
|
|
|
|
void *p = malloc (n);
|
|
|
|
if (!p)
|
|
|
|
die ("out of core");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
xcalloc (size_t n, size_t m)
|
|
|
|
{
|
|
|
|
void *p = calloc (n, m);
|
|
|
|
if (!p)
|
|
|
|
die ("out of core");
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
xrealloc (void *old, size_t n)
|
|
|
|
{
|
|
|
|
void *p = realloc (old, n);
|
|
|
|
if (!p)
|
|
|
|
die ("out of core");
|
|
|
|
return p;
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
struct client_s {
|
|
|
|
struct client_s *next;
|
|
|
|
int fd;
|
|
|
|
size_t size; /* Allocated size of buffer. */
|
|
|
|
size_t len; /* Current length of buffer. */
|
|
|
|
unsigned char *buffer; /* Buffer to with data already read. */
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
};
|
|
|
|
typedef struct client_s *client_t;
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
/* The list of all connected peers. */
|
|
|
|
static client_t client_list;
|
|
|
|
|
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
|
2003-12-16 16:31:42 +00:00
|
|
|
static void
|
|
|
|
print_fd_and_time (int fd)
|
|
|
|
{
|
|
|
|
struct tm *tp;
|
|
|
|
time_t atime = time (NULL);
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2003-12-16 16:31:42 +00:00
|
|
|
tp = localtime (&atime);
|
2011-02-03 16:50:01 +01:00
|
|
|
if (time_only)
|
2010-10-06 11:29:10 +00:00
|
|
|
printf ("%3d - %02d:%02d:%02d ",
|
|
|
|
fd,
|
|
|
|
tp->tm_hour, tp->tm_min, tp->tm_sec );
|
|
|
|
else
|
|
|
|
printf ("%3d - %04d-%02d-%02d %02d:%02d:%02d ",
|
|
|
|
fd,
|
|
|
|
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
|
|
|
|
tp->tm_hour, tp->tm_min, tp->tm_sec );
|
2003-12-16 16:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
/* Print LINE for the client identified by C. Calling this function
|
2015-03-19 11:14:52 +01:00
|
|
|
with LINE set to NULL, will flush the internal buffer. */
|
2003-12-16 11:32:52 +00:00
|
|
|
static void
|
|
|
|
print_line (client_t c, const char *line)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
{
|
|
|
|
if (c->buffer && c->len)
|
|
|
|
{
|
2003-12-16 16:31:42 +00:00
|
|
|
print_fd_and_time (c->fd);
|
2011-02-03 16:50:01 +01:00
|
|
|
fwrite (c->buffer, c->len, 1, stdout);
|
2003-12-16 11:32:52 +00:00
|
|
|
putc ('\n', stdout);
|
|
|
|
c->len = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((s = strchr (line, '\n')))
|
|
|
|
{
|
2003-12-16 16:31:42 +00:00
|
|
|
print_fd_and_time (c->fd);
|
2003-12-16 11:32:52 +00:00
|
|
|
if (c->buffer && c->len)
|
|
|
|
{
|
2011-02-03 16:50:01 +01:00
|
|
|
fwrite (c->buffer, c->len, 1, stdout);
|
2003-12-16 11:32:52 +00:00
|
|
|
c->len = 0;
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
fwrite (line, s - line + 1, 1, stdout);
|
2003-12-16 11:32:52 +00:00
|
|
|
line = s + 1;
|
|
|
|
}
|
|
|
|
n = strlen (line);
|
2003-12-16 14:20:45 +00:00
|
|
|
if (n)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
2003-12-16 14:20:45 +00:00
|
|
|
if (c->len + n >= c->size)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
2003-12-16 14:20:45 +00:00
|
|
|
c->size += ((n + 255) & ~255);
|
|
|
|
c->buffer = (c->buffer
|
|
|
|
? xrealloc (c->buffer, c->size)
|
|
|
|
: xmalloc (c->size));
|
2003-12-16 11:32:52 +00:00
|
|
|
}
|
|
|
|
memcpy (c->buffer + c->len, line, n);
|
|
|
|
c->len += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
static void
|
|
|
|
setup_client (int server_fd, int is_un)
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2010-10-05 19:05:43 +00:00
|
|
|
struct sockaddr_un addr_un;
|
|
|
|
struct sockaddr_in addr_in;
|
|
|
|
struct sockaddr *addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int fd;
|
|
|
|
client_t client;
|
|
|
|
|
|
|
|
if (is_un)
|
|
|
|
{
|
|
|
|
addr = (struct sockaddr *)&addr_un;
|
|
|
|
addrlen = sizeof addr_un;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addr = (struct sockaddr *)&addr_in;
|
|
|
|
addrlen = sizeof addr_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = accept (server_fd, addr, &addrlen);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
printf ("[accepting %s connection failed: %s]\n",
|
|
|
|
is_un? "local":"tcp", strerror (errno));
|
|
|
|
}
|
|
|
|
else if (fd >= FD_SETSIZE)
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
printf ("[connection request denied: too many connections]\n");
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else
|
2010-10-05 19:05:43 +00:00
|
|
|
{
|
|
|
|
for (client = client_list; client && client->fd != -1;
|
|
|
|
client = client->next)
|
|
|
|
;
|
|
|
|
if (!client)
|
|
|
|
{
|
|
|
|
client = xcalloc (1, sizeof *client);
|
|
|
|
client->next = client_list;
|
|
|
|
client_list = client;
|
|
|
|
}
|
|
|
|
client->fd = fd;
|
|
|
|
printf ("[client at fd %d connected (%s)]\n",
|
|
|
|
client->fd, is_un? "local":"tcp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-20 16:50:56 +01:00
|
|
|
/* Get the logsocketname by reading from gpgconf. Caller needs to
|
|
|
|
* release the buffer. */
|
|
|
|
static char *
|
|
|
|
get_logname (const char *homedir)
|
|
|
|
{
|
|
|
|
int rp[2];
|
|
|
|
pid_t pid;
|
|
|
|
int i, c;
|
|
|
|
unsigned int pos;
|
|
|
|
char filename[1024], *buf;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (pipe (rp) == -1)
|
|
|
|
die ("error creating a pipe: %s", strerror (errno));
|
|
|
|
|
|
|
|
pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
die ("error forking process: %s", strerror (errno));
|
|
|
|
|
|
|
|
if (!pid)
|
|
|
|
{ /* Child. */
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open ("/dev/null", O_WRONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
die ("can't open '/dev/null': %s", strerror (errno));
|
|
|
|
if (fd != 0 && dup2 (fd, 0) == -1)
|
|
|
|
die ("dup2 stderr failed: %s", strerror (errno));
|
|
|
|
|
|
|
|
/* Connect stdout to our pipe. */
|
|
|
|
if (rp[1] != 1 && dup2 (rp[1], 1) == -1)
|
|
|
|
die ("dup2 stdout failed: %s", strerror (errno));
|
|
|
|
|
|
|
|
/* Close other files. 20 is an arbitrary number; at this point
|
|
|
|
* we have not opened many files. */
|
|
|
|
for (i=3; i < 20; i++)
|
|
|
|
close (i);
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
if (homedir)
|
|
|
|
execlp ("gpgconf", "gpgconf", "--homedir", homedir,
|
|
|
|
"-0", "--list-dirs", "socketdir", NULL);
|
|
|
|
else
|
|
|
|
execlp ("gpgconf", "gpgconf",
|
|
|
|
"-0", "--list-dirs", "socketdir", NULL);
|
|
|
|
die ("failed to exec gpgconf: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent. */
|
|
|
|
close (rp[1]);
|
|
|
|
|
|
|
|
fp = fdopen (rp[0], "r");
|
|
|
|
if (!fp)
|
|
|
|
die ("can't fdopen pipe for reading: %s", strerror (errno));
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while ((c=getc (fp)) != EOF)
|
|
|
|
{
|
|
|
|
if (pos+1 >= sizeof filename)
|
|
|
|
die ("gpgconf returned a too long filename\n");
|
|
|
|
filename[pos++] = c;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
if (c == EOF)
|
|
|
|
die ("error reading from gpgconf: %s", "premature EOF");
|
|
|
|
|
|
|
|
while ((i=waitpid (pid, NULL, 0)) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (i == -1)
|
|
|
|
die ("waiting for gpgconf failed: %s", strerror (errno));
|
|
|
|
|
|
|
|
buf = xmalloc (strlen (filename) + 6 + 1);
|
|
|
|
strcpy (buf, filename);
|
|
|
|
strcat (buf, "/S.log");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
|
2004-02-12 10:02:22 +00:00
|
|
|
static void
|
|
|
|
print_version (int with_help)
|
|
|
|
{
|
|
|
|
fputs (MYVERSION_LINE "\n"
|
2018-06-12 16:11:19 +02:00
|
|
|
GNUPG_DEF_COPYRIGHT_LINE "\n"
|
2010-08-09 15:40:29 +00:00
|
|
|
"License GPLv3+: "
|
2017-01-23 19:16:55 +01:00
|
|
|
"GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n"
|
2010-08-09 15:40:29 +00:00
|
|
|
"This is free software: you are free to change and redistribute it.\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\n",
|
2004-02-12 10:02:22 +00:00
|
|
|
stdout);
|
|
|
|
if (with_help)
|
2011-02-03 16:50:01 +01:00
|
|
|
fputs
|
2010-10-05 19:05:43 +00:00
|
|
|
("\n"
|
2020-01-20 16:50:56 +01:00
|
|
|
"Usage: " PGM " [OPTIONS] [SOCKETNAME]\n"
|
2010-10-05 19:05:43 +00:00
|
|
|
" " PGM " [OPTIONS] PORT [SOCKETNAME]\n"
|
|
|
|
"Open the local socket SOCKETNAME (or the TCP port PORT)\n"
|
2020-01-20 16:50:56 +01:00
|
|
|
"and display log messages. If --tcp is not used and no\n"
|
|
|
|
"socket name is given, it is taken from the gpgconf tool.\n"
|
2010-10-05 19:05:43 +00:00
|
|
|
"\n"
|
2020-01-20 16:50:56 +01:00
|
|
|
" --tcp listen on a TCP port and optionally on a local socket\n"
|
|
|
|
" --force delete an already existing socket file\n"
|
|
|
|
" --verbose enable extra informational output\n"
|
|
|
|
" --time-only print only the time; not a full timestamp\n"
|
|
|
|
" --homedir DIR use DIR for gpgconf's --homedir option\n"
|
|
|
|
" --version print version of the program and exit\n"
|
|
|
|
" --help display this help and exit\n"
|
2010-10-05 19:05:43 +00:00
|
|
|
BUGREPORT_LINE, stdout );
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2004-02-12 10:02:22 +00:00
|
|
|
exit (0);
|
|
|
|
}
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2011-02-03 16:50:01 +01:00
|
|
|
int
|
2003-12-16 11:32:52 +00:00
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int last_argc = -1;
|
|
|
|
int force = 0;
|
2010-08-09 15:40:29 +00:00
|
|
|
int tcp = 0;
|
2020-01-20 16:50:56 +01:00
|
|
|
char *homedir = NULL;
|
|
|
|
char *logname = NULL;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2010-08-09 15:40:29 +00:00
|
|
|
struct sockaddr_un srvr_addr_un;
|
|
|
|
struct sockaddr_in srvr_addr_in;
|
2010-10-05 19:05:43 +00:00
|
|
|
struct sockaddr *addr_in = NULL;
|
|
|
|
struct sockaddr *addr_un = NULL;
|
|
|
|
socklen_t addrlen_in, addrlen_un;
|
2010-08-09 15:40:29 +00:00
|
|
|
unsigned short port;
|
2010-10-05 19:05:43 +00:00
|
|
|
int server_un, server_in;
|
2004-01-16 22:38:58 +00:00
|
|
|
int flags;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
if (argc)
|
|
|
|
{
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
while (argc && last_argc != argc )
|
|
|
|
{
|
|
|
|
last_argc = argc;
|
2004-02-12 10:02:22 +00:00
|
|
|
if (!strcmp (*argv, "--"))
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
|
|
|
argc--; argv++;
|
2004-02-12 10:02:22 +00:00
|
|
|
break;
|
2003-12-16 11:32:52 +00:00
|
|
|
}
|
2004-02-12 10:02:22 +00:00
|
|
|
else if (!strcmp (*argv, "--version"))
|
|
|
|
print_version (0);
|
|
|
|
else if (!strcmp (*argv, "--help"))
|
|
|
|
print_version (1);
|
|
|
|
else if (!strcmp (*argv, "--verbose"))
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
2004-02-12 10:02:22 +00:00
|
|
|
verbose = 1;
|
2003-12-16 11:32:52 +00:00
|
|
|
argc--; argv++;
|
|
|
|
}
|
2010-10-06 11:29:10 +00:00
|
|
|
else if (!strcmp (*argv, "--time-only"))
|
|
|
|
{
|
|
|
|
time_only = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2003-12-16 11:32:52 +00:00
|
|
|
else if (!strcmp (*argv, "--force"))
|
|
|
|
{
|
|
|
|
force = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2010-08-09 15:40:29 +00:00
|
|
|
else if (!strcmp (*argv, "--tcp"))
|
|
|
|
{
|
|
|
|
tcp = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
2020-01-20 16:50:56 +01:00
|
|
|
else if (!strcmp (*argv, "--homedir"))
|
|
|
|
{
|
|
|
|
argc--; argv++;
|
|
|
|
if (!argc)
|
|
|
|
die ("option --homedir requires an argument\n");
|
|
|
|
argc--;
|
|
|
|
homedir = *argv++;
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:50:56 +01:00
|
|
|
if (!tcp && argc == 1)
|
|
|
|
;
|
|
|
|
else if (tcp && (argc == 1 || argc == 2))
|
|
|
|
; /* Option --tcp optionally allows to also read from a socket. */
|
|
|
|
else if (!tcp && !argc)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
2020-01-20 16:50:56 +01:00
|
|
|
/* No args given - figure out the socket using gpgconf. We also
|
|
|
|
* force overwriting the socket because the constructed name
|
Spelling cleanup.
No functional changes, just fixing minor spelling issues.
---
Most of these were identified from the command line by running:
codespell \
--ignore-words-list fpr,stati,keyserver,keyservers,asign,cas,iff,ifset \
--skip '*.po,ChangeLog*,help.*.txt,*.jpg,*.eps,*.pdf,*.png,*.gpg,*.asc' \
doc g13 g10 kbx agent artwork scd tests tools am common dirmngr sm \
NEWS README README.maint TODO
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2020-02-18 09:34:42 -05:00
|
|
|
* can't be some accidentally given name. */
|
2020-01-20 16:50:56 +01:00
|
|
|
logname = get_logname (homedir);
|
|
|
|
force = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "usage: " PGM " [socketname]\n"
|
2010-10-05 19:05:43 +00:00
|
|
|
" " PGM " --tcp port [socketname]\n");
|
2004-02-12 10:02:22 +00:00
|
|
|
exit (1);
|
2003-12-16 11:32:52 +00:00
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
if (tcp)
|
|
|
|
{
|
|
|
|
port = atoi (*argv);
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
port = 0;
|
|
|
|
}
|
2020-01-20 16:50:56 +01:00
|
|
|
if (argc)
|
|
|
|
logname = *argv;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
setvbuf (stdout, NULL, _IOLBF, 0);
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
if (tcp)
|
2010-08-09 15:40:29 +00:00
|
|
|
{
|
2010-10-05 19:05:43 +00:00
|
|
|
int i = 1;
|
|
|
|
server_in = socket (PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (server_in == -1)
|
|
|
|
die ("socket(PF_INET) failed: %s\n", strerror (errno));
|
2011-02-03 16:50:01 +01:00
|
|
|
if (setsockopt (server_in, SOL_SOCKET, SO_REUSEADDR,
|
2010-10-05 19:05:43 +00:00
|
|
|
(unsigned char *)&i, sizeof (i)))
|
|
|
|
err ("setsockopt(SO_REUSEADDR) failed: %s\n", strerror (errno));
|
|
|
|
if (verbose)
|
2010-08-09 15:40:29 +00:00
|
|
|
fprintf (stderr, "listening on port %hu\n", port);
|
|
|
|
}
|
2010-10-05 19:05:43 +00:00
|
|
|
else
|
|
|
|
server_in = -1;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2020-01-20 16:50:56 +01:00
|
|
|
if (logname)
|
2010-10-05 19:05:43 +00:00
|
|
|
{
|
|
|
|
server_un = socket (PF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (server_un == -1)
|
|
|
|
die ("socket(PF_LOCAL) failed: %s\n", strerror (errno));
|
|
|
|
if (verbose)
|
2020-01-20 16:50:56 +01:00
|
|
|
fprintf (stderr, "listening on socket '%s'\n", logname);
|
2010-10-05 19:05:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
server_un = -1;
|
2003-12-16 11:32:52 +00:00
|
|
|
|
2004-01-16 22:38:58 +00:00
|
|
|
/* We better set the listening socket to non-blocking so that we
|
|
|
|
don't get bitten by race conditions in accept. The should not
|
|
|
|
happen for Unix Domain sockets but well, shit happens. */
|
2010-10-05 19:05:43 +00:00
|
|
|
if (server_in != -1)
|
|
|
|
{
|
|
|
|
flags = fcntl (server_in, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
die ("fcntl (F_GETFL) failed: %s\n", strerror (errno));
|
|
|
|
if ( fcntl (server_in, F_SETFL, (flags | O_NONBLOCK)) == -1)
|
|
|
|
die ("fcntl (F_SETFL) failed: %s\n", strerror (errno));
|
|
|
|
}
|
|
|
|
if (server_un != -1)
|
|
|
|
{
|
|
|
|
flags = fcntl (server_un, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
die ("fcntl (F_GETFL) failed: %s\n", strerror (errno));
|
|
|
|
if ( fcntl (server_un, F_SETFL, (flags | O_NONBLOCK)) == -1)
|
|
|
|
die ("fcntl (F_SETFL) failed: %s\n", strerror (errno));
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2010-08-09 15:40:29 +00:00
|
|
|
if (tcp)
|
|
|
|
{
|
|
|
|
memset (&srvr_addr_in, 0, sizeof srvr_addr_in);
|
|
|
|
srvr_addr_in.sin_family = AF_INET;
|
|
|
|
srvr_addr_in.sin_port = htons (port);
|
|
|
|
srvr_addr_in.sin_addr.s_addr = htonl (INADDR_ANY);
|
2010-10-05 19:05:43 +00:00
|
|
|
addr_in = (struct sockaddr *)&srvr_addr_in;
|
|
|
|
addrlen_in = sizeof srvr_addr_in;
|
2010-08-09 15:40:29 +00:00
|
|
|
}
|
2020-01-20 16:50:56 +01:00
|
|
|
if (logname)
|
2010-08-09 15:40:29 +00:00
|
|
|
{
|
|
|
|
memset (&srvr_addr_un, 0, sizeof srvr_addr_un);
|
|
|
|
srvr_addr_un.sun_family = AF_LOCAL;
|
2020-01-20 16:50:56 +01:00
|
|
|
strncpy (srvr_addr_un.sun_path, logname,
|
|
|
|
sizeof (srvr_addr_un.sun_path)-1);
|
2010-08-09 15:40:29 +00:00
|
|
|
srvr_addr_un.sun_path[sizeof (srvr_addr_un.sun_path) - 1] = 0;
|
2010-10-05 19:05:43 +00:00
|
|
|
addr_un = (struct sockaddr *)&srvr_addr_un;
|
|
|
|
addrlen_un = SUN_LEN (&srvr_addr_un);
|
2010-08-09 15:40:29 +00:00
|
|
|
}
|
2010-10-05 19:05:43 +00:00
|
|
|
else
|
|
|
|
addrlen_un = 0; /* Silent gcc. */
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
if (server_in != -1 && bind (server_in, addr_in, addrlen_in))
|
|
|
|
die ("bind to port %hu failed: %s\n", port, strerror (errno));
|
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
again:
|
2010-10-05 19:05:43 +00:00
|
|
|
if (server_un != -1 && bind (server_un, addr_un, addrlen_un))
|
2011-02-03 16:50:01 +01:00
|
|
|
{
|
2010-10-05 19:05:43 +00:00
|
|
|
if (errno == EADDRINUSE && force)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
|
|
|
force = 0;
|
2010-08-09 15:40:29 +00:00
|
|
|
remove (srvr_addr_un.sun_path);
|
2003-12-16 11:32:52 +00:00
|
|
|
goto again;
|
|
|
|
}
|
2010-08-09 15:40:29 +00:00
|
|
|
else
|
2020-01-20 16:50:56 +01:00
|
|
|
die ("bind to '%s' failed: %s\n", logname, strerror (errno));
|
2003-12-16 11:32:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
if (server_in != -1 && listen (server_in, 5))
|
|
|
|
die ("listen on inet failed: %s\n", strerror (errno));
|
|
|
|
if (server_un != -1 && listen (server_un, 5))
|
|
|
|
die ("listen on local failed: %s\n", strerror (errno));
|
2003-12-16 11:32:52 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
int max_fd;
|
|
|
|
client_t client;
|
|
|
|
|
|
|
|
/* Usually we don't have that many connections, thus it is okay
|
2017-04-28 10:06:33 +09:00
|
|
|
to set them always from scratch and don't maintain an active
|
2004-01-16 22:38:58 +00:00
|
|
|
fd_set. */
|
2003-12-16 11:32:52 +00:00
|
|
|
FD_ZERO (&rfds);
|
2010-10-05 19:05:43 +00:00
|
|
|
max_fd = -1;
|
|
|
|
if (server_in != -1)
|
|
|
|
{
|
|
|
|
FD_SET (server_in, &rfds);
|
|
|
|
max_fd = server_in;
|
|
|
|
}
|
|
|
|
if (server_un != -1)
|
|
|
|
{
|
|
|
|
FD_SET (server_un, &rfds);
|
|
|
|
if (server_un > max_fd)
|
|
|
|
max_fd = server_un;
|
|
|
|
}
|
2003-12-16 11:32:52 +00:00
|
|
|
for (client = client_list; client; client = client->next)
|
|
|
|
if (client->fd != -1)
|
|
|
|
{
|
|
|
|
FD_SET (client->fd, &rfds);
|
|
|
|
if (client->fd > max_fd)
|
|
|
|
max_fd = client->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (select (max_fd + 1, &rfds, NULL, NULL, NULL) <= 0)
|
|
|
|
continue; /* Ignore any errors. */
|
|
|
|
|
2010-10-05 19:05:43 +00:00
|
|
|
if (server_in != -1 && FD_ISSET (server_in, &rfds))
|
|
|
|
setup_client (server_in, 0);
|
|
|
|
if (server_un != -1 && FD_ISSET (server_un, &rfds))
|
|
|
|
setup_client (server_un, 1);
|
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
for (client = client_list; client; client = client->next)
|
2004-01-16 22:38:58 +00:00
|
|
|
if (client->fd != -1 && FD_ISSET (client->fd, &rfds))
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
|
|
|
char line[256];
|
|
|
|
int n;
|
2011-02-03 16:50:01 +01:00
|
|
|
|
2003-12-16 11:32:52 +00:00
|
|
|
n = read (client->fd, line, sizeof line - 1);
|
2004-11-23 17:09:51 +00:00
|
|
|
if (n < 0)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
print_line (client, NULL); /* flush */
|
|
|
|
printf ("[client at fd %d read error: %s]\n",
|
|
|
|
client->fd, strerror (save_errno));
|
|
|
|
close (client->fd);
|
|
|
|
client->fd = -1;
|
|
|
|
}
|
2011-02-03 16:50:01 +01:00
|
|
|
else if (!n)
|
2003-12-16 11:32:52 +00:00
|
|
|
{
|
|
|
|
print_line (client, NULL); /* flush */
|
|
|
|
close (client->fd);
|
|
|
|
printf ("[client at fd %d disconnected]\n", client->fd);
|
|
|
|
client->fd = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line[n] = 0;
|
|
|
|
print_line (client, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
compile-command: "gcc -Wall -g -o watchgnupg watchgnupg.c"
|
|
|
|
End:
|
|
|
|
*/
|