~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to jnlib/w32-afunix.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* w32-afunix.c
2
 
 * Copyright (C) 2004 g10 Code GmbH
3
 
 *
4
 
 * This file is part of GnuPG.
5
 
 *
6
 
 * GnuPG is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * GnuPG is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
1
/* w32-afunix.c - AF_UNIX emulation for Windows (Client only).
 
2
 * Copyright (C) 2004, 2006 g10 Code GmbH
 
3
 *
 
4
 * This file is part of JNLIB.
 
5
 *
 
6
 * JNLIB 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 3 of
 
9
 * the License, or (at your option) any later version.
 
10
 *
 
11
 * JNLIB 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
 
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
 
19
 
 
20
/* Use of this code is preprecated - you better use the sockt wrappers
 
21
   from libassuan. */
 
22
 
20
23
#ifdef _WIN32
21
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#define WIN32_LEAN_AND_MEAN
22
27
#include <windows.h>
 
28
#include <fcntl.h>
 
29
#include <sys/stat.h>
23
30
#include <io.h>
 
31
#include <errno.h>
24
32
 
25
33
#include "w32-afunix.h"
26
34
 
 
35
 
 
36
 
 
37
/* The buffer for NONCE needs to be at least 16 bytes.  Returns 0 on
 
38
   success. */
 
39
static int
 
40
read_port_and_nonce (const char *fname, unsigned short *port, char *nonce)
 
41
{
 
42
  FILE *fp;
 
43
  char buffer[50], *p;
 
44
  size_t nread;
 
45
  int aval;
 
46
 
 
47
  fp = fopen (fname, "rb");
 
48
  if (!fp)
 
49
    return -1;
 
50
  nread = fread (buffer, 1, sizeof buffer - 1, fp);
 
51
  fclose (fp);
 
52
  if (!nread)
 
53
    {
 
54
      errno = ENOFILE;
 
55
      return -1;
 
56
    }
 
57
  buffer[nread] = 0;
 
58
  aval = atoi (buffer);
 
59
  if (aval < 1 || aval > 65535)
 
60
    {
 
61
      errno = EINVAL;
 
62
      return -1;
 
63
    }
 
64
  *port = (unsigned int)aval;
 
65
  for (p=buffer; nread && *p != '\n'; p++, nread--)
 
66
    ;
 
67
  if (*p != '\n' || nread != 17)
 
68
    {
 
69
      errno = EINVAL;
 
70
      return -1;
 
71
    }
 
72
  p++; nread--;
 
73
  memcpy (nonce, p, 16);
 
74
  return 0;
 
75
}
 
76
 
 
77
 
 
78
 
27
79
int
28
80
_w32_close (int fd)
29
81
{
44
96
 
45
97
 
46
98
int
47
 
_w32_sock_connect (int sockfd, struct sockaddr * addr, int addrlen)
 
99
_w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
48
100
{
49
101
  struct sockaddr_in myaddr;
50
 
  struct sockaddr_un * unaddr;
51
 
  FILE * fp;
52
 
  int port;
53
 
  
 
102
  struct sockaddr_un *unaddr;
 
103
  unsigned short port;
 
104
  char nonce[16];
 
105
  int ret;
 
106
      
54
107
  unaddr = (struct sockaddr_un *)addr;
55
 
  fp = fopen (unaddr->sun_path, "rb");
56
 
  if (!fp)
57
 
    return -1;
58
 
  fscanf (fp, "%d", &port);
59
 
  fclose (fp);
60
 
 
61
 
  /* XXX: set errno in this case */
62
 
  if (port < 0 || port > 65535)
63
 
    return -1;
64
 
  
 
108
  if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
 
109
    return -1;
 
110
      
65
111
  myaddr.sin_family = AF_INET;
66
 
  myaddr.sin_port = port; 
 
112
  myaddr.sin_port = htons (port); 
67
113
  myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
68
 
 
69
 
  /* we need this later. */
 
114
  
 
115
  /* Set return values.  */
70
116
  unaddr->sun_family = myaddr.sin_family;
71
117
  unaddr->sun_port = myaddr.sin_port;
72
118
  unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
73
119
  
74
 
  return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
75
 
}
76
 
 
77
 
 
78
 
int
79
 
_w32_sock_bind (int sockfd, struct sockaddr * addr, int addrlen)
80
 
{
81
 
  if (addr->sa_family == AF_LOCAL || addr->sa_family == AF_UNIX)
 
120
  ret = connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
 
121
  if (!ret)
82
122
    {
83
 
      struct sockaddr_in myaddr;
84
 
      struct sockaddr_un * unaddr;
85
 
      FILE * fp;
86
 
      int len = sizeof myaddr;
87
 
      int rc;
88
 
 
89
 
      myaddr.sin_port = 0;
90
 
      myaddr.sin_family = AF_INET;
91
 
      myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
92
 
 
93
 
      rc = bind (sockfd, (struct sockaddr *)&myaddr, len);
94
 
      if (rc)
95
 
        return rc;
96
 
      rc = getsockname (sockfd, (struct sockaddr *)&myaddr, &len);
97
 
      if (rc)
98
 
        return rc;
99
 
      unaddr = (struct sockaddr_un *)addr;
100
 
      fp = fopen (unaddr->sun_path, "wb");
101
 
      if (!fp)
102
 
        return -1;
103
 
      fprintf (fp, "%d", myaddr.sin_port);
104
 
      fclose (fp);
105
 
 
106
 
      /* we need this later. */
107
 
      unaddr->sun_family = myaddr.sin_family;
108
 
      unaddr->sun_port = myaddr.sin_port;
109
 
      unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
110
 
      
111
 
      return 0;
 
123
      /* Send the nonce. */
 
124
      ret = send (sockfd, nonce, 16, 0);
 
125
      if (ret >= 0 && ret != 16)
 
126
        {
 
127
          errno = EIO;
 
128
          ret = -1;
 
129
        }
112
130
    }
113
 
  return bind (sockfd, addr, addrlen);
 
131
  return ret;
114
132
}
115
133
 
 
134
 
116
135
#endif /*_WIN32*/