~xnox/ubuntu/quantal/shadow/clear-locks

« back to all changes in this revision

Viewing changes to libmisc/find_new_uid.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2009-05-05 09:45:21 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505094521-wpk2wn3q7957tlah
Tags: 1:4.1.3.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Ubuntu specific:
    + debian/login.defs: use SHA512 by default for password crypt routine.
  - debian/patches/stdout-encrypted-password.patch: chpasswd can report
    password hashes on stdout (debian bug 505640).
  - debian/login.pam: Enable SELinux support (debian bug 527106).
  - debian/securetty.linux: support Freescale MX-series (debian bug 527095).
* Add debian/patches/300_lastlog_failure: fixed upstream (debian bug 524873).
* Drop debian/patches/593_omit_lastchange_field_if_clock_is_misset: fixed
  upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
 
3
 * Copyright (c) 2008       , Nicolas François
 
4
 * All rights reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions
 
8
 * are met:
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 * 3. The name of the copyright holders or contributors may not be used to
 
15
 *    endorse or promote products derived from this software without
 
16
 *    specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
21
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
 
22
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
#include <config.h>
 
32
 
 
33
#include <assert.h>
 
34
#include <stdio.h>
 
35
 
 
36
#include "prototypes.h"
 
37
#include "pwio.h"
 
38
#include "getdef.h"
 
39
 
 
40
/*
 
41
 * find_new_uid - Find a new unused UID.
 
42
 *
 
43
 * If successful, find_new_uid provides an unused user ID in the
 
44
 * [UID_MIN:UID_MAX] range.
 
45
 * This ID should be higher than all the used UID, but if not possible,
 
46
 * the lowest unused ID in the range will be returned.
 
47
 * 
 
48
 * Return 0 on success, -1 if no unused UIDs are available.
 
49
 */
 
50
int find_new_uid (bool sys_user, uid_t *uid, uid_t const *preferred_uid)
 
51
{
 
52
        const struct passwd *pwd;
 
53
        uid_t uid_min, uid_max, user_id;
 
54
        char *used_uids;
 
55
 
 
56
        assert (uid != NULL);
 
57
 
 
58
        if (!sys_user) {
 
59
                uid_min = getdef_ulong ("UID_MIN", 1000L);
 
60
                uid_max = getdef_ulong ("UID_MAX", 60000L);
 
61
        } else {
 
62
                uid_min = getdef_ulong ("SYS_UID_MIN", 1L);
 
63
                uid_max = getdef_ulong ("UID_MIN", 1000L) - 1;
 
64
                uid_max = getdef_ulong ("SYS_UID_MAX", (unsigned long) uid_max);
 
65
        }
 
66
        used_uids = alloca (sizeof (char) * uid_max +1);
 
67
        memset (used_uids, 0, sizeof (char) * uid_max + 1);
 
68
 
 
69
        if (   (NULL != preferred_uid)
 
70
            && (*preferred_uid >= uid_min)
 
71
            && (*preferred_uid <= uid_max)
 
72
            /* Check if the user exists according to NSS */
 
73
            && (getpwuid (*preferred_uid) == NULL)
 
74
            /* Check also the local database in case of uncommitted
 
75
             * changes */
 
76
            && (pw_locate_uid (*preferred_uid) == NULL)) {
 
77
                *uid = *preferred_uid;
 
78
                return 0;
 
79
        }
 
80
 
 
81
 
 
82
        user_id = uid_min;
 
83
 
 
84
        /*
 
85
         * Search the entire password file,
 
86
         * looking for the largest unused value.
 
87
         *
 
88
         * We check the list of users according to NSS (setpwent/getpwent),
 
89
         * but we also check the local database (pw_rewind/pw_next) in case
 
90
         * some users were created but the changes were not committed yet.
 
91
         */
 
92
        setpwent ();
 
93
        while ((pwd = getpwent ()) != NULL) {
 
94
                if ((pwd->pw_uid >= user_id) && (pwd->pw_uid <= uid_max)) {
 
95
                        user_id = pwd->pw_uid + 1;
 
96
                }
 
97
                /* create index of used UIDs */
 
98
                if (pwd->pw_uid <= uid_max) {
 
99
                        used_uids[pwd->pw_uid] = 1;
 
100
                }
 
101
        }
 
102
        endpwent ();
 
103
        pw_rewind ();
 
104
        while ((pwd = pw_next ()) != NULL) {
 
105
                if ((pwd->pw_uid >= user_id) && (pwd->pw_uid <= uid_max)) {
 
106
                        user_id = pwd->pw_uid + 1;
 
107
                }
 
108
                /* create index of used UIDs */
 
109
                if (pwd->pw_uid <= uid_max) {
 
110
                        used_uids[pwd->pw_uid] = 1;
 
111
                }
 
112
        }
 
113
 
 
114
        /* find free system account in reverse order */
 
115
        if (sys_user) {
 
116
                for (user_id = uid_max; user_id >= uid_min; user_id--) {
 
117
                        if (0 == used_uids[user_id]) {
 
118
                                break;
 
119
                        }
 
120
                }
 
121
                if (user_id < uid_min ) {
 
122
                        fprintf (stderr,
 
123
                                 _("%s: Can't get unique system UID (no more available UIDs)\n"),
 
124
                                 Prog);
 
125
                        SYSLOG ((LOG_WARN,
 
126
                                 "no more available UID on the system"));
 
127
                        return -1;
 
128
                }
 
129
        }
 
130
 
 
131
        /*
 
132
         * If a user with UID equal to UID_MAX exists, the above algorithm
 
133
         * will give us UID_MAX+1 even if not unique. Search for the first
 
134
         * free UID starting with UID_MIN.
 
135
         */
 
136
        if (user_id == uid_max + 1) {
 
137
                for (user_id = uid_min; user_id < uid_max; user_id++) {
 
138
                        if (0 == used_uids[user_id]) {
 
139
                                break;
 
140
                        }
 
141
                }
 
142
                if (user_id == uid_max) {
 
143
                        fprintf (stderr, _("%s: Can't get unique UID (no more available UIDs)\n"), Prog);
 
144
                        SYSLOG ((LOG_WARN, "no more available UID on the system"));
 
145
                        return -1;
 
146
                }
 
147
        }
 
148
 
 
149
        *uid = user_id;
 
150
        return 0;
 
151
}
 
152