Deleted Added
full compact
logintest.c (124208) logintest.c (137015)
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/**
26 ** logintest.c: simple test driver for platform-independent login recording
27 ** and lastlog retrieval
28 **/
29
30#include "includes.h"
31
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <pwd.h>
39#include <netdb.h>
40#ifdef HAVE_TIME_H
41#include <time.h>
42#endif
43
44#include "loginrec.h"
45
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/**
26 ** logintest.c: simple test driver for platform-independent login recording
27 ** and lastlog retrieval
28 **/
29
30#include "includes.h"
31
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <pwd.h>
39#include <netdb.h>
40#ifdef HAVE_TIME_H
41#include <time.h>
42#endif
43
44#include "loginrec.h"
45
46RCSID("$Id: logintest.c,v 1.10 2003/08/21 23:34:41 djm Exp $");
46RCSID("$Id: logintest.c,v 1.11 2004/07/17 04:07:42 dtucker Exp $");
47
47
48#ifdef HAVE___PROGNAME
49extern char *__progname;
48extern char *__progname;
50#else
51char *__progname;
52#endif
53
54#define PAUSE_BEFORE_LOGOUT 3
55
56int nologtest = 0;
57int compile_opts_only = 0;
58int be_verbose = 0;
59
60
61/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
62void
63dump_logininfo(struct logininfo *li, char *descname)
64{
65 /* yes I know how nasty this is */
66 printf("struct logininfo %s = {\n\t"
67 "progname\t'%s'\n\ttype\t\t%d\n\t"
68 "pid\t\t%d\n\tuid\t\t%d\n\t"
69 "line\t\t'%s'\n\tusername\t'%s'\n\t"
70 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
71 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
72 "struct login_netinfo hostaddr {\n\t\t"
73 "struct sockaddr sa {\n"
74 "\t\t\tfamily\t%d\n\t\t}\n"
75 "\t}\n"
76 "}\n",
77 descname, li->progname, li->type,
78 li->pid, li->uid, li->line,
79 li->username, li->hostname, li->exit,
80 li->termination, li->tv_sec, li->tv_usec,
81 li->hostaddr.sa.sa_family);
82}
83
84
85int
86testAPI()
87{
88 struct logininfo *li1;
89 struct passwd *pw;
90 struct hostent *he;
91 struct sockaddr_in sa_in4;
92 char cmdstring[256], stripline[8];
93 char username[32];
94#ifdef HAVE_TIME_H
95 time_t t0, t1, t2, logintime, logouttime;
96 char s_t0[64],s_t1[64],s_t2[64];
97 char s_logintime[64], s_logouttime[64]; /* ctime() strings */
98#endif
99
100 printf("**\n** Testing the API...\n**\n");
101
102 pw = getpwuid(getuid());
103 strlcpy(username, pw->pw_name, sizeof(username));
104
105 /* gethostname(hostname, sizeof(hostname)); */
106
107 printf("login_alloc_entry test (no host info):\n");
108
109 /* FIXME fake tty more effectively - this could upset some platforms */
110 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
111 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
112
113 if (be_verbose)
114 dump_logininfo(li1, "li1");
115
116 printf("Setting host address info for 'localhost' (may call out):\n");
117 if (! (he = gethostbyname("localhost"))) {
118 printf("Couldn't set hostname(lookup failed)\n");
119 } else {
120 /* NOTE: this is messy, but typically a program wouldn't have to set
121 * any of this, a sockaddr_in* would be already prepared */
122 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
123 sizeof(struct in_addr));
124 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
125 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
126 }
127 if (be_verbose)
128 dump_logininfo(li1, "li1");
129
130 if ((int)geteuid() != 0) {
131 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
132 return 1;
133 }
134
135 if (nologtest)
136 return 1;
137
138 line_stripname(stripline, li1->line, sizeof(stripline));
139
140 printf("Performing an invalid login attempt (no type field)\n--\n");
141 login_write(li1);
142 printf("--\n(Should have written errors to stderr)\n");
143
144#ifdef HAVE_TIME_H
145 (void)time(&t0);
146 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
147 t1 = login_get_lastlog_time(getuid());
148 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
149 printf("Before logging in:\n\tcurrent time is %d - %s\t"
150 "lastlog time is %d - %s\n",
151 (int)t0, s_t0, (int)t1, s_t1);
152#endif
153
154 printf("Performing a login on line %s ", stripline);
155#ifdef HAVE_TIME_H
156 (void)time(&logintime);
157 strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
158 printf("at %d - %s", (int)logintime, s_logintime);
159#endif
160 printf("--\n");
161 login_login(li1);
162
163 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
164 stripline);
165 system(cmdstring);
166
167 printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
168 sleep(PAUSE_BEFORE_LOGOUT);
169
170 printf("Performing a logout ");
171#ifdef HAVE_TIME_H
172 (void)time(&logouttime);
173 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
174 printf("at %d - %s", (int)logouttime, s_logouttime);
175#endif
176 printf("\nThe root login shown above should be gone.\n"
177 "If the root login hasn't gone, but another user on the same\n"
178 "pty has, this is OK - we're hacking it here, and there\n"
179 "shouldn't be two users on one pty in reality...\n"
180 "-- ('who' output follows)\n");
181 login_logout(li1);
182
183 system(cmdstring);
184 printf("-- ('who' output ends)\n");
185
186#ifdef HAVE_TIME_H
187 t2 = login_get_lastlog_time(getuid());
188 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
189 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
190 if (t1 == t2)
191 printf("The lastlog times before and after logging in are the "
192 "same.\nThis indicates that lastlog is ** NOT WORKING "
193 "CORRECTLY **\n");
194 else if (t0 != t2)
195 /* We can be off by a second or so, even when recording works fine.
196 * I'm not 100% sure why, but it's true. */
197 printf("** The login time and the lastlog time differ.\n"
198 "** This indicates that lastlog is either recording the "
199 "wrong time,\n** or retrieving the wrong entry.\n"
200 "If it's off by less than %d second(s) "
201 "run the test again.\n", PAUSE_BEFORE_LOGOUT);
202 else
203 printf("lastlog agrees with the login time. This is a good thing.\n");
204
205#endif
206
207 printf("--\nThe output of 'last' shown next should have "
208 "an entry for root \n on %s for the time shown above:\n--\n",
209 stripline);
210 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
211 stripline);
212 system(cmdstring);
213
214 printf("--\nEnd of login test.\n");
215
216 login_free_entry(li1);
217
218 return 1;
219} /* testAPI() */
220
221
222void
223testLineName(char *line)
224{
225 /* have to null-terminate - these functions are designed for
226 * structures with fixed-length char arrays, and don't null-term.*/
227 char full[17], strip[9], abbrev[5];
228
229 memset(full, '\0', sizeof(full));
230 memset(strip, '\0', sizeof(strip));
231 memset(abbrev, '\0', sizeof(abbrev));
232
233 line_fullname(full, line, sizeof(full)-1);
234 line_stripname(strip, full, sizeof(strip)-1);
235 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
236 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
237
238} /* testLineName() */
239
240
241int
242testOutput()
243{
244 printf("**\n** Testing linename functions\n**\n");
245 testLineName("/dev/pts/1");
246 testLineName("pts/1");
247 testLineName("pts/999");
248 testLineName("/dev/ttyp00");
249 testLineName("ttyp00");
250
251 return 1;
252} /* testOutput() */
253
254
255/* show which options got compiled in */
256void
257showOptions(void)
258{
259 printf("**\n** Compile-time options\n**\n");
260
261 printf("login recording methods selected:\n");
262#ifdef USE_LOGIN
263 printf("\tUSE_LOGIN\n");
264#endif
265#ifdef USE_UTMP
266 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
267#endif
268#ifdef USE_UTMPX
269 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
270#endif
271#ifdef USE_WTMP
272 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
273#endif
274#ifdef USE_WTMPX
275 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
276#endif
277#ifdef USE_LASTLOG
278 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
279#endif
280 printf("\n");
281
282} /* showOptions() */
283
284
285int
286main(int argc, char *argv[])
287{
288 printf("Platform-independent login recording test driver\n");
289
290 __progname = ssh_get_progname(argv[0]);
291 if (argc == 2) {
292 if (strncmp(argv[1], "-i", 3) == 0)
293 compile_opts_only = 1;
294 else if (strncmp(argv[1], "-v", 3) == 0)
295 be_verbose=1;
296 }
297
298 if (!compile_opts_only) {
299 if (be_verbose && !testOutput())
300 return 1;
301
302 if (!testAPI())
303 return 1;
304 }
305
306 showOptions();
307
308 return 0;
309} /* main() */
310
49
50#define PAUSE_BEFORE_LOGOUT 3
51
52int nologtest = 0;
53int compile_opts_only = 0;
54int be_verbose = 0;
55
56
57/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
58void
59dump_logininfo(struct logininfo *li, char *descname)
60{
61 /* yes I know how nasty this is */
62 printf("struct logininfo %s = {\n\t"
63 "progname\t'%s'\n\ttype\t\t%d\n\t"
64 "pid\t\t%d\n\tuid\t\t%d\n\t"
65 "line\t\t'%s'\n\tusername\t'%s'\n\t"
66 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
67 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
68 "struct login_netinfo hostaddr {\n\t\t"
69 "struct sockaddr sa {\n"
70 "\t\t\tfamily\t%d\n\t\t}\n"
71 "\t}\n"
72 "}\n",
73 descname, li->progname, li->type,
74 li->pid, li->uid, li->line,
75 li->username, li->hostname, li->exit,
76 li->termination, li->tv_sec, li->tv_usec,
77 li->hostaddr.sa.sa_family);
78}
79
80
81int
82testAPI()
83{
84 struct logininfo *li1;
85 struct passwd *pw;
86 struct hostent *he;
87 struct sockaddr_in sa_in4;
88 char cmdstring[256], stripline[8];
89 char username[32];
90#ifdef HAVE_TIME_H
91 time_t t0, t1, t2, logintime, logouttime;
92 char s_t0[64],s_t1[64],s_t2[64];
93 char s_logintime[64], s_logouttime[64]; /* ctime() strings */
94#endif
95
96 printf("**\n** Testing the API...\n**\n");
97
98 pw = getpwuid(getuid());
99 strlcpy(username, pw->pw_name, sizeof(username));
100
101 /* gethostname(hostname, sizeof(hostname)); */
102
103 printf("login_alloc_entry test (no host info):\n");
104
105 /* FIXME fake tty more effectively - this could upset some platforms */
106 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
107 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
108
109 if (be_verbose)
110 dump_logininfo(li1, "li1");
111
112 printf("Setting host address info for 'localhost' (may call out):\n");
113 if (! (he = gethostbyname("localhost"))) {
114 printf("Couldn't set hostname(lookup failed)\n");
115 } else {
116 /* NOTE: this is messy, but typically a program wouldn't have to set
117 * any of this, a sockaddr_in* would be already prepared */
118 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
119 sizeof(struct in_addr));
120 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
121 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
122 }
123 if (be_verbose)
124 dump_logininfo(li1, "li1");
125
126 if ((int)geteuid() != 0) {
127 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
128 return 1;
129 }
130
131 if (nologtest)
132 return 1;
133
134 line_stripname(stripline, li1->line, sizeof(stripline));
135
136 printf("Performing an invalid login attempt (no type field)\n--\n");
137 login_write(li1);
138 printf("--\n(Should have written errors to stderr)\n");
139
140#ifdef HAVE_TIME_H
141 (void)time(&t0);
142 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
143 t1 = login_get_lastlog_time(getuid());
144 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
145 printf("Before logging in:\n\tcurrent time is %d - %s\t"
146 "lastlog time is %d - %s\n",
147 (int)t0, s_t0, (int)t1, s_t1);
148#endif
149
150 printf("Performing a login on line %s ", stripline);
151#ifdef HAVE_TIME_H
152 (void)time(&logintime);
153 strlcpy(s_logintime, ctime(&logintime), sizeof(s_logintime));
154 printf("at %d - %s", (int)logintime, s_logintime);
155#endif
156 printf("--\n");
157 login_login(li1);
158
159 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
160 stripline);
161 system(cmdstring);
162
163 printf("--\nPausing for %d second(s)...\n", PAUSE_BEFORE_LOGOUT);
164 sleep(PAUSE_BEFORE_LOGOUT);
165
166 printf("Performing a logout ");
167#ifdef HAVE_TIME_H
168 (void)time(&logouttime);
169 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
170 printf("at %d - %s", (int)logouttime, s_logouttime);
171#endif
172 printf("\nThe root login shown above should be gone.\n"
173 "If the root login hasn't gone, but another user on the same\n"
174 "pty has, this is OK - we're hacking it here, and there\n"
175 "shouldn't be two users on one pty in reality...\n"
176 "-- ('who' output follows)\n");
177 login_logout(li1);
178
179 system(cmdstring);
180 printf("-- ('who' output ends)\n");
181
182#ifdef HAVE_TIME_H
183 t2 = login_get_lastlog_time(getuid());
184 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
185 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
186 if (t1 == t2)
187 printf("The lastlog times before and after logging in are the "
188 "same.\nThis indicates that lastlog is ** NOT WORKING "
189 "CORRECTLY **\n");
190 else if (t0 != t2)
191 /* We can be off by a second or so, even when recording works fine.
192 * I'm not 100% sure why, but it's true. */
193 printf("** The login time and the lastlog time differ.\n"
194 "** This indicates that lastlog is either recording the "
195 "wrong time,\n** or retrieving the wrong entry.\n"
196 "If it's off by less than %d second(s) "
197 "run the test again.\n", PAUSE_BEFORE_LOGOUT);
198 else
199 printf("lastlog agrees with the login time. This is a good thing.\n");
200
201#endif
202
203 printf("--\nThe output of 'last' shown next should have "
204 "an entry for root \n on %s for the time shown above:\n--\n",
205 stripline);
206 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
207 stripline);
208 system(cmdstring);
209
210 printf("--\nEnd of login test.\n");
211
212 login_free_entry(li1);
213
214 return 1;
215} /* testAPI() */
216
217
218void
219testLineName(char *line)
220{
221 /* have to null-terminate - these functions are designed for
222 * structures with fixed-length char arrays, and don't null-term.*/
223 char full[17], strip[9], abbrev[5];
224
225 memset(full, '\0', sizeof(full));
226 memset(strip, '\0', sizeof(strip));
227 memset(abbrev, '\0', sizeof(abbrev));
228
229 line_fullname(full, line, sizeof(full)-1);
230 line_stripname(strip, full, sizeof(strip)-1);
231 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
232 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
233
234} /* testLineName() */
235
236
237int
238testOutput()
239{
240 printf("**\n** Testing linename functions\n**\n");
241 testLineName("/dev/pts/1");
242 testLineName("pts/1");
243 testLineName("pts/999");
244 testLineName("/dev/ttyp00");
245 testLineName("ttyp00");
246
247 return 1;
248} /* testOutput() */
249
250
251/* show which options got compiled in */
252void
253showOptions(void)
254{
255 printf("**\n** Compile-time options\n**\n");
256
257 printf("login recording methods selected:\n");
258#ifdef USE_LOGIN
259 printf("\tUSE_LOGIN\n");
260#endif
261#ifdef USE_UTMP
262 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
263#endif
264#ifdef USE_UTMPX
265 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
266#endif
267#ifdef USE_WTMP
268 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
269#endif
270#ifdef USE_WTMPX
271 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
272#endif
273#ifdef USE_LASTLOG
274 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
275#endif
276 printf("\n");
277
278} /* showOptions() */
279
280
281int
282main(int argc, char *argv[])
283{
284 printf("Platform-independent login recording test driver\n");
285
286 __progname = ssh_get_progname(argv[0]);
287 if (argc == 2) {
288 if (strncmp(argv[1], "-i", 3) == 0)
289 compile_opts_only = 1;
290 else if (strncmp(argv[1], "-v", 3) == 0)
291 be_verbose=1;
292 }
293
294 if (!compile_opts_only) {
295 if (be_verbose && !testOutput())
296 return 1;
297
298 if (!testAPI())
299 return 1;
300 }
301
302 showOptions();
303
304 return 0;
305} /* main() */
306