Update.
[glibc.git] / login / utmp_daemon.c
blob705c8b3de95c9839e3b09e95184ae3335578d529
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <utmp.h>
29 #include "utmp-private.h"
30 #include "programs/utmpd.h"
33 /* Descriptor for the socket. */
34 static int daemon_sock = INT_MIN;
37 /* Functions defined here. */
38 static int setutent_daemon (int reset);
39 static int getutent_r_daemon (struct utmp *buffer, struct utmp **result);
40 static int getutid_r_daemon (const struct utmp *line, struct utmp *buffer,
41 struct utmp **result);
42 static int getutline_r_daemon (const struct utmp *id, struct utmp *buffer,
43 struct utmp **result);
44 static struct utmp *pututline_daemon (const struct utmp *utmp);
45 static void endutent_daemon (void);
46 static int updwtmp_daemon (const char *file, const struct utmp *utmp);
48 /* Jump table for daemon functions. */
49 struct utfuncs __libc_utmp_daemon_functions =
51 setutent_daemon,
52 getutent_r_daemon,
53 getutid_r_daemon,
54 getutline_r_daemon,
55 pututline_daemon,
56 endutent_daemon,
57 updwtmp_daemon
60 static int do_setutent (int sock);
61 static int do_getutent (int sock, struct utmp *buffer);
62 static int do_endutent (int sock);
63 static int do_getutline (int sock, const struct utmp *line,
64 struct utmp *buffer);
65 static int do_getutid (int sock, const struct utmp *id,
66 struct utmp *buffer);
67 static int do_pututline (int sock, const struct utmp *utmp);
68 static int do_updwtmp (int sock, const char *file,
69 const struct utmp *utmp);
71 static int open_socket (const char *name);
72 static int send_request (int sock, const request_header *request,
73 reply_header *reply);
76 static int
77 setutent_daemon (int reset)
79 if (daemon_sock == INT_MIN)
81 daemon_sock = open_socket (_PATH_UTMPD_RW);
82 if (daemon_sock < 0)
84 /* Hhm, read-write access did not work. Try read-only. */
85 daemon_sock = open_socket (_PATH_UTMPD_RO);
86 if (daemon_sock < 0)
87 return 0;
90 /* Send request to the daemon. */
91 if (do_setutent (daemon_sock) < 0)
92 return 0;
94 else if (reset)
96 /* Send request to the daemon. */
97 if (do_setutent (daemon_sock) < 0)
98 return 0;
101 return 1;
105 static void
106 endutent_daemon (void)
108 if (daemon_sock >= 0)
110 /* Send request to the daemon. */
111 do_endutent (daemon_sock);
112 close (daemon_sock);
115 daemon_sock = INT_MIN;
119 static int
120 getutent_r_daemon (struct utmp *buffer, struct utmp **result)
122 /* Open connection if not already done. */
123 if (daemon_sock == INT_MIN)
124 setutent_daemon (1);
126 if (daemon_sock < 0)
128 /* Not available. */
129 *result = NULL;
130 return -1;
133 /* Send request to the daemon. */
134 if (do_getutent (daemon_sock, buffer) < 0)
136 *result = NULL;
137 return -1;;
140 *result = buffer;
141 return 0;
145 static int
146 getutline_r_daemon (const struct utmp *line, struct utmp *buffer,
147 struct utmp **result)
149 if (daemon_sock < 0)
151 *result = NULL;
152 return -1;
155 /* Send request to the daemon. */
156 if (do_getutline (daemon_sock, line, buffer) < 0)
158 *result = NULL;
159 return -1;;
162 *result = buffer;
163 return 0;
167 static int
168 getutid_r_daemon (const struct utmp *id, struct utmp *buffer,
169 struct utmp **result)
171 if (daemon_sock < 0)
173 *result = NULL;
174 return -1;
177 /* Send request to the daemon. */
178 if (do_getutid (daemon_sock, id, buffer) < 0)
180 *result = NULL;
181 return -1;
184 *result = buffer;
185 return 0;
189 static struct utmp *
190 pututline_daemon (const struct utmp *utmp)
192 if (daemon_sock == INT_MIN)
193 /* The connection is closed. Open it again. */
194 setutent_daemon (0);
196 if (daemon_sock < 0)
197 /* Something went wrong. */
198 return NULL;
200 /* Send request to the daemon. */
201 if (do_pututline (daemon_sock, utmp) < 0)
202 return NULL;
204 return (struct utmp *)utmp;
208 static int
209 updwtmp_daemon (const char *file, const struct utmp *utmp)
211 int sock;
213 /* Only try to open for both reading and writing. */
214 sock = open_socket (_PATH_UTMPD_RW);
215 if (sock < 0)
216 return -1;
218 /* Send request to the daemon. */
219 if (do_updwtmp (sock, file, utmp) < 0)
220 return -1;
222 close (sock);
223 return 0;
227 static int
228 do_setutent (int sock)
230 setutent_request request;
231 setutent_reply reply;
233 request.header.version = UTMPD_VERSION;
234 request.header.size = sizeof (setutent_request);
235 request.header.type = UTMPD_REQ_SETUTENT;
236 strncpy (request.file, __libc_utmp_file_name, sizeof request.file);
238 reply.header.version = UTMPD_VERSION;
239 reply.header.size = sizeof (setutent_reply);
240 reply.header.type = UTMPD_REQ_SETUTENT;
242 if (send_request (sock, &request.header, &reply.header) < 0)
243 return -1;
245 if (reply.result < 0)
246 __set_errno (reply.errnum);
248 return reply.result;
251 static int
252 do_getutent (int sock, struct utmp *buffer)
254 getutent_request request;
255 getutent_reply reply;
257 request.header.version = UTMPD_VERSION;
258 request.header.size = sizeof (getutent_request);
259 request.header.type = UTMPD_REQ_GETUTENT;
261 reply.header.version = UTMPD_VERSION;
262 reply.header.size = sizeof (getutent_reply);
263 reply.header.type = UTMPD_REQ_GETUTENT;
265 if (send_request (sock, &request.header, &reply.header) < 0)
266 return -1;
268 if (reply.result < 0)
269 __set_errno (reply.errnum);
270 else
271 memcpy (buffer, &reply.entry, sizeof (struct utmp));
273 return reply.result;
276 static int
277 do_endutent (int sock)
279 endutent_request request;
280 endutent_reply reply;
282 request.header.version = UTMPD_VERSION;
283 request.header.size = sizeof (endutent_request);
284 request.header.type = UTMPD_REQ_ENDUTENT;
286 reply.header.version = UTMPD_VERSION;
287 reply.header.size = sizeof (endutent_reply);
288 reply.header.type = UTMPD_REQ_ENDUTENT;
290 if (send_request (sock, &request.header, &reply.header) < 0)
291 return -1;
293 if (reply.result < 0)
294 __set_errno (reply.errnum);
296 return reply.result;
299 static int
300 do_getutline (int sock, const struct utmp *line, struct utmp *buffer)
302 getutline_request request;
303 getutline_reply reply;
305 request.header.version = UTMPD_VERSION;
306 request.header.size = sizeof (getutline_request);
307 request.header.type = UTMPD_REQ_GETUTLINE;
308 memcpy (&request.line, line, sizeof (struct utmp));
310 reply.header.version = UTMPD_VERSION;
311 reply.header.size = sizeof (getutline_reply);
312 reply.header.type = UTMPD_REQ_GETUTLINE;
314 if (send_request (sock, &request.header, &reply.header) < 0)
315 return -1;
317 if (reply.result < 0)
318 __set_errno (reply.errnum);
319 else
320 memcpy (buffer, &reply.entry, sizeof (struct utmp));
322 return reply.result;
325 static int
326 do_getutid (int sock, const struct utmp *id, struct utmp *buffer)
328 getutid_request request;
329 getutid_reply reply;
331 request.header.version = UTMPD_VERSION;
332 request.header.size = sizeof (getutid_request);
333 request.header.type = UTMPD_REQ_GETUTID;
334 memcpy (&request.id, id, sizeof (struct utmp));
336 reply.header.version = UTMPD_VERSION;
337 reply.header.size = sizeof (getutid_reply);
338 reply.header.type = UTMPD_REQ_GETUTID;
340 if (send_request (sock, &request.header, &reply.header) < 0)
341 return -1;
343 if (reply.result < 0)
344 __set_errno (reply.errnum);
345 else
346 memcpy (buffer, &reply.entry, sizeof (struct utmp));
348 return reply.result;
351 static int
352 do_pututline (int sock, const struct utmp *utmp)
354 pututline_request request;
355 pututline_reply reply;
357 request.header.version = UTMPD_VERSION;
358 request.header.size = sizeof (pututline_request);
359 request.header.type = UTMPD_REQ_PUTUTLINE;
360 memcpy (&request.utmp, utmp, sizeof (struct utmp));
362 reply.header.version = UTMPD_VERSION;
363 reply.header.size = sizeof (pututline_reply);
364 reply.header.type = UTMPD_REQ_PUTUTLINE;
366 if (send_request (sock, &request.header, &reply.header) < 0)
367 return -1;
369 if (reply.result < 0)
370 __set_errno (reply.errnum);
372 return reply.result;
375 static int
376 do_updwtmp (int sock, const char *file, const struct utmp *utmp)
378 updwtmp_request request;
379 updwtmp_reply reply;
381 request.header.version = UTMPD_VERSION;
382 request.header.size = sizeof (updwtmp_request);
383 request.header.type = UTMPD_REQ_UPDWTMP;
384 strncpy (request.file, file, sizeof request.file);
385 memcpy (&request.utmp, utmp, sizeof (struct utmp));
387 reply.header.version = UTMPD_VERSION;
388 reply.header.size = sizeof (updwtmp_reply);
389 reply.header.type = UTMPD_REQ_UPDWTMP;
391 if (send_request (sock, &request.header, &reply.header) < 0)
392 return -1;
394 if (reply.result < 0)
395 __set_errno (reply.errnum);
397 return reply.result;
401 /* Create a socket connected to NAME. */
402 static int
403 open_socket (const char *name)
405 struct sockaddr_un addr;
406 int sock;
408 sock = socket (PF_UNIX, SOCK_STREAM, 0);
409 if (sock < 0)
410 return -1;
412 addr.sun_family = AF_UNIX;
413 strcpy (addr.sun_path, name);
414 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
416 close (sock);
417 return -1;
420 return sock;
423 /* Send REQUEST to SOCK, and wait for reply. Returns 0 if successful,
424 storing the reply in REPLY, and -1 if not. */
425 static int
426 send_request (int sock, const request_header *request,
427 reply_header *reply)
429 reply_header header;
430 ssize_t nbytes;
432 nbytes = write (sock, request, request->size);
433 if (nbytes != (ssize_t) request->size)
434 return -1;
436 nbytes = read (sock, &header, sizeof (reply_header));
437 if (nbytes != sizeof (reply_header))
438 return -1;
440 if (reply->version != header.version
441 || reply->size != header.size
442 || reply->type != header.type)
443 return -1;
445 nbytes = read (sock, reply + 1, reply->size - sizeof (reply_header));
446 if (nbytes != (ssize_t) (reply->size - sizeof (reply_header)))
447 return -1;
449 return 0;