Add configuration parameter "tls_dh_params_file".
[libpwmd.git] / src / status.c
blob2df3af06616ba15ccf272bd7d3a3b63b60ad0eb7
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
3 2016
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <pthread.h>
26 #include <stdarg.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <errno.h>
30 #include <poll.h>
32 #ifdef HAVE_LINUX_SOCKIOS_H
33 #include <linux/sockios.h>
34 #endif
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
38 #endif
40 #include "pwmd-error.h"
41 #include "mutex.h"
42 #include "util-misc.h"
43 #include "common.h"
44 #include "util-string.h"
45 #include "status.h"
46 #include "cache.h"
47 #include "mem.h"
49 gpg_error_t
50 send_status (assuan_context_t ctx, status_msg_t which, const char *fmt, ...)
52 const char *line = NULL;
53 char buf[ASSUAN_LINELENGTH + 1];
54 const char *status = NULL;
55 #ifdef WITH_GNUTLS
56 struct client_s *client = ctx ? assuan_get_pointer (ctx) : NULL;
57 #endif
58 gpg_error_t rc = 0;
60 if (fmt)
62 va_list ap;
64 va_start (ap, fmt);
65 vsnprintf (buf, sizeof (buf), fmt, ap);
66 va_end (ap);
67 line = buf;
70 switch (which)
72 case STATUS_EXPIRE:
73 status = "EXPIRE";
74 break;
75 case STATUS_GENKEY:
76 status = "GENKEY";
77 break;
78 case STATUS_XFER:
79 status = "XFER";
80 break;
81 case STATUS_CACHE:
82 snprintf (buf, sizeof (buf), "%u", cache_file_count ());
83 line = buf;
84 status = "CACHE";
85 break;
86 case STATUS_CLIENTS:
87 MUTEX_LOCK (&cn_mutex);
88 snprintf (buf, sizeof (buf), "%u", slist_length (cn_thread_list));
89 line = buf;
90 MUTEX_UNLOCK (&cn_mutex);
91 status = "CLIENTS";
92 break;
93 case STATUS_LOCKED:
94 status = "LOCKED";
95 line = _("Waiting for lock");
96 break;
97 case STATUS_ENCRYPT:
98 status = "ENCRYPT";
99 break;
100 case STATUS_DECRYPT:
101 status = "DECRYPT";
102 break;
103 case STATUS_NEWFILE:
104 status = "NEWFILE";
105 break;
106 case STATUS_KEEPALIVE:
107 status = "KEEPALIVE";
108 break;
109 case STATUS_GPGME:
110 status = "GPGME";
111 break;
112 case STATUS_STATE:
113 status = "STATE";
114 break;
117 if (!ctx)
119 log_write ("%s %s", status, line ? line : "");
120 return 0;
123 #ifdef WITH_GNUTLS
124 if (client && client->thd->remote && which == STATUS_KEEPALIVE)
126 int buffered = 0;
128 #ifdef HAVE_DECL_SIOCOUTQ
129 if (ioctl (client->thd->fd, SIOCOUTQ, &buffered) == -1)
130 log_write ("%s(%i): ioctl: %s", __FUNCTION__, __LINE__,
131 pwmd_strerror (gpg_error_from_errno (errno)));
132 #elif defined (HAVE_DECL_FIONWRITE)
133 if (ioctl (client->thd->fd, FIONWRITE, &buffered) == -1)
134 log_write ("%s(%i): ioctl: %s", __FUNCTION__, __LINE__,
135 pwmd_strerror (gpg_error_from_errno (errno)));
136 #else
137 if (1)
139 int sndbuf;
140 socklen_t len = sizeof(int);
142 if (getsockopt (client->thd->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
143 &len) == -1)
144 log_write ("%s(%i): getsockopt: %s", __FUNCTION__, __LINE__,
145 pwmd_strerror (gpg_error_from_errno (errno)));
146 else
148 int lowat;
150 len = sizeof(int);
151 if (getsockopt (client->thd->fd, SOL_SOCKET, SO_SNDLOWAT,
152 &lowat, &len) == -1)
153 log_write ("%s(%i): getsockopt: %s", __FUNCTION__,
154 __LINE__, pwmd_strerror (gpg_error_from_errno (errno)));
155 else
157 len = sizeof(int);
158 if (setsockopt (client->thd->fd, SOL_SOCKET, SO_SNDLOWAT,
159 &sndbuf, len) == -1)
160 log_write ("%s(%i): setsockopt: %s", __FUNCTION__,
161 __LINE__,
162 pwmd_strerror (gpg_error_from_errno (errno)));
163 else
165 struct pollfd fds[1];
166 int n;
168 fds[0].fd = client->thd->fd;
169 fds[0].events = POLLOUT;
171 buffered = client->thd->last_buffer_size + 1;
172 n = poll (fds, 1, 0);
173 len = sizeof(int);
174 if (setsockopt (client->thd->fd, SOL_SOCKET, SO_SNDLOWAT,
175 &lowat, len) == -1)
176 log_write ("%s(%i): setsockopt: %s", __FUNCTION__,
177 __LINE__,
178 pwmd_strerror (gpg_error_from_errno (errno)));
179 if (n > 0 && (fds[0].revents & POLLOUT))
180 buffered = 0;
185 #endif
186 if (buffered)
188 int interval = config_get_integer ("global", "keepalive_interval");
189 int timeout = config_get_integer ("global", "tls_timeout");
191 if (buffered < client->thd->last_buffer_size)
192 client->thd->buffer_timeout = 0;
194 client->thd->last_buffer_size = buffered;
196 if (++client->thd->buffer_timeout * interval >= timeout)
197 rc = gpg_error (GPG_ERR_ETIMEDOUT);
199 else
200 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
202 #endif
204 if (!rc)
205 rc = assuan_write_status (ctx, status, line);
207 #ifdef WITH_GNUTLS
208 if (client && client->thd->remote && which != STATUS_KEEPALIVE)
209 client->thd->buffer_timeout = client->thd->last_buffer_size = 0;
210 #endif
212 return rc;
215 static void
216 do_send_status_all (status_msg_t s, const char *line, pthread_t *not_tid,
217 int invoker)
219 MUTEX_LOCK (&cn_mutex);
220 int i = 0;
221 int t = slist_length (cn_thread_list);
223 for (; i < t; i++)
225 struct client_thread_s *thd = slist_nth_data (cn_thread_list, i);
226 struct status_msg_s *msg, *p;
227 char c = 0xff;
228 int match = 0;
229 gpg_error_t rc;
231 if (not_tid && pthread_equal (*not_tid, thd->tid))
232 continue;
234 /* Only want to send this status message to invoking_user clients'. */
235 if (thd->state == CLIENT_STATE_UNKNOWN
236 || thd->state == CLIENT_STATE_DISCON
237 || (invoker && peer_is_invoker (thd->cl)))
238 continue;
240 MUTEX_LOCK (&thd->status_mutex);
242 for (p = thd->msg_queue; p; p = p->next)
244 if (p->s == s)
246 match = 1;
247 break;
251 if (match && s != STATUS_STATE)
253 xfree (p->line);
254 p->line = line ? str_dup (line) : NULL;
256 if (!thd->wrote_status)
258 ssize_t ret = write (thd->status_msg_pipe[1], &c, 1);
260 rc = gpg_error_from_syserror ();
261 if (ret == -1)
262 log_write ("%s (%i): %s", __FUNCTION__, __LINE__,
263 pwmd_strerror (rc));
266 thd->wrote_status = 1;
267 MUTEX_UNLOCK (&thd->status_mutex);
268 continue;
271 msg = xcalloc (1, sizeof (struct status_msg_s));
272 msg->s = s;
273 msg->line = line ? str_dup (line) : NULL;
275 for (p = thd->msg_queue; p && p->next; p = p->next);
276 if (!p)
277 thd->msg_queue = msg;
278 else
279 p->next = msg;
281 if (!thd->wrote_status)
283 ssize_t ret = write (thd->status_msg_pipe[1], &c, 1);
285 rc = gpg_error_from_syserror ();
286 if (ret == -1)
287 log_write ("%s (%i): %s", __FUNCTION__, __LINE__,
288 pwmd_strerror (rc));
291 thd->wrote_status = 1;
292 MUTEX_UNLOCK (&thd->status_mutex);
295 MUTEX_UNLOCK (&cn_mutex);
298 void
299 send_status_all_not_self (int invoker, status_msg_t s, const char *fmt, ...)
301 char *line = NULL;
302 pthread_t tid = pthread_self ();
304 if (fmt)
306 va_list ap;
308 va_start (ap, fmt);
309 str_vasprintf (&line, fmt, ap);
310 va_end (ap);
313 do_send_status_all (s, line, &tid, invoker);
314 xfree (line);
317 void
318 send_status_all (status_msg_t s, const char *fmt, ...)
320 char *line = NULL;
322 if (fmt)
324 va_list ap;
326 va_start (ap, fmt);
327 str_vasprintf (&line, fmt, ap);
328 va_end (ap);
331 do_send_status_all (s, line, NULL, 0);
332 xfree (line);