1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
NAME
testclient - test imap client library using GNU imap4d
SYNOPSIS
testclient CONFIG_FILE CLIENT_COMMAND
DESCRIPTION
Auxiliary tool for testing the mailutils IMAP client library.
Arguments are:
CONFIG_FILE - name of the imap4d configuration file
CLIENT_COMMAND - command to run.
The tool finds first unused TCP port on localhost and starts
listening on that port in the background. When a connection
arrives, it starts imap4d in inetd mode with the given
configuratiom file.
The master process then adds the following two variables to the
environment:
PORT - port number it is listening on
URL - mailutils URL (imap://127.0.0.1:$PORT)
Finally, it executes /bin/sh -c CLIENT_COMMAND. The client
command is supposed to connect to the port and issue some IMAP
commands.
The program imposes a 60 second timeout on the execution time.
LICENSE
Copyright (C) 2021-2025 Free Software Foundation, Inc.
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, 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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
char const *progname;
static void
error (int exit_code, int error_code, char const *fmt, ...)
{
va_list ap;
fprintf (stderr, "%s: ", progname);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
if (error_code)
fprintf (stderr, ": %s", strerror (error_code));
fputc ('\n', stderr);
if (exit_code)
exit (exit_code);
}
static int
listener_setup (char *scheme)
{
struct addrinfo *ap, hints;
socklen_t len;
int fd;
int i;
char serv[80];
char urlbuf[93];
/* Find first free port */
memset (&hints, 0, sizeof (hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_family = AF_INET;
if ((i = getaddrinfo ("127.0.0.1", NULL, &hints, &ap)) != 0)
error (EX_OSERR, errno, "getaddrinfo: %s", gai_strerror (i));
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd < 0)
error (EX_OSERR, errno, "socket");
i = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof (i));
((struct sockaddr_in*)ap->ai_addr)->sin_port = 0;
if (bind (fd, ap->ai_addr, ap->ai_addrlen) < 0)
error (EX_OSERR, errno, "bind");
if (listen (fd, 8) == -1)
error (EX_OSERR, errno, "listen");
len = ap->ai_addrlen;
if (getsockname (fd, ap->ai_addr, &len))
error(EX_OSERR, errno, "getsockname");
/* Prepare environment */
if ((i = getnameinfo (ap->ai_addr, len, NULL, 0, serv, sizeof serv,
NI_NUMERICSERV)) != 0)
error (EX_OSERR, errno, "getnameinfo: %s", gai_strerror (i));
setenv ("PORT", serv, 1);
snprintf (urlbuf, sizeof urlbuf, "%s://127.0.0.1:%s", scheme, serv);
setenv ("URL", urlbuf, 1);
return fd;
}
void
usage (void)
{
printf ("usage: %s CONFIG_FILE COMMAND\n", progname);
}
int
main (int argc, char **argv)
{
int lfd;
pid_t pid;
progname = argv[0];
if (argc != 3)
{
usage ();
exit (EX_USAGE);
}
lfd = listener_setup ("imap");
pid = fork ();
if (pid == -1)
error (EX_OSERR, errno, "fork");
if (pid == 0)
{
/* Run server */
int fd;
char *sargv[] = {
"imap4d",
"--inetd",
"--preauth",
"--foreground",
"--no-config",
"--config-file",
argv[1],
"--set",
".logging.syslog=off",
NULL
};
struct sockaddr_in sin;
socklen_t len = sizeof sin;
alarm (60);//FIXME
fd = accept (lfd, (struct sockaddr *)&sin, &len);
if (fd == -1)
error (EX_OSERR, errno, "accept");
dup2 (fd, 0);
dup2 (fd, 1);
if (fd > 1)
close (fd);
close (lfd);
execvp (sargv[0], sargv);
_exit (127);
}
execlp ("/bin/sh", "/bin/sh", "-c", argv[2], NULL);
error (EX_OSERR, errno, "execlp");
}
|