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