]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/pam_unix_sess.c
Relevant BUGIDs: 872943
[linux-pam] / modules / pam_unix / pam_unix_sess.c
1 /*
2  * $Id$
3  *
4  * Copyright Alexander O. Yuriev, 1996.  All rights reserved.
5  * Copyright Jan Rêkorajski, 1999.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, and the entire permission notice in its entirety,
12  *    including the disclaimer of warranties.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior
18  *    written permission.
19  * 
20  * ALTERNATIVELY, this product may be distributed under the terms of
21  * the GNU Public License, in which case the provisions of the GPL are
22  * required INSTEAD OF the above restrictions.  (This clause is
23  * necessary due to a potential bad interaction between the GPL and
24  * the restrictions contained in a BSD-style copyright.)
25  * 
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36  * OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include <security/_pam_aconf.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <unistd.h>
45 #include <syslog.h>
46 #include <fcntl.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49
50 /* indicate the following groups are defined */
51
52 #define PAM_SM_SESSION
53
54 #include <security/_pam_macros.h>
55 #include <security/pam_modules.h>
56 #include <security/_pam_modutil.h>
57
58 #ifndef LINUX_PAM
59 #include <security/pam_appl.h>
60 #endif                          /* LINUX_PAM */
61
62 #include "support.h"
63
64 /*
65  * PAM framework looks for these entry-points to pass control to the
66  * session module.
67  */
68
69 PAM_EXTERN int pam_sm_open_session(pam_handle_t * pamh, int flags,
70                                    int argc, const char **argv)
71 {
72         char *user_name, *service;
73         unsigned int ctrl;
74         int retval;
75     const char *login_name;
76
77         D(("called."));
78
79         ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
80
81         retval = pam_get_item(pamh, PAM_USER, (void *) &user_name);
82         if (user_name == NULL || retval != PAM_SUCCESS) {
83                 _log_err(LOG_CRIT, pamh,
84                          "open_session - error recovering username");
85                 return PAM_SESSION_ERR;         /* How did we get authenticated with
86                                                    no username?! */
87         }
88         retval = pam_get_item(pamh, PAM_SERVICE, (void *) &service);
89         if (service == NULL || retval != PAM_SUCCESS) {
90                 _log_err(LOG_CRIT, pamh,
91                          "open_session - error recovering service");
92                 return PAM_SESSION_ERR;
93         }
94         login_name = _pammodutil_getlogin(pamh);
95         if (login_name == NULL) {
96             login_name = "";
97         }
98         _log_err(LOG_INFO, pamh, "session opened for user %s by %s(uid=%d)",
99                  user_name, login_name, getuid());
100
101         return PAM_SUCCESS;
102 }
103
104 PAM_EXTERN int pam_sm_close_session(pam_handle_t * pamh, int flags,
105                                     int argc, const char **argv)
106 {
107         char *user_name, *service;
108         unsigned int ctrl;
109         int retval;
110
111         D(("called."));
112
113         ctrl = _set_ctrl(pamh, flags, NULL, argc, argv);
114
115         retval = pam_get_item(pamh, PAM_USER, (void *) &user_name);
116         if (user_name == NULL || retval != PAM_SUCCESS) {
117                 _log_err(LOG_CRIT, pamh,
118                          "close_session - error recovering username");
119                 return PAM_SESSION_ERR;         /* How did we get authenticated with
120                                                    no username?! */
121         }
122         retval = pam_get_item(pamh, PAM_SERVICE, (void *) &service);
123         if (service == NULL || retval != PAM_SUCCESS) {
124                 _log_err(LOG_CRIT, pamh,
125                          "close_session - error recovering service");
126                 return PAM_SESSION_ERR;
127         }
128         _log_err(LOG_INFO, pamh, "session closed for user %s"
129                  ,user_name);
130
131         return PAM_SUCCESS;
132 }
133
134 /* static module data */
135 #ifdef PAM_STATIC
136 struct pam_module _pam_unix_session_modstruct = {
137     "pam_unix_session",
138     NULL,
139     NULL,
140     NULL,
141     pam_sm_open_session,
142     pam_sm_close_session,
143     NULL,
144 };
145 #endif
146