~ubuntu-branches/ubuntu/raring/shadow/raring-proposed

« back to all changes in this revision

Viewing changes to lib/shadow.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
1
/*
2
 
 * Copyright 1989 - 1994, Julianne Frances Haugh
 
2
 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
 
3
 * Copyright (c) 1996 - 1998, Marek Michałkiewicz
 
4
 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
 
5
 * Copyright (c) 2009       , Nicolas François
3
6
 * All rights reserved.
4
7
 *
5
8
 * Redistribution and use in source and binary forms, with or without
10
13
 * 2. Redistributions in binary form must reproduce the above copyright
11
14
 *    notice, this list of conditions and the following disclaimer in the
12
15
 *    documentation and/or other materials provided with the distribution.
13
 
 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14
 
 *    may be used to endorse or promote products derived from this software
15
 
 *    without specific prior written permission.
 
16
 * 3. The name of the copyright holders or contributors may not be used to
 
17
 *    endorse or promote products derived from this software without
 
18
 *    specific prior written permission.
16
19
 *
17
 
 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 
 * SUCH DAMAGE.
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
23
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
 
24
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
25
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
26
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
27
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
28
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
29
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
30
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
31
 */
29
32
 
30
33
#include <config.h>
32
35
/* Newer versions of Linux libc already have shadow support.  */
33
36
#ifndef HAVE_GETSPNAM
34
37
 
35
 
#ident "$Id: shadow.c 1621 2008-01-01 22:21:55Z nekral-guest $"
 
38
#ident "$Id: shadow.c 2620 2009-04-10 22:35:14Z nekral-guest $"
36
39
 
37
40
#include <sys/types.h>
38
41
#include "prototypes.h"
53
56
#endif
54
57
 
55
58
static FILE *shadow;
56
 
static char spwbuf[BUFSIZ];
57
 
static struct spwd spwd;
58
59
 
59
60
#define FIELDS  9
60
61
#define OFIELDS 5
69
70
{
70
71
        nis_ignore = !flag;
71
72
 
72
 
        if (nis_ignore)
 
73
        if (nis_ignore) {
73
74
                nis_used = 0;
 
75
        }
74
76
}
75
77
 
76
78
/*
121
123
 
122
124
static struct spwd *my_sgetspent (const char *string)
123
125
{
 
126
        static char spwbuf[BUFSIZ];
 
127
        static struct spwd spwd;
124
128
        char *fields[FIELDS];
125
129
        char *cp;
126
130
        char *cpp;
179
183
         * incorrectly formatted number, unless we are using NIS.
180
184
         */
181
185
 
182
 
        spwd.sp_lstchg = strtol (fields[2], &cpp, 10);
183
 
        if ((spwd.sp_lstchg == 0) && *cpp) {
184
 
#ifdef  USE_NIS
185
 
                if (!nis_used)
186
 
                        return 0;
187
 
                else
188
 
                        spwd.sp_lstchg = -1;
189
 
#else
190
 
                return 0;
191
 
#endif
192
 
        } else if (fields[2][0] == '\0')
 
186
        if (fields[2][0] == '\0') {
193
187
                spwd.sp_lstchg = -1;
 
188
        } else {
 
189
                if (getlong (fields[2], &spwd.sp_lstchg) == 0) {
 
190
#ifdef  USE_NIS
 
191
                        if (nis_used) {
 
192
                                spwd.sp_lstchg = -1;
 
193
                        } else
 
194
#endif
 
195
                                return 0;
 
196
                } else if (spwd.sp_lstchg < 0) {
 
197
                        return 0;
 
198
                }
 
199
        }
194
200
 
195
201
        /*
196
202
         * Get the minimum period between password changes.
197
203
         */
198
204
 
199
 
        spwd.sp_min = strtol (fields[3], &cpp, 10);
200
 
        if ((spwd.sp_min == 0) && *cpp) {
201
 
#ifdef  USE_NIS
202
 
                if (!nis_used)
203
 
                        return 0;
204
 
                else
205
 
                        spwd.sp_min = -1;
206
 
#else
207
 
                return 0;
208
 
#endif
209
 
        } else if (fields[3][0] == '\0')
 
