See if we can get slightly valid C for the non-PAM case here.
[tprouty/samba.git] / source / lib / pam_errors.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  PAM error mapping functions
4  *  Copyright (C) Andrew Bartlett 2002
5  *  
6  *  This program 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 2 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 #ifdef WITH_PAM
24 #include <security/pam_appl.h>
25
26 /* PAM -> NT_STATUS map */
27 static struct {
28         int pam_code;
29         NTSTATUS ntstatus;
30 } pam_to_nt_status_map[] = {
31         {PAM_OPEN_ERR, NT_STATUS_UNSUCCESSFUL},
32         {PAM_SYMBOL_ERR, NT_STATUS_UNSUCCESSFUL},
33         {PAM_SERVICE_ERR, NT_STATUS_UNSUCCESSFUL},
34         {PAM_SYSTEM_ERR,  NT_STATUS_UNSUCCESSFUL},
35         {PAM_BUF_ERR, NT_STATUS_UNSUCCESSFUL},
36         {PAM_PERM_DENIED, NT_STATUS_ACCESS_DENIED},
37         {PAM_AUTH_ERR, NT_STATUS_WRONG_PASSWORD},
38         {PAM_CRED_INSUFFICIENT, NT_STATUS_INSUFFICIENT_LOGON_INFO}, /* FIXME:  Is this correct? */
39         {PAM_AUTHINFO_UNAVAIL, NT_STATUS_LOGON_FAILURE},
40         {PAM_USER_UNKNOWN, NT_STATUS_NO_SUCH_USER},
41         {PAM_MAXTRIES, NT_STATUS_REMOTE_SESSION_LIMIT}, /* FIXME:  Is this correct? */
42         {PAM_NEW_AUTHTOK_REQD, NT_STATUS_PASSWORD_MUST_CHANGE},
43         {PAM_ACCT_EXPIRED, NT_STATUS_ACCOUNT_EXPIRED},
44         {PAM_SESSION_ERR, NT_STATUS_INSUFFICIENT_RESOURCES},
45         {PAM_CRED_UNAVAIL, NT_STATUS_NO_TOKEN},  /* FIXME:  Is this correct? */
46         {PAM_CRED_EXPIRED, NT_STATUS_PASSWORD_EXPIRED},  /* FIXME:  Is this correct? */
47         {PAM_CRED_ERR, NT_STATUS_UNSUCCESSFUL},
48         {PAM_AUTHTOK_ERR, NT_STATUS_UNSUCCESSFUL},
49 #if defined(PAM_AUTHTOK_RECOVERY_ERR) && !defined(PAM_AUTHTOK_RECOVER_ERR)
50 #define PAM_AUTHTOK_RECOVER_ERR PAM_AUTHTOK_RECOVERY_ERR
51 #endif  
52         {PAM_AUTHTOK_RECOVER_ERR, NT_STATUS_UNSUCCESSFUL},
53         {PAM_AUTHTOK_EXPIRED, NT_STATUS_PASSWORD_EXPIRED},
54         {PAM_SUCCESS, NT_STATUS_OK}
55 };
56
57 /* NT_STATUS -> PAM map */
58 static struct {
59         NTSTATUS ntstatus;
60         int pam_code;
61 } nt_status_to_pam_map[] = {
62         {NT_STATUS_UNSUCCESSFUL, PAM_SYSTEM_ERR},
63         {NT_STATUS_NO_SUCH_USER, PAM_USER_UNKNOWN},
64         {NT_STATUS_WRONG_PASSWORD, PAM_AUTH_ERR},
65         {NT_STATUS_LOGON_FAILURE, PAM_AUTH_ERR},
66         {NT_STATUS_ACCOUNT_EXPIRED, PAM_ACCT_EXPIRED},
67         {NT_STATUS_PASSWORD_MUST_CHANGE, PAM_NEW_AUTHTOK_REQD},
68         {NT_STATUS_OK, PAM_SUCCESS}
69 };
70
71 /*****************************************************************************
72 convert a PAM error to a NT status32 code
73  *****************************************************************************/
74 NTSTATUS pam_to_nt_status(int pam_error)
75 {
76         int i;
77         if (pam_error == 0) return NT_STATUS_OK;
78         
79         for (i=0; NT_STATUS_V(pam_to_nt_status_map[i].ntstatus); i++) {
80                 if (pam_error == pam_to_nt_status_map[i].pam_code)
81                         return pam_to_nt_status_map[i].ntstatus;
82         }
83         return NT_STATUS_UNSUCCESSFUL;
84 }
85
86 /*****************************************************************************
87 convert an NT status32 code to a PAM error
88  *****************************************************************************/
89 int nt_status_to_pam(NTSTATUS nt_status)
90 {
91         int i;
92         if NT_STATUS_IS_OK(nt_status) return PAM_SUCCESS;
93         
94         for (i=0; NT_STATUS_V(nt_status_to_pam_map[i].ntstatus); i++) {
95                 if (NT_STATUS_EQUAL(nt_status,nt_status_to_pam_map[i].ntstatus))
96                         return nt_status_to_pam_map[i].pam_code;
97         }
98         return PAM_SYSTEM_ERR;
99 }
100
101 #else 
102
103 /*****************************************************************************
104 convert a PAM error to a NT status32 code
105  *****************************************************************************/
106 NTSTATUS pam_to_ntstatus(int pam_error)
107 {
108         if (pam_error == 0) return NT_STATUS_OK;
109         return NT_STATUS_UNSUCCESSFUL;
110 }
111
112 /*****************************************************************************
113 convert an NT status32 code to a PAM error
114  *****************************************************************************/
115 int ntstatus_to_pam(NTSTATUS nt_status)
116 {
117         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_OK)) return 0;
118         return 4; /* PAM_SYSTEM_ERR */
119 }
120
121 #endif
122