File: privsock.c

package info (click to toggle)
vsftpd 3.0.3-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,548 kB
  • sloc: ansic: 16,632; sh: 267; makefile: 51; python: 18
file content (214 lines) | stat: -rw-r--r-- 4,016 bytes parent folder | download | duplicates (6)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * 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)
{
  struct vsf_sysutil_socketpair_retval retval;
  if (p_sess->parent_fd != -1)
  {
    bug("parent_fd active");
  }
  if (p_sess->child_fd != -1)
  {
    bug("child_fd active");
  }
  retval = vsf_sysutil_unix_stream_socketpair();
  p_sess->parent_fd = retval.socket_one;
  p_sess->child_fd = retval.socket_two;
}

void
priv_sock_close(struct vsf_session* p_sess)
{
  if (p_sess->parent_fd != -1)
  {
    vsf_sysutil_close(p_sess->parent_fd);
    p_sess->parent_fd = -1;
  }
  if (p_sess->child_fd != -1)
  {
    vsf_sysutil_close(p_sess->child_fd);
    p_sess->child_fd = -1;
  }
}

void
priv_sock_set_parent_context(struct vsf_session* p_sess)
{
  if (p_sess->child_fd == -1)
  {
    bug("child_fd not active");
  }
  vsf_sysutil_close(p_sess->child_fd);
  p_sess->child_fd = -1;
}

void
priv_sock_set_child_context(struct vsf_session* p_sess)
{
  if (p_sess->parent_fd == -1)
  {
    bug("parent_fd not active");
  }
  vsf_sysutil_close(p_sess->parent_fd);
  p_sess->parent_fd = -1;
}

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);
  }
}

void
priv_sock_send_buf(int fd, const char* p_buf, unsigned int len)
{
  priv_sock_send_int(fd, (int) len);
  if (len > 0)
  {
    if (vsf_sysutil_write_loop(fd, p_buf, len) != (int) len)
    {
      die("priv_sock_send_buf");
    }
  }
}

void
priv_sock_recv_buf(int fd, char* p_buf, unsigned int len)
{
  unsigned int recv_len = (unsigned int) priv_sock_get_int(fd);
  if (recv_len > len)
  {
    bug("recv_len bigger than buffer");
  }
  if (recv_len > 0)
  {
    if (vsf_sysutil_read_loop(fd, p_buf, recv_len) != (int) recv_len)
    {
      die("priv_sock_recv_buf");
    }
  }
}

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;
}