205
        if (fields[3][0] == '\0') {
210
206
                spwd.sp_min = -1;
 
207
        } else {
 
208
                if (getlong (fields[3], &spwd.sp_min) == 0) {
 
209
#ifdef  USE_NIS
 
210
                        if (nis_used) {
 
211
                                spwd.sp_min = -1;
 
212
                        } else
 
213
#endif
 
214
                                return 0;
 
215
                } else if (spwd.sp_min < 0) {
 
216
                        return 0;
 
217
                }
 
218
        }
211
219
 
212
220
        /*
213
221
         * Get the maximum number of days a password is valid.
214
222
         */
215
223
 
216
 
        spwd.sp_max = strtol (fields[4], &cpp, 10);
217
 
        if ((spwd.sp_max == 0) && *cpp) {
218
 
#ifdef  USE_NIS
219
 
                if (!nis_used)
220
 
                        return 0;
221
 
                else
222
 
                        spwd.sp_max = -1;
223
 
#else
224
 
                return 0;
225
 
#endif
226
 
        } else if (fields[4][0] == '\0')
 
224
        if (fields[4][0] == '\0') {
227
225
                spwd.sp_max = -1;
 
226
        } else {
 
227
                if (getlong (fields[4], &spwd.sp_max) == 0) {
 
228
#ifdef  USE_NIS
 
229
                        if (nis_used) {
 
230
                                spwd.sp_max = -1;
 
231
                        } else
 
232
#endif
 
233
                                return 0;
 
234
                } else if (spwd.sp_max < 0) {
 
235
                        return 0;
 
236
                }
 
237
        }
228
238
 
229
239
        /*
230
240
         * If there are only OFIELDS fields (this is a SVR3.2 /etc/shadow
232
242
         */
233
243
 
234
244
        if (i == OFIELDS) {
235
 
                spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
236
 
                    spwd.sp_flag = -1;
 
245
                spwd.sp_warn   = -1;
 
246
                spwd.sp_inact  = -1;
 
247
                spwd.sp_expire = -1;
 
248
                spwd.sp_flag   = SHADOW_SP_FLAG_UNSET;
237
249
 
238
250
                return &spwd;
239
251
        }
242
254
         * Get the number of days of password expiry warning.
243
255
         */
244
256
 
245
 
        spwd.sp_warn = strtol (fields[5], &cpp, 10);
246
 
        if ((spwd.sp_warn == 0) && *cpp) {
247
 
#ifdef  USE_NIS
248
 
                if (!nis_used)
249
 
                        return 0;
250
 
                else
251
 
                        spwd.sp_warn = -1;
252
 
#else
253
 
                return 0;
254
 
#endif
255
 
        } else if (fields[5][0] == '\0')
 
257
        if (fields[5][0] == '\0') {
256
258
                spwd.sp_warn = -1;
 
259
        } else {
 
260
                if (getlong (fields[5], &spwd.sp_warn) == 0) {
 
261
#ifdef  USE_NIS
 
262
                        if (nis_used) {
 
263
                                spwd.sp_warn = -1;
 
264
                        } else
 
265
#endif
 
266
                                return 0;
 
267
                } else if (spwd.sp_warn < 0) {
 
268
                        return 0;
 
269
                }
 
270
        }
257
271
 
258
272
        /*
259
273
         * Get the number of days of inactivity before an account is
260
274
         * disabled.
261
275
         */
262
276
 
263
 
        spwd.sp_inact = strtol (fields[6], &cpp, 10);
264
 
        if ((spwd.sp_inact == 0) && *cpp) {
265
 
#ifdef  USE_NIS
266
 
                if (!nis_used)
267
 
                        return 0;
268
 
                else
269
 
                        spwd.sp_inact = -1;
270
 
#else
271
 
                return 0;
272
 
#endif
273
 
        } else if (fields[6][0] == '\0')
 
