Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / opie / libopie / login.c
1 /* login.c: The opielogin() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
8
9         History:
10
11         Modified by cmetz for OPIE 2.4. Add support for ut_id and
12                 ut_syslen. Don't zero-terminate ut_name and ut_host.
13         Modified by cmetz for OPIE 2.31. If the OS won't tell us where
14                 _PATH_WTMP[X] is, try playing the SVID game, then use
15                 Autoconf-discovered values. Fixed gettimeofday() call
16                 and updwtmpx() call. Call endutxent for utmpx. Added
17                 DISABLE_UTMP.
18         Created by cmetz for OPIE 2.3.
19 */
20
21 #include "opie_cfg.h"
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <utmp.h>
25
26 #if DOUTMPX
27 #include <utmpx.h>
28 #define pututline(x) pututxline(x)
29 #define endutent endutxent
30 #define utmp utmpx
31 #endif /* DOUTMPX */
32
33 #if HAVE_STRING_H
34 #include <string.h>
35 #endif /* HAVE_STRING_H */
36 #include <sys/stat.h>
37 #if DEBUG
38 #include <syslog.h>
39 #include <errno.h>
40 #endif /* DEBUG */
41 #include "opie.h"
42
43 #define IDLEN 4
44
45 int opielogin FUNCTION((line, name, host), char *line AND char *name AND char *host)
46 {
47   int rval = 0;
48 #if !DISABLE_UTMP
49   struct utmp u;
50   char id[IDLEN + 1] = "";
51
52   if (__opiegetutmpentry(line, &u)) {
53 #if DEBUG
54     syslog(LOG_DEBUG, "opielogin: __opiegetutmpentry(line=%s, &u) failed", line);
55 #endif /* DEBUG */
56     memset(&u, 0, sizeof(struct utmp));
57     if (!strncmp(line, "/dev/", 5))
58       strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
59     else
60       strncpy(u.ut_line, line, sizeof(u.ut_line));
61 #if DEBUG
62     syslog(LOG_DEBUG, "opielogin: continuing with ut_line=%s", u.ut_line);
63 #endif /* DEBUG */
64   }
65
66 #if DOUTMPX || HAVE_UT_ID
67   strncpy(id, u.ut_id, sizeof(u.ut_id));
68   id[sizeof(id)-1] = 0;
69 #endif /* DOUTMPX || HAVE_UT_ID */
70
71 #if HAVE_UT_TYPE && defined(USER_PROCESS)
72   u.ut_type = USER_PROCESS;
73 #endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */
74 #if HAVE_UT_PID
75   u.ut_pid = getpid();
76 #endif /* HAVE_UT_PID */
77
78 #if HAVE_UT_NAME
79   strncpy(u.ut_name, name, sizeof(u.ut_name));
80 #else /* HAVE_UT_NAME */
81 #error No ut_name field in struct utmp? (Please send in a bug report)
82 #endif /* HAVE_UT_NAME */
83
84 #if HAVE_UT_HOST
85   strncpy(u.ut_host, host, sizeof(u.ut_host));
86 #endif /* HAVE_UT_HOST */
87 #if DOUTMPX && HAVE_UTX_SYSLEN
88   u.ut_syslen = strlen(host) + 1;
89 #endif /* DOUTMPX && HAVE_UT_SYSLEN */
90
91 #if DOUTMPX
92 #ifdef HAVE_ONE_ARG_GETTIMEOFDAY
93   gettimeofday(&u.ut_tv);
94 #else /* HAVE_ONE_ARG_GETTIMEOFDAY */
95   gettimeofday(&u.ut_tv, NULL);
96 #endif /* HAVE_ONE_ARG_GETTIMEOFDAY */
97 #else /* DOUTMPX */
98   time(&u.ut_time);
99 #endif /* DOUTMPX */
100
101   pututline(&u);
102   endutent();
103
104 #if DEBUG
105   syslog(LOG_DEBUG, "opielogin: utmp suceeded");
106 #endif /* DEBUG */
107 #endif /* !DISABLE_UTMP */
108
109 dowtmp:
110   opielogwtmp(line, name, host, id);
111   opielogwtmp(NULL, NULL, NULL);
112
113 dosetlogin:
114 #if HAVE_SETLOGIN
115   setlogin(name);
116 #endif /* HAVE_SETLOGIN */
117
118 #if DEBUG
119   syslog(LOG_DEBUG, "opielogin: rval=%d", rval);
120 #endif /* DEBUG */
121
122   return rval;
123 }