login.c revision 22347
1/* login.c: The opielogin() library function.
2
3%%% copyright-cmetz
4This software is Copyright 1996 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 2 applies to this software.
6You should have received a copy of the license with this software. If
7you didn't get a copy, you may request one from <license@inner.net>.
8
9        History:
10
11        Created by cmetz for OPIE 2.3.
12*/
13
14#include "opie_cfg.h"
15#include <stdio.h>
16#include <sys/types.h>
17#include <utmp.h>
18
19#if DOUTMPX
20#include <utmpx.h>
21#define pututline(x) pututxline(x)
22#define utmp utmpx
23#endif /* DOUTMPX */
24
25#if HAVE_STRING_H
26#include <string.h>
27#endif /* HAVE_STRING_H */
28#include <sys/stat.h>
29#if DEBUG
30#include <syslog.h>
31#include <errno.h>
32#endif /* DEBUG */
33#include "opie.h"
34
35int opielogin FUNCTION((line, name, host), char *line AND char *name AND char *host)
36{
37  struct utmp u;
38  int rval = 0;
39
40  if (__opiegetutmpentry(line, &u)) {
41#if DEBUG
42    syslog(LOG_DEBUG, "opielogin: __opiegetutmpentry(line=%s, &u) failed", line);
43#endif /* DEBUG */
44    memset(&u, 0, sizeof(struct utmp));
45    if (!strncmp(line, "/dev/", 5))
46      strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
47    else
48      strncpy(u.ut_line, line, sizeof(u.ut_line));
49#if DEBUG
50    syslog(LOG_DEBUG, "opielogin: continuing with ut_line=%s", u.ut_line);
51#endif /* DEBUG */
52  }
53
54#if HAVE_UT_TYPE && defined(USER_PROCESS)
55  u.ut_type = USER_PROCESS;
56#endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */
57#if HAVE_UT_PID
58  u.ut_pid = getpid();
59#endif /* HAVE_UT_PID */
60
61#if HAVE_UT_NAME
62  strncpy(u.ut_name, name, sizeof(u.ut_name));
63  u.ut_name[sizeof(u.ut_name)] = 0;
64#else /* HAVE_UT_NAME */
65#error No ut_name field in struct utmp? (Please send in a bug report)
66#endif /* HAVE_UT_NAME */
67
68#if HAVE_UT_HOST
69  strncpy(u.ut_host, host, sizeof(u.ut_host));
70  u.ut_host[sizeof(u.ut_host)] = 0;
71#endif /* HAVE_UT_HOST */
72
73#if DOUTMPX
74#ifdef HAVE_ONE_ARG_GETTIMEOFDAY
75  gettimeofday(&u->ut_tv);
76#else /* HAVE_ONE_ARG_GETTIMEOFDAY */
77  gettimeofday(&u->ut_tv, NULL);
78#endif /* HAVE_ONE_ARG_GETTIMEOFDAY */
79#else /* DOUTMPX */
80  time(&u.ut_time);
81#endif /* DOUTMPX */
82
83  pututline(&u);
84  endutent();
85
86#if DEBUG
87  syslog(LOG_DEBUG, "opielogin: utmp suceeded");
88#endif /* DEBUG */
89
90dowtmp:
91  {
92    FILE *f;
93
94#if DOUTMPX
95    updutmpx(_PATH_WTMPX, &u);
96#else /* DOUTMPX */
97    if (!(f = __opieopen(_PATH_WTMP, 2, 0664))) {
98      rval = -1;
99#if DEBUG
100      syslog(LOG_DEBUG, "opielogin: wtmp open failed: %s (%d)", strerror(errno), errno);
101#endif /* DEBUG */
102      goto dosetlogin;
103    }
104
105    if (fwrite(&u, sizeof(struct utmp), 1, f) != sizeof(struct utmp)) {
106#if DEBUG
107      syslog(LOG_DEBUG, "opielogin: wtmp write failed: %s (%d)", strerror(errno), errno);
108#endif /* DEBUG */
109      rval = -1;
110    }
111
112    fclose(f);
113#endif /* DOUTMPX */
114  }
115
116#if DEBUG
117  syslog(LOG_DEBUG, "opielogin: wtmp suceeded");
118#endif /* DEBUG */
119
120dosetlogin:
121#if HAVE_SETLOGIN
122  setlogin(name);
123#endif /* HAVE_SETLOGIN */
124
125#if DEBUG
126  syslog(LOG_DEBUG, "opielogin: rval=%d", rval);
127#endif /* DEBUG */
128
129  return rval;
130}
131