277
        if (fields[6][0] == '\0') {
274
278
                spwd.sp_inact = -1;
 
279
        } else {
 
280
                if (getlong (fields[6], &spwd.sp_inact) == 0) {
 
281
#ifdef  USE_NIS
 
282
                        if (nis_used) {
 
283
                                spwd.sp_inact = -1;
 
284
                        } else
 
285
#endif
 
286
                                return 0;
 
287
                } else if (spwd.sp_inact < 0) {
 
288
                        return 0;
 
289
                }
 
290
        }
275
291
 
276
292
        /*
277
293
         * Get the number of days after the epoch before the account is
278
294
         * set to expire.
279
295
         */
280
296
 
281
 
        spwd.sp_expire = strtol (fields[7], &cpp, 10);
282
 
        if ((spwd.sp_expire == 0) && *cpp) {
 
297
        if (fields[7][0] == '\0') {
 
298
                spwd.sp_expire = -1;
 
299
        } else {
 
300
                if (getlong (fields[7], &spwd.sp_expire) == 0) {
283
301
#ifdef  USE_NIS
284
 
                if (!nis_used)
 
302
                        if (nis_used) {
 
303
                                spwd.sp_expire = -1;
 
304
                        } else
 
305
#endif
 
306
                                return 0;
 
307
                } else if (spwd.sp_expire < 0) {
285
308
                        return 0;
286
 
                else
287
 
                        spwd.sp_expire = -1;
288
 
#else
289
 
                return 0;
290
 
#endif
291
 
        } else if (fields[7][0] == '\0')
292
 
                spwd.sp_expire = -1;
 
309
                }
 
310
        }
293
311
 
294
312
        /*
295
313
         * This field is reserved for future use.  But it isn't supposed
296
314
         * to have anything other than a valid integer in it.
297
315
         */
298
316
 
299
 
        spwd.sp_flag = strtol (fields[8], &cpp, 10);
300
 
        if ((spwd.sp_flag == 0) && *cpp) {
 
317
        if (fields[8][0] == '\0') {
 
318
                spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
 
319
        } else {
 
320
                if (getlong (fields[8], &spwd.sp_flag) == 0) {
 
321
                        /* FIXME: add a getulong function */
301
322
#ifdef  USE_NIS
302
 
                if (!nis_used)
 
323
                        if (nis_used) {
 
324
                                spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
 
325
                        } else
 
326
#endif
 
327
                                return 0;
 
328
                } else if (spwd.sp_flag < 0) {
303
329
                        return 0;
304
 
                else
305
 
                        spwd.sp_flag = -1;
306
 
#else
307
 
                return 0;
308
 
#endif
309
 
        } else if (fields[8][0] == '\0')
310
 
                spwd.sp_flag = -1;
 
330
                }
 
331
        }
311
332
 
312
333
        return (&spwd);
313
334
}
321
342
        char buf[BUFSIZ];
322
343
        char *cp;
323
344
 
324
 
        if (!fp)
 
345
        if (NULL == fp) {
325
346
                return (0);
 
347
        }
326
348
 
327
349
#ifdef  USE_NIS
328
 
        while (fgets (buf, sizeof buf, fp) != (char *) 0)
 
350
        while (fgets (buf, (int) sizeof buf, fp) != (char *) 0)
329
351
#else
330
 
        if (fgets (buf, sizeof buf, fp) != (char *) 0)
 
352
        if (fgets (buf, (int) sizeof buf, fp) != (char *) 0)
331
353
#endif
332
354
        {
333
355
                cp = strchr (buf, '\n');
334
 
                if (NULL != cp)
 
356
                if (NULL != cp) {
335
357
                        *cp = '\0';
 
358
                }
336
359
#ifdef  USE_NIS
337
 
                if (nis_ignore && IS_NISCHAR (buf[0]))
 
360
                if (nis_ignore && IS_NISCHAR (buf[0])) {
338
361
                        continue;
 
362
                }
339
363
#endif
340
364
                return my_sgetspent (buf);
341
365
        }
510
534
#else
511
535
extern int errno;               /* warning: ANSI C forbids an empty source file */
512
536
#endif
 
537