File: privsock.c

package info (click to toggle)
vsftpd 2.0.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 1,059
  • sloc: ansic: 11,743; sh: 86; makefile: 80
file content (139 lines) | stat: -rw-r--r-- 2,623 bytes parent folder | download | duplicates (3)
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
/*
 * Part of Very Secure FTPd
 * Licence: GPL v2
 * Author: Chris Evans
 * privsock.c
 *
 * This file contains code for a simple message and file descriptor passing
 * API, over a pair of UNIX sockets.
 * The messages are typically travelling across a privilege boundary, with
 * heavy distrust of messages on the side of more privilege.
 */

#include "privsock.h"

#include "utility.h"
#include "defs.h"
#include "str.h"
#include "netstr.h"
#include "sysutil.h"
#include "sysdeputil.h"
#include "session.h"

void
priv_sock_init(struct vsf_session* p_sess)
{
  const struct vsf_sysutil_socketpair_retval retval =
    vsf_sysutil_unix_stream_socketpair();
  p_sess->parent_fd = retval.socket_one;
  p_sess->child_fd = retval.socket_two;
}

void
priv_sock_send_cmd(int fd, char cmd)
{
  int retval = vsf_sysutil_write_loop(fd, &cmd, sizeof(cmd));
  if (retval != sizeof(cmd))
  {
    die("priv_sock_send_cmd");
  }
}

void
priv_sock_send_str(int fd, const struct mystr* p_str)
{
  unsigned int len = str_getlen(p_str);
  priv_sock_send_int(fd, (int) len);
  if (len > 0)
  {
    str_netfd_write(p_str, fd);
  }
}

char
priv_sock_get_result(int fd)
{
  char res;
  int retval = vsf_sysutil_read_loop(fd, &res, sizeof(res));
  if (retval != sizeof(res))
  {
    die("priv_sock_get_result");
  }
  return res;
}

char
priv_sock_get_cmd(int fd)
{
  char res;
  int retval = vsf_sysutil_read_loop(fd, &res, sizeof(res));
  if (retval != sizeof(res))
  {
    die("priv_sock_get_cmd");
  }
  return res;
}

void
priv_sock_get_str(int fd, struct mystr* p_dest)
{
  unsigned int len = (unsigned int) priv_sock_get_int(fd);
  if (len > VSFTP_PRIVSOCK_MAXSTR)
  {
    die("priv_sock_get_str: too big");
  }
  str_empty(p_dest);
  if (len > 0)
  {
    int retval = str_netfd_read(p_dest, fd, len);
    if ((unsigned int) retval != len)
    {
      die("priv_sock_get_str: read error");
    }
  }
}

void
priv_sock_send_result(int fd, char res)
{
  int retval = vsf_sysutil_write_loop(fd, &res, sizeof(res));
  if (retval != sizeof(res))
  {
    die("priv_sock_send_result");
  }
}

void
priv_sock_send_fd(int fd, int send_fd)
{
  vsf_sysutil_send_fd(fd, send_fd);
}

int
priv_sock_recv_fd(int fd)
{
  return vsf_sysutil_recv_fd(fd);
}

void
priv_sock_send_int(int fd, int the_int)
{
  int retval = vsf_sysutil_write_loop(fd, &the_int, sizeof(the_int));
  if (retval != sizeof(the_int))
  {
    die("priv_sock_send_int");
  }
}

int
priv_sock_get_int(int fd)
{
  int the_int;
  int retval = vsf_sysutil_read_loop(fd, &the_int, sizeof(the_int));
  if (retval != sizeof(the_int))
  {
    die("priv_sock_get_int");
  }
  return the_int;
}