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