1 /* assuan-socket-connect.c - Assuan socket based client
2  *	Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of Assuan.
5  *
6  * Assuan is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * Assuan is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /* Please note that this is a stripped down and modified version of
21    the orginal Assuan code from libassuan. */
22 
23 #include <config.h>
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #ifndef HAVE_W32_SYSTEM
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #else
35 #include <windows.h>
36 #endif
37 
38 #include "assuan-defs.h"
39 
40 /* Hacks for Slowaris.  */
41 #ifndef PF_LOCAL
42 # ifdef PF_UNIX
43 #  define PF_LOCAL PF_UNIX
44 # else
45 #  define PF_LOCAL AF_UNIX
46 # endif
47 #endif
48 #ifndef AF_LOCAL
49 # define AF_LOCAL AF_UNIX
50 #endif
51 
52 #ifndef SUN_LEN
53 # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
54 	               + strlen ((ptr)->sun_path))
55 #endif
56 
57 
58 static int
do_finish(assuan_context_t ctx)59 do_finish (assuan_context_t ctx)
60 {
61   if (ctx->inbound.fd != -1)
62     {
63       _assuan_close (ctx->inbound.fd);
64     }
65   ctx->inbound.fd = -1;
66   ctx->outbound.fd = -1;
67   return 0;
68 }
69 
70 static void
do_deinit(assuan_context_t ctx)71 do_deinit (assuan_context_t ctx)
72 {
73   do_finish (ctx);
74 }
75 
76 
77 static ssize_t
simple_read(assuan_context_t ctx,void * buffer,size_t size)78 simple_read (assuan_context_t ctx, void *buffer, size_t size)
79 {
80 #ifndef HAVE_W32_SYSTEM
81   return read (ctx->inbound.fd, buffer, size);
82 #else
83   return recv (ctx->inbound.fd, buffer, size, 0);
84 #endif
85 }
86 
87 static ssize_t
simple_write(assuan_context_t ctx,const void * buffer,size_t size)88 simple_write (assuan_context_t ctx, const void *buffer, size_t size)
89 {
90 #ifndef HAVE_W32_SYSTEM
91   return write (ctx->outbound.fd, buffer, size);
92 #else
93   return send (ctx->outbound.fd, buffer, size, 0);
94 #endif
95 }
96 
97 
98 /* Make a connection to the Unix domain socket NAME and return a new
99    Assuan context in CTX.  SERVER_PID is currently not used but may
100    become handy in the future.  */
101 assuan_error_t
assuan_socket_connect(assuan_context_t * r_ctx,const char * name,pid_t server_pid)102 assuan_socket_connect (assuan_context_t *r_ctx,
103                        const char *name, pid_t server_pid)
104 {
105   static struct assuan_io io = { simple_read, simple_write };
106 
107   assuan_error_t err;
108   assuan_context_t ctx;
109   int fd;
110   struct sockaddr_un srvr_addr;
111   size_t len;
112   const char *s;
113 
114   if (!r_ctx || !name)
115     return ASSUAN_Invalid_Value;
116   *r_ctx = NULL;
117 
118   /* We require that the name starts with a slash, so that we can
119      alter reuse this function for other socket types.  To make things
120      easier we allow an optional dirver prefix.  */
121   s = name;
122   if (*s && s[1] == ':')
123     s += 2;
124   if (*s != DIRSEP_C && *s != '/')
125     return ASSUAN_Invalid_Value;
126 
127   if (strlen (name)+1 >= sizeof srvr_addr.sun_path)
128     return ASSUAN_Invalid_Value;
129 
130   err = _assuan_new_context (&ctx);
131   if (err)
132       return err;
133   ctx->deinit_handler = do_deinit;
134   ctx->finish_handler = do_finish;
135 
136 
137   fd = _assuan_sock_new (PF_LOCAL, SOCK_STREAM, 0);
138   if (fd == -1)
139     {
140       _assuan_log_printf ("can't create socket: %s\n", strerror (errno));
141       _assuan_release_context (ctx);
142       return ASSUAN_General_Error;
143     }
144 
145   memset (&srvr_addr, 0, sizeof srvr_addr);
146   srvr_addr.sun_family = AF_LOCAL;
147   strncpy (srvr_addr.sun_path, name, sizeof (srvr_addr.sun_path) - 1);
148   srvr_addr.sun_path[sizeof (srvr_addr.sun_path) - 1] = 0;
149   len = SUN_LEN (&srvr_addr);
150 
151 
152   if (_assuan_sock_connect (fd, (struct sockaddr *) &srvr_addr, len) == -1)
153     {
154       _assuan_log_printf ("can't connect to `%s': %s\n",
155                           name, strerror (errno));
156       _assuan_release_context (ctx);
157       _assuan_close (fd);
158       return ASSUAN_Connect_Failed;
159     }
160 
161   ctx->inbound.fd = fd;
162   ctx->outbound.fd = fd;
163   ctx->io = &io;
164 
165   /* initial handshake */
166   {
167     int okay, off;
168 
169     err = _assuan_read_from_server (ctx, &okay, &off);
170     if (err)
171       _assuan_log_printf ("can't connect to server: %s\n",
172                           assuan_strerror (err));
173     else if (okay != 1)
174       {
175         /*LOG ("can't connect to server: `");*/
176 	_assuan_log_sanitized_string (ctx->inbound.line);
177 	fprintf (assuan_get_assuan_log_stream (), "'\n");
178 	err = ASSUAN_Connect_Failed;
179       }
180   }
181 
182   if (err)
183     {
184       assuan_disconnect (ctx);
185     }
186   else
187     *r_ctx = ctx;
188   return 0;
189 }
190