122347Spst/* login.c: The opielogin() library function.
222347Spst
329964Sache%%% copyright-cmetz-96
492906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
592906SmarkmThe Inner Net License Version 3 applies to this software.
622347SpstYou should have received a copy of the license with this software. If
722347Spstyou didn't get a copy, you may request one from <license@inner.net>.
822347Spst
922347Spst        History:
1022347Spst
1192906Smarkm	Modified by cmetz for OPIE 2.4. Add support for ut_id and
1292906Smarkm		ut_syslen. Don't zero-terminate ut_name and ut_host.
1329964Sache	Modified by cmetz for OPIE 2.31. If the OS won't tell us where
1429964Sache		_PATH_WTMP[X] is, try playing the SVID game, then use
1529964Sache		Autoconf-discovered values. Fixed gettimeofday() call
1629964Sache		and updwtmpx() call. Call endutxent for utmpx. Added
1729964Sache		DISABLE_UTMP.
1822347Spst        Created by cmetz for OPIE 2.3.
1922347Spst*/
2022347Spst
2122347Spst#include "opie_cfg.h"
2222347Spst#include <stdio.h>
2322347Spst#include <sys/types.h>
2422347Spst
2522347Spst#if DOUTMPX
2622347Spst#include <utmpx.h>
2722347Spst#define pututline(x) pututxline(x)
2829964Sache#define endutent endutxent
2922347Spst#define utmp utmpx
30202086Sed#else
31202086Sed#include <utmp.h>
3222347Spst#endif /* DOUTMPX */
3322347Spst
3422347Spst#if HAVE_STRING_H
3522347Spst#include <string.h>
3622347Spst#endif /* HAVE_STRING_H */
3722347Spst#include <sys/stat.h>
3822347Spst#if DEBUG
3922347Spst#include <syslog.h>
4022347Spst#include <errno.h>
4122347Spst#endif /* DEBUG */
4222347Spst#include "opie.h"
4322347Spst
4492906Smarkm#define IDLEN 4
4592906Smarkm
4622347Spstint opielogin FUNCTION((line, name, host), char *line AND char *name AND char *host)
4722347Spst{
4892906Smarkm  int rval = 0;
4992906Smarkm#if !DISABLE_UTMP
5022347Spst  struct utmp u;
5192906Smarkm  char id[IDLEN + 1] = "";
5222347Spst
5322347Spst  if (__opiegetutmpentry(line, &u)) {
5422347Spst#if DEBUG
5522347Spst    syslog(LOG_DEBUG, "opielogin: __opiegetutmpentry(line=%s, &u) failed", line);
5622347Spst#endif /* DEBUG */
5722347Spst    memset(&u, 0, sizeof(struct utmp));
5822347Spst    if (!strncmp(line, "/dev/", 5))
5922347Spst      strncpy(u.ut_line, line + 5, sizeof(u.ut_line));
6022347Spst    else
6122347Spst      strncpy(u.ut_line, line, sizeof(u.ut_line));
6222347Spst#if DEBUG
6322347Spst    syslog(LOG_DEBUG, "opielogin: continuing with ut_line=%s", u.ut_line);
6422347Spst#endif /* DEBUG */
6522347Spst  }
6622347Spst
6792906Smarkm#if DOUTMPX || HAVE_UT_ID
6892906Smarkm  strncpy(id, u.ut_id, sizeof(u.ut_id));
6992906Smarkm  id[sizeof(id)-1] = 0;
7092906Smarkm#endif /* DOUTMPX || HAVE_UT_ID */
7192906Smarkm
7222347Spst#if HAVE_UT_TYPE && defined(USER_PROCESS)
7322347Spst  u.ut_type = USER_PROCESS;
7422347Spst#endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */
7522347Spst#if HAVE_UT_PID
7622347Spst  u.ut_pid = getpid();
7722347Spst#endif /* HAVE_UT_PID */
7822347Spst
7922347Spst#if HAVE_UT_NAME
8022347Spst  strncpy(u.ut_name, name, sizeof(u.ut_name));
8122347Spst#else /* HAVE_UT_NAME */
8222347Spst#error No ut_name field in struct utmp? (Please send in a bug report)
8322347Spst#endif /* HAVE_UT_NAME */
8422347Spst
8522347Spst#if HAVE_UT_HOST
8622347Spst  strncpy(u.ut_host, host, sizeof(u.ut_host));
8722347Spst#endif /* HAVE_UT_HOST */
8892906Smarkm#if DOUTMPX && HAVE_UTX_SYSLEN
8992906Smarkm  u.ut_syslen = strlen(host) + 1;
9092906Smarkm#endif /* DOUTMPX && HAVE_UT_SYSLEN */
9122347Spst
9222347Spst#if DOUTMPX
9322347Spst#ifdef HAVE_ONE_ARG_GETTIMEOFDAY
9429964Sache  gettimeofday(&u.ut_tv);
9522347Spst#else /* HAVE_ONE_ARG_GETTIMEOFDAY */
9629964Sache  gettimeofday(&u.ut_tv, NULL);
9722347Spst#endif /* HAVE_ONE_ARG_GETTIMEOFDAY */
9822347Spst#else /* DOUTMPX */
9922347Spst  time(&u.ut_time);
10022347Spst#endif /* DOUTMPX */
10122347Spst
10222347Spst  pututline(&u);
10322347Spst  endutent();
10422347Spst
10522347Spst#if DEBUG
10622347Spst  syslog(LOG_DEBUG, "opielogin: utmp suceeded");
10722347Spst#endif /* DEBUG */
10829964Sache#endif /* !DISABLE_UTMP */
10922347Spst
11022347Spstdowtmp:
11192906Smarkm  opielogwtmp(line, name, host, id);
11229964Sache  opielogwtmp(NULL, NULL, NULL);
11322347Spst
11422347Spstdosetlogin:
11522347Spst#if HAVE_SETLOGIN
11622347Spst  setlogin(name);
11722347Spst#endif /* HAVE_SETLOGIN */
11822347Spst
11922347Spst#if DEBUG
12022347Spst  syslog(LOG_DEBUG, "opielogin: rval=%d", rval);
12122347Spst#endif /* DEBUG */
12222347Spst
12322347Spst  return rval;
12422347Spst}
125