7a3a8a556acdada011b2bd423ef9894d9a3cb2be
[gd/samba/.git] / source / pam_smbpass / pam_smb_acct.c
1 /* Unix NT password database implementation, version 0.7.5.
2  *
3  * This program is free software; you can redistribute it and/or modify it under
4  * the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 3 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 675
15  * Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 /* indicate the following groups are defined */
19 #define PAM_SM_ACCT
20
21 #include "includes.h"
22
23 #ifndef LINUX
24
25 /* This is only used in the Sun implementation. */
26 #if defined(HAVE_SECURITY_PAM_APPL_H)
27 #include <security/pam_appl.h>
28 #elif defined(HAVE_PAM_PAM_APPL_H)
29 #include <pam/pam_appl.h>
30 #endif
31
32 #endif  /* LINUX */
33
34 #if defined(HAVE_SECURITY_PAM_MODULES_H)
35 #include <security/pam_modules.h>
36 #elif defined(HAVE_PAM_PAM_MODULES_H)
37 #include <pam/pam_modules.h>
38 #endif
39
40 #include "general.h"
41
42 #include "support.h"
43
44
45 /*
46  * pam_sm_acct_mgmt() verifies whether or not the account is disabled.
47  *
48  */
49
50 int pam_sm_acct_mgmt( pam_handle_t *pamh, int flags,
51                       int argc, const char **argv )
52 {
53         unsigned int ctrl;
54         int retval;
55
56         const char *name;
57         struct samu *sampass = NULL;
58         void (*oldsig_handler)(int);
59         extern BOOL in_client;
60
61         /* Samba initialization. */
62         load_case_tables();
63         setup_logging( "pam_smbpass", False );
64         in_client = True;
65
66         ctrl = set_ctrl( flags, argc, argv );
67
68         /* get the username */
69
70         retval = pam_get_user( pamh, &name, "Username: " );
71         if (retval != PAM_SUCCESS) {
72                 if (on( SMB_DEBUG, ctrl )) {
73                         _log_err( LOG_DEBUG, "acct: could not identify user" );
74                 }
75                 return retval;
76         }
77         if (on( SMB_DEBUG, ctrl )) {
78                 _log_err( LOG_DEBUG, "acct: username [%s] obtained", name );
79         }
80
81         /* Getting into places that might use LDAP -- protect the app
82                 from a SIGPIPE it's not expecting */
83         oldsig_handler = CatchSignal(SIGPIPE, SIGNAL_CAST SIG_IGN);
84         if (!initialize_password_db(True, NULL)) {
85                 _log_err( LOG_ALERT, "Cannot access samba password database" );
86                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
87                 return PAM_AUTHINFO_UNAVAIL;
88         }
89
90         /* Get the user's record. */
91
92         if (!(sampass = samu_new( NULL ))) {
93                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
94                 /* malloc fail. */
95                 return nt_status_to_pam(NT_STATUS_NO_MEMORY);
96         }
97
98         if (!pdb_getsampwnam(sampass, name )) {
99                 _log_err( LOG_DEBUG, "acct: could not identify user" );
100                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
101                 return PAM_USER_UNKNOWN;
102         }
103
104         /* check for lookup failure */
105         if (!strlen(pdb_get_username(sampass)) ) {
106                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
107                 return PAM_USER_UNKNOWN;
108         }
109
110         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
111                 if (on( SMB_DEBUG, ctrl )) {
112                         _log_err( LOG_DEBUG
113                                 , "acct: account %s is administratively disabled", name );
114                 }
115                 make_remark( pamh, ctrl, PAM_ERROR_MSG
116                         , "Your account has been disabled; "
117                         "please see your system administrator." );
118
119                 CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
120                 return PAM_ACCT_EXPIRED;
121         }
122
123         /* TODO: support for expired passwords. */
124
125         CatchSignal(SIGPIPE, SIGNAL_CAST oldsig_handler);
126         return PAM_SUCCESS;
127 }
128
129 /* static module data */
130 #ifdef PAM_STATIC
131 struct pam_module _pam_smbpass_acct_modstruct = {
132      "pam_smbpass",
133      NULL,
134      NULL,
135      pam_sm_acct_mgmt,
136      NULL,
137      NULL,
138      NULL
139 };
140 #endif
141