]> git.ozlabs.org Git - ppp.git/blob - pppd/session.c
05dcb769994190879741a83dca51fe952113ad6d
[ppp.git] / pppd / session.c
1 /*
2  * session.c - PPP session control.
3  *
4  * Copyright (c) 2007 Diego Rivera. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. The name(s) of the authors of this software must not be used to
14  *    endorse or promote products derived from this software without
15  *    prior written permission.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  *    acknowledgment:
19  *    "This product includes software developed by Paul Mackerras
20  *     <paulus@samba.org>".
21  *
22  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29  *
30  * Derived from auth.c, which is:
31  *
32  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  *
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *
46  * 3. The name "Carnegie Mellon University" must not be used to
47  *    endorse or promote products derived from this software without
48  *    prior written permission. For permission or any legal
49  *    details, please contact
50  *      Office of Technology Transfer
51  *      Carnegie Mellon University
52  *      5000 Forbes Avenue
53  *      Pittsburgh, PA  15213-3890
54  *      (412) 268-4387, fax: (412) 268-7395
55  *      tech-transfer@andrew.cmu.edu
56  *
57  * 4. Redistributions of any form whatsoever must retain the following
58  *    acknowledgment:
59  *    "This product includes software developed by Computing Services
60  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
61  *
62  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
63  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
65  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
69  */
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <pwd.h>
75 #include <crypt.h>
76 #include "pppd.h"
77 #include "session.h"
78
79 #ifdef USE_PAM
80 #include <security/pam_appl.h>
81 #endif /* #ifdef USE_PAM */
82
83 #define SET_MSG(var, msg) if (var != NULL) { var[0] = msg; }
84 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
85
86 #define SUCCESS_MSG "Session started successfully"
87 #define ABORT_MSG "Session can't be started without a username"
88 #define SERVICE_NAME "ppp"
89
90 #define SESSION_FAILED  0
91 #define SESSION_OK      1
92
93 /* We have successfully started a session */
94 static bool logged_in = 0;
95
96 #ifdef USE_PAM
97 /*
98  * Static variables used to communicate between the conversation function
99  * and the server_login function
100  */
101 static const char *PAM_username;
102 static const char *PAM_password;
103 static int   PAM_session = 0;
104 static pam_handle_t *pamh = NULL;
105
106 /* PAM conversation function
107  * Here we assume (for now, at least) that echo on means login name, and
108  * echo off means password.
109  */
110
111 static int conversation (int num_msg,
112 #ifndef SOL2
113     const
114 #endif
115     struct pam_message **msg,
116     struct pam_response **resp, void *appdata_ptr)
117 {
118     int replies = 0;
119     struct pam_response *reply = NULL;
120
121     reply = malloc(sizeof(struct pam_response) * num_msg);
122     if (!reply) return PAM_CONV_ERR;
123
124     for (replies = 0; replies < num_msg; replies++) {
125         switch (msg[replies]->msg_style) {
126             case PAM_PROMPT_ECHO_ON:
127                 reply[replies].resp_retcode = PAM_SUCCESS;
128                 reply[replies].resp = COPY_STRING(PAM_username);
129                 /* PAM frees resp */
130                 break;
131             case PAM_PROMPT_ECHO_OFF:
132                 reply[replies].resp_retcode = PAM_SUCCESS;
133                 reply[replies].resp = COPY_STRING(PAM_password);
134                 /* PAM frees resp */
135                 break;
136             case PAM_TEXT_INFO:
137                 /* fall through */
138             case PAM_ERROR_MSG:
139                 /* ignore it, but pam still wants a NULL response... */
140                 reply[replies].resp_retcode = PAM_SUCCESS;
141                 reply[replies].resp = NULL;
142                 break;
143             default:
144                 /* Must be an error of some sort... */
145                 free (reply);
146                 return PAM_CONV_ERR;
147         }
148     }
149     *resp = reply;
150     return PAM_SUCCESS;
151 }
152
153 static struct pam_conv pam_conv_data = {
154     &conversation,
155     NULL
156 };
157 #endif /* #ifdef USE_PAM */
158
159 int
160 session_start(flags, user, passwd, ttyName, msg)
161     const int flags;
162     const char *user;
163     const char *passwd;
164     const char *ttyName;
165     char **msg;
166 {
167     bool ok = 1;
168 #ifdef USE_PAM
169     const char *usr;
170     int pam_error;
171     bool try_session = 0;
172 #else /* #ifdef USE_PAM */
173     struct passwd *pw;
174 #ifdef HAS_SHADOW
175     struct spwd *spwd;
176     struct spwd *getspnam();
177     long now = 0;
178 #endif /* #ifdef HAS_SHADOW */
179 #endif /* #ifdef USE_PAM */
180
181     SET_MSG(msg, SUCCESS_MSG);
182
183     /* If no verification is requested, then simply return an OK */
184     if (!(SESS_ALL & flags)) {
185         return SESSION_OK;
186     }
187
188     if (user == NULL) {
189        SET_MSG(msg, ABORT_MSG);
190        return SESSION_FAILED;
191     }
192
193 #ifdef USE_PAM
194     /* Find the '\\' in the username */
195     /* This needs to be fixed to support different username schemes */
196     if ((usr = strchr(user, '\\')) == NULL)
197         usr = user;
198     else
199         usr++;
200
201     PAM_session = 0;
202     PAM_username = usr;
203     PAM_password = passwd;
204
205     dbglog("Initializing PAM (%d) for user %s", flags, usr);
206     pam_error = pam_start (SERVICE_NAME, usr, &pam_conv_data, &pamh);
207     dbglog("---> PAM INIT Result = %d", pam_error);
208     ok = (pam_error == PAM_SUCCESS);
209
210     if (ok) {
211         ok = (pam_set_item(pamh, PAM_TTY, ttyName) == PAM_SUCCESS) &&
212             (pam_set_item(pamh, PAM_RHOST, ifname) == PAM_SUCCESS);
213     }
214
215     if (ok && (SESS_AUTH & flags)) {
216         dbglog("Attempting PAM authentication");
217         pam_error = pam_authenticate (pamh, PAM_SILENT);
218         if (pam_error == PAM_SUCCESS) {
219             /* PAM auth was OK */
220             dbglog("PAM Authentication OK for %s", user);
221         } else {
222             /* No matter the reason, we fail because we're authenticating */
223             ok = 0;
224             if (pam_error == PAM_USER_UNKNOWN) {
225                 dbglog("User unknown, failing PAM authentication");
226                 SET_MSG(msg, "User unknown - cannot authenticate via PAM");
227             } else {
228                 /* Any other error means authentication was bad */
229                 dbglog("PAM Authentication failed: %d: %s", pam_error,
230                        pam_strerror(pamh, pam_error));
231                 SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
232             }
233         }
234     }
235
236     if (ok && (SESS_ACCT & flags)) {
237         dbglog("Attempting PAM account checks");
238         pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
239         if (pam_error == PAM_SUCCESS) {
240             /*
241              * PAM account was OK, set the flag which indicates that we should
242              * try to perform the session checks.
243              */
244             try_session = 1;
245             dbglog("PAM Account OK for %s", user);
246         } else {
247             /*
248              * If the account checks fail, then we should not try to perform
249              * the session check, because they don't make sense.
250              */
251             try_session = 0;
252             if (pam_error == PAM_USER_UNKNOWN) {
253                 /*
254                  * We're checking the account, so it's ok to not have one
255                  * because the user might come from the secrets files, or some
256                  * other plugin.
257                  */
258                 dbglog("User unknown, ignoring PAM restrictions");
259                 SET_MSG(msg, "User unknown - ignoring PAM restrictions");
260             } else {
261                 /* Any other error means session is rejected */
262                 ok = 0;
263                 dbglog("PAM Account checks failed: %d: %s", pam_error,
264                        pam_strerror(pamh, pam_error));
265                 SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
266             }
267         }
268     }
269
270     if (ok && try_session && (SESS_ACCT & flags)) {
271         /* Only open a session if the user's account was found */
272         pam_error = pam_open_session (pamh, PAM_SILENT);
273         if (pam_error == PAM_SUCCESS) {
274             dbglog("PAM Session opened for user %s", user);
275             PAM_session = 1;
276         } else {
277             dbglog("PAM Session denied for user %s", user);
278             SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
279             ok = 0;
280         }
281     }
282
283     /* This is needed because apparently the PAM stuff closes the log */
284     reopen_log();
285
286     /* If our PAM checks have already failed, then we must return a failure */
287     if (!ok) return SESSION_FAILED;
288
289 #else /* #ifdef USE_PAM */
290
291 /*
292  * Use the non-PAM methods directly
293  */
294
295     if ((SESS_AUTH & flags)) {
296         pw = getpwnam(user);
297
298         endpwent();
299         /*
300          * Here, we bail if we have no user account, because there is nothing
301          * to verify against.
302          */
303         if (pw == NULL)
304             return SESSION_FAILED;
305
306 #ifdef HAS_SHADOW
307
308         spwd = getspnam(user);
309         endspent();
310
311         /*
312          * If there is no shadow entry for the user, then we can't verify the
313          * account.
314          */
315         if (spwd == NULL)
316             return SESSION_FAILED;
317
318         /*
319          * We check validity all the time, because if the password has expired,
320          * then clearly we should not authenticate against it (if we're being
321          * called for authentication only).  Thus, in this particular instance,
322          * there is no real difference between using the AUTH, SESS or ACCT
323          * flags, or combinations thereof.
324          */
325         now = time(NULL) / 86400L;
326         if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
327             || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
328             && spwd->sp_lstchg >= 0
329             && now >= spwd->sp_lstchg + spwd->sp_max)) {
330             warn("Password for %s has expired", user);
331             return SESSION_FAILED;
332         }
333
334         /* We have a valid shadow entry, keep the password */
335         pw->pw_passwd = spwd->sp_pwdp;
336
337 #endif /* #ifdef HAS_SHADOW */
338
339         /*
340          * If no passwd, don't let them login if we're authenticating.
341          */
342         if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
343             || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
344             return SESSION_FAILED;
345     }
346
347 #endif /* #ifdef USE_PAM */
348
349     /*
350      * Write a wtmp entry for this user.
351      */
352
353     if (SESS_ACCT & flags) {
354         if (strncmp(ttyName, "/dev/", 5) == 0)
355             ttyName += 5;
356         logwtmp(ttyName, user, ifname); /* Add wtmp login entry */
357         logged_in = 1;
358
359 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
360         {
361             struct lastlog ll;
362             int fd;
363
364             if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
365                 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
366                 memset((void *)&ll, 0, sizeof(ll));
367                 (void)time(&ll.ll_time);
368                 (void)strncpy(ll.ll_line, ttyName, sizeof(ll.ll_line));
369                 (void)strncpy(ll.ll_host, ifname, sizeof(ll.ll_host));
370                 (void)write(fd, (char *)&ll, sizeof(ll));
371                 (void)close(fd);
372             }
373         }
374 #endif /* _PATH_LASTLOG and not USE_PAM */
375         info("user %s logged in on tty %s intf %s", user, ttyName, ifname);
376     }
377
378     return SESSION_OK;
379 }
380
381 /*
382  * session_end - Logout the user.
383  */
384 void
385 session_end(const char* ttyName)
386 {
387 #ifdef USE_PAM
388     int pam_error = PAM_SUCCESS;
389
390     if (pamh != NULL) {
391         if (PAM_session) pam_error = pam_close_session (pamh, PAM_SILENT);
392         PAM_session = 0;
393         pam_end (pamh, pam_error);
394         pamh = NULL;
395         /* Apparently the pam stuff does closelog(). */
396         reopen_log();
397     }
398 #endif
399     if (logged_in) {
400         if (strncmp(ttyName, "/dev/", 5) == 0)
401             ttyName += 5;
402         logwtmp(ttyName, "", ""); /* Wipe out utmp logout entry */
403         logged_in = 0;
404     }
405 }