2 * Part of Very Secure FTPd
7 * This file contains code for a simple message and file descriptor passing
8 * API, over a pair of UNIX sockets.
9 * The messages are typically travelling across a privilege boundary, with
10 * heavy distrust of messages on the side of more privilege.
20 #include "sysdeputil.h"
24 priv_sock_init(struct vsf_session
* p_sess
)
26 struct vsf_sysutil_socketpair_retval retval
;
27 if (p_sess
->parent_fd
!= -1)
29 bug("parent_fd active");
31 if (p_sess
->child_fd
!= -1)
33 bug("child_fd active");
35 retval
= vsf_sysutil_unix_stream_socketpair();
36 p_sess
->parent_fd
= retval
.socket_one
;
37 p_sess
->child_fd
= retval
.socket_two
;
41 priv_sock_close(struct vsf_session
* p_sess
)
43 if (p_sess
->parent_fd
!= -1)
45 vsf_sysutil_close(p_sess
->parent_fd
);
46 p_sess
->parent_fd
= -1;
48 if (p_sess
->child_fd
!= -1)
50 vsf_sysutil_close(p_sess
->child_fd
);
51 p_sess
->child_fd
= -1;
56 priv_sock_set_parent_context(struct vsf_session
* p_sess
)
58 if (p_sess
->child_fd
== -1)
60 bug("child_fd not active");
62 vsf_sysutil_close(p_sess
->child_fd
);
63 p_sess
->child_fd
= -1;
67 priv_sock_set_child_context(struct vsf_session
* p_sess
)
69 if (p_sess
->parent_fd
== -1)
71 bug("parent_fd not active");
73 vsf_sysutil_close(p_sess
->parent_fd
);
74 p_sess
->parent_fd
= -1;
78 priv_sock_send_cmd(int fd
, char cmd
)
80 int retval
= vsf_sysutil_write_loop(fd
, &cmd
, sizeof(cmd
));
81 if (retval
!= sizeof(cmd
))
83 die("priv_sock_send_cmd");
88 priv_sock_send_str(int fd
, const struct mystr
* p_str
)
90 unsigned int len
= str_getlen(p_str
);
91 priv_sock_send_int(fd
, (int) len
);
94 str_netfd_write(p_str
, fd
);
99 priv_sock_send_buf(int fd
, const char* p_buf
, unsigned int len
)
101 priv_sock_send_int(fd
, (int) len
);
104 if (vsf_sysutil_write_loop(fd
, p_buf
, len
) != (int) len
)
106 die("priv_sock_send_buf");
112 priv_sock_recv_buf(int fd
, char* p_buf
, unsigned int len
)
114 unsigned int recv_len
= (unsigned int) priv_sock_get_int(fd
);
117 bug("recv_len bigger than buffer");
121 if (vsf_sysutil_read_loop(fd
, p_buf
, recv_len
) != (int) recv_len
)
123 die("priv_sock_recv_buf");
129 priv_sock_get_result(int fd
)
132 int retval
= vsf_sysutil_read_loop(fd
, &res
, sizeof(res
));
133 if (retval
!= sizeof(res
))
135 die("priv_sock_get_result");
141 priv_sock_get_cmd(int fd
)
144 int retval
= vsf_sysutil_read_loop(fd
, &res
, sizeof(res
));
145 if (retval
!= sizeof(res
))
147 die("priv_sock_get_cmd");
153 priv_sock_get_str(int fd
, struct mystr
* p_dest
)
155 unsigned int len
= (unsigned int) priv_sock_get_int(fd
);
156 if (len
> VSFTP_PRIVSOCK_MAXSTR
)
158 die("priv_sock_get_str: too big");
163 int retval
= str_netfd_read(p_dest
, fd
, len
);
164 if ((unsigned int) retval
!= len
)
166 die("priv_sock_get_str: read error");
172 priv_sock_send_result(int fd
, char res
)
174 int retval
= vsf_sysutil_write_loop(fd
, &res
, sizeof(res
));
175 if (retval
!= sizeof(res
))
177 die("priv_sock_send_result");
182 priv_sock_send_fd(int fd
, int send_fd
)
184 vsf_sysutil_send_fd(fd
, send_fd
);
188 priv_sock_recv_fd(int fd
)
190 return vsf_sysutil_recv_fd(fd
);
194 priv_sock_send_int(int fd
, int the_int
)
196 int retval
= vsf_sysutil_write_loop(fd
, &the_int
, sizeof(the_int
));
197 if (retval
!= sizeof(the_int
))
199 die("priv_sock_send_int");
204 priv_sock_get_int(int fd
)
207 int retval
= vsf_sysutil_read_loop(fd
, &the_int
, sizeof(the_int
));
208 if (retval
!= sizeof(the_int
))
210 die("priv_sock_get_int");