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 25 #if DOUTMPX 26 #include <utmpx.h> 27 #define pututline(x) pututxline(x) 28 #define endutent endutxent 29 #define utmp utmpx 30 #else 31 #include <utmp.h> 32 #endif /* DOUTMPX */ 33 34 #if HAVE_STRING_H 35 #include <string.h> 36 #endif /* HAVE_STRING_H */ 37 #include <sys/stat.h> 38 #if DEBUG 39 #include <syslog.h> 40 #include <errno.h> 41 #endif /* DEBUG */ 42 #include "opie.h" 43 44 #define IDLEN 4 45 46 int opielogin FUNCTION((line, name, host), char *line AND char *name AND char *host) 47 { 48 int rval = 0; 49 #if !DISABLE_UTMP 50 struct utmp u; 51 char id[IDLEN + 1] = ""; 52 53 if (__opiegetutmpentry(line, &u)) { 54 #if DEBUG 55 syslog(LOG_DEBUG, "opielogin: __opiegetutmpentry(line=%s, &u) failed", line); 56 #endif /* DEBUG */ 57 memset(&u, 0, sizeof(struct utmp)); 58 if (!strncmp(line, "/dev/", 5)) 59 strncpy(u.ut_line, line + 5, sizeof(u.ut_line)); 60 else 61 strncpy(u.ut_line, line, sizeof(u.ut_line)); 62 #if DEBUG 63 syslog(LOG_DEBUG, "opielogin: continuing with ut_line=%s", u.ut_line); 64 #endif /* DEBUG */ 65 } 66 67 #if DOUTMPX || HAVE_UT_ID 68 strncpy(id, u.ut_id, sizeof(u.ut_id)); 69 id[sizeof(id)-1] = 0; 70 #endif /* DOUTMPX || HAVE_UT_ID */ 71 72 #if HAVE_UT_TYPE && defined(USER_PROCESS) 73 u.ut_type = USER_PROCESS; 74 #endif /* HAVE_UT_TYPE && defined(USER_PROCESS) */ 75 #if HAVE_UT_PID 76 u.ut_pid = getpid(); 77 #endif /* HAVE_UT_PID */ 78 79 #if HAVE_UT_NAME 80 strncpy(u.ut_name, name, sizeof(u.ut_name)); 81 #else /* HAVE_UT_NAME */ 82 #error No ut_name field in struct utmp? (Please send in a bug report) 83 #endif /* HAVE_UT_NAME */ 84 85 #if HAVE_UT_HOST 86 strncpy(u.ut_host, host, sizeof(u.ut_host)); 87 #endif /* HAVE_UT_HOST */ 88 #if DOUTMPX && HAVE_UTX_SYSLEN 89 u.ut_syslen = strlen(host) + 1; 90 #endif /* DOUTMPX && HAVE_UT_SYSLEN */ 91 92 #if DOUTMPX 93 #ifdef HAVE_ONE_ARG_GETTIMEOFDAY 94 gettimeofday(&u.ut_tv); 95 #else /* HAVE_ONE_ARG_GETTIMEOFDAY */ 96 gettimeofday(&u.ut_tv, NULL); 97 #endif /* HAVE_ONE_ARG_GETTIMEOFDAY */ 98 #else /* DOUTMPX */ 99 time(&u.ut_time); 100 #endif /* DOUTMPX */ 101 102 pututline(&u); 103 endutent(); 104 105 #if DEBUG 106 syslog(LOG_DEBUG, "opielogin: utmp suceeded"); 107 #endif /* DEBUG */ 108 #endif /* !DISABLE_UTMP */ 109 110 dowtmp: 111 opielogwtmp(line, name, host, id); 112 opielogwtmp(NULL, NULL, NULL); 113 114 dosetlogin: 115 #if HAVE_SETLOGIN 116 setlogin(name); 117 #endif /* HAVE_SETLOGIN */ 118 119 #if DEBUG 120 syslog(LOG_DEBUG, "opielogin: rval=%d", rval); 121 #endif /* DEBUG */ 122 123 return rval; 124 } 125