File: get_user.c

package info (click to toggle)
lbcd 3.5.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: ansic: 11,073; sh: 1,816; perl: 503; makefile: 165
file content (265 lines) | stat: -rw-r--r-- 6,036 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Get statistics about logged-in users.
 *
 * Written by Larry Schwimmer
 * Updates by Russ Allbery <eagle@eyrie.org>
 * Copyright 1996, 1997, 1998, 2006, 2008, 2012, 2013
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * See LICENSE for licensing terms.
 */

#include <config.h>
#include <portable/system.h>

#include <fcntl.h>
#ifdef HAVE_SEARCH_H
# include <search.h>
#endif
#include <sys/stat.h>
#if defined(HAVE_UTMPX_H)
# include <utmpx.h>
#elif defined(HAVE_UTMP_H)
# include <utmp.h>
#endif

#include <server/internal.h>
#include <util/messages.h>
#include <util/vector.h>
#include <util/xmalloc.h>

/*
 * The utmpx and utmp function sets have the same API, so we can use a set of
 * defines to fall back to the pre-utmpx versions if needed.
 */
#if !defined(HAVE_GETUTXENT) && defined(HAVE_GETUTENT)
# define utmpx       utmp
# define getutxent() getutent()
# define setutxent() setutent()
#endif

/* The logged-in user database.  We want to stat it for modification time. */
#if defined(HAVE_UTMPX_H)
# ifdef UTMPX_FILE
#  define LBCD_UTMP_FILE UTMPX_FILE
# elif defined(UTMP_FILE)
#  define LBCD_UTMP_FILE UTMP_FILE
# else
#  define LBCD_UTMP_FILE "/etc/utmpx"
# endif
#elif defined(HAVE_UTMP_H)
# ifdef UTMP_FILE
#  define LBCD_UTMP_FILE UTMP_FILE
# else
#  define LBCD_UTMP_FILE "/etc/utmp"
# endif
#endif
static const char *utmp = LBCD_UTMP_FILE;

/* Stores the list of unique users found so far. */
static struct vector *users = NULL;


/*
 * Create the (one, global) static hash table used to track users and
 * initialize variables.
 */
static void
uniq_start(void)
{
#ifdef HAVE_HSEARCH
    hcreate(211);
#endif
    users = vector_new();
}


/*
 * Clean up.  Free the users stored in the global users variable.
 */
static void
uniq_end(void)
{
#ifdef HAVE_HSEARCH
    hdestroy();
#endif
    if (users != NULL)
        vector_free(users);
}


/*
 * Count a given user.  See if they're already in our hash and, if not, add
 * them and count them.
 */
#ifdef HAVE_HSEARCH

static void
uniq_add(char *name)
{
    ENTRY item, *i;

    item.key = name;
    item.data = NULL;
    i = hsearch(item, FIND);
    if (i == NULL) {
        vector_add(users, name);
        item.key = users->strings[users->count - 1];
        hsearch(item, ENTER);
    }
}

#else /* !HAVE_HSEARCH */

static void
uniq_add(char *name)
{
    size_t i;

    if (i = 0; i < users->count; i++)
        if (strcmp(users->strings[i], name) == 0)
            return;
    vector_add(users, name);
}

#endif /* !HAVE_HSEARCH */


/*
 * Return the count of unique users.
 */
static size_t
uniq_count(void)
{
    return users->count;
}


/*
 * The public entry point.  Returns the total users, the unique users, a flag
 * indicating whether there is a user on console, and the time of the last
 * change.  Returns 0 on success and a negative value on error.
 *
 * Currently, this implementation ignores all characters after the first eight
 * when counting uniqueness.
 */
int
get_user_stats(int *total, int *uniq, int *on_console, time_t *user_mtime)
{
    char *name;
    struct stat sbuf;
    static int last_total = 0;
    static int last_uniq = 0;
    static int last_on_console = 0;
    static time_t last_user_mtime = 0;

    *total      = 0;
    *uniq       = 0;
    *on_console = 0;
    *user_mtime = 0;

    /*
     * Stat the utmp file to see if it's changed.  If it hasn't, use our
     * static cached values to save resources.
     */
    if (stat(utmp, &sbuf) == 0)
        *user_mtime = sbuf.st_mtime;
    if (*user_mtime > 0 && *user_mtime == last_user_mtime) {
        *total      = last_total;
        *uniq       = last_uniq;
        *on_console = last_on_console;
        return 0;
    }

    /*
     * Check two common names for console devices and assume we have a user on
     * console if they're owned by a non-root user.
     */
    if (stat("/dev/console", &sbuf) == 0 && sbuf.st_uid != 0)
        *on_console = 1;
    if (stat("/dev/tty1", &sbuf) == 0 && sbuf.st_uid  != 0)
        *on_console = 1;

    /*
     * Now count the number of unique users.  There are two implementations
     * depending on whether we have getutent or have to read the utmp file
     * ourselves.
     */
    uniq_start();
#if defined(HAVE_GETUTXENT) || defined(HAVE_GETUTENT)
    {
        struct utmpx *ut;

        setutxent();
        while ((ut = getutxent()) != NULL) {
            if (ut->ut_type != USER_PROCESS)
                continue;
            (*total)++;
            if (strncmp(ut->ut_line, "console", 7) == 0)
                *on_console = 1;
            if (strncmp(ut->ut_host, ":0", 2) == 0)
                *on_console = 1;
            name = xstrndup(ut->ut_user, sizeof(ut->ut_user));
            uniq_add(name);
            free(name);
        }
        endutxent();
    }
#else
    {
        struct utmp ut;
        int fd;

        fd = open(utmp, O_RDONLY);
        if (fd < 0) {
            syswarn("cannot open %s", utmp);
            return -1;
        }
        while (read(fd, &ut, sizeof(ut)) > 0) {
# ifndef USER_PROCESS
            if (ut.ut_name[0] == '\0')
                continue;
# else
            if (ut.ut_type != USER_PROCESS)
                continue;
# endif
            (*total)++;
            if (strncmp(ut.ut_line, "console", 7) == 0)
                *on_console = 1;
            if (strncmp(ut->ut_host, ":0", 2) == 0)
                *on_console = 1;
            name = xstrndup(ut.ut_name, sizeof(ut.ut_name));
            uniq_add(name);
            free(name);
        }
        close(fd);
    }
#endif /* !HAVE_GETUTENT */
    *uniq = uniq_count();
    uniq_end();

    /* Save our cached values. */
    last_total      = *total;
    last_uniq       = *uniq;
    last_on_console = *on_console;
    last_user_mtime = *user_mtime;

    return 0;
}


/*
 * Test routine.
 */
#ifdef MAIN
int
main(void)
{
    int t, u, oc;
    time_t mtime;

    get_user_stats(&t, &u, &oc, &mtime);
    printf("total = %d  uniq = %d  on_cons = %d\n", t, u, oc);
    return 0;
}
#endif