- Add an entry for iwl(4)
[dragonfly.git] / usr.sbin / pppd / auth.c
blob48f1c13dcd294ab64f077fe19cdf73316a8ff818
1 /*
2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993 The Australian National University.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Copyright (c) 1989 Carnegie Mellon University.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by Carnegie Mellon University. The name of the
28 * University may not be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34 * $FreeBSD: src/usr.sbin/pppd/auth.c,v 1.24.2.2 2002/03/12 08:55:22 maxim Exp $
35 * $DragonFly: src/usr.sbin/pppd/auth.c,v 1.5 2005/11/24 23:42:54 swildner Exp $
38 #include <stdio.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <syslog.h>
43 #include <paths.h>
44 #include <pwd.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/socket.h>
49 #include <utmp.h>
50 #include <fcntl.h>
51 #if defined(_PATH_LASTLOG) && defined(_linux_)
52 #include <lastlog.h>
53 #endif
55 #include <netdb.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <sys/time.h>
59 #include <utmp.h>
61 #ifdef USE_PAM
62 #include <security/pam_appl.h>
63 #endif
65 #ifdef HAS_SHADOW
66 #include <shadow.h>
67 #ifndef PW_PPP
68 #define PW_PPP PW_LOGIN
69 #endif
70 #endif
72 #include "pppd.h"
73 #include "fsm.h"
74 #include "lcp.h"
75 #include "ipcp.h"
76 #include "upap.h"
77 #include "chap.h"
78 #ifdef CBCP_SUPPORT
79 #include "cbcp.h"
80 #endif
81 #include "pathnames.h"
83 /* Used for storing a sequence of words. Usually malloced. */
84 struct wordlist {
85 struct wordlist *next;
86 char word[1];
89 /* Bits in scan_authfile return value */
90 #define NONWILD_SERVER 1
91 #define NONWILD_CLIENT 2
93 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
95 #define FALSE 0
96 #define TRUE 1
98 /* The name by which the peer authenticated itself to us. */
99 char peer_authname[MAXNAMELEN];
101 /* Records which authentication operations haven't completed yet. */
102 static int auth_pending[NUM_PPP];
104 /* Set if we have successfully called plogin() */
105 static int logged_in;
107 /* Set if not wild or blank */
108 static int non_wildclient;
110 /* Set if we have run the /etc/ppp/auth-up script. */
111 static int did_authup;
113 /* List of addresses which the peer may use. */
114 static struct wordlist *addresses[NUM_PPP];
116 /* Number of network protocols which we have opened. */
117 static int num_np_open;
119 /* Number of network protocols which have come up. */
120 static int num_np_up;
122 /* Set if we got the contents of passwd[] from the pap-secrets file. */
123 static int passwd_from_file;
125 /* Bits in auth_pending[] */
126 #define PAP_WITHPEER 1
127 #define PAP_PEER 2
128 #define CHAP_WITHPEER 4
129 #define CHAP_PEER 8
131 extern char *crypt(const char *, const char *);
133 /* Prototypes for procedures local to this file. */
135 static void network_phase(int);
136 static void check_idle(void *);
137 static void connect_time_expired(void *);
138 static int plogin(char *, char *, char **, int *);
139 static void plogout(void);
140 static int null_login(int);
141 static int get_pap_passwd(char *);
142 static int have_pap_secret(void);
143 static int have_chap_secret(char *, char *, u_int32_t);
144 static int ip_addr_check(u_int32_t, struct wordlist *);
145 static int scan_authfile(FILE *, char *, char *, u_int32_t, char *,
146 struct wordlist **, char *);
147 static void free_wordlist(struct wordlist *);
148 static void auth_set_ip_addr(int);
149 static void auth_script(char *);
150 static void set_allowed_addrs(int, struct wordlist *);
153 * An Open on LCP has requested a change from Dead to Establish phase.
154 * Do what's necessary to bring the physical layer up.
156 void
157 link_required(int unit)
162 * LCP has terminated the link; go to the Dead phase and take the
163 * physical layer down.
165 void
166 link_terminated(int unit)
168 extern time_t etime, stime;
169 extern int minutes;
171 if (phase == PHASE_DEAD)
172 return;
173 if (logged_in)
174 plogout();
175 phase = PHASE_DEAD;
176 etime = time((time_t *) NULL);
177 minutes = (etime-stime)/60;
178 syslog(LOG_NOTICE, "Connection terminated, connected for %d minutes\n",
179 minutes > 1 ? minutes : 1);
183 * LCP has gone down; it will either die or try to re-establish.
185 void
186 link_down(int unit)
188 int i;
189 struct protent *protp;
191 if (did_authup) {
192 auth_script(_PATH_AUTHDOWN);
193 did_authup = 0;
195 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
196 if (!protp->enabled_flag)
197 continue;
198 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
199 (*protp->lowerdown)(unit);
200 if (protp->protocol < 0xC000 && protp->close != NULL)
201 (*protp->close)(unit, "LCP down");
203 num_np_open = 0;
204 num_np_up = 0;
205 if (phase != PHASE_DEAD)
206 phase = PHASE_TERMINATE;
210 * The link is established.
211 * Proceed to the Dead, Authenticate or Network phase as appropriate.
213 void
214 link_established(int unit)
216 int auth;
217 lcp_options *wo = &lcp_wantoptions[unit];
218 lcp_options *go = &lcp_gotoptions[unit];
219 lcp_options *ho = &lcp_hisoptions[unit];
220 int i;
221 struct protent *protp;
224 * Tell higher-level protocols that LCP is up.
226 for (i = 0; (protp = protocols[i]) != NULL; ++i)
227 if (protp->protocol != PPP_LCP && protp->enabled_flag
228 && protp->lowerup != NULL)
229 (*protp->lowerup)(unit);
231 if (auth_required && !(go->neg_chap || go->neg_upap)) {
233 * We wanted the peer to authenticate itself, and it refused:
234 * treat it as though it authenticated with PAP using a username
235 * of "" and a password of "". If that's not OK, boot it out.
237 if (!wo->neg_upap || !null_login(unit)) {
238 syslog(LOG_WARNING, "peer refused to authenticate");
239 lcp_close(unit, "peer refused to authenticate");
240 return;
244 phase = PHASE_AUTHENTICATE;
245 auth = 0;
246 if (go->neg_chap) {
247 ChapAuthPeer(unit, our_name, go->chap_mdtype);
248 auth |= CHAP_PEER;
249 } else if (go->neg_upap) {
250 upap_authpeer(unit);
251 auth |= PAP_PEER;
253 if (ho->neg_chap) {
254 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
255 auth |= CHAP_WITHPEER;
256 } else if (ho->neg_upap) {
257 if (passwd[0] == 0) {
258 passwd_from_file = 1;
259 if (!get_pap_passwd(passwd))
260 syslog(LOG_ERR, "No secret found for PAP login");
262 upap_authwithpeer(unit, user, passwd);
263 auth |= PAP_WITHPEER;
265 auth_pending[unit] = auth;
267 if (!auth)
268 network_phase(unit);
272 * Proceed to the network phase.
274 static void
275 network_phase(int unit)
277 int i;
278 struct protent *protp;
279 lcp_options *go = &lcp_gotoptions[unit];
282 * If the peer had to authenticate, run the auth-up script now.
284 if ((go->neg_chap || go->neg_upap) && !did_authup) {
285 auth_script(_PATH_AUTHUP);
286 did_authup = 1;
289 #ifdef CBCP_SUPPORT
291 * If we negotiated callback, do it now.
293 if (go->neg_cbcp) {
294 phase = PHASE_CALLBACK;
295 (*cbcp_protent.open)(unit);
296 return;
298 #endif
300 phase = PHASE_NETWORK;
301 #if 0
302 if (!demand)
303 set_filters(&pass_filter, &active_filter);
304 #endif
305 for (i = 0; (protp = protocols[i]) != NULL; ++i)
306 if (protp->protocol < 0xC000 && protp->enabled_flag
307 && protp->open != NULL) {
308 (*protp->open)(unit);
309 if (protp->protocol != PPP_CCP)
310 ++num_np_open;
313 if (num_np_open == 0)
314 /* nothing to do */
315 lcp_close(0, "No network protocols running");
319 * The peer has failed to authenticate himself using `protocol'.
321 void
322 auth_peer_fail(int unit, int protocol)
325 * Authentication failure: take the link down
327 lcp_close(unit, "Authentication failed");
331 * The peer has been successfully authenticated using `protocol'.
333 void
334 auth_peer_success(int unit, int protocol, char *name, int namelen)
336 int bit;
338 switch (protocol) {
339 case PPP_CHAP:
340 bit = CHAP_PEER;
341 break;
342 case PPP_PAP:
343 bit = PAP_PEER;
344 break;
345 default:
346 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
347 protocol);
348 return;
352 * Save the authenticated name of the peer for later.
354 if (namelen > sizeof(peer_authname) - 1)
355 namelen = sizeof(peer_authname) - 1;
356 BCOPY(name, peer_authname, namelen);
357 peer_authname[namelen] = 0;
360 * If we have overridden addresses based on auth info
361 * then set that information now before continuing.
363 auth_set_ip_addr(unit);
365 script_setenv("PEERNAME", peer_authname);
368 * If there is no more authentication still to be done,
369 * proceed to the network (or callback) phase.
371 if ((auth_pending[unit] &= ~bit) == 0)
372 network_phase(unit);
376 * We have failed to authenticate ourselves to the peer using `protocol'.
378 void
379 auth_withpeer_fail(int unit, int protocol)
381 if (passwd_from_file)
382 BZERO(passwd, MAXSECRETLEN);
384 * We've failed to authenticate ourselves to our peer.
385 * He'll probably take the link down, and there's not much
386 * we can do except wait for that.
391 * We have successfully authenticated ourselves with the peer using `protocol'.
393 void
394 auth_withpeer_success(int unit, int protocol)
396 int bit;
398 switch (protocol) {
399 case PPP_CHAP:
400 bit = CHAP_WITHPEER;
401 break;
402 case PPP_PAP:
403 if (passwd_from_file)
404 BZERO(passwd, MAXSECRETLEN);
405 bit = PAP_WITHPEER;
406 break;
407 default:
408 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
409 protocol);
410 bit = 0;
414 * If we have overridden addresses based on auth info
415 * then set that information now before continuing.
417 auth_set_ip_addr(unit);
420 * If there is no more authentication still being done,
421 * proceed to the network (or callback) phase.
423 if ((auth_pending[unit] &= ~bit) == 0)
424 network_phase(unit);
429 * np_up - a network protocol has come up.
431 void
432 np_up(int unit, int proto)
434 if (num_np_up == 0) {
436 * At this point we consider that the link has come up successfully.
438 need_holdoff = 0;
440 if (idle_time_limit > 0)
441 TIMEOUT(check_idle, NULL, idle_time_limit);
444 * Set a timeout to close the connection once the maximum
445 * connect time has expired.
447 if (maxconnect > 0)
448 TIMEOUT(connect_time_expired, 0, maxconnect);
451 * Detach now, if the updetach option was given.
453 if (nodetach == -1)
454 detach();
456 ++num_np_up;
460 * np_down - a network protocol has gone down.
462 void
463 np_down(int unit, int proto)
465 if (--num_np_up == 0 && idle_time_limit > 0) {
466 UNTIMEOUT(check_idle, NULL);
471 * np_finished - a network protocol has finished using the link.
473 void
474 np_finished(int unit, int proto)
476 if (--num_np_open <= 0) {
477 /* no further use for the link: shut up shop. */
478 lcp_close(0, "No network protocols running");
483 * check_idle - check whether the link has been idle for long
484 * enough that we can shut it down.
486 static void
487 check_idle(void *arg)
489 struct ppp_idle idle;
490 time_t itime;
492 if (!get_idle_time(0, &idle))
493 return;
494 itime = MIN(idle.xmit_idle, idle.recv_idle);
495 if (itime >= idle_time_limit) {
496 /* link is idle: shut it down. */
497 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
498 lcp_close(0, "Link inactive");
499 } else {
500 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
505 * connect_time_expired - log a message and close the connection.
507 static void
508 connect_time_expired(void *arg)
510 syslog(LOG_INFO, "Connect time expired");
511 lcp_close(0, "Connect time expired"); /* Close connection */
515 * auth_check_options - called to check authentication options.
517 void
518 auth_check_options(void)
520 lcp_options *wo = &lcp_wantoptions[0];
521 int can_auth;
522 ipcp_options *ipwo = &ipcp_wantoptions[0];
523 u_int32_t remote;
525 /* Default our_name to hostname, and user to our_name */
526 if (our_name[0] == 0 || usehostname)
527 strcpy(our_name, hostname);
528 if (user[0] == 0)
529 strcpy(user, our_name);
531 /* If authentication is required, ask peer for CHAP or PAP. */
532 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
533 wo->neg_chap = 1;
534 wo->neg_upap = 1;
538 * Check whether we have appropriate secrets to use
539 * to authenticate the peer.
541 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
542 if (!can_auth && wo->neg_chap) {
543 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
544 can_auth = have_chap_secret(remote_name, our_name, remote);
547 if (auth_required && !can_auth) {
548 option_error("peer authentication required but no suitable secret(s) found\n");
549 if (remote_name[0] == 0)
550 option_error("for authenticating any peer to us (%s)\n", our_name);
551 else
552 option_error("for authenticating peer %s to us (%s)\n",
553 remote_name, our_name);
554 exit(1);
558 * Check whether the user tried to override certain values
559 * set by root.
561 if (!auth_required && auth_req_info.priv > 0) {
562 if (!default_device && devnam_info.priv == 0) {
563 option_error("can't override device name when noauth option used");
564 exit(1);
566 if ((connector != NULL && connector_info.priv == 0)
567 || (disconnector != NULL && disconnector_info.priv == 0)
568 || (welcomer != NULL && welcomer_info.priv == 0)) {
569 option_error("can't override connect, disconnect or welcome");
570 option_error("option values when noauth option used");
571 exit(1);
577 * auth_reset - called when LCP is starting negotiations to recheck
578 * authentication options, i.e. whether we have appropriate secrets
579 * to use for authenticating ourselves and/or the peer.
581 void
582 auth_reset(int unit)
584 lcp_options *go = &lcp_gotoptions[unit];
585 lcp_options *ao = &lcp_allowoptions[0];
586 ipcp_options *ipwo = &ipcp_wantoptions[0];
587 u_int32_t remote;
589 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
590 ao->neg_chap = !refuse_chap
591 && have_chap_secret(user, remote_name, (u_int32_t)0);
593 if (go->neg_upap && !uselogin && !have_pap_secret())
594 go->neg_upap = 0;
595 if (go->neg_chap) {
596 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
597 if (!have_chap_secret(remote_name, our_name, remote))
598 go->neg_chap = 0;
604 * check_passwd - Check the user name and passwd against the PAP secrets
605 * file. If requested, also check against the system password database,
606 * and login the user if OK.
608 * returns:
609 * UPAP_AUTHNAK: Authentication failed.
610 * UPAP_AUTHACK: Authentication succeeded.
611 * In either case, msg points to an appropriate message.
614 check_passwd(int unit, char *auser, int userlen, char *apasswd, int passwdlen,
615 char **msg, int *msglen)
617 int ret;
618 char *filename;
619 FILE *f;
620 struct wordlist *addrs;
621 u_int32_t remote;
622 ipcp_options *ipwo = &ipcp_wantoptions[unit];
623 char passwd[256], user[256];
624 char secret[MAXWORDLEN];
625 static int attempts = 0;
626 int len;
629 * Make copies of apasswd and auser, then null-terminate them.
631 len = MIN(passwdlen, sizeof(passwd) - 1);
632 BCOPY(apasswd, passwd, len);
633 passwd[len] = '\0';
634 len = MIN(userlen, sizeof(user) - 1);
635 BCOPY(auser, user, len);
636 user[len] = '\0';
637 *msg = (char *) 0;
640 * Open the file of pap secrets and scan for a suitable secret
641 * for authenticating this user.
643 filename = _PATH_UPAPFILE;
644 addrs = NULL;
645 ret = UPAP_AUTHACK;
646 f = fopen(filename, "r");
647 if (f == NULL) {
648 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
649 ret = UPAP_AUTHNAK;
651 } else {
652 check_access(f, filename);
653 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
654 if (scan_authfile(f, user, our_name, remote,
655 secret, &addrs, filename) < 0) {
656 warn("no PAP secret found for %s", user);
657 } else {
658 if (secret[0] != 0) {
659 /* password given in pap-secrets - must match */
660 if ((cryptpap || strcmp(passwd, secret) != 0)
661 && strcmp(crypt(passwd, secret), secret) != 0) {
662 ret = UPAP_AUTHNAK;
663 warn("PAP authentication failure for %s", user);
667 fclose(f);
670 if (uselogin && ret == UPAP_AUTHACK) {
671 ret = plogin(user, passwd, msg, msglen);
672 if (ret == UPAP_AUTHNAK) {
673 syslog(LOG_WARNING, "PAP login failure for %s", user);
677 if (ret == UPAP_AUTHNAK) {
678 if (*msg == (char *) 0)
679 *msg = "Login incorrect";
680 *msglen = strlen(*msg);
682 * Frustrate passwd stealer programs.
683 * Allow 10 tries, but start backing off after 3 (stolen from login).
684 * On 10'th, drop the connection.
686 if (attempts++ >= 10) {
687 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
688 attempts, devnam, user);
689 quit();
691 if (attempts > 3)
692 sleep((u_int) (attempts - 3) * 5);
693 if (addrs != NULL)
694 free_wordlist(addrs);
696 } else {
697 attempts = 0; /* Reset count */
698 if (*msg == (char *) 0)
699 *msg = "Login ok";
700 *msglen = strlen(*msg);
701 set_allowed_addrs(unit, addrs);
704 BZERO(passwd, sizeof(passwd));
705 BZERO(secret, sizeof(secret));
707 return ret;
711 * Check if an "entry" is in the file "fname" - used by ppplogin.
712 * Taken from libexec/ftpd/ftpd.c
713 * Returns: 0 if not found, 1 if found, 2 if file can't be opened for reading.
715 static int
716 checkfile(char *fname, char *name)
718 FILE *fd;
719 int found = 0;
720 char *p, line[BUFSIZ];
722 if ((fd = fopen(fname, "r")) != NULL) {
723 while (fgets(line, sizeof(line), fd) != NULL)
724 if ((p = strchr(line, '\n')) != NULL) {
725 *p = '\0';
726 if (line[0] == '#')
727 continue;
728 if (strcmp(line, name) == 0) {
729 found = 1;
730 break;
733 fclose(fd);
734 } else {
735 return(2);
737 return (found);
741 * This function is needed for PAM.
744 #ifdef USE_PAM
745 static char *PAM_username = "";
746 static char *PAM_password = "";
748 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
749 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
750 #else
751 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
752 #endif
754 static int pam_conv (int num_msg,
755 const struct pam_message **msg,
756 struct pam_response **resp,
757 void *appdata_ptr)
759 int count = 0, replies = 0;
760 struct pam_response *reply = NULL;
761 int size = 0;
763 for (count = 0; count < num_msg; count++)
765 size += sizeof (struct pam_response);
766 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
767 if (!reply)
768 return PAM_CONV_ERR;
770 switch (msg[count]->msg_style)
772 case PAM_PROMPT_ECHO_ON:
773 reply[replies].resp_retcode = PAM_SUCCESS;
774 reply[replies++].resp = strdup(PAM_username); /* never NULL */
775 break;
777 case PAM_PROMPT_ECHO_OFF:
778 reply[replies].resp_retcode = PAM_SUCCESS;
779 reply[replies++].resp = strdup(PAM_password); /* never NULL */
780 break;
782 case PAM_TEXT_INFO:
783 reply[replies].resp_retcode = PAM_SUCCESS;
784 reply[replies++].resp = NULL;
785 break;
787 case PAM_ERROR_MSG:
788 default:
789 free (reply);
790 return PAM_CONV_ERR;
794 if (resp)
795 *resp = reply;
796 else
797 free (reply);
799 return PAM_SUCCESS;
801 #endif
804 * plogin - Check the user name and password against the system
805 * password database, and login the user if OK.
807 * returns:
808 * UPAP_AUTHNAK: Login failed.
809 * UPAP_AUTHACK: Login succeeded.
810 * In either case, msg points to an appropriate message.
812 * UPAP_AUTHACK should only be returned *after* wtmp and utmp are updated.
815 static int
816 plogin(char *user, char *passwd, char **msg, int *msglen)
819 #ifdef USE_PAM
821 struct pam_conv pam_conversation;
822 pam_handle_t *pamh;
823 int pam_error;
825 * Fill the pam_conversion structure
827 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
828 pam_conversation.conv = &pam_conv;
830 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
832 if (pam_error != PAM_SUCCESS) {
833 *msg = MY_PAM_STRERROR (pam_error);
834 return UPAP_AUTHNAK;
837 * Define the fields for the credintial validation
839 pam_set_item (pamh, PAM_TTY, devnam);
840 PAM_username = user;
841 PAM_password = passwd;
843 * Validate the user
845 pam_error = pam_authenticate (pamh, PAM_SILENT);
846 if (pam_error == PAM_SUCCESS) {
847 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
849 /* start a session for this user. Session closed when link ends. */
850 if (pam_error == PAM_SUCCESS)
851 pam_open_session (pamh, PAM_SILENT);
854 *msg = MY_PAM_STRERROR (pam_error);
856 PAM_username =
857 PAM_password = "";
859 * Clean up the mess
861 pam_end (pamh, pam_error);
863 if (pam_error != PAM_SUCCESS)
864 return UPAP_AUTHNAK;
866 * Use the non-PAM methods directly
868 #else /* #ifdef USE_PAM */
870 struct passwd *pw;
871 struct utmp utmp;
872 struct timeval tp;
873 char *tty;
875 #ifdef HAS_SHADOW
876 struct spwd *spwd;
877 struct spwd *getspnam();
878 #endif
880 pw = getpwnam(user);
881 endpwent();
882 if (pw == NULL) {
883 return (UPAP_AUTHNAK);
886 * Check that the user is not listed in /etc/ppp/ppp.deny
887 * and that the user's shell is listed in /etc/ppp/ppp.shells
888 * if /etc/ppp/ppp.shells exists.
891 if (checkfile(_PATH_PPPDENY, user) == 1) {
892 syslog(LOG_WARNING, "upap user %s: login denied in %s",
893 user, _PATH_PPPDENY);
894 return (UPAP_AUTHNAK);
897 if (checkfile(_PATH_PPPSHELLS, pw->pw_shell) == 0) {
898 syslog(LOG_WARNING, "upap user %s: shell %s not in %s",
899 user, pw->pw_shell, _PATH_PPPSHELLS);
900 return (UPAP_AUTHNAK);
903 #ifdef HAS_SHADOW
904 spwd = getspnam(user);
905 endspent();
906 if (spwd) {
907 /* check the age of the password entry */
908 long now = time(NULL) / 86400L;
910 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
911 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
912 && spwd->sp_lstchg >= 0
913 && now >= spwd->sp_lstchg + spwd->sp_max)) {
914 syslog(LOG_WARNING, "Password for %s has expired", user);
915 return (UPAP_AUTHNAK);
917 pw->pw_passwd = spwd->sp_pwdp;
919 #endif
922 * If no passwd, don't let them login.
924 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
925 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
926 return (UPAP_AUTHNAK);
928 if (pw->pw_expire) {
929 gettimeofday(&tp, (struct timezone *)NULL);
930 if (tp.tv_sec >= pw->pw_expire) {
931 syslog(LOG_INFO, "pap user %s account expired", user);
932 return (UPAP_AUTHNAK);
936 /* These functions are not enabled for PAM. The reason for this is that */
937 /* there is not necessarily a "passwd" entry for this user. That is */
938 /* real purpose of 'PAM' -- to virtualize the account data from the */
939 /* application. If you want to do the same thing, write the entry in */
940 /* the 'session' hook. */
942 /* Log in wtmp and utmp using login() */
944 tty = devnam;
945 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
946 tty += 5;
948 if (logout(tty)) /* Already entered (by login?) */
949 logwtmp(tty, "", "");
951 #if defined(_PATH_LASTLOG)
953 struct lastlog ll;
954 int fd;
956 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
957 lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
958 memset((void *)&ll, 0, sizeof(ll));
959 time(&ll.ll_time);
960 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
961 write(fd, (char *)&ll, sizeof(ll));
962 close(fd);
965 #endif
967 memset((void *)&utmp, 0, sizeof(utmp));
968 time(&utmp.ut_time);
969 strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
970 strncpy(utmp.ut_host, ":PPP", sizeof(utmp.ut_host));
971 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
972 login(&utmp); /* This logs us in wtmp too */
974 #endif /* #ifdef USE_PAM */
976 syslog(LOG_INFO, "user %s logged in", user);
977 logged_in = TRUE;
979 return (UPAP_AUTHACK);
983 * plogout - Logout the user.
985 static void
986 plogout(void)
988 #ifdef USE_PAM
989 struct pam_conv pam_conversation;
990 pam_handle_t *pamh;
991 int pam_error;
993 * Fill the pam_conversion structure. The PAM specification states that the
994 * session must be able to be closed by a totally different handle from which
995 * it was created. Hold the PAM group to their own specification!
997 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
998 pam_conversation.conv = &pam_conv;
1000 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
1001 if (pam_error == PAM_SUCCESS) {
1002 pam_set_item (pamh, PAM_TTY, devnam);
1003 pam_close_session (pamh, PAM_SILENT);
1004 pam_end (pamh, PAM_SUCCESS);
1007 #else
1008 char *tty;
1010 tty = devnam;
1011 if (strncmp(tty, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
1012 tty += 5;
1013 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
1014 logout(tty); /* Wipe out utmp */
1015 #endif
1017 logged_in = FALSE;
1022 * null_login - Check if a username of "" and a password of "" are
1023 * acceptable, and iff so, set the list of acceptable IP addresses
1024 * and return 1.
1026 static int
1027 null_login(int unit)
1029 char *filename;
1030 FILE *f;
1031 int i, ret;
1032 struct wordlist *addrs;
1033 char secret[MAXWORDLEN];
1036 * Open the file of pap secrets and scan for a suitable secret.
1037 * We don't accept a wildcard client.
1039 filename = _PATH_UPAPFILE;
1040 addrs = NULL;
1041 f = fopen(filename, "r");
1042 if (f == NULL)
1043 return 0;
1044 check_access(f, filename);
1046 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
1047 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
1048 BZERO(secret, sizeof(secret));
1050 if (ret)
1051 set_allowed_addrs(unit, addrs);
1052 else
1053 free_wordlist(addrs);
1055 fclose(f);
1056 return ret;
1061 * get_pap_passwd - get a password for authenticating ourselves with
1062 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1063 * could be found.
1065 static int
1066 get_pap_passwd(char *passwd)
1068 char *filename;
1069 FILE *f;
1070 int ret;
1071 struct wordlist *addrs;
1072 char secret[MAXWORDLEN];
1074 filename = _PATH_UPAPFILE;
1075 addrs = NULL;
1076 f = fopen(filename, "r");
1077 if (f == NULL)
1078 return 0;
1079 check_access(f, filename);
1080 ret = scan_authfile(f, user,
1081 remote_name[0]? remote_name: NULL,
1082 (u_int32_t)0, secret, NULL, filename);
1083 fclose(f);
1084 if (ret < 0)
1085 return 0;
1086 if (passwd != NULL) {
1087 strncpy(passwd, secret, MAXSECRETLEN);
1088 passwd[MAXSECRETLEN-1] = 0;
1090 BZERO(secret, sizeof(secret));
1091 return 1;
1096 * have_pap_secret - check whether we have a PAP file with any
1097 * secrets that we could possibly use for authenticating the peer.
1099 static int
1100 have_pap_secret(void)
1102 FILE *f;
1103 int ret;
1104 char *filename;
1105 ipcp_options *ipwo = &ipcp_wantoptions[0];
1106 u_int32_t remote;
1108 filename = _PATH_UPAPFILE;
1109 f = fopen(filename, "r");
1110 if (f == NULL)
1111 return 0;
1113 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1114 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1115 fclose(f);
1116 if (ret < 0)
1117 return 0;
1119 return 1;
1124 * have_chap_secret - check whether we have a CHAP file with a
1125 * secret that we could possibly use for authenticating `client'
1126 * on `server'. Either can be the null string, meaning we don't
1127 * know the identity yet.
1129 static int
1130 have_chap_secret(char *client, char *server, u_int32_t remote)
1132 FILE *f;
1133 int ret;
1134 char *filename;
1136 filename = _PATH_CHAPFILE;
1137 f = fopen(filename, "r");
1138 if (f == NULL)
1139 return 0;
1141 if (client[0] == 0)
1142 client = NULL;
1143 else if (server[0] == 0)
1144 server = NULL;
1146 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1147 fclose(f);
1148 if (ret < 0)
1149 return 0;
1151 return 1;
1156 * get_secret - open the CHAP secret file and return the secret
1157 * for authenticating the given client on the given server.
1158 * (We could be either client or server).
1161 get_secret(int unit, char *client, char *server, char *secret, int *secret_len,
1162 int save_addrs)
1164 FILE *f;
1165 int ret, len;
1166 char *filename;
1167 struct wordlist *addrs;
1168 char secbuf[MAXWORDLEN];
1170 filename = _PATH_CHAPFILE;
1171 addrs = NULL;
1172 secbuf[0] = 0;
1174 f = fopen(filename, "r");
1175 if (f == NULL) {
1176 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1177 return 0;
1179 check_access(f, filename);
1181 ret = scan_authfile(f, client, server, (u_int32_t)0,
1182 secbuf, &addrs, filename);
1183 fclose(f);
1184 if (ret < 0)
1185 return 0;
1187 if (save_addrs)
1188 set_allowed_addrs(unit, addrs);
1190 len = strlen(secbuf);
1191 if (len > MAXSECRETLEN) {
1192 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1193 len = MAXSECRETLEN;
1195 BCOPY(secbuf, secret, len);
1196 BZERO(secbuf, sizeof(secbuf));
1197 *secret_len = len;
1199 return 1;
1203 * set_allowed_addrs() - set the list of allowed addresses.
1205 static void
1206 set_allowed_addrs(int unit, struct wordlist *addrs)
1208 if (addresses[unit] != NULL)
1209 free_wordlist(addresses[unit]);
1210 addresses[unit] = addrs;
1213 * If there's only one authorized address we might as well
1214 * ask our peer for that one right away
1216 if (addrs != NULL && addrs->next == NULL) {
1217 char *p = addrs->word;
1218 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1219 u_int32_t a;
1220 struct hostent *hp;
1222 if (*p != '!' && *p != '-' && strchr(p, '/') == NULL) {
1223 hp = gethostbyname(p);
1224 if (hp != NULL && hp->h_addrtype == AF_INET)
1225 a = *(u_int32_t *)hp->h_addr;
1226 else
1227 a = inet_addr(p);
1228 if (a != (u_int32_t) -1)
1229 wo->hisaddr = a;
1234 static void
1235 auth_set_ip_addr(int unit)
1237 struct wordlist *addrs;
1239 if (non_wildclient && (addrs = addresses[unit]) != NULL) {
1240 for (; addrs != NULL; addrs = addrs->next) {
1241 /* Look for address overrides, and set them if we have any */
1242 if (strchr(addrs->word, ':') != NULL) {
1243 if (setipaddr(addrs->word))
1244 break;
1251 * auth_ip_addr - check whether the peer is authorized to use
1252 * a given IP address. Returns 1 if authorized, 0 otherwise.
1255 auth_ip_addr(int unit, u_int32_t addr)
1257 return ip_addr_check(addr, addresses[unit]);
1260 static int
1261 ip_addr_check(u_int32_t addr, struct wordlist *addrs)
1263 int x, y;
1264 u_int32_t a, mask, ah;
1265 int accept;
1266 char *ptr_word, *ptr_mask;
1267 struct hostent *hp;
1268 struct netent *np;
1270 /* don't allow loopback or multicast address */
1271 if (bad_ip_adrs(addr))
1272 return 0;
1274 if (addrs == NULL)
1275 return !auth_required; /* no addresses authorized */
1277 x = y = 0;
1278 for (; addrs != NULL; addrs = addrs->next) {
1279 y++;
1280 /* "-" means no addresses authorized, "*" means any address allowed */
1281 ptr_word = addrs->word;
1282 if (strcmp(ptr_word, "-") == 0)
1283 break;
1284 if (strcmp(ptr_word, "*") == 0)
1285 return 1;
1288 * A colon in the string means that we wish to force a specific
1289 * local:remote address, but we ignore these for now.
1291 if (strchr(addrs->word, ':') != NULL)
1292 x++;
1293 else {
1295 accept = 1;
1296 if (*ptr_word == '!') {
1297 accept = 0;
1298 ++ptr_word;
1301 mask = ~ (u_int32_t) 0;
1302 ptr_mask = strchr (ptr_word, '/');
1303 if (ptr_mask != NULL) {
1304 int bit_count;
1306 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1307 if (bit_count <= 0 || bit_count > 32) {
1308 syslog (LOG_WARNING,
1309 "invalid address length %s in auth. address list",
1310 ptr_mask);
1311 continue;
1313 *ptr_mask = '\0';
1314 mask <<= 32 - bit_count;
1317 hp = gethostbyname(ptr_word);
1318 if (hp != NULL && hp->h_addrtype == AF_INET) {
1319 a = *(u_int32_t *)hp->h_addr;
1320 } else {
1321 np = getnetbyname (ptr_word);
1322 if (np != NULL && np->n_addrtype == AF_INET) {
1323 a = htonl (*(u_int32_t *)np->n_net);
1324 if (ptr_mask == NULL) {
1325 /* calculate appropriate mask for net */
1326 ah = ntohl(a);
1327 if (IN_CLASSA(ah))
1328 mask = IN_CLASSA_NET;
1329 else if (IN_CLASSB(ah))
1330 mask = IN_CLASSB_NET;
1331 else if (IN_CLASSC(ah))
1332 mask = IN_CLASSC_NET;
1334 } else {
1335 a = inet_addr (ptr_word);
1339 if (ptr_mask != NULL)
1340 *ptr_mask = '/';
1342 if (a == (u_int32_t)-1L)
1343 syslog (LOG_WARNING,
1344 "unknown host %s in auth. address list",
1345 addrs->word);
1346 else
1347 /* Here a and addr are in network byte order,
1348 and mask is in host order. */
1349 if (((addr ^ a) & htonl(mask)) == 0)
1350 return accept;
1351 } /* else */
1353 return x == y; /* not in list => can't have it */
1357 * bad_ip_adrs - return 1 if the IP address is one we don't want
1358 * to use, such as an address in the loopback net or a multicast address.
1359 * addr is in network byte order.
1362 bad_ip_adrs(u_int32_t addr)
1364 addr = ntohl(addr);
1365 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1366 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1370 * check_access - complain if a secret file has too-liberal permissions.
1372 void
1373 check_access(FILE *f, char *filename)
1375 struct stat sbuf;
1377 if (fstat(fileno(f), &sbuf) < 0) {
1378 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1379 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1380 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1386 * scan_authfile - Scan an authorization file for a secret suitable
1387 * for authenticating `client' on `server'. The return value is -1
1388 * if no secret is found, otherwise >= 0. The return value has
1389 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1390 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1391 * Any following words on the line (i.e. address authorization
1392 * info) are placed in a wordlist and returned in *addrs.
1394 static int
1395 scan_authfile(FILE *f, char *client, char *server, u_int32_t ipaddr,
1396 char *secret, struct wordlist **addrs, char *filename)
1398 int newline, xxx;
1399 int got_flag, best_flag;
1400 FILE *sf;
1401 struct wordlist *ap, *addr_list, *alist, *alast;
1402 char word[MAXWORDLEN];
1403 char atfile[MAXWORDLEN];
1404 char lsecret[MAXWORDLEN];
1406 if (addrs != NULL)
1407 *addrs = NULL;
1408 addr_list = NULL;
1409 if (!getword(f, word, &newline, filename))
1410 return -1; /* file is empty??? */
1411 newline = 1;
1412 best_flag = -1;
1413 for (;;) {
1415 * Skip until we find a word at the start of a line.
1417 while (!newline && getword(f, word, &newline, filename))
1419 if (!newline)
1420 break; /* got to end of file */
1423 * Got a client - check if it's a match or a wildcard.
1425 got_flag = 0;
1426 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1427 newline = 0;
1428 continue;
1430 if (!ISWILD(word))
1431 got_flag = NONWILD_CLIENT;
1434 * Now get a server and check if it matches.
1436 if (!getword(f, word, &newline, filename))
1437 break;
1438 if (newline)
1439 continue;
1440 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1441 continue;
1442 if (!ISWILD(word))
1443 got_flag |= NONWILD_SERVER;
1446 * Got some sort of a match - see if it's better than what
1447 * we have already.
1449 if (got_flag <= best_flag)
1450 continue;
1453 * Get the secret.
1455 if (!getword(f, word, &newline, filename))
1456 break;
1457 if (newline)
1458 continue;
1461 * Special syntax: @filename means read secret from file.
1463 if (word[0] == '@') {
1464 strcpy(atfile, word+1);
1465 if ((sf = fopen(atfile, "r")) == NULL) {
1466 syslog(LOG_WARNING, "can't open indirect secret file %s",
1467 atfile);
1468 continue;
1470 check_access(sf, atfile);
1471 if (!getword(sf, word, &xxx, atfile)) {
1472 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1473 atfile);
1474 fclose(sf);
1475 continue;
1477 fclose(sf);
1479 if (secret != NULL)
1480 strcpy(lsecret, word);
1483 * Now read address authorization info and make a wordlist.
1485 alist = alast = NULL;
1486 for (;;) {
1487 if (!getword(f, word, &newline, filename) || newline)
1488 break;
1489 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1490 + strlen(word));
1491 if (ap == NULL)
1492 novm("authorized addresses");
1493 ap->next = NULL;
1494 strcpy(ap->word, word);
1495 if (alist == NULL)
1496 alist = ap;
1497 else
1498 alast->next = ap;
1499 alast = ap;
1503 * Check if the given IP address is allowed by the wordlist.
1505 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1506 free_wordlist(alist);
1507 continue;
1511 * This is the best so far; remember it.
1513 best_flag = got_flag;
1514 if (addr_list)
1515 free_wordlist(addr_list);
1516 addr_list = alist;
1517 if (secret != NULL)
1518 strcpy(secret, lsecret);
1520 if (!newline)
1521 break;
1524 if (addrs != NULL)
1525 *addrs = addr_list;
1526 else if (addr_list != NULL)
1527 free_wordlist(addr_list);
1529 non_wildclient = (best_flag & NONWILD_CLIENT) && client != NULL &&
1530 *client != '\0';
1531 return best_flag;
1535 * free_wordlist - release memory allocated for a wordlist.
1537 static void
1538 free_wordlist(struct wordlist *wp)
1540 struct wordlist *next;
1542 while (wp != NULL) {
1543 next = wp->next;
1544 free(wp);
1545 wp = next;
1550 * auth_script - execute a script with arguments
1551 * interface-name peer-name real-user tty speed
1553 static void
1554 auth_script(char *script)
1556 char strspeed[32];
1557 struct passwd *pw;
1558 char struid[32];
1559 char *user_name;
1560 char *argv[8];
1562 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1563 user_name = pw->pw_name;
1564 else {
1565 sprintf(struid, "%d", getuid());
1566 user_name = struid;
1568 sprintf(strspeed, "%d", baud_rate);
1570 argv[0] = script;
1571 argv[1] = ifname;
1572 argv[2] = peer_authname;
1573 argv[3] = user_name;
1574 argv[4] = devnam;
1575 argv[5] = strspeed;
1576 argv[6] = NULL;
1578 run_program(script, argv, 0);