blob: f68c0073af2b4a26737548c4701fbfd1d9f118ac [file] [log] [blame]
Willy Tarreau0d06df62020-08-28 15:10:11 +02001/*
2 * SOCK_UNIX socket management
3 *
4 * Copyright 2000-2020 Willy Tarreau <w@1wt.eu>
5 *
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 <string.h>
15
16#include <sys/param.h>
17#include <sys/socket.h>
18#include <sys/types.h>
19
20#include <sys/socket.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <sys/un.h>
24
25#include <haproxy/api.h>
26#include <haproxy/listener.h>
27#include <haproxy/namespace.h>
28#include <haproxy/sock_unix.h>
29#include <haproxy/tools.h>
30
31
32/* PLEASE NOTE for functions below:
33 *
34 * The address family SHOULD always be checked. In some cases a function will
35 * be used in a situation where the address family is guaranteed (e.g. protocol
36 * definitions), so the test may be avoided. This special case must then be
37 * mentioned in the comment before the function definition.
38 */
39
40
41/* Compares two AF_UNIX sockaddr addresses. Returns 0 if they match or non-zero
42 * if they do not match. It also supports ABNS socket addresses (those starting
43 * with \0). For regular UNIX sockets however, this does explicitly support
44 * matching names ending exactly with .XXXXX.tmp which are newly bound sockets
45 * about to be replaced; this suffix is then ignored. Note that our UNIX socket
46 * paths are always zero-terminated.
47 */
48int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
49{
50 const struct sockaddr_un *au = (const struct sockaddr_un *)a;
51 const struct sockaddr_un *bu = (const struct sockaddr_un *)b;
52 int idx, dot, idx2;
53
54 if (a->ss_family != b->ss_family)
55 return -1;
56
57 if (a->ss_family != AF_UNIX)
58 return -1;
59
60 if (au->sun_path[0] != bu->sun_path[0])
61 return -1;
62
63 if (au->sun_path[0] == 0)
64 return memcmp(au->sun_path, bu->sun_path, sizeof(au->sun_path));
65
66 idx = 1; dot = 0;
67 while (au->sun_path[idx] == bu->sun_path[idx]) {
68 if (au->sun_path[idx] == 0)
69 return 0;
70 if (au->sun_path[idx] == '.')
71 dot = idx;
72 idx++;
73 }
74
75 /* Now we have a difference. It's OK if they are within or after a
76 * sequence of digits following a dot, and are followed by ".tmp".
77 */
78 if (!dot)
79 return -1;
80
81 /* First, check in path "a" */
82 if (au->sun_path[idx] != 0) {
Willy Tarreau1c34b882020-08-29 06:44:37 +020083 for (idx2 = dot + 1; idx2 && isdigit((unsigned char)au->sun_path[idx2]);)
Willy Tarreau0d06df62020-08-28 15:10:11 +020084 idx2++;
85 if (strcmp(au->sun_path + idx2, ".tmp") != 0)
86 return -1;
87 }
88
89 /* Then check in path "b" */
90 if (bu->sun_path[idx] != 0) {
Willy Tarreau1c34b882020-08-29 06:44:37 +020091 for (idx2 = dot + 1; idx2 && isdigit((unsigned char)bu->sun_path[idx2]); idx2++)
Willy Tarreau0d06df62020-08-28 15:10:11 +020092 ;
93 if (strcmp(bu->sun_path + idx2, ".tmp") != 0)
94 return -1;
95 }
96
97 /* OK that's a match */
98 return 0;
99}