155682Smarkm/*
2233294Sstas * Copyright (c) 1995 - 2001 Kungliga Tekniska H��gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
5233294Sstas *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
9233294Sstas *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
12233294Sstas *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
16233294Sstas *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
20233294Sstas *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "login_locl.h"
3555682Smarkm
36233294SstasRCSID("$Id$");
3755682Smarkm
3878527Sassar/* try to put something useful from hostname into dst, dst_sz:
3978527Sassar * full name, first component or address */
4078527Sassar
4155682Smarkmvoid
4278527Sassarshrink_hostname (const char *hostname,
4378527Sassar		 char *dst, size_t dst_sz)
4478527Sassar{
4578527Sassar    char local_hostname[MaxHostNameLen];
4678527Sassar    char *ld, *hd;
4778527Sassar    int ret;
4878527Sassar    struct addrinfo *ai;
4978527Sassar
5078527Sassar    if (strlen(hostname) < dst_sz) {
5178527Sassar	strlcpy (dst, hostname, dst_sz);
5278527Sassar	return;
5378527Sassar    }
5478527Sassar    gethostname (local_hostname, sizeof(local_hostname));
5578527Sassar    hd = strchr (hostname, '.');
5678527Sassar    ld = strchr (local_hostname, '.');
5778527Sassar    if (hd != NULL && ld != NULL && strcmp(hd, ld) == 0
5878527Sassar	&& hd - hostname < dst_sz) {
5978527Sassar	strlcpy (dst, hostname, dst_sz);
6078527Sassar	dst[hd - hostname] = '\0';
6178527Sassar	return;
6278527Sassar    }
6378527Sassar
6478527Sassar    ret = getaddrinfo (hostname, NULL, NULL, &ai);
6578527Sassar    if (ret) {
6678527Sassar	strncpy (dst, hostname, dst_sz);
6778527Sassar	return;
6878527Sassar    }
6978527Sassar    ret = getnameinfo (ai->ai_addr, ai->ai_addrlen,
7078527Sassar		       dst, dst_sz,
7178527Sassar		       NULL, 0,
7278527Sassar		       NI_NUMERICHOST);
7378527Sassar    freeaddrinfo (ai);
7478527Sassar    if (ret) {
7578527Sassar	strncpy (dst, hostname, dst_sz);
7678527Sassar	return;
7778527Sassar    }
7878527Sassar}
7978527Sassar
80233294Sstas/* update utmp and wtmp - the BSD way */
81233294Sstas
82233294Sstas#if !defined(HAVE_UTMPX_H) || (defined(WTMP_FILE) && !defined(WTMPX_FILE))
83233294Sstas
8478527Sassarvoid
85233294Sstasprepare_utmp (struct utmp *utmp, char *tty,
8655682Smarkm	      const char *username, const char *hostname)
8755682Smarkm{
8855682Smarkm    char *ttyx = clean_ttyname (tty);
8955682Smarkm
9055682Smarkm    memset(utmp, 0, sizeof(*utmp));
9155682Smarkm    utmp->ut_time = time(NULL);
9255682Smarkm    strncpy(utmp->ut_line, ttyx, sizeof(utmp->ut_line));
9355682Smarkm    strncpy(utmp->ut_name, username, sizeof(utmp->ut_name));
9455682Smarkm
9555682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_USER
9655682Smarkm    strncpy(utmp->ut_user, username, sizeof(utmp->ut_user));
9755682Smarkm# endif
9855682Smarkm
9955682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_ADDR
10055682Smarkm    if (hostname[0]) {
10155682Smarkm        struct hostent *he;
10255682Smarkm	if ((he = gethostbyname(hostname)))
10355682Smarkm	    memcpy(&utmp->ut_addr, he->h_addr_list[0],
10455682Smarkm		   sizeof(utmp->ut_addr));
10555682Smarkm    }
10655682Smarkm# endif
10755682Smarkm
10855682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_HOST
10978527Sassar    shrink_hostname (hostname, utmp->ut_host, sizeof(utmp->ut_host));
11055682Smarkm# endif
11155682Smarkm
11255682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_TYPE
11355682Smarkm    utmp->ut_type = USER_PROCESS;
11455682Smarkm# endif
11555682Smarkm
11655682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_PID
11755682Smarkm    utmp->ut_pid = getpid();
11855682Smarkm# endif
11955682Smarkm
12055682Smarkm# ifdef HAVE_STRUCT_UTMP_UT_ID
12155682Smarkm    strncpy(utmp->ut_id, make_id(ttyx), sizeof(utmp->ut_id));
12255682Smarkm# endif
12355682Smarkm}
124233294Sstas#endif
12555682Smarkm
12655682Smarkm#ifdef HAVE_UTMPX_H
12755682Smarkmvoid utmp_login(char *tty, const char *username, const char *hostname)
128233294Sstas{
12955682Smarkm    return;
13055682Smarkm}
13155682Smarkm#else
13255682Smarkm
13355682Smarkmvoid utmp_login(char *tty, const char *username, const char *hostname)
13455682Smarkm{
13555682Smarkm    struct utmp utmp;
13655682Smarkm    int fd;
13755682Smarkm
13855682Smarkm    prepare_utmp (&utmp, tty, username, hostname);
13955682Smarkm
14055682Smarkm#ifdef HAVE_SETUTENT
14155682Smarkm    utmpname(_PATH_UTMP);
14255682Smarkm    setutent();
14355682Smarkm    pututline(&utmp);
14455682Smarkm    endutent();
14555682Smarkm#else
14655682Smarkm
14755682Smarkm#ifdef HAVE_TTYSLOT
14855682Smarkm    {
14955682Smarkm      int ttyno;
15055682Smarkm      ttyno = ttyslot();
15155682Smarkm      if (ttyno > 0 && (fd = open(_PATH_UTMP, O_WRONLY, 0)) >= 0) {
15255682Smarkm	lseek(fd, (long)(ttyno * sizeof(struct utmp)), SEEK_SET);
15355682Smarkm	write(fd, &utmp, sizeof(struct utmp));
15455682Smarkm	close(fd);
15555682Smarkm      }
15655682Smarkm    }
15755682Smarkm#endif /* HAVE_TTYSLOT */
15855682Smarkm#endif /* HAVE_SETUTENT */
15955682Smarkm
16055682Smarkm    if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
16155682Smarkm	write(fd, &utmp, sizeof(struct utmp));
16255682Smarkm	close(fd);
16355682Smarkm    }
16455682Smarkm}
165233294Sstas
16655682Smarkm#endif /* !HAVE_UTMPX_H */
167