1 /* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
2 
3 #include "common.h"
4 #include "hash.h"
5 #include "str.h"
6 #include "strescape.h"
7 #include "ostream.h"
8 #include "connect-limit.h"
9 
10 struct ident_pid {
11 	/* ident string points to ident_hash keys */
12 	const char *ident;
13 	pid_t pid;
14 	unsigned int refcount;
15 };
16 
17 struct connect_limit {
18 	/* ident => unsigned int refcount */
19 	HASH_TABLE(char *, void *) ident_hash;
20 	/* struct ident_pid => struct ident_pid */
21 	HASH_TABLE(struct ident_pid *, struct ident_pid *) ident_pid_hash;
22 };
23 
ident_pid_hash(const struct ident_pid * i)24 static unsigned int ident_pid_hash(const struct ident_pid *i)
25 {
26 	return str_hash(i->ident) ^ i->pid;
27 }
28 
ident_pid_cmp(const struct ident_pid * i1,const struct ident_pid * i2)29 static int ident_pid_cmp(const struct ident_pid *i1, const struct ident_pid *i2)
30 {
31 	if (i1->pid < i2->pid)
32 		return -1;
33 	else if (i1->pid > i2->pid)
34 		return 1;
35 	else
36 		return strcmp(i1->ident, i2->ident);
37 }
38 
connect_limit_init(void)39 struct connect_limit *connect_limit_init(void)
40 {
41 	struct connect_limit *limit;
42 
43 	limit = i_new(struct connect_limit, 1);
44 	hash_table_create(&limit->ident_hash, default_pool, 0, str_hash, strcmp);
45 	hash_table_create(&limit->ident_pid_hash, default_pool, 0,
46 			  ident_pid_hash, ident_pid_cmp);
47 	return limit;
48 }
49 
connect_limit_deinit(struct connect_limit ** _limit)50 void connect_limit_deinit(struct connect_limit **_limit)
51 {
52 	struct connect_limit *limit = *_limit;
53 
54 	*_limit = NULL;
55 	hash_table_destroy(&limit->ident_hash);
56 	hash_table_destroy(&limit->ident_pid_hash);
57 	i_free(limit);
58 }
59 
connect_limit_lookup(struct connect_limit * limit,const char * ident)60 unsigned int connect_limit_lookup(struct connect_limit *limit,
61 				  const char *ident)
62 {
63 	void *value;
64 
65 	value = hash_table_lookup(limit->ident_hash, ident);
66 	return POINTER_CAST_TO(value, unsigned int);
67 }
68 
connect_limit_connect(struct connect_limit * limit,pid_t pid,const char * ident)69 void connect_limit_connect(struct connect_limit *limit, pid_t pid,
70 			   const char *ident)
71 {
72 	struct ident_pid *i, lookup_i;
73 	char *key;
74 	void *value;
75 
76 	if (!hash_table_lookup_full(limit->ident_hash, ident,
77 				    &key, &value)) {
78 		key = i_strdup(ident);
79 		value = POINTER_CAST(1);
80 		hash_table_insert(limit->ident_hash, key, value);
81 	} else {
82 		value = POINTER_CAST(POINTER_CAST_TO(value, unsigned int) + 1);
83 		hash_table_update(limit->ident_hash, key, value);
84 	}
85 
86 	lookup_i.ident = ident;
87 	lookup_i.pid = pid;
88 	i = hash_table_lookup(limit->ident_pid_hash, &lookup_i);
89 	if (i == NULL) {
90 		i = i_new(struct ident_pid, 1);
91 		i->ident = key;
92 		i->pid = pid;
93 		i->refcount = 1;
94 		hash_table_insert(limit->ident_pid_hash, i, i);
95 	} else {
96 		i->refcount++;
97 	}
98 }
99 
100 static void
connect_limit_ident_hash_unref(struct connect_limit * limit,const char * ident)101 connect_limit_ident_hash_unref(struct connect_limit *limit, const char *ident)
102 {
103 	char *key;
104 	void *value;
105 	unsigned int new_refcount;
106 
107 	if (!hash_table_lookup_full(limit->ident_hash, ident, &key, &value))
108 		i_panic("connect limit hash tables are inconsistent");
109 
110 	new_refcount = POINTER_CAST_TO(value, unsigned int) - 1;
111 	if (new_refcount > 0) {
112 		value = POINTER_CAST(new_refcount);
113 		hash_table_update(limit->ident_hash, key, value);
114 	} else {
115 		hash_table_remove(limit->ident_hash, key);
116 		i_free(key);
117 	}
118 }
119 
connect_limit_disconnect(struct connect_limit * limit,pid_t pid,const char * ident)120 void connect_limit_disconnect(struct connect_limit *limit, pid_t pid,
121 			      const char *ident)
122 {
123 	struct ident_pid *i, lookup_i;
124 
125 	lookup_i.ident = ident;
126 	lookup_i.pid = pid;
127 
128 	i = hash_table_lookup(limit->ident_pid_hash, &lookup_i);
129 	if (i == NULL) {
130 		i_error("connect limit: disconnection for unknown "
131 			"pid %s + ident %s", dec2str(pid), ident);
132 		return;
133 	}
134 
135 	if (--i->refcount == 0) {
136 		hash_table_remove(limit->ident_pid_hash, i);
137 		i_free(i);
138 	}
139 
140 	connect_limit_ident_hash_unref(limit, ident);
141 }
142 
connect_limit_disconnect_pid(struct connect_limit * limit,pid_t pid)143 void connect_limit_disconnect_pid(struct connect_limit *limit, pid_t pid)
144 {
145 	struct hash_iterate_context *iter;
146 	struct ident_pid *i, *value;
147 
148 	/* this should happen rarely (or never), so this slow implementation
149 	   should be fine. */
150 	iter = hash_table_iterate_init(limit->ident_pid_hash);
151 	while (hash_table_iterate(iter, limit->ident_pid_hash, &i, &value)) {
152 		if (i->pid == pid) {
153 			hash_table_remove(limit->ident_pid_hash, i);
154 			for (; i->refcount > 0; i->refcount--)
155 				connect_limit_ident_hash_unref(limit, i->ident);
156 			i_free(i);
157 		}
158 	}
159 	hash_table_iterate_deinit(&iter);
160 }
161 
connect_limit_dump(struct connect_limit * limit,struct ostream * output)162 void connect_limit_dump(struct connect_limit *limit, struct ostream *output)
163 {
164 	struct hash_iterate_context *iter;
165 	struct ident_pid *i, *value;
166 	string_t *str = t_str_new(256);
167 
168 	iter = hash_table_iterate_init(limit->ident_pid_hash);
169 	while (hash_table_iterate(iter, limit->ident_pid_hash, &i, &value)) {
170 		str_truncate(str, 0);
171 		str_append_tabescaped(str, i->ident);
172 		str_printfa(str, "\t%ld\t%u\n", (long)i->pid, i->refcount);
173 		if (o_stream_send(output, str_data(str), str_len(str)) < 0)
174 			break;
175 	}
176 	hash_table_iterate_deinit(&iter);
177 	o_stream_nsend(output, "\n", 1);
178 }
179