]> git.gag.com Git - debian/sudo/blob - auth/passwd.c
Imported Upstream version 1.6.6
[debian/sudo] / auth / passwd.c
1 /*
2  * Copyright (c) 1999-2001 Todd C. Miller <Todd.Miller@courtesan.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * 4. Products derived from this software may not be called "Sudo" nor
20  *    may "Sudo" appear in their names without specific prior written
21  *    permission from the author.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "config.h"
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <stdio.h>
40 #ifdef STDC_HEADERS
41 # include <stdlib.h>
42 # include <stddef.h>
43 #else
44 # ifdef HAVE_STDLIB_H
45 #  include <stdlib.h>
46 # endif
47 #endif /* STDC_HEADERS */
48 #ifdef HAVE_STRING_H
49 # include <string.h>
50 #else
51 # ifdef HAVE_STRINGS_H
52 #  include <strings.h>
53 # endif
54 #endif /* HAVE_STRING_H */
55 #ifdef HAVE_UNISTD_H
56 # include <unistd.h>
57 #endif /* HAVE_UNISTD_H */
58 #include <pwd.h>
59
60 #include "sudo.h"
61 #include "sudo_auth.h"
62
63 #ifndef lint
64 static const char rcsid[] = "$Sudo: passwd.c,v 1.11 2002/01/17 15:56:15 millert Exp $";
65 #endif /* lint */
66
67 #define DESLEN                  13
68 #define HAS_AGEINFO(p, l)       (l == 18 && p[DESLEN] == ',')
69
70 int
71 passwd_init(pw, promptp, auth)
72     struct passwd *pw;
73     char **promptp;
74     sudo_auth *auth;
75 {
76 #ifdef HAVE_SKEYACCESS
77     if (skeyaccess(pw, user_tty, NULL, NULL) == 0)
78         return(AUTH_FAILURE);
79 #endif
80     return(AUTH_SUCCESS);
81 }
82
83 int
84 passwd_verify(pw, pass, auth)
85     struct passwd *pw;
86     char *pass;
87     sudo_auth *auth;
88 {
89     char sav, *epass;
90     size_t pw_len;
91     int error;
92
93     pw_len = strlen(pw->pw_passwd);
94
95 #ifdef HAVE_GETAUTHUID
96     /* Ultrix shadow passwords may use crypt16() */
97     error = strcmp(pw->pw_passwd, (char *) crypt16(pass, pw->pw_passwd));
98     if (!error)
99         return(AUTH_SUCCESS);
100 #endif /* HAVE_GETAUTHUID */
101
102     /*
103      * Truncate to 8 chars if standard DES since not all crypt()'s do this.
104      * If this turns out not to be safe we will have to use OS #ifdef's (sigh).
105      */
106     sav = pass[8];
107     if (pw_len == DESLEN || HAS_AGEINFO(pw->pw_passwd, pw_len))
108         pass[8] = '\0';
109
110     /*
111      * Normal UN*X password check.
112      * HP-UX may add aging info (separated by a ',') at the end so
113      * only compare the first DESLEN characters in that case.
114      */
115     epass = (char *) crypt(pass, pw->pw_passwd);
116     pass[8] = sav;
117     if (HAS_AGEINFO(pw->pw_passwd, pw_len) && strlen(epass) == DESLEN)
118         error = strncmp(pw->pw_passwd, epass, DESLEN);
119     else
120         error = strcmp(pw->pw_passwd, epass);
121
122     return(error ? AUTH_FAILURE : AUTH_SUCCESS);
123 }