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 <event.h>
23 #include <limits.h>
24 #include <stdint.h>
25 #include <tls.h>
27 /* TODO: make these customizable */
28 #define KD_CONF_FILE "/etc/kamid.conf"
29 #define KD_USER "_kamid"
30 #define KD_SOCKET "/var/run/kamid.sock"
32 #define IMSG_DATA_SIZE(imsg) ((imsg).hdr.len - IMSG_HEADER_SIZE)
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 struct imsgev {
37 struct imsgbuf ibuf;
38 void (*handler)(int, short, void *);
39 struct event ev;
40 short events;
41 };
43 enum imsg_type {
44 IMSG_NONE,
45 IMSG_CTL_LOG_VERBOSE,
46 IMSG_CTL_RELOAD,
47 IMSG_CONTROLFD,
48 IMSG_STARTUP,
49 IMSG_RECONF_CONF,
50 IMSG_RECONF_PKI,
51 IMSG_RECONF_PKI_CERT,
52 IMSG_RECONF_PKI_KEY,
53 IMSG_RECONF_LISTEN,
54 IMSG_RECONF_END,
55 IMSG_AUTH,
56 IMSG_AUTH_DIR,
57 IMSG_AUTH_TLS,
58 IMSG_CONN_GONE,
59 IMSG_BUF,
60 };
62 struct kd_options_conf {
63 /* ... */
64 };
66 enum table_type {
67 T_NONE = 0,
68 T_HASH = 0x01,
69 };
71 struct table {
72 char t_name[LINE_MAX];
73 enum table_type t_type;
74 char t_path[PATH_MAX];
75 void *t_handle;
76 struct table_backend *t_backend;
77 };
79 struct table_backend {
80 const char *name;
81 int (*open)(struct table *);
82 int (*add)(struct table *, const char *, const char *);
83 int (*lookup)(struct table *, const char *, char **);
84 void (*close)(struct table *);
85 };
87 /* table_static.c */
88 extern struct table_backend table_static;
90 #define L_NONE 0x0
91 #define L_TLS 0x1
92 struct kd_listen_conf {
93 SIMPLEQ_ENTRY(kd_listen_conf) entry;
94 uint32_t id;
95 uint32_t flags;
96 int fd;
97 char iface[LINE_MAX];
98 uint16_t port;
99 struct table *auth_table;
100 char pki[LINE_MAX];
101 struct event ev;
102 struct tls *ctx;
103 };
105 struct kd_pki_conf {
106 SIMPLEQ_ENTRY(kd_pki_conf) entry;
107 char name[LINE_MAX];
108 uint8_t *cert;
109 size_t certlen;
110 uint8_t *key;
111 size_t keylen;
112 struct tls_config *tlsconf;
113 };
115 struct kd_tables_conf {
116 SIMPLEQ_ENTRY(kd_tables_conf) entry;
117 struct table *table;
118 };
120 struct kd_conf {
121 struct kd_options_conf kd_options;
122 SIMPLEQ_HEAD(kd_pki_conf_head, kd_pki_conf) pki_head;
123 SIMPLEQ_HEAD(kd_tables_conf_head, kd_tables_conf) table_head;
124 SIMPLEQ_HEAD(kd_listen_conf_head, kd_listen_conf) listen_head;
125 };
127 struct kd_auth_req {
128 uint32_t listen_id;
129 char hash[128+1];
130 };
132 /*
133 * 9p message header.
135 * The message itself is len bytes long (counting the whole header
136 * too.)
137 */
138 struct np_msg_header {
139 uint32_t len;
140 uint8_t type;
141 uint16_t tag;
142 };
144 /* useful constants */
145 #define NOTAG ((uint16_t)~0U)
146 #define NOFID ((uint32_t)~0U)
147 #define NOUID (-1)
149 /* 9p message types */
150 enum {
151 Tversion = 100,
152 Rversion,
153 Tauth = 102,
154 Rauth,
155 Tattach = 104,
156 Rattach,
157 Terror = 106, /* illegal */
158 Rerror,
159 Tflush = 108,
160 Rflush,
161 Twalk = 110,
162 Rwalk,
163 Topen = 112,
164 Ropen,
165 Tcreate = 114,
166 Rcreate,
167 Tread = 116,
168 Rread,
169 Twrite = 118,
170 Rwrite,
171 Tclunk = 120,
172 Rclunk,
173 Tremove = 122,
174 Rremove,
175 Tstat = 124,
176 Rstat,
177 Twstat = 126,
178 Rwstat,
179 Tmax,
181 /*
182 * plan9ports' include/fcall.h also has a
184 * Topenfd = 98,
185 * Ropenfd,
187 * which it's not mentioned in the 9p "rfc" over at
188 * 9p.cat-v.org. Ignoring that for now.
189 */
190 };
192 /* kamid.c */
193 extern int verbose;
194 int main_imsg_compose_listener(int, int, uint32_t, const void *, uint16_t);
195 void merge_config(struct kd_conf *, struct kd_conf *);
196 void imsg_event_add(struct imsgev *);
197 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, pid_t,
198 int, const void *, uint16_t);
200 struct kd_conf *config_new_empty(void);
201 void config_clear(struct kd_conf *);
203 /* parse.y */
204 struct kd_conf *parse_config(const char *);
205 int cmdline_symset(char *);
207 #endif