| 1 |
/* readutmp - Basic test of existing utmp/utmpx access. |
| 2 |
Copyright (C) 2010-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Mats Erik Andersson. */ |
| 20 |
|
| 21 |
/* Readutmp reads the system's standard UTMP or UTMPX file |
| 22 |
* and verifies that a specific user is logged in. |
| 23 |
* |
| 24 |
* Invocation: |
| 25 |
* |
| 26 |
* readutmp |
| 27 |
* readutmp name |
| 28 |
* |
| 29 |
* The first mode investigates the present user. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <sys/types.h> |
| 39 |
#include <pwd.h> |
| 40 |
|
| 41 |
#include <progname.h> |
| 42 |
#include <readutmp.h> |
| 43 |
#include <xalloc.h> |
| 44 |
|
| 45 |
int |
| 46 |
main (int argc, char *argv[]) |
| 47 |
{ |
| 48 |
#ifndef HAVE_GETUTXUSER |
| 49 |
STRUCT_UTMP *utmpp, *uptr; |
| 50 |
size_t count; |
| 51 |
#endif |
| 52 |
struct passwd *pw; |
| 53 |
char *name; |
| 54 |
int found = 0; |
| 55 |
|
| 56 |
set_program_name (argv[0]); |
| 57 |
|
| 58 |
if (argc > 1) |
| 59 |
pw = getpwnam (argv[1]); |
| 60 |
else |
| 61 |
pw = getpwuid (getuid ()); |
| 62 |
|
| 63 |
if (pw) |
| 64 |
name = xstrdup (pw->pw_name); |
| 65 |
else |
| 66 |
{ |
| 67 |
fprintf (stderr, "Unknown user '%s'.\n", |
| 68 |
(argc > 1) ? argv[1] : "my own UID"); |
| 69 |
return EXIT_FAILURE; |
| 70 |
} |
| 71 |
|
| 72 |
#ifdef HAVE_GETUTXUSER |
| 73 |
setutxent (); |
| 74 |
found = (getutxuser (name) != 0); |
| 75 |
endutxent (); |
| 76 |
#else /* !HAVE_GETUTXUSER */ |
| 77 |
if (read_utmp (UTMP_FILE, &count, &utmpp, |
| 78 |
READ_UTMP_USER_PROCESS | READ_UTMP_CHECK_PIDS)) |
| 79 |
{ |
| 80 |
perror ("read_utmp"); |
| 81 |
return EXIT_FAILURE; |
| 82 |
} |
| 83 |
|
| 84 |
for (uptr = utmpp; uptr < utmpp + count; uptr++) |
| 85 |
if (!strncmp (name, UT_USER (uptr), sizeof (UT_USER (uptr)))) |
| 86 |
{ |
| 87 |
found = 1; |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
free (utmpp); |
| 92 |
#endif /* HAVE_GETUTXUSER */ |
| 93 |
|
| 94 |
if (found) |
| 95 |
return EXIT_SUCCESS; |
| 96 |
|
| 97 |
fprintf (stderr, "User '%s' is not logged in.\n", name); |
| 98 |
return EXIT_FAILURE; |
| 99 |
} |