Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef KAMID_H
18 #define KAMID_H
20 #include "compat.h"
22 #include <limits.h>
23 #include <stdint.h>
24 #include <tls.h>
26 /* TODO: make these customizable */
27 #define KD_CONF_FILE "/etc/kamid.conf"
28 #define KD_USER "_kamid"
29 #define KD_SOCKET "/var/run/kamid.sock"
31 #define IMSG_DATA_SIZE(imsg) ((imsg).hdr.len - IMSG_HEADER_SIZE)
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 struct imsgev {
36 struct imsgbuf ibuf;
37 void (*handler)(int, short, void *);
38 struct event ev;
39 short events;
40 };
42 enum imsg_type {
43 IMSG_NONE,
44 IMSG_CTL_LOG_VERBOSE,
45 IMSG_CTL_RELOAD,
46 IMSG_CONTROLFD,
47 IMSG_STARTUP,
48 IMSG_RECONF_CONF,
49 IMSG_RECONF_PKI,
50 IMSG_RECONF_PKI_CERT,
51 IMSG_RECONF_PKI_KEY,
52 IMSG_RECONF_LISTEN,
53 IMSG_RECONF_END,
54 IMSG_AUTH,
55 IMSG_AUTH_DIR,
56 IMSG_AUTH_TLS,
57 IMSG_CONN_GONE,
58 IMSG_BUF,
59 IMSG_MSIZE,
60 IMSG_CLOSE,
61 };
63 struct kd_options_conf {
64 /* ... */
65 };
67 enum table_type {
68 T_NONE = 0,
69 T_HASH = 0x01,
70 };
72 struct table {
73 char t_name[LINE_MAX];
74 enum table_type t_type;
75 char t_path[PATH_MAX];
76 void *t_handle;
77 struct table_backend *t_backend;
78 };
80 struct table_backend {
81 const char *name;
82 int (*open)(struct table *);
83 int (*add)(struct table *, const char *, const char *);
84 int (*lookup)(struct table *, const char *, char **);
85 void (*close)(struct table *);
86 };
88 /* table_static.c */
89 extern struct table_backend table_static;
91 #define L_NONE 0x0
92 #define L_TLS 0x1
93 struct kd_listen_conf {
94 STAILQ_ENTRY(kd_listen_conf) entry;
95 uint32_t id;
96 uint32_t flags;
97 int fd;
98 char iface[LINE_MAX];
99 uint16_t port;
101 /* certificate hash => (virtual) user */
102 struct table *auth_table;
104 /* virtual user => local user */
105 struct table *virtual_table;
107 /* (virtual) user => export directory */
108 struct table *userdata_table;
110 char pki[LINE_MAX];
111 struct event ev;
112 struct tls *ctx;
113 };
115 struct kd_pki_conf {
116 STAILQ_ENTRY(kd_pki_conf) entry;
117 char name[LINE_MAX];
118 uint8_t *cert;
119 size_t certlen;
120 uint8_t *key;
121 size_t keylen;
122 struct tls_config *tlsconf;
123 };
125 struct kd_tables_conf {
126 STAILQ_ENTRY(kd_tables_conf) entry;
127 struct table *table;
128 };
130 struct kd_conf {
131 struct kd_options_conf kd_options;
132 STAILQ_HEAD(kd_pki_conf_head, kd_pki_conf) pki_head;
133 STAILQ_HEAD(kd_tables_conf_head, kd_tables_conf) table_head;
134 STAILQ_HEAD(kd_listen_conf_head, kd_listen_conf) listen_head;
135 };
137 struct kd_auth_req {
138 uint32_t listen_id;
139 char hash[128+1];
140 };
142 /*
143 * 9p message header.
145 * The message itself is len bytes long (counting the whole header
146 * too.)
147 */
148 struct np_msg_header {
149 uint32_t len;
150 uint8_t type;
151 uint16_t tag;
152 };
154 struct qid {
155 uint64_t path;
156 uint32_t vers;
157 uint8_t type;
158 };
160 /* useful constants */
161 #define HEADERSIZE (4 + 1 + 2)
162 #define VERSION9P "9P2000"
163 #define MSIZE9P ((uint32_t)4*1024*1024)
164 #define NOTAG ((uint16_t)~0U)
165 #define NOFID ((uint32_t)~0U)
166 #define NOUID (-1)
167 #define QIDSIZE 13
168 #define MAXWELEM 16
170 #define NPSTATSIZ(namlen, uidnam, gidnam, unam) \
171 (6 + QIDSIZE + 20 + 2 + namlen + 2 + uidnam + 2 + gidnam + 2 + unam)
173 /* bits in Qid.type */
174 #define QTDIR 0x80 /* type bit for directories */
175 #define QTAPPEND 0x40 /* type bit for append only files */
176 #define QTEXCL 0x20 /* type bit for exclusive use files */
177 #define QTMOUNT 0x10 /* type bit for mounted channel */
178 #define QTAUTH 0x08 /* type bit for authentication file */
179 #define QTTMP 0x04 /* type bit for non-backed-up file */
180 #define QTSYMLINK 0x02 /* type bit for symbolic link */
181 #define QTFILE 0x00 /* type bits for plain file */
183 /* Topen mode/flags */
184 #define KOREAD 0x00
185 #define KOWRITE 0x01
186 #define KORDWR 0x02
187 #define KOEXEC 0x03
188 #define KOTRUNC 0x10
189 #define KORCLOSE 0x40
191 /* 9p message types */
192 enum {
193 Treaddir = 40, /* .L */
194 Rreaddir,
196 Tversion = 100,
197 Rversion,
198 Tauth = 102,
199 Rauth,
200 Tattach = 104,
201 Rattach,
202 Terror = 106, /* illegal */
203 Rerror,
204 Tflush = 108,
205 Rflush,
206 Twalk = 110,
207 Rwalk,
208 Topen = 112,
209 Ropen,
210 Tcreate = 114,
211 Rcreate,
212 Tread = 116,
213 Rread,
214 Twrite = 118,
215 Rwrite,
216 Tclunk = 120,
217 Rclunk,
218 Tremove = 122,
219 Rremove,
220 Tstat = 124,
221 Rstat,
222 Twstat = 126,
223 Rwstat,
224 Tmax,
226 /*
227 * plan9ports' include/fcall.h also has a
229 * Topenfd = 98,
230 * Ropenfd,
232 * which it's not mentioned in the 9p "rfc" over at
233 * 9p.cat-v.org. Ignoring that for now.
234 */
235 };
237 /* kamid.c */
238 extern int verbose;
239 int main_imsg_compose_listener(int, int, uint32_t, const void *, uint16_t);
240 void merge_config(struct kd_conf *, struct kd_conf *);
242 struct kd_conf *config_new_empty(void);
243 void config_clear(struct kd_conf *);
245 /* parse.y */
246 struct kd_conf *parse_config(const char *);
247 int cmdline_symset(char *);
249 #endif