server/admin.c: Remove spurious `ping' in usage message.
[tripe] / priv / priv.h
1 /* -*-c-*-
2 *
3 * Privilege separation definitions
4 *
5 * (c) 2008 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
16 *
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 #ifndef PRIV_H
27 #define PRIV_H
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*----- Header files ------------------------------------------------------*/
34
35 #include "config.h"
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45
46 #include <sys/socket.h>
47 #include <sys/un.h>
48
49 #include <mLib/dstr.h>
50 #include <mLib/fdpass.h>
51 #include <mLib/quis.h>
52 #include <mLib/report.h>
53 #include <mLib/trace.h>
54
55 #include "util.h"
56
57 #undef sun
58
59 /*----- Protocol ----------------------------------------------------------*/
60
61 /* --- Notes --- *
62 *
63 * The protocol is synchronous. The socket is not marked as nonblocking;
64 * instead we just trust the helper to respond in good time; this is
65 * reasonable since it's not doing anything complicated. The helper is
66 * completely trusted.
67 *
68 * The protocol works like this. Messages begin with a request code which is
69 * a single @unsigned int@. The server sends a request @PS_TUNRQ@ to the
70 * helper, followed by a strin naming the tunnel driver of interest. The
71 * server responds with a sequence of @PS_TRACE@ and/or @PS_WARN@ messages,
72 * followed by either a @PS_TUNFD@ carrying a file descriptor, or a
73 * @PS_TUNERR@ followed by an integer @errno@ code.
74 *
75 * Simple data items are sent as native representations. A string is sent as
76 * a @size_t@ giving the string's length in bytes followed by that many
77 * characters. There is no padding for alignment.
78 *
79 * If all else fails, the helper process will just quit.
80 */
81
82 enum {
83 PS_TUNRQ, /* Request (string) */
84 PS_TUNFD, /* Tunnel descriptor (nothing) */
85 PS_TUNERR, /* Error (@int errno@) */
86 #ifndef NTRACE
87 PS_TRACE, /* Trace (@unsigned mask@, string) */
88 #endif
89 PS_WARN, /* Warning (string) */
90 };
91
92 /*----- Tracing definitions -----------------------------------------------*/
93
94 #define T_PRIVSEP 512u
95
96 /*----- Global variables --------------------------------------------------*/
97
98 extern int pc_fd; /* File descriptor for comms */
99
100 /*----- Functions provided ------------------------------------------------*/
101
102 #define COMM_TYPES(_) \
103 _(err, int) \
104 _(uint, unsigned int) \
105 _(sz, size_t)
106
107 /* --- @pc_put@ --- *
108 *
109 * Arguments: @const void *p@ = pointer to buffer
110 * @size_t sz@ = size of the buffer
111 *
112 * Returns: Zero on success, @-1@ on error (and @errno@ set).
113 *
114 * Use: Writes a buffer, handling short writes and other bogosity.
115 */
116
117 extern int pc_put(const void */*p*/, size_t /*sz*/);
118
119 /* --- @pc_puterr@, @pc_putuint@, @pc_putsz@ --- *
120 *
121 * Arguments: @int err@ = error number to write
122 * @uint u@ = unsigned integer to write
123 * @size_t sz@ = size to write
124 *
125 * Returns: Zero on success, @-1@ on error (and @errno@ set).
126 *
127 * Use: Sends an error/integer/size.
128 */
129
130 #define DECL(abbr, type) extern int pc_put##abbr(type /*x*/);
131 COMM_TYPES(DECL)
132 #undef DECL
133
134 /* --- @pc_putstring@ --- *
135 *
136 * Arguments: @const char *s@ = pointer to string to write
137 *
138 * Returns: Zero on success, @-1@ on error (and @errno@ set).
139 *
140 * Use: Sends a string.
141 */
142
143 extern int pc_putstring(const char */*s*/);
144
145 /* --- @pc_get@ --- *
146 *
147 * Arguments: @void *p@ = pointer to buffer
148 * @size_t sz@ = size of the buffer
149 *
150 * Returns: Zero on success, @-1@ on error (and @errno@ set).
151 *
152 * Use: Receives a buffer, handling short reads and other bogosity.
153 */
154
155 extern int pc_get(void */*p*/, size_t /*sz*/);
156
157 /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@ --- *
158 *
159 * Arguments: @int *err@ = where to put the error number
160 * @uint *u@ = where to put the unsigned integer
161 * @size_t *sz@ = where to put the size
162 *
163 * Returns: Zero on success, @-1@ on error (and @errno@ set).
164 *
165 * Use: Receives an error/integer/size.
166 */
167
168 #define DECL(abbr, type) extern int pc_get##abbr(type */*x*/);
169 COMM_TYPES(DECL)
170 #undef DECL
171
172 /* --- @pc_getstring@ --- *
173 *
174 * Arguments: @dstr *d@ = where to put the string
175 *
176 * Returns: Zero on success, @-1@ on error (and @errno@ set).
177 *
178 * Use: Receives a string.
179 */
180
181 extern int pc_getstring(dstr */*d*/);
182
183 /*----- That's all, folks -------------------------------------------------*/
184
185 #ifdef __cplusplus
186 }
187 #endif
188
189 #endif