No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / login / utmpx_login.c
blobbcf5f22a70bd4b2e0f27b6c16c6076af7593289d
1 /************************************************************************
2 * Copyright 1995 by Wietse Venema. All rights reserved. Some individual
3 * files may be covered by other copyrights.
5 * This material was originally written and compiled by Wietse Venema at
6 * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
7 * 1992, 1993, 1994 and 1995.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that this entire copyright notice
11 * is duplicated in all such copies.
13 * This software is provided "as is" and without any expressed or implied
14 * warranties, including, without limitation, the implied warranties of
15 * merchantibility and fitness for any particular purpose.
16 ************************************************************************/
17 /* Author: Wietse Venema <wietse@wzv.win.tue.nl> */
19 #include "login_locl.h"
21 __RCSID("$Heimdal: utmpx_login.c 10020 2001-06-04 14:10:19Z assar $"
22 "$NetBSD$");
24 /* utmpx_login - update utmp and wtmp after login */
26 #ifndef HAVE_UTMPX_H
27 int utmpx_login(char *line, const char *user, const char *host) { return 0; }
28 #else
30 static void
31 utmpx_update(struct utmpx *ut, char *line, const char *user, const char *host)
33 struct timeval tmp;
34 char *clean_tty = clean_ttyname(line);
36 strncpy(ut->ut_line, clean_tty, sizeof(ut->ut_line));
37 #ifdef HAVE_STRUCT_UTMPX_UT_ID
38 strncpy(ut->ut_id, make_id(clean_tty), sizeof(ut->ut_id));
39 #endif
40 strncpy(ut->ut_user, user, sizeof(ut->ut_user));
41 shrink_hostname (host, ut->ut_host, sizeof(ut->ut_host));
42 #ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN
43 ut->ut_syslen = strlen(host) + 1;
44 if (ut->ut_syslen > sizeof(ut->ut_host))
45 ut->ut_syslen = sizeof(ut->ut_host);
46 #endif
47 ut->ut_type = USER_PROCESS;
48 gettimeofday (&tmp, 0);
49 ut->ut_tv.tv_sec = tmp.tv_sec;
50 ut->ut_tv.tv_usec = tmp.tv_usec;
51 pututxline(ut);
52 #ifdef WTMPX_FILE
53 updwtmpx(WTMPX_FILE, ut);
54 #elif defined(WTMP_FILE)
56 struct utmp utmp;
57 int fd;
59 prepare_utmp (&utmp, line, user, host);
60 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
61 write(fd, &utmp, sizeof(struct utmp));
62 close(fd);
65 #endif
68 int
69 utmpx_login(char *line, const char *user, const char *host)
71 struct utmpx *ut, save_ut;
72 pid_t mypid = getpid();
73 int ret = (-1);
76 * SYSV4 ttymon and login use tty port names with the "/dev/" prefix
77 * stripped off. Rlogind and telnetd, on the other hand, make utmpx
78 * entries with device names like /dev/pts/nnn. We therefore cannot use
79 * getutxline(). Return nonzero if no utmp entry was found with our own
80 * process ID for a login or user process.
83 while ((ut = getutxent())) {
84 /* Try to find a reusable entry */
85 if (ut->ut_pid == mypid
86 && ( ut->ut_type == INIT_PROCESS
87 || ut->ut_type == LOGIN_PROCESS
88 || ut->ut_type == USER_PROCESS)) {
89 save_ut = *ut;
90 utmpx_update(&save_ut, line, user, host);
91 ret = 0;
92 break;
95 if (ret == -1) {
96 /* Grow utmpx file by one record. */
97 struct utmpx newut;
98 memset(&newut, 0, sizeof(newut));
99 newut.ut_pid = mypid;
100 utmpx_update(&newut, line, user, host);
101 ret = 0;
103 endutxent();
104 return (ret);
106 #endif /* HAVE_UTMPX_H */