2002-01-22 16:29:12 +00:00
|
|
|
/* assuan-socket-connect.c - Assuan socket based client
|
2005-08-09 13:19:24 +00:00
|
|
|
* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
|
2002-01-22 16:29:12 +00:00
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
* This file is part of Assuan.
|
2002-01-22 16:29:12 +00:00
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
* Assuan is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
2002-01-22 16:29:12 +00:00
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
* Assuan 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
|
|
|
|
* Lesser General Public License for more details.
|
2002-01-22 16:29:12 +00:00
|
|
|
*
|
2003-02-01 18:53:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, write to the Free Software
|
2006-09-19 14:01:54 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
2002-01-22 16:29:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2005-08-09 13:19:24 +00:00
|
|
|
#include <string.h>
|
2002-01-22 16:29:12 +00:00
|
|
|
#include <errno.h>
|
2005-08-09 13:19:24 +00:00
|
|
|
#include <unistd.h>
|
2004-04-19 14:24:57 +00:00
|
|
|
#include <sys/types.h>
|
2005-08-09 13:19:24 +00:00
|
|
|
#ifndef HAVE_W32_SYSTEM
|
2002-01-22 16:29:12 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2005-08-09 13:19:24 +00:00
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2002-01-22 16:29:12 +00:00
|
|
|
|
|
|
|
#include "assuan-defs.h"
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
/* Hacks for Slowaris. */
|
|
|
|
#ifndef PF_LOCAL
|
|
|
|
# ifdef PF_UNIX
|
|
|
|
# define PF_LOCAL PF_UNIX
|
|
|
|
# else
|
|
|
|
# define PF_LOCAL AF_UNIX
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifndef AF_LOCAL
|
|
|
|
# define AF_LOCAL AF_UNIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SUN_LEN
|
|
|
|
# define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
|
|
|
|
+ strlen ((ptr)->sun_path))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
static int
|
2006-09-19 14:01:54 +00:00
|
|
|
do_finish (assuan_context_t ctx)
|
2002-01-22 16:29:12 +00:00
|
|
|
{
|
|
|
|
if (ctx->inbound.fd != -1)
|
|
|
|
{
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_close (ctx->inbound.fd);
|
2002-01-22 16:29:12 +00:00
|
|
|
}
|
|
|
|
ctx->inbound.fd = -1;
|
|
|
|
ctx->outbound.fd = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-09-19 14:01:54 +00:00
|
|
|
do_deinit (assuan_context_t ctx)
|
2002-01-22 16:29:12 +00:00
|
|
|
{
|
|
|
|
do_finish (ctx);
|
|
|
|
}
|
2006-09-19 14:01:54 +00:00
|
|
|
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
/* Make a connection to the Unix domain socket NAME and return a new
|
|
|
|
Assuan context in CTX. SERVER_PID is currently not used but may
|
2003-08-18 19:17:08 +00:00
|
|
|
become handy in the future. */
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t
|
2006-09-19 14:01:54 +00:00
|
|
|
assuan_socket_connect (assuan_context_t *r_ctx,
|
2002-01-22 16:29:12 +00:00
|
|
|
const char *name, pid_t server_pid)
|
2006-09-19 14:01:54 +00:00
|
|
|
{
|
|
|
|
return assuan_socket_connect_ext (r_ctx, name, server_pid, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Make a connection to the Unix domain socket NAME and return a new
|
|
|
|
Assuan context in CTX. SERVER_PID is currently not used but may
|
|
|
|
become handy in the future. With flags set to 1 sendmsg and
|
|
|
|
recvmesg are used. */
|
|
|
|
assuan_error_t
|
|
|
|
assuan_socket_connect_ext (assuan_context_t *r_ctx,
|
|
|
|
const char *name, pid_t server_pid,
|
|
|
|
unsigned int flags)
|
2002-01-22 16:29:12 +00:00
|
|
|
{
|
2003-08-18 19:17:08 +00:00
|
|
|
static struct assuan_io io = { _assuan_simple_read,
|
|
|
|
_assuan_simple_write };
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
assuan_error_t err;
|
2006-09-19 14:01:54 +00:00
|
|
|
assuan_context_t ctx;
|
2002-01-22 16:29:12 +00:00
|
|
|
int fd;
|
|
|
|
struct sockaddr_un srvr_addr;
|
|
|
|
size_t len;
|
2005-08-09 13:19:24 +00:00
|
|
|
const char *s;
|
2002-01-22 16:29:12 +00:00
|
|
|
|
|
|
|
if (!r_ctx || !name)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2002-01-22 16:29:12 +00:00
|
|
|
*r_ctx = NULL;
|
|
|
|
|
2006-09-19 14:01:54 +00:00
|
|
|
/* We require that the name starts with a slash, so that we
|
|
|
|
eventually can reuse this function for other socket types. To
|
|
|
|
make things easier we allow an optional dirver prefix. */
|
2005-08-09 13:19:24 +00:00
|
|
|
s = name;
|
|
|
|
if (*s && s[1] == ':')
|
|
|
|
s += 2;
|
|
|
|
if (*s != DIRSEP_C && *s != '/')
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2005-08-09 13:19:24 +00:00
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
if (strlen (name)+1 >= sizeof srvr_addr.sun_path)
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Invalid_Value);
|
2002-01-22 16:29:12 +00:00
|
|
|
|
|
|
|
err = _assuan_new_context (&ctx);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2006-09-19 14:01:54 +00:00
|
|
|
ctx->deinit_handler = ((flags&1))? _assuan_uds_deinit : do_deinit;
|
2002-01-22 16:29:12 +00:00
|
|
|
ctx->finish_handler = do_finish;
|
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
fd = _assuan_sock_new (PF_LOCAL, SOCK_STREAM, 0);
|
2002-01-22 16:29:12 +00:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_log_printf ("can't create socket: %s\n", strerror (errno));
|
2002-01-22 16:29:12 +00:00
|
|
|
_assuan_release_context (ctx);
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_General_Error);
|
2002-01-22 16:29:12 +00:00
|
|
|
}
|
2003-08-18 19:17:08 +00:00
|
|
|
|
|
|
|
memset (&srvr_addr, 0, sizeof srvr_addr);
|
|
|
|
srvr_addr.sun_family = AF_LOCAL;
|
2005-08-09 13:19:24 +00:00
|
|
|
strncpy (srvr_addr.sun_path, name, sizeof (srvr_addr.sun_path) - 1);
|
|
|
|
srvr_addr.sun_path[sizeof (srvr_addr.sun_path) - 1] = 0;
|
|
|
|
len = SUN_LEN (&srvr_addr);
|
|
|
|
|
2003-08-18 19:17:08 +00:00
|
|
|
|
2005-08-09 13:19:24 +00:00
|
|
|
if (_assuan_sock_connect (fd, (struct sockaddr *) &srvr_addr, len) == -1)
|
2002-01-22 16:29:12 +00:00
|
|
|
{
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_log_printf ("can't connect to `%s': %s\n",
|
|
|
|
name, strerror (errno));
|
2002-01-22 16:29:12 +00:00
|
|
|
_assuan_release_context (ctx);
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_close (fd);
|
2006-09-19 14:01:54 +00:00
|
|
|
return _assuan_error (ASSUAN_Connect_Failed);
|
2002-01-22 16:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->inbound.fd = fd;
|
|
|
|
ctx->outbound.fd = fd;
|
2003-08-18 19:17:08 +00:00
|
|
|
ctx->io = &io;
|
2006-09-19 14:01:54 +00:00
|
|
|
if ((flags&1))
|
|
|
|
_assuan_init_uds_io (ctx);
|
|
|
|
|
2002-01-22 16:29:12 +00:00
|
|
|
/* initial handshake */
|
|
|
|
{
|
|
|
|
int okay, off;
|
|
|
|
|
|
|
|
err = _assuan_read_from_server (ctx, &okay, &off);
|
|
|
|
if (err)
|
2005-08-09 13:19:24 +00:00
|
|
|
_assuan_log_printf ("can't connect to server: %s\n",
|
|
|
|
assuan_strerror (err));
|
2002-01-22 16:29:12 +00:00
|
|
|
else if (okay != 1)
|
|
|
|
{
|
2005-08-09 13:19:24 +00:00
|
|
|
/*LOG ("can't connect to server: `");*/
|
2003-08-18 19:17:08 +00:00
|
|
|
_assuan_log_sanitized_string (ctx->inbound.line);
|
|
|
|
fprintf (assuan_get_assuan_log_stream (), "'\n");
|
2006-09-19 14:01:54 +00:00
|
|
|
err = _assuan_error (ASSUAN_Connect_Failed);
|
2002-01-22 16:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
assuan_disconnect (ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*r_ctx = ctx;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-19 14:01:54 +00:00
|
|
|
|
|
|
|
|