summaryrefslogtreecommitdiffstats
path: root/pam-ck-connector/pam-ck-connector.c
blob: 835be0c463c5e751649c32cf7c17af6fd7ad4bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * pam-ck-connector.c : PAM module for registering with CK
 *
 * Copyright (c) 2007 David Zeuthen <davidz@redhat.com>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <ctype.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

#define PAM_SM_SESSION

#include <security/pam_modules.h>
#include <security/_pam_macros.h>
#include <security/pam_modutil.h>
#include <security/pam_ext.h>

#include "ck-connector.h"

static int opt_debug = FALSE;

static void
_parse_pam_args (const pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	int i;

	opt_debug = FALSE;
	for (i = 0; i < argc && argv[i] != NULL; i++) {
		if (strcmp (argv[i] ,"debug") == 0) {
			opt_debug = TRUE;
		} else {
			pam_syslog (pamh, LOG_ERR, "unknown option: %s", argv[i]);
		}
	}
}


PAM_EXTERN int
pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return PAM_IGNORE;
}

PAM_EXTERN int 
pam_sm_setcred (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	return PAM_IGNORE;
}

static uid_t
_util_name_to_uid (const char *username, gid_t *default_gid)
{
        int rc;
        uid_t res;
        char *buf = NULL;
        unsigned int bufsize;
        struct passwd pwd;
        struct passwd *pwdp;

        res = (uid_t) -1;

        bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
        buf = calloc (sizeof (char), bufsize);
        rc = getpwnam_r (username, &pwd, buf, bufsize, &pwdp);
        if (rc != 0 || pwdp == NULL) {
                goto out;
        }

        res = pwdp->pw_uid;
        if (default_gid != NULL)
                *default_gid = pwdp->pw_gid;

out:
        free (buf);
        return res;
}

/* our singleton */
static CKConnector *ckc = NULL;

PAM_EXTERN int 
pam_sm_close_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	if (ckc != NULL) {
		ckc_free (ckc);
		ckc = NULL;
	}
	return PAM_SUCCESS;
}

PAM_EXTERN int 
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
	int ret;
	const char *user;
	const char *tty;
	const char *x11_display;
	const char *s;
	uid_t uid;
	char buf[256];

	ret = PAM_IGNORE;

	_parse_pam_args (pamh, flags, argc, argv);

	/* Register with ConsoleKit as part of the session management */
	if (ckc != NULL) {
		pam_syslog (pamh, LOG_ERR, "process already registered with ConsoleKit");
		goto out;
	}

	ckc = ckc_new ();
	if (ckc == NULL) {
		pam_syslog (pamh, LOG_ERR, "oom creating ConsoleKit connector object");
		goto out;
	}

	if (pam_get_user (pamh, &user, NULL) != PAM_SUCCESS || user == NULL) {
		pam_syslog (pamh, LOG_ERR, "cannot determine username");
		goto out;
	}

	if (pam_get_item (pamh, PAM_TTY, (const void **) &tty) != PAM_SUCCESS || tty == NULL) {
		pam_syslog (pamh, LOG_ERR, "cannot determine tty");
		goto out;
	}

	if ((s = pam_getenv (pamh, "CKCON_TTY")) != NULL) {
		tty = s;
		if (opt_debug)
			pam_syslog (pamh, LOG_INFO, "using '%s' as tty (from CKCON_TTY)", tty);
	}

	x11_display = NULL;
	if ((s = pam_getenv (pamh, "CKCON_X11_DISPLAY")) != NULL) {
		x11_display = s;
		if (opt_debug)
			pam_syslog (pamh, LOG_INFO, "using '%s' as X11 display (from CKCON_X11_DISPLAY)", x11_display);
	}

	uid = _util_name_to_uid (user, NULL);
	if (uid == (uid_t) -1) {
		pam_syslog (pamh, LOG_ERR, "cannot determine uid for user '%s'", user);
		goto out;
	}

	if (!ckc_create_local_session (ckc, uid, tty, x11_display)) {
		/* this might not be a bug for servers that don't have
		 * the message bus or ConsoleKit daemon running - so
		 * only log a message in debugging mode.
		 */
		if (opt_debug)
			pam_syslog (pamh, LOG_DEBUG, "insufficient privileges or D-Bus / ConsoleKit not available");
		goto out;
	}

	/* now set the cookie */
	buf[sizeof (buf) - 1] = '\0';
	snprintf (buf, sizeof (buf) - 1, "XDG_SESSION_COOKIE=%s", ckc_get_cookie (ckc));
	if (pam_putenv (pamh, buf) != PAM_SUCCESS) {
		pam_syslog (pamh, LOG_ERR, "unable to set XDG_SESSION_COOKIE vairable");
		/* tear down session the hard way */
		ckc_free (ckc);
		ckc = NULL;
		goto out;
	}
		    
	if (opt_debug) {
		pam_syslog (pamh, LOG_DEBUG, "registered uid=%d on tty='%s' with ConsoleKit", uid, tty);
	}

	/* note that we're leaking our CKConnector instance ckc - this
	 * is *by design* such that when the login manager (that uses
	 * us) exits / crashes / etc. ConsoleKit will notice, via D-Bus
	 * connection tracking, that the login session ended.
	 */

	ret = PAM_SUCCESS;

out:
	return ret;
}

#ifdef PAM_STATIC

struct pam_module _pam_ckconnector_modstruct = {
	"pam_ck_connector",
	pam_sm_authenticate,
	pam_sm_setcred,
	NULL,
	pam_sm_open_session,
	pam_sm_close_session,
	NULL,
};

#endif

/* end of module definition */