update from main archive 970619
[glibc/history.git] / login / utmp_daemon.c
blobe0a20e9a9f707018c8e7e281f5f9eb1a2a31428b
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;
232 size_t size;
234 size = sizeof (setutent_request) + strlen (__libc_utmp_file_name) + 1;
236 request = malloc (size);
237 if (request == NULL)
238 return -1;
240 request->header.version = UTMPD_VERSION;
241 request->header.size = size;
242 request->header.type = UTMPD_REQ_SETUTENT;
243 strcpy (request->file, __libc_utmp_file_name);
245 reply.header.version = UTMPD_VERSION;
246 reply.header.size = sizeof (setutent_reply);
247 reply.header.type = UTMPD_REQ_SETUTENT;
249 if (send_request (sock, &request->header, &reply.header) < 0)
251 free (request);
252 return -1;
255 if (reply.result < 0)
256 __set_errno (reply.errnum);
258 free (request);
259 return reply.result;
262 static int
263 do_getutent (int sock, struct utmp *buffer)
265 getutent_request request;
266 getutent_reply reply;
268 request.header.version = UTMPD_VERSION;
269 request.header.size = sizeof (getutent_request);
270 request.header.type = UTMPD_REQ_GETUTENT;
272 reply.header.version = UTMPD_VERSION;
273 reply.header.size = sizeof (getutent_reply);
274 reply.header.type = UTMPD_REQ_GETUTENT;
276 if (send_request (sock, &request.header, &reply.header) < 0)
277 return -1;
279 if (reply.result < 0)
280 __set_errno (reply.errnum);
281 else
282 memcpy (buffer, &reply.entry, sizeof (struct utmp));
284 return reply.result;
287 static int
288 do_endutent (int sock)
290 endutent_request request;
291 endutent_reply reply;
293 request.header.version = UTMPD_VERSION;
294 request.header.size = sizeof (endutent_request);
295 request.header.type = UTMPD_REQ_ENDUTENT;
297 reply.header.version = UTMPD_VERSION;
298 reply.header.size = sizeof (endutent_reply);
299 reply.header.type = UTMPD_REQ_ENDUTENT;
301 if (send_request (sock, &request.header, &reply.header) < 0)
302 return -1;
304 if (reply.result < 0)
305 __set_errno (reply.errnum);
307 return reply.result;
310 static int
311 do_getutline (int sock, const struct utmp *line, struct utmp *buffer)
313 getutline_request request;
314 getutline_reply reply;
316 request.header.version = UTMPD_VERSION;
317 request.header.size = sizeof (getutline_request);
318 request.header.type = UTMPD_REQ_GETUTLINE;
319 memcpy (&request.line, line, sizeof (struct utmp));
321 reply.header.version = UTMPD_VERSION;
322 reply.header.size = sizeof (getutline_reply);
323 reply.header.type = UTMPD_REQ_GETUTLINE;
325 if (send_request (sock, &request.header, &reply.header) < 0)
326 return -1;
328 if (reply.result < 0)
329 __set_errno (reply.errnum);
330 else
331 memcpy (buffer, &reply.entry, sizeof (struct utmp));
333 return reply.result;
336 static int
337 do_getutid (int sock, const struct utmp *id, struct utmp *buffer)
339 getutid_request request;
340 getutid_reply reply;
342 request.header.version = UTMPD_VERSION;
343 request.header.size = sizeof (getutid_request);
344 request.header.type = UTMPD_REQ_GETUTID;
345 memcpy (&request.id, id, sizeof (struct utmp));
347 reply.header.version = UTMPD_VERSION;
348 reply.header.size = sizeof (getutid_reply);
349 reply.header.type = UTMPD_REQ_GETUTID;
351 if (send_request (sock, &request.header, &reply.header) < 0)
352 return -1;
354 if (reply.result < 0)
355 __set_errno (reply.errnum);
356 else
357 memcpy (buffer, &reply.entry, sizeof (struct utmp));
359 return reply.result;
362 static int
363 do_pututline (int sock, const struct utmp *utmp)
365 pututline_request request;
366 pututline_reply reply;
368 request.header.version = UTMPD_VERSION;
369 request.header.size = sizeof (pututline_request);
370 request.header.type = UTMPD_REQ_PUTUTLINE;
371 memcpy (&request.utmp, utmp, sizeof (struct utmp));
373 reply.header.version = UTMPD_VERSION;
374 reply.header.size = sizeof (pututline_reply);
375 reply.header.type = UTMPD_REQ_PUTUTLINE;
377 if (send_request (sock, &request.header, &reply.header) < 0)
378 return -1;
380 if (reply.result < 0)
381 __set_errno (reply.errnum);
383 return reply.result;
386 static int
387 do_updwtmp (int sock, const char *file, const struct utmp *utmp)
389 updwtmp_request *request;
390 updwtmp_reply reply;
391 size_t size;
393 size = sizeof (updwtmp_request) + strlen (file) + 1;
395 request = malloc (size);
396 if (request == NULL)
397 return -1;
399 request->header.version = UTMPD_VERSION;
400 request->header.size = size;
401 request->header.type = UTMPD_REQ_UPDWTMP;
402 memcpy (&request->utmp, utmp, sizeof (struct utmp));
403 strcpy (request->file, file);
405 reply.header.version = UTMPD_VERSION;
406 reply.header.size = sizeof (updwtmp_reply);
407 reply.header.type = UTMPD_REQ_UPDWTMP;
409 if (send_request (sock, &request->header, &reply.header) < 0)
411 free (request);
412 return -1;
415 if (reply.result < 0)
416 __set_errno (reply.errnum);
418 free (request);
419 return reply.result;
423 /* Create a socket connected to NAME. */
424 static int
425 open_socket (const char *name)
427 struct sockaddr_un addr;
428 int sock;
430 sock = socket (PF_UNIX, SOCK_STREAM, 0);
431 if (sock < 0)
432 return -1;
434 addr.sun_family = AF_UNIX;
435 strcpy (addr.sun_path, name);
436 if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0)
438 close (sock);
439 return -1;
442 return sock;
445 /* Send REQUEST to SOCK, and wait for reply. Returns 0 if successful,
446 storing the reply in REPLY, and -1 if not. */
447 static int
448 send_request (int sock, const request_header *request,
449 reply_header *reply)
451 reply_header header;
452 ssize_t nbytes;
454 nbytes = write (sock, request, request->size);
455 if (nbytes != (ssize_t) request->size)
456 return -1;
458 nbytes = read (sock, &header, sizeof (reply_header));
459 if (nbytes != sizeof (reply_header))
460 return -1;
462 if (reply->version != header.version
463 || reply->size != header.size
464 || reply->type != header.type)
465 return -1;
467 nbytes = read (sock, reply + 1, reply->size - sizeof (reply_header));
468 if (nbytes != (ssize_t) (reply->size - sizeof (reply_header)))
469 return -1;
471 return 0;