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 /* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
60 void dump_logininfo(struct logininfo *li, char *descname)
62 /* yes I know how nasty this is */
63 printf("struct logininfo %s = {\n\t"
64 "progname\t'%s'\n\ttype\t\t%d\n\t"
65 "pid\t\t%d\n\tuid\t\t%d\n\t"
66 "line\t\t'%s'\n\tusername\t'%s'\n\t"
67 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
68 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
69 "struct login_netinfo hostaddr {\n\t\t"
70 "struct sockaddr sa {\n"
71 "\t\t\tfamily\t%d\n\t\t}\n"
72 "\t\t** !!! IP6 stuff not supported yet **\n"
75 descname, li->progname, li->type,
76 li->pid, li->uid, li->line,
77 li->username, li->hostname, li->exit,
78 li->termination, li->tv_sec, li->tv_usec,
79 li->hostaddr.sa.sa_family);
85 struct logininfo *li1;
88 struct sockaddr_in sa_in4;
89 char cmdstring[256], stripline[8];
93 char s_t0[64],s_t1[64],s_t2[64]; /* ctime() strings */
96 printf("**\n** Testing the API...\n**\n");
98 pw = getpwuid(getuid());
99 strlcpy(username, pw->pw_name, sizeof(username));
101 /* gethostname(hostname, sizeof(hostname)); */
103 printf("login_alloc_entry test (no host info):\n");
104 /* !!! fake tty more effectively */
105 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
106 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
109 dump_logininfo(li1, "li1");
111 printf("Setting host address info for 'localhost' (may call out):\n");
112 if (! (he = gethostbyname("localhost"))) {
113 printf("Couldn't set hostname(lookup failed)\n");
115 /* NOTE: this is messy, but typically a program wouldn't have to set
116 * any of this, a sockaddr_in* would be already prepared */
117 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
118 sizeof(struct in_addr));
119 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
120 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
123 dump_logininfo(li1, "li1");
125 if ((int)geteuid() != 0) {
126 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
127 return 1; /* this isn't necessarily an error */
133 line_stripname(stripline, li1->line, sizeof(stripline));
135 printf("Performing an invalid login attempt (no type field)\n--\n");
137 printf("--\n(Should have written an error to stderr)\n");
141 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
142 t1 = login_get_lastlog_time(getuid());
143 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
144 printf("Before logging in:\n\tcurrent time is %d - %s\t"
145 "lastlog time is %d - %s\n",
146 (int)t0, s_t0, (int)t1, s_t1);
149 printf("Performing a login on line %s...\n--\n", stripline);
152 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
156 printf("--\nWaiting for a few seconds...\n");
159 printf("Performing a logout (the root login "
160 "shown above should be gone)\n"
161 "If the root login hasn't gone, but another user on the same\n"
162 "pty has, this is OK - we're hacking it here, and there\n"
163 "shouldn't be two users on one pty in reality...\n"
164 "-- ('who' output follows)\n");
168 printf("-- ('who' output ends)\n");
171 t2 = login_get_lastlog_time(getuid());
172 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
173 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
175 printf("The lastlog times before and after logging in are the "
176 "same.\nThis indicates that lastlog is ** NOT WORKING "
179 printf("** The login time and the lastlog time differ.\n"
180 "** This indicates that lastlog is either recording the "
181 "wrong time,\n** or retrieving the wrong entry.\n");
183 printf("lastlog agrees with the login time. This is a good thing.\n");
187 printf("--\nThe output of 'last' shown next should have "
188 "an entry for root \n on %s for the time shown above:\n--\n",
190 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
194 printf("--\nEnd of login test.\n");
196 login_free_entry(li1);
202 void testLineName(char *line)
204 /* have to null-terminate - these functions are designed for
205 * structures with fixed-length char arrays, and don't null-term.*/
206 char full[17], strip[9], abbrev[5];
208 memset(full, '\0', sizeof(full));
209 memset(strip, '\0', sizeof(strip));
210 memset(abbrev, '\0', sizeof(abbrev));
212 line_fullname(full, line, sizeof(full)-1);
213 line_stripname(strip, full, sizeof(strip)-1);
214 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
215 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
217 } /* testLineName() */
221 printf("**\n** Testing linename functions\n**\n");
222 testLineName("/dev/pts/1");
223 testLineName("pts/1");
224 testLineName("pts/999");
225 testLineName("/dev/ttyp00");
226 testLineName("ttyp00");
232 /* show which options got compiled in */
233 void showOptions(void)
235 printf("**\n** Compile-time options\n**\n");
237 printf("login recording methods selected:\n");
239 printf("\tUSE_LOGIN\n");
242 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
245 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
248 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
251 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
254 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
258 } /* showOptions() */
261 int main(int argc, char *argv[])
263 printf("Platform-independent login recording test driver\n");
266 if (strncmp(argv[1], "-i", 3) == 0)
267 compile_opts_only = 1;
268 else if (strncmp(argv[1], "-v", 3) == 0)
272 if (!compile_opts_only) {
273 if (be_verbose && !testOutput())