2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ** logintest.c: simple test driver for platform-independent login recording
32 ** and lastlog retrieval
37 #include <sys/types.h>
55 int compile_opts_only = 0;
59 #define DOTQUAD_MAXSIZE 17
60 void dump_dotquad(char *s, struct in_addr *sin4) {
63 addr = ntohl(sin4->s_addr);
64 snprintf(s, DOTQUAD_MAXSIZE, "%d.%d.%d.%d",
65 (addr >> 24)& 0xff, (addr >>16) & 0xff,
66 (addr >>8) & 0xff, addr & 0xff );
70 /* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
71 void dump_logininfo(struct logininfo *li, char *descname) {
72 char a4[DOTQUAD_MAXSIZE];
74 dump_dotquad(a4, &(li->hostaddr.sa_in4.sin_addr));
76 /* yes I know how nasty this is */
77 printf("struct logininfo %s = {\n\t"
78 "progname\t'%s'\n\ttype\t\t%d\n\t"
79 "pid\t\t%d\n\tuid\t\t%d\n\t"
80 "line\t\t'%s'\n\tusername\t'%s'\n\t"
81 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
82 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
83 "struct login_netinfo hostaddr {\n\t\t"
84 "struct sockaddr_in sa_in4 {\n"
85 "\t\t\tsin_port\t%d\n\t\t\t*sin_addr\t%d(%s)\n\t\t}\n"
86 "\t\t** !!! IP6 stuff not supported yet **\n"
89 descname, li->progname, li->type,
90 li->pid, li->uid, li->line,
91 li->username, li->hostname, li->exit,
92 li->termination, li->tv_sec, li->tv_usec,
93 ntohs(li->hostaddr.sa_in4.sin_port),
94 ntohl(li->hostaddr.sa_in4.sin_addr.s_addr), a4);
95 /* FIXME: (ATL) print sockaddr_in6 stuff */
100 struct logininfo *li1;
103 struct sockaddr_in sa_in4;
104 char cmdstring[256], stripline[8];
108 char s_t0[64],s_t1[64],s_t2[64]; /* ctime() strings */
111 printf("**\n** Testing the API...\n**\n");
113 pw = getpwuid(getuid());
114 strlcpy(username, pw->pw_name, sizeof(username));
116 /* gethostname(hostname, sizeof(hostname)); */
118 printf("login_alloc_entry test (no host info):\n");
119 /* !!! fake tty more effectively */
120 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
121 login_set_progname(li1, "testlogin");
124 dump_logininfo(li1, "li1");
126 printf("Setting IPv4 host info for 'localhost' (may call out):\n");
127 if (! (he = gethostbyname("localhost"))) {
128 printf("Couldn't set hostname(lookup failed)\n");
130 /* NOTE: this is messy, but typically a program wouldn't have to set
131 * any of this, a sockaddr_in* would be already prepared */
132 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
133 sizeof(struct in_addr));
134 login_set_ip4(li1, &sa_in4);
135 login_set_hostname(li1, "localhost");
138 dump_logininfo(li1, "li1");
140 if ((int)geteuid() != 0) {
141 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
142 return 1; /* this isn't necessarily an error */
148 line_stripname(stripline, li1->line, sizeof(stripline));
150 printf("Performing an invalid login attempt (no type field)\n--\n");
152 printf("--\n(Should have written an error to stderr)\n");
156 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
157 t1 = login_getlasttime_uid(getuid());
158 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
159 printf("Before logging in:\n\tcurrent time is %d - %s\t"
160 "lastlog time is %d - %s\n",
161 (int)t0, s_t0, (int)t1, s_t1);
164 printf("Performing a login on line %s...\n--\n", stripline);
167 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
171 printf("--\nWaiting for a few seconds...\n");
174 printf("Performing a logout (the root login "
175 "shown above should be gone)\n"
176 "If the root login hasn't gone, but another user on the same\n"
177 "pty has, this is OK - we're hacking it here, and there\n"
178 "shouldn't be two users on one pty in reality...\n"
179 "-- ('who' output follows)\n");
183 printf("-- ('who' output ends)\n");
186 t2 = login_getlasttime_uid(getuid());
187 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
188 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
190 printf("The lastlog times before and after logging in are the "
191 "same.\nThis indicates that lastlog is ** NOT WORKING "
194 printf("** The login time and the lastlog time differ.\n"
195 "** This indicates that lastlog is either recording the "
196 "wrong time,\n** or retrieving the wrong entry.\n");
198 printf("lastlog agrees with the login time. This is a good thing.\n");
202 printf("--\nThe output of 'last' shown next should have "
203 "an entry for root \n on %s for the time shown above:\n--\n",
205 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
209 printf("--\nEnd of login test.\n");
211 login_free_entry(li1);
217 void testLineName(char *line) {
218 /* have to null-terminate - these functions are designed for
219 * structures with fixed-length char arrays, and don't null-term.*/
220 char full[17], strip[9], abbrev[5];
222 memset(full, '\0', sizeof(full));
223 memset(strip, '\0', sizeof(strip));
224 memset(abbrev, '\0', sizeof(abbrev));
226 line_fullname(full, line, sizeof(full)-1);
227 line_stripname(strip, full, sizeof(strip)-1);
228 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
229 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
231 } /* testLineName() */
235 printf("**\n** Testing linename functions\n**\n");
236 testLineName("/dev/pts/1");
237 testLineName("pts/1");
238 testLineName("pts/999");
239 testLineName("/dev/ttyp00");
240 testLineName("ttyp00");
246 /* show which options got compiled in */
247 void showOptions(void) {
249 printf("**\n** Compile-time options\n**\n");
251 printf("login recording methods selected:\n");
253 printf("\tUSE_LOGIN\n");
256 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
259 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
262 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
265 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
268 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
272 printf("IP6 support: %s\n",
281 } /* showOptions() */
284 int main(int argc, char *argv[]) {
286 printf("Platform-independent login recording test driver");
289 if (strncmp(argv[1], "-i", 3) == 0)
290 compile_opts_only = 1;
291 else if (strncmp(argv[1], "-v", 3) == 0)
295 if (!compile_opts_only) {
296 if (be_verbose && !testOutput())