blob: 85eb3ee8610b7a8389e78b3f342f6101467d31c3 [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
Willy Tarreaud0a895d2012-09-18 17:40:35 +020016#include <pwd.h>
17#include <grp.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
Willy Tarreau92fb9832007-10-16 17:34:28 +020024#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28
29#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
Willy Tarreaud740bab2007-10-28 11:14:07 +010032#include <common/errors.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020033#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
Willy Tarreau92fb9832007-10-16 17:34:28 +020038#include <types/global.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020039
Willy Tarreau92fb9832007-10-16 17:34:28 +020040#include <proto/fd.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020041#include <proto/listener.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020042#include <proto/log.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/protocol.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020044#include <proto/proto_uxst.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020045#include <proto/task.h>
46
Emeric Bruncf20bf12010-10-22 16:06:11 +020047static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
48static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010049static int uxst_unbind_listeners(struct protocol *proto);
50
51/* Note: must not be declared <const> as its list will be overwritten */
52static struct protocol proto_unix = {
53 .name = "unix_stream",
54 .sock_domain = PF_UNIX,
55 .sock_type = SOCK_STREAM,
56 .sock_prot = 0,
57 .sock_family = AF_UNIX,
58 .sock_addrlen = sizeof(struct sockaddr_un),
59 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020060 .accept = &listener_accept,
Emeric Bruncf20bf12010-10-22 16:06:11 +020061 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 .bind_all = uxst_bind_listeners,
63 .unbind_all = uxst_unbind_listeners,
64 .enable_all = enable_all_listeners,
65 .disable_all = disable_all_listeners,
Willy Tarreau59b94792012-05-11 16:16:40 +020066 .get_src = uxst_get_src,
67 .get_dst = uxst_get_dst,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
69 .nb_listeners = 0,
70};
71
Willy Tarreaudabf2e22007-10-28 21:59:24 +010072/********************************
73 * 1) low-level socket functions
74 ********************************/
75
Willy Tarreau59b94792012-05-11 16:16:40 +020076/*
77 * Retrieves the source address for the socket <fd>, with <dir> indicating
78 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
79 * success, -1 in case of error. The socket's source address is stored in
80 * <sa> for <salen> bytes.
81 */
82int uxst_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
83{
84 if (dir)
85 return getsockname(fd, sa, &salen);
86 else
87 return getpeername(fd, sa, &salen);
88}
89
90
91/*
92 * Retrieves the original destination address for the socket <fd>, with <dir>
93 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
94 * case of success, -1 in case of error. The socket's source address is stored
95 * in <sa> for <salen> bytes.
96 */
97int uxst_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
98{
99 if (dir)
100 return getpeername(fd, sa, &salen);
101 else
102 return getsockname(fd, sa, &salen);
103}
104
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100105
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100106/* Tries to destroy the UNIX stream socket <path>. The socket must not be used
107 * anymore. It practises best effort, and no error is returned.
108 */
109static void destroy_uxst_socket(const char *path)
110{
111 struct sockaddr_un addr;
112 int sock, ret;
113
Willy Tarreau40aa0702013-03-10 23:51:38 +0100114 /* if the path was cleared, we do nothing */
115 if (!*path)
116 return;
117
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100118 /* We might have been chrooted, so we may not be able to access the
119 * socket. In order to avoid bothering the other end, we connect with a
120 * wrong protocol, namely SOCK_DGRAM. The return code from connect()
121 * is enough to know if the socket is still live or not. If it's live
122 * in mode SOCK_STREAM, we get EPROTOTYPE or anything else but not
123 * ECONNREFUSED. In this case, we do not touch it because it's used
124 * by some other process.
125 */
126 sock = socket(PF_UNIX, SOCK_DGRAM, 0);
127 if (sock < 0)
128 return;
129
130 addr.sun_family = AF_UNIX;
131 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
132 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
133 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
134 if (ret < 0 && errno == ECONNREFUSED) {
135 /* Connect failed: the socket still exists but is not used
136 * anymore. Let's remove this socket now.
137 */
138 unlink(path);
139 }
140 close(sock);
141}
142
143
144/********************************
145 * 2) listener-oriented functions
146 ********************************/
147
148
149/* This function creates a UNIX socket associated to the listener. It changes
150 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100151 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
152 * may return a warning or an error message in <errmsg> if the message is at
153 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
154 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200155 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100156static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200157{
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100158 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200159 char tempname[MAXPATHLEN];
160 char backname[MAXPATHLEN];
161 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100162 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100163 const char *path;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100164 int ext, ready;
165 socklen_t ready_len;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100166
167 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200168
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100169 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100170 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100171 *errmsg = 0;
172
173 if (listener->state != LI_ASSIGNED)
174 return ERR_NONE; /* already bound */
175
176 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200177
Willy Tarreau40aa0702013-03-10 23:51:38 +0100178 /* if the listener already has an fd assigned, then we were offered the
179 * fd by an external process (most likely the parent), and we don't want
180 * to create a new socket. However we still want to set a few flags on
181 * the socket.
182 */
183 fd = listener->fd;
184 ext = (fd >= 0);
185 if (ext)
186 goto fd_ready;
187
Willy Tarreau92fb9832007-10-16 17:34:28 +0200188 /* 1. create socket names */
189 if (!path[0]) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100190 msg = "Invalid empty name for a UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200191 goto err_return;
192 }
193
194 ret = snprintf(tempname, MAXPATHLEN, "%s.%d.tmp", path, pid);
195 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100196 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200197 goto err_return;
198 }
199
200 ret = snprintf(backname, MAXPATHLEN, "%s.%d.bak", path, pid);
201 if (ret < 0 || ret >= MAXPATHLEN) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100202 msg = "name too long for UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200203 goto err_return;
204 }
205
206 /* 2. clean existing orphaned entries */
207 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100208 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200209 goto err_return;
210 }
211
212 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100213 msg = "error when trying to unlink previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200214 goto err_return;
215 }
216
217 /* 3. backup existing socket */
218 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100219 msg = "error when trying to preserve previous UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200220 goto err_return;
221 }
222
223 /* 4. prepare new socket */
224 addr.sun_family = AF_UNIX;
225 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path));
226 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
227
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100228 fd = socket(PF_UNIX, SOCK_STREAM, 0);
229 if (fd < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100230 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200231 goto err_unlink_back;
232 }
233
Willy Tarreau40aa0702013-03-10 23:51:38 +0100234 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100235 if (fd >= global.maxsock) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100236 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200237 goto err_unlink_temp;
238 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100239
240 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100241 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200242 goto err_unlink_temp;
243 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100244
Willy Tarreau40aa0702013-03-10 23:51:38 +0100245 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200246 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreaub40dc942010-11-07 12:10:51 +0100247 msg = "cannot bind UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200248 goto err_unlink_temp;
249 }
250
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100251 /* <uid> and <gid> different of -1 will be used to change the socket owner.
252 * If <mode> is not 0, it will be used to restrict access to the socket.
253 * While it is known not to be portable on every OS, it's still useful
254 * where it works.
255 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100256 if (!ext &&
257 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
258 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
259 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100260 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200261 goto err_unlink_temp;
262 }
263
Willy Tarreau40aa0702013-03-10 23:51:38 +0100264 ready = 0;
265 ready_len = sizeof(ready);
266 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
267 ready = 0;
268
269 if (!(ext && ready) && /* only listen if not already done by external process */
270 listen(fd, listener->backlog ? listener->backlog : listener->maxconn) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100271 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200272 goto err_unlink_temp;
273 }
274
275 /* 5. install.
276 * Point of no return: we are ready, we'll switch the sockets. We don't
277 * fear loosing the socket <path> because we have a copy of it in
278 * backname.
279 */
Willy Tarreau40aa0702013-03-10 23:51:38 +0100280 if (!ext && rename(tempname, path) < 0) {
Willy Tarreaub40dc942010-11-07 12:10:51 +0100281 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200282 goto err_rename;
283 }
284
Willy Tarreau40aa0702013-03-10 23:51:38 +0100285 /* 6. cleanup. If we're bound to an fd inherited from the parent, we
286 * want to ensure that destroy_uxst_socket() will never remove the
287 * path, and for this we simply clear the path to the socket.
288 */
289 if (!ext)
290 unlink(backname);
291 else
292 ((struct sockaddr_un *)&listener->addr)->sun_path[0] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200293
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100294 /* the socket is now listening */
295 listener->fd = fd;
296 listener->state = LI_LISTEN;
297
298 /* the function for the accept() event */
299 fd_insert(fd);
Willy Tarreauaece46a2012-07-06 12:25:58 +0200300 fdtab[fd].iocb = listener->proto->accept;
Willy Tarreaueabf3132008-08-29 23:36:51 +0200301 fdtab[fd].owner = listener; /* reference the listener instead of a task */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100302 return ERR_NONE;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100303 err_rename:
304 ret = rename(backname, path);
305 if (ret < 0 && errno == ENOENT)
306 unlink(path);
307 err_unlink_temp:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100308 if (!ext)
309 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100310 close(fd);
311 err_unlink_back:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100312 if (!ext)
313 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100314 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100315 if (msg && errlen) {
316 if (!ext)
317 snprintf(errmsg, errlen, "%s [%s]", msg, path);
318 else
319 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
320 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100321 return ERR_FATAL | ERR_ALERT;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100322}
323
324/* This function closes the UNIX sockets for the specified listener.
325 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
326 */
327static int uxst_unbind_listener(struct listener *listener)
328{
Willy Tarreaube58c382011-07-24 18:28:10 +0200329 if (listener->state > LI_ASSIGNED) {
330 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100331 destroy_uxst_socket(((struct sockaddr_un *)&listener->addr)->sun_path);
332 }
333 return ERR_NONE;
334}
335
336/* Add a listener to the list of unix stream listeners. The listener's state
337 * is automatically updated from LI_INIT to LI_ASSIGNED. The number of
338 * listeners is updated. This is the function to use to add a new listener.
339 */
340void uxst_add_listener(struct listener *listener)
341{
342 if (listener->state != LI_INIT)
343 return;
344 listener->state = LI_ASSIGNED;
345 listener->proto = &proto_unix;
346 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
347 proto_unix.nb_listeners++;
348}
349
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100350/********************************
351 * 3) protocol-oriented functions
352 ********************************/
353
354
Willy Tarreau92fb9832007-10-16 17:34:28 +0200355/* This function creates all UNIX sockets bound to the protocol entry <proto>.
356 * It is intended to be used as the protocol's bind_all() function.
357 * The sockets will be registered but not added to any fd_set, in order not to
358 * loose them across the fork(). A call to uxst_enable_listeners() is needed
359 * to complete initialization.
360 *
361 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
362 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200363static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200364{
365 struct listener *listener;
366 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200367
368 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200369 err |= uxst_bind_listener(listener, errmsg, errlen);
370 if (err & ERR_ABORT)
371 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200372 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200373 return err;
374}
375
Willy Tarreau92fb9832007-10-16 17:34:28 +0200376
377/* This function stops all listening UNIX sockets bound to the protocol
378 * <proto>. It does not detaches them from the protocol.
379 * It always returns ERR_NONE.
380 */
381static int uxst_unbind_listeners(struct protocol *proto)
382{
383 struct listener *listener;
384
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100385 list_for_each_entry(listener, &proto->listeners, proto_list)
386 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200387 return ERR_NONE;
388}
389
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200390/* parse the "mode" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200391static int bind_parse_mode(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200392{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200393 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200394 memprintf(err, "'%s' : missing mode (octal integer expected)", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200395 return ERR_ALERT | ERR_FATAL;
396 }
397
Willy Tarreau290e63a2012-09-20 18:07:14 +0200398 conf->ux.mode = strtol(args[cur_arg + 1], NULL, 8);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200399 return 0;
400}
401
402/* parse the "gid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200403static int bind_parse_gid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200404{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200405 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200406 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200407 return ERR_ALERT | ERR_FATAL;
408 }
409
Willy Tarreau290e63a2012-09-20 18:07:14 +0200410 conf->ux.gid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200411 return 0;
412}
413
414/* parse the "group" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200415static int bind_parse_group(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200416{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200417 struct group *group;
418
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200419 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200420 memprintf(err, "'%s' : missing group name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200421 return ERR_ALERT | ERR_FATAL;
422 }
423
424 group = getgrnam(args[cur_arg + 1]);
425 if (!group) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200426 memprintf(err, "'%s' : unknown group name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200427 return ERR_ALERT | ERR_FATAL;
428 }
429
Willy Tarreau290e63a2012-09-20 18:07:14 +0200430 conf->ux.gid = group->gr_gid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200431 return 0;
432}
433
434/* parse the "uid" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200435static int bind_parse_uid(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200436{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200437 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200438 memprintf(err, "'%s' : missing value", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200439 return ERR_ALERT | ERR_FATAL;
440 }
441
Willy Tarreau290e63a2012-09-20 18:07:14 +0200442 conf->ux.uid = atol(args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200443 return 0;
444}
445
446/* parse the "user" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200447static int bind_parse_user(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200448{
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200449 struct passwd *user;
450
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200451 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200452 memprintf(err, "'%s' : missing user name", args[cur_arg]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200453 return ERR_ALERT | ERR_FATAL;
454 }
455
456 user = getpwnam(args[cur_arg + 1]);
457 if (!user) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200458 memprintf(err, "'%s' : unknown user name '%s'", args[cur_arg], args[cur_arg + 1]);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200459 return ERR_ALERT | ERR_FATAL;
460 }
461
Willy Tarreau290e63a2012-09-20 18:07:14 +0200462 conf->ux.uid = user->pw_uid;
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200463 return 0;
464}
465
466/* Note: must not be declared <const> as its list will be overwritten.
467 * Please take care of keeping this list alphabetically sorted, doing so helps
468 * all code contributors.
469 * Optional keywords are also declared with a NULL ->parse() function so that
470 * the config parser can report an appropriate error when a known keyword was
471 * not enabled.
472 */
Willy Tarreau51fb7652012-09-18 18:24:39 +0200473static struct bind_kw_list bind_kws = { "UNIX", { }, {
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200474 { "gid", bind_parse_gid, 1 }, /* set the socket's gid */
475 { "group", bind_parse_group, 1 }, /* set the socket's gid from the group name */
476 { "mode", bind_parse_mode, 1 }, /* set the socket's mode (eg: 0644)*/
477 { "uid", bind_parse_uid, 1 }, /* set the socket's uid */
478 { "user", bind_parse_user, 1 }, /* set the socket's uid from the user name */
479 { NULL, NULL, 0 },
480}};
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100481
482/********************************
483 * 4) high-level functions
484 ********************************/
485
Willy Tarreau92fb9832007-10-16 17:34:28 +0200486__attribute__((constructor))
487static void __uxst_protocol_init(void)
488{
489 protocol_register(&proto_unix);
Willy Tarreaud0a895d2012-09-18 17:40:35 +0200490 bind_register_keywords(&bind_kws);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200491}
492
493
494/*
495 * Local variables:
496 * c-indent-level: 8
497 * c-basic-offset: 8
498 * End:
499 */