summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandre <andre>2000-06-13 00:43:47 +0000
committerandre <andre>2000-06-13 00:43:47 +0000
commit6e5d3475a54b33f73d057a83a0f9e4e6d01a5336 (patch)
tree754b49788ce75bc38ecbc22d867e922b8c90bff3
parent55661d99b83cf8480993cd39e94d58d473c37c45 (diff)
Changed to OpenBSD indent and function declaration style
Added display of time when logging out, to try to catch lastlog bugs
-rw-r--r--logintest.c334
1 files changed, 173 insertions, 161 deletions
diff --git a/logintest.c b/logintest.c
index d7c62ab87..d974f6e49 100644
--- a/logintest.c
+++ b/logintest.c
@@ -48,7 +48,7 @@
48 48
49#include "loginrec.h" 49#include "loginrec.h"
50 50
51RCSID("$Id: logintest.c,v 1.4 2000/06/13 00:29:34 andre Exp $"); 51RCSID("$Id: logintest.c,v 1.5 2000/06/13 00:43:47 andre Exp $");
52 52
53 53
54int nologtest = 0; 54int nologtest = 0;
@@ -57,228 +57,240 @@ int be_verbose = 0;
57 57
58 58
59/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */ 59/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
60void dump_logininfo(struct logininfo *li, char *descname) 60void
61dump_logininfo(struct logininfo *li, char *descname)
61{ 62{
62 /* yes I know how nasty this is */ 63 /* yes I know how nasty this is */
63 printf("struct logininfo %s = {\n\t" 64 printf("struct logininfo %s = {\n\t"
64 "progname\t'%s'\n\ttype\t\t%d\n\t" 65 "progname\t'%s'\n\ttype\t\t%d\n\t"
65 "pid\t\t%d\n\tuid\t\t%d\n\t" 66 "pid\t\t%d\n\tuid\t\t%d\n\t"
66 "line\t\t'%s'\n\tusername\t'%s'\n\t" 67 "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 "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 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
69 "struct login_netinfo hostaddr {\n\t\t" 70 "struct login_netinfo hostaddr {\n\t\t"
70 "struct sockaddr sa {\n" 71 "struct sockaddr sa {\n"
71 "\t\t\tfamily\t%d\n\t\t}\n" 72 "\t\t\tfamily\t%d\n\t\t}\n"
72 "\t\t** !!! IP6 stuff not supported yet **\n" 73 "\t\t** !!! IP6 stuff not supported yet **\n"
73 "\t}\n" 74 "\t}\n"
74 "}\n", 75 "}\n",
75 descname, li->progname, li->type, 76 descname, li->progname, li->type,
76 li->pid, li->uid, li->line, 77 li->pid, li->uid, li->line,
77 li->username, li->hostname, li->exit, 78 li->username, li->hostname, li->exit,
78 li->termination, li->tv_sec, li->tv_usec, 79 li->termination, li->tv_sec, li->tv_usec,
79 li->hostaddr.sa.sa_family); 80 li->hostaddr.sa.sa_family);
80} 81}
81 82
82 83
83int testAPI() 84int
85testAPI()
84{ 86{
85 struct logininfo *li1; 87 struct logininfo *li1;
86 struct passwd *pw; 88 struct passwd *pw;
87 struct hostent *he; 89 struct hostent *he;
88 struct sockaddr_in sa_in4; 90 struct sockaddr_in sa_in4;
89 char cmdstring[256], stripline[8]; 91 char cmdstring[256], stripline[8];
90 char username[32]; 92 char username[32];
91#ifdef HAVE_TIME_H 93#ifdef HAVE_TIME_H
92 time_t t0, t1, t2; 94 time_t t0, t1, t2, logouttime;
93 char s_t0[64],s_t1[64],s_t2[64]; /* ctime() strings */ 95 char s_t0[64],s_t1[64],s_t2[64], s_logouttime[64]; /* ctime() strings */
94#endif 96#endif
95 97
96 printf("**\n** Testing the API...\n**\n"); 98 printf("**\n** Testing the API...\n**\n");
97 99
98 pw = getpwuid(getuid()); 100 pw = getpwuid(getuid());
99 strlcpy(username, pw->pw_name, sizeof(username)); 101 strlcpy(username, pw->pw_name, sizeof(username));
100 102
101 /* gethostname(hostname, sizeof(hostname)); */ 103 /* gethostname(hostname, sizeof(hostname)); */
102 104
103 printf("login_alloc_entry test (no host info):\n"); 105 printf("login_alloc_entry test (no host info):\n");
104 /* !!! fake tty more effectively */ 106 /* !!! fake tty more effectively */
105 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0)); 107 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
106 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname)); 108 strlcpy(li1->progname, "OpenSSH-logintest", sizeof(li1->progname));
107 109
108 if (be_verbose) 110 if (be_verbose)
109 dump_logininfo(li1, "li1"); 111 dump_logininfo(li1, "li1");
110 112
111 printf("Setting host address info for 'localhost' (may call out):\n"); 113 printf("Setting host address info for 'localhost' (may call out):\n");
112 if (! (he = gethostbyname("localhost"))) { 114 if (! (he = gethostbyname("localhost"))) {
113 printf("Couldn't set hostname(lookup failed)\n"); 115 printf("Couldn't set hostname(lookup failed)\n");
114 } else { 116 } else {
115 /* NOTE: this is messy, but typically a program wouldn't have to set 117 /* NOTE: this is messy, but typically a program wouldn't have to set
116 * any of this, a sockaddr_in* would be already prepared */ 118 * any of this, a sockaddr_in* would be already prepared */
117 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]), 119 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
118 sizeof(struct in_addr)); 120 sizeof(struct in_addr));
119 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4)); 121 login_set_addr(li1, (struct sockaddr *) &sa_in4, sizeof(sa_in4));
120 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname)); 122 strlcpy(li1->hostname, "localhost", sizeof(li1->hostname));
121 } 123 }
122 if (be_verbose) 124 if (be_verbose)
123 dump_logininfo(li1, "li1"); 125 dump_logininfo(li1, "li1");
124 126
125 if ((int)geteuid() != 0) { 127 if ((int)geteuid() != 0) {
126 printf("NOT RUNNING LOGIN TESTS - you are not root!\n"); 128 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
127 return 1; /* this isn't necessarily an error */ 129 return 1; /* this isn't necessarily an error */
128 } 130 }
129 131
130 if (nologtest) 132 if (nologtest)
131 return 1; 133 return 1;
132 134
133 line_stripname(stripline, li1->line, sizeof(stripline)); 135 line_stripname(stripline, li1->line, sizeof(stripline));
134 136
135 printf("Performing an invalid login attempt (no type field)\n--\n"); 137 printf("Performing an invalid login attempt (no type field)\n--\n");
136 login_write(li1); 138 login_write(li1);
137 printf("--\n(Should have written an error to stderr)\n"); 139 printf("--\n(Should have written an error to stderr)\n");
138 140
139#ifdef HAVE_TIME_H 141#ifdef HAVE_TIME_H
140 (void)time(&t0); 142 (void)time(&t0);
141 strlcpy(s_t0, ctime(&t0), sizeof(s_t0)); 143 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
142 t1 = login_get_lastlog_time(getuid()); 144 t1 = login_get_lastlog_time(getuid());
143 strlcpy(s_t1, ctime(&t1), sizeof(s_t1)); 145 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
144 printf("Before logging in:\n\tcurrent time is %d - %s\t" 146 printf("Before logging in:\n\tcurrent time is %d - %s\t"
145 "lastlog time is %d - %s\n", 147 "lastlog time is %d - %s\n",
146 (int)t0, s_t0, (int)t1, s_t1); 148 (int)t0, s_t0, (int)t1, s_t1);
147#endif 149#endif
148 150
149 printf("Performing a login on line %s...\n--\n", stripline); 151 printf("Performing a login on line %s...\n--\n", stripline);
150 login_login(li1); 152 login_login(li1);
151 153
152 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '", 154 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
153 stripline); 155 stripline);
154 system(cmdstring); 156 system(cmdstring);
155 157
156 printf("--\nWaiting for a few seconds...\n"); 158 printf("--\nWaiting for a few seconds...\n");
157 sleep(2); 159 sleep(2);
158 160
159 printf("Performing a logout (the root login " 161 printf("Performing a logout ");
160 "shown above should be gone)\n" 162#ifdef HAVE_TIME_H
161 "If the root login hasn't gone, but another user on the same\n" 163 (void)time(&logouttime);
162 "pty has, this is OK - we're hacking it here, and there\n" 164 strlcpy(s_logouttime, ctime(&logouttime), sizeof(s_logouttime));
163 "shouldn't be two users on one pty in reality...\n" 165 printf("at %d - %s", (int)logouttime, s_logouttime);
164 "-- ('who' output follows)\n"); 166#endif
165 login_logout(li1); 167 printf("(the root login shown above should be gone)\n"
168 "If the root login hasn't gone, but another user on the same\n"
169 "pty has, this is OK - we're hacking it here, and there\n"
170 "shouldn't be two users on one pty in reality...\n"
171 "-- ('who' output follows)\n");
172 login_logout(li1);
166 173
167 system(cmdstring); 174 system(cmdstring);
168 printf("-- ('who' output ends)\n"); 175 printf("-- ('who' output ends)\n");
169 176
170#ifdef HAVE_TIME_H 177#ifdef HAVE_TIME_H
171 t2 = login_get_lastlog_time(getuid()); 178 t2 = login_get_lastlog_time(getuid());
172 strlcpy(s_t2, ctime(&t2), sizeof(s_t2)); 179 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
173 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2); 180 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
174 if (t1 == t2) 181 if (t1 == t2)
175 printf("The lastlog times before and after logging in are the " 182 printf("The lastlog times before and after logging in are the "
176 "same.\nThis indicates that lastlog is ** NOT WORKING " 183 "same.\nThis indicates that lastlog is ** NOT WORKING "
177 "CORRECTLY **\n"); 184 "CORRECTLY **\n");
178 else if (t0 != t2) 185 else if (t0 != t2)
179 printf("** The login time and the lastlog time differ.\n" 186 printf("** The login time and the lastlog time differ.\n"
180 "** This indicates that lastlog is either recording the " 187 "** This indicates that lastlog is either recording the "
181 "wrong time,\n** or retrieving the wrong entry.\n"); 188 "wrong time,\n** or retrieving the wrong entry.\n");
182 else 189 else
183 printf("lastlog agrees with the login time. This is a good thing.\n"); 190 printf("lastlog agrees with the login time. This is a good thing.\n");
184 191
185#endif 192#endif
186 193
187 printf("--\nThe output of 'last' shown next should have " 194 printf("--\nThe output of 'last' shown next should have "
188 "an entry for root \n on %s for the time shown above:\n--\n", 195 "an entry for root \n on %s for the time shown above:\n--\n",
189 stripline); 196 stripline);
190 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3", 197 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
191 stripline); 198 stripline);
192 system(cmdstring); 199 system(cmdstring);
193 200
194 printf("--\nEnd of login test.\n"); 201 printf("--\nEnd of login test.\n");
195 202
196 login_free_entry(li1); 203 login_free_entry(li1);
197 204
198 return 1; 205 return 1;
199} /* testAPI() */ 206} /* testAPI() */
200 207
201 208
202void testLineName(char *line) 209void
210testLineName(char *line)
203{ 211{
204 /* have to null-terminate - these functions are designed for 212 /* have to null-terminate - these functions are designed for
205 * structures with fixed-length char arrays, and don't null-term.*/ 213 * structures with fixed-length char arrays, and don't null-term.*/
206 char full[17], strip[9], abbrev[5]; 214 char full[17], strip[9], abbrev[5];
207 215
208 memset(full, '\0', sizeof(full)); 216 memset(full, '\0', sizeof(full));
209 memset(strip, '\0', sizeof(strip)); 217 memset(strip, '\0', sizeof(strip));
210 memset(abbrev, '\0', sizeof(abbrev)); 218 memset(abbrev, '\0', sizeof(abbrev));
211 219
212 line_fullname(full, line, sizeof(full)-1); 220 line_fullname(full, line, sizeof(full)-1);
213 line_stripname(strip, full, sizeof(strip)-1); 221 line_stripname(strip, full, sizeof(strip)-1);
214 line_abbrevname(abbrev, full, sizeof(abbrev)-1); 222 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
215 printf("%s: %s, %s, %s\n", line, full, strip, abbrev); 223 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
216 224
217} /* testLineName() */ 225} /* testLineName() */
218 226
219 227
220int testOutput() { 228int
221 printf("**\n** Testing linename functions\n**\n"); 229testOutput()
222 testLineName("/dev/pts/1"); 230{
223 testLineName("pts/1"); 231 printf("**\n** Testing linename functions\n**\n");
224 testLineName("pts/999"); 232 testLineName("/dev/pts/1");
225 testLineName("/dev/ttyp00"); 233 testLineName("pts/1");
226 testLineName("ttyp00"); 234 testLineName("pts/999");
227 235 testLineName("/dev/ttyp00");
228 return 1; 236 testLineName("ttyp00");
237
238 return 1;
229} /* testOutput() */ 239} /* testOutput() */
230 240
231 241
232/* show which options got compiled in */ 242/* show which options got compiled in */
233void showOptions(void) 243void
244showOptions(void)
234{ 245{
235 printf("**\n** Compile-time options\n**\n"); 246 printf("**\n** Compile-time options\n**\n");
236 247
237 printf("login recording methods selected:\n"); 248 printf("login recording methods selected:\n");
238#ifdef USE_LOGIN 249#ifdef USE_LOGIN
239 printf("\tUSE_LOGIN\n"); 250 printf("\tUSE_LOGIN\n");
240#endif 251#endif
241#ifdef USE_UTMP 252#ifdef USE_UTMP
242 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE); 253 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
243#endif 254#endif
244#ifdef USE_UTMPX 255#ifdef USE_UTMPX
245 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE); 256 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
246#endif 257#endif
247#ifdef USE_WTMP 258#ifdef USE_WTMP
248 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE); 259 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
249#endif 260#endif
250#ifdef USE_WTMPX 261#ifdef USE_WTMPX
251 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE); 262 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
252#endif 263#endif
253#ifdef USE_LASTLOG 264#ifdef USE_LASTLOG
254 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE); 265 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
255#endif 266#endif
256 printf("\n"); 267 printf("\n");
257 268
258} /* showOptions() */ 269} /* showOptions() */
259 270
260 271
261int main(int argc, char *argv[]) 272int
273main(int argc, char *argv[])
262{ 274{
263 printf("Platform-independent login recording test driver\n"); 275 printf("Platform-independent login recording test driver\n");
264 276
265 if (argc == 2) { 277 if (argc == 2) {
266 if (strncmp(argv[1], "-i", 3) == 0) 278 if (strncmp(argv[1], "-i", 3) == 0)
267 compile_opts_only = 1; 279 compile_opts_only = 1;
268 else if (strncmp(argv[1], "-v", 3) == 0) 280 else if (strncmp(argv[1], "-v", 3) == 0)
269 be_verbose=1; 281 be_verbose=1;
270 } 282 }
271 283
272 if (!compile_opts_only) { 284 if (!compile_opts_only) {
273 if (be_verbose && !testOutput()) 285 if (be_verbose && !testOutput())
274 return 1; 286 return 1;
275 287
276 if (!testAPI()) 288 if (!testAPI())
277 return 1; 289 return 1;
278 } 290 }
279 291
280 showOptions(); 292 showOptions();
281 293
282 return 0; 294 return 0;
283} /* main() */ 295} /* main() */
284 296