s3:web/swat: use short term event and message contexts
[kai/samba.git] / source3 / 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, see <http://www.gnu.org/licenses/>.
15 */
16
17 /* indicate the following groups are defined */
18 #define PAM_SM_ACCT
19
20 #include "includes.h"
21
22 #ifndef LINUX
23
24 /* This is only used in the Sun implementation. */
25 #if defined(HAVE_SECURITY_PAM_APPL_H)
26 #include <security/pam_appl.h>
27 #elif defined(HAVE_PAM_PAM_APPL_H)
28 #include <pam/pam_appl.h>
29 #endif
30
31 #endif  /* LINUX */
32
33 #if defined(HAVE_SECURITY_PAM_MODULES_H)
34 #include <security/pam_modules.h>
35 #elif defined(HAVE_PAM_PAM_MODULES_H)
36 #include <pam/pam_modules.h>
37 #endif
38
39 #include "general.h"
40
41 #include "support.h"
42
43
44 /*
45  * pam_sm_acct_mgmt() verifies whether or not the account is disabled.
46  *
47  */
48
49 int pam_sm_acct_mgmt( pam_handle_t *pamh, int flags,
50                       int argc, const char **argv )
51 {
52         unsigned int ctrl;
53         int retval;
54
55         const char *name;
56         struct samu *sampass = NULL;
57         void (*oldsig_handler)(int);
58
59         /* Samba initialization. */
60         load_case_tables_library();
61
62         ctrl = set_ctrl(pamh, flags, argc, argv );
63
64         /* get the username */
65
66         retval = pam_get_user( pamh, &name, "Username: " );
67         if (retval != PAM_SUCCESS) {
68                 if (on( SMB_DEBUG, ctrl )) {
69                         _log_err(pamh, LOG_DEBUG, "acct: could not identify user" );
70                 }
71                 return retval;
72         }
73         if (on( SMB_DEBUG, ctrl )) {
74                 _log_err(pamh, LOG_DEBUG, "acct: username [%s] obtained", name );
75         }
76
77         if (geteuid() != 0) {
78                 _log_err(pamh, LOG_DEBUG, "Cannot access samba password database, not running as root.");
79                 return PAM_AUTHINFO_UNAVAIL;
80         }
81
82         /* Getting into places that might use LDAP -- protect the app
83                 from a SIGPIPE it's not expecting */
84         oldsig_handler = CatchSignal(SIGPIPE, SIG_IGN);
85         if (!initialize_password_db(True, NULL)) {
86           _log_err(pamh, LOG_ALERT, "Cannot access samba password database" );
87                 CatchSignal(SIGPIPE, oldsig_handler);
88                 return PAM_AUTHINFO_UNAVAIL;
89         }
90
91         /* Get the user's record. */
92
93         if (!(sampass = samu_new( NULL ))) {
94                 CatchSignal(SIGPIPE, oldsig_handler);
95                 /* malloc fail. */
96                 return nt_status_to_pam(NT_STATUS_NO_MEMORY);
97         }
98
99         if (!pdb_getsampwnam(sampass, name )) {
100                 _log_err(pamh, LOG_DEBUG, "acct: could not identify user");
101                 CatchSignal(SIGPIPE, oldsig_handler);
102                 return PAM_USER_UNKNOWN;
103         }
104
105         /* check for lookup failure */
106         if (!strlen(pdb_get_username(sampass)) ) {
107                 CatchSignal(SIGPIPE, oldsig_handler);
108                 return PAM_USER_UNKNOWN;
109         }
110
111         if (pdb_get_acct_ctrl(sampass) & ACB_DISABLED) {
112                 if (on( SMB_DEBUG, ctrl )) {
113                         _log_err(pamh, LOG_DEBUG,
114                                  "acct: account %s is administratively disabled", name);
115                 }
116                 make_remark( pamh, ctrl, PAM_ERROR_MSG
117                         , "Your account has been disabled; "
118                         "please see your system administrator." );
119
120                 CatchSignal(SIGPIPE, oldsig_handler);
121                 return PAM_ACCT_EXPIRED;
122         }
123
124         /* TODO: support for expired passwords. */
125
126         CatchSignal(SIGPIPE, oldsig_handler);
127         return PAM_SUCCESS;
128 }
129
130 /* static module data */
131 #ifdef PAM_STATIC
132 struct pam_module _pam_smbpass_acct_modstruct = {
133      "pam_smbpass",
134      NULL,
135      NULL,
136      pam_sm_acct_mgmt,
137      NULL,
138      NULL,
139      NULL
140 };
141 #endif
142