]> git.ozlabs.org Git - ppp.git/blob - pppd/session.c
pppd: Eliminate some more compiler warnings
[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 #ifdef HAS_SHADOW
77 #include <shadow.h>
78 #endif
79 #include <time.h>
80 #include <utmp.h>
81 #include <fcntl.h>
82 #include <unistd.h>
83 #include "pppd.h"
84 #include "session.h"
85
86 #ifdef USE_PAM
87 #include <security/pam_appl.h>
88 #endif /* #ifdef USE_PAM */
89
90 #define SET_MSG(var, msg) if (var != NULL) { var[0] = msg; }
91 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
92
93 #define SUCCESS_MSG "Session started successfully"
94 #define ABORT_MSG "Session can't be started without a username"
95 #define SERVICE_NAME "ppp"
96
97 #define SESSION_FAILED  0
98 #define SESSION_OK      1
99
100 /* We have successfully started a session */
101 static bool logged_in = 0;
102
103 #ifdef USE_PAM
104 /*
105  * Static variables used to communicate between the conversation function
106  * and the server_login function
107  */
108 static const char *PAM_username;
109 static const char *PAM_password;
110 static int   PAM_session = 0;
111 static pam_handle_t *pamh = NULL;
112
113 /* PAM conversation function
114  * Here we assume (for now, at least) that echo on means login name, and
115  * echo off means password.
116  */
117
118 static int conversation (int num_msg,
119 #ifndef SOL2
120     const
121 #endif
122     struct pam_message **msg,
123     struct pam_response **resp, void *appdata_ptr)
124 {
125     int replies = 0;
126     struct pam_response *reply = NULL;
127
128     reply = malloc(sizeof(struct pam_response) * num_msg);
129     if (!reply) return PAM_CONV_ERR;
130
131     for (replies = 0; replies < num_msg; replies++) {
132         switch (msg[replies]->msg_style) {
133             case PAM_PROMPT_ECHO_ON:
134                 reply[replies].resp_retcode = PAM_SUCCESS;
135                 reply[replies].resp = COPY_STRING(PAM_username);
136                 /* PAM frees resp */
137                 break;
138             case PAM_PROMPT_ECHO_OFF:
139                 reply[replies].resp_retcode = PAM_SUCCESS;
140                 reply[replies].resp = COPY_STRING(PAM_password);
141                 /* PAM frees resp */
142                 break;
143             case PAM_TEXT_INFO:
144                 /* fall through */
145             case PAM_ERROR_MSG:
146                 /* ignore it, but pam still wants a NULL response... */
147                 reply[replies].resp_retcode = PAM_SUCCESS;
148                 reply[replies].resp = NULL;
149                 break;
150             default:
151                 /* Must be an error of some sort... */
152                 free (reply);
153                 return PAM_CONV_ERR;
154         }
155     }
156     *resp = reply;
157     return PAM_SUCCESS;
158 }
159
160 static struct pam_conv pam_conv_data = {
161     &conversation,
162     NULL
163 };
164 #endif /* #ifdef USE_PAM */
165
166 int
167 session_start(flags, user, passwd, ttyName, msg)
168     const int flags;
169     const char *user;
170     const char *passwd;
171     const char *ttyName;
172     char **msg;
173 {
174 #ifdef USE_PAM
175     bool ok = 1;
176     const char *usr;
177     int pam_error;
178     bool try_session = 0;
179 #else /* #ifdef USE_PAM */
180     struct passwd *pw;
181     char *cbuf;
182 #ifdef HAS_SHADOW
183     struct spwd *spwd;
184     struct spwd *getspnam();
185     long now = 0;
186 #endif /* #ifdef HAS_SHADOW */
187 #endif /* #ifdef USE_PAM */
188
189     SET_MSG(msg, SUCCESS_MSG);
190
191     /* If no verification is requested, then simply return an OK */
192     if (!(SESS_ALL & flags)) {
193         return SESSION_OK;
194     }
195
196     if (user == NULL) {
197        SET_MSG(msg, ABORT_MSG);
198        return SESSION_FAILED;
199     }
200
201 #ifdef USE_PAM
202     /* Find the '\\' in the username */
203     /* This needs to be fixed to support different username schemes */
204     if ((usr = strchr(user, '\\')) == NULL)
205         usr = user;
206     else
207         usr++;
208
209     PAM_session = 0;
210     PAM_username = usr;
211     PAM_password = passwd;
212
213     dbglog("Initializing PAM (%d) for user %s", flags, usr);
214     pam_error = pam_start (SERVICE_NAME, usr, &pam_conv_data, &pamh);
215     dbglog("---> PAM INIT Result = %d", pam_error);
216     ok = (pam_error == PAM_SUCCESS);
217
218     if (ok) {
219         ok = (pam_set_item(pamh, PAM_TTY, ttyName) == PAM_SUCCESS) &&
220             (pam_set_item(pamh, PAM_RHOST, ifname) == PAM_SUCCESS);
221     }
222
223     if (ok && (SESS_AUTH & flags)) {
224         dbglog("Attempting PAM authentication");
225         pam_error = pam_authenticate (pamh, PAM_SILENT);
226         if (pam_error == PAM_SUCCESS) {
227             /* PAM auth was OK */
228             dbglog("PAM Authentication OK for %s", user);
229         } else {
230             /* No matter the reason, we fail because we're authenticating */
231             ok = 0;
232             if (pam_error == PAM_USER_UNKNOWN) {
233                 dbglog("User unknown, failing PAM authentication");
234                 SET_MSG(msg, "User unknown - cannot authenticate via PAM");
235             } else {
236                 /* Any other error means authentication was bad */
237                 dbglog("PAM Authentication failed: %d: %s", pam_error,
238                        pam_strerror(pamh, pam_error));
239                 SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
240             }
241         }
242     }
243
244     if (ok && (SESS_ACCT & flags)) {
245         dbglog("Attempting PAM account checks");
246         pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
247         if (pam_error == PAM_SUCCESS) {
248             /*
249              * PAM account was OK, set the flag which indicates that we should
250              * try to perform the session checks.
251              */
252             try_session = 1;
253             dbglog("PAM Account OK for %s", user);
254         } else {
255             /*
256              * If the account checks fail, then we should not try to perform
257              * the session check, because they don't make sense.
258              */
259             try_session = 0;
260             if (pam_error == PAM_USER_UNKNOWN) {
261                 /*
262                  * We're checking the account, so it's ok to not have one
263                  * because the user might come from the secrets files, or some
264                  * other plugin.
265                  */
266                 dbglog("User unknown, ignoring PAM restrictions");
267                 SET_MSG(msg, "User unknown - ignoring PAM restrictions");
268             } else {
269                 /* Any other error means session is rejected */
270                 ok = 0;
271                 dbglog("PAM Account checks failed: %d: %s", pam_error,
272                        pam_strerror(pamh, pam_error));
273                 SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
274             }
275         }
276     }
277
278     if (ok && try_session && (SESS_ACCT & flags)) {
279         /* Only open a session if the user's account was found */
280         pam_error = pam_open_session (pamh, PAM_SILENT);
281         if (pam_error == PAM_SUCCESS) {
282             dbglog("PAM Session opened for user %s", user);
283             PAM_session = 1;
284         } else {
285             dbglog("PAM Session denied for user %s", user);
286             SET_MSG(msg, (char *) pam_strerror (pamh, pam_error));
287             ok = 0;
288         }
289     }
290
291     /* This is needed because apparently the PAM stuff closes the log */
292     reopen_log();
293
294     /* If our PAM checks have already failed, then we must return a failure */
295     if (!ok) return SESSION_FAILED;
296
297 #else /* #ifdef USE_PAM */
298
299 /*
300  * Use the non-PAM methods directly.  'pw' will remain NULL if the user
301  * has not been authenticated using local UNIX system services.
302  */
303
304     pw = NULL;
305     if ((SESS_AUTH & flags)) {
306         pw = getpwnam(user);
307
308         endpwent();
309         /*
310          * Here, we bail if we have no user account, because there is nothing
311          * to verify against.
312          */
313         if (pw == NULL)
314             return SESSION_FAILED;
315
316 #ifdef HAS_SHADOW
317
318         spwd = getspnam(user);
319         endspent();
320
321         /*
322          * If there is no shadow entry for the user, then we can't verify the
323          * account.
324          */
325         if (spwd == NULL)
326             return SESSION_FAILED;
327
328         /*
329          * We check validity all the time, because if the password has expired,
330          * then clearly we should not authenticate against it (if we're being
331          * called for authentication only).  Thus, in this particular instance,
332          * there is no real difference between using the AUTH, SESS or ACCT
333          * flags, or combinations thereof.
334          */
335         now = time(NULL) / 86400L;
336         if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
337             || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
338             && spwd->sp_lstchg >= 0
339             && now >= spwd->sp_lstchg + spwd->sp_max)) {
340             warn("Password for %s has expired", user);
341             return SESSION_FAILED;
342         }
343
344         /* We have a valid shadow entry, keep the password */
345         pw->pw_passwd = spwd->sp_pwdp;
346
347 #endif /* #ifdef HAS_SHADOW */
348
349         /*
350          * If no passwd, don't let them login if we're authenticating.
351          */
352         if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2)
353             return SESSION_FAILED;
354         cbuf = crypt(passwd, pw->pw_passwd);
355         if (!cbuf || strcmp(cbuf, pw->pw_passwd) != 0)
356             return SESSION_FAILED;
357     }
358
359 #endif /* #ifdef USE_PAM */
360
361     /*
362      * Write a wtmp entry for this user.
363      */
364
365     if (SESS_ACCT & flags) {
366         if (strncmp(ttyName, "/dev/", 5) == 0)
367             ttyName += 5;
368         logwtmp(ttyName, user, ifname); /* Add wtmp login entry */
369         logged_in = 1;
370
371 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
372         /*
373          * Enter the user in lastlog only if he has been authenticated using
374          * local system services.  If he has not, then we don't know what his
375          * UID might be, and lastlog is indexed by UID.
376          */
377         if (pw != NULL) {
378             struct lastlog ll;
379             int fd;
380             time_t tnow;
381
382             if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
383                 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
384                 memset((void *)&ll, 0, sizeof(ll));
385                 (void)time(&tnow);
386                 ll.ll_time = tnow;
387                 strlcpy(ll.ll_line, ttyName, sizeof(ll.ll_line));
388                 strlcpy(ll.ll_host, ifname, sizeof(ll.ll_host));
389                 (void)write(fd, (char *)&ll, sizeof(ll));
390                 (void)close(fd);
391             }
392         }
393 #endif /* _PATH_LASTLOG and not USE_PAM */
394         info("user %s logged in on tty %s intf %s", user, ttyName, ifname);
395     }
396
397     return SESSION_OK;
398 }
399
400 /*
401  * session_end - Logout the user.
402  */
403 void
404 session_end(const char* ttyName)
405 {
406 #ifdef USE_PAM
407     int pam_error = PAM_SUCCESS;
408
409     if (pamh != NULL) {
410         if (PAM_session) pam_error = pam_close_session (pamh, PAM_SILENT);
411         PAM_session = 0;
412         pam_end (pamh, pam_error);
413         pamh = NULL;
414         /* Apparently the pam stuff does closelog(). */
415         reopen_log();
416     }
417 #endif
418     if (logged_in) {
419         if (strncmp(ttyName, "/dev/", 5) == 0)
420             ttyName += 5;
421         logwtmp(ttyName, "", ""); /* Wipe out utmp logout entry */
422         logged_in = 0;
423     }
424 }