2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993 The Australian National University.
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.
36 static char rcsid
[] = "$Id: auth.c,v 1.40 1999/01/19 23:59:14 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #if defined(_PATH_LASTLOG) && defined(_linux_)
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
60 #include <security/pam_appl.h>
66 #define PW_PPP PW_LOGIN
79 #include "pathnames.h"
81 /* Used for storing a sequence of words. Usually malloced. */
83 struct wordlist
*next
;
87 /* Bits in scan_authfile return value */
88 #define NONWILD_SERVER 1
89 #define NONWILD_CLIENT 2
91 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
96 /* The name by which the peer authenticated itself to us. */
97 char peer_authname
[MAXNAMELEN
];
99 /* Records which authentication operations haven't completed yet. */
100 static int auth_pending
[NUM_PPP
];
102 /* Set if we have successfully called plogin() */
103 static int logged_in
;
105 /* Set if we have run the /etc/ppp/auth-up script. */
106 static int did_authup
;
107 static pid_t authup_pid
; /* process ID of auth-up/down script */
109 /* List of addresses which the peer may use. */
110 static struct wordlist
*addresses
[NUM_PPP
];
112 /* Number of network protocols which we have opened. */
113 static int num_np_open
;
115 /* Number of network protocols which have come up. */
116 static int num_np_up
;
118 /* Set if we got the contents of passwd[] from the pap-secrets file. */
119 static int passwd_from_file
;
124 bool uselogin
= 0; /* Use /etc/passwd for checking PAP */
125 bool cryptpap
= 0; /* Passwords in pap-secrets are encrypted */
126 bool refuse_pap
= 0; /* Don't wanna auth. ourselves with PAP */
127 bool refuse_chap
= 0; /* Don't wanna auth. ourselves with CHAP */
128 bool usehostname
= 0; /* Use hostname for our_name */
129 bool auth_required
= 0; /* Always require authentication from peer */
130 bool allow_any_ip
= 0; /* Allow peer to use any IP address */
132 /* Bits in auth_pending[] */
133 #define PAP_WITHPEER 1
135 #define CHAP_WITHPEER 4
138 extern char *crypt
__P((const char *, const char *));
140 /* Prototypes for procedures local to this file. */
142 static void network_phase
__P((int));
143 static void check_idle
__P((void *));
144 static void connect_time_expired
__P((void *));
145 static int plogin
__P((char *, char *, char **, int *));
146 static void plogout
__P((void));
147 static int null_login
__P((int));
148 static int get_pap_passwd
__P((char *));
149 static int have_pap_secret
__P((void));
150 static int have_chap_secret
__P((char *, char *, u_int32_t
));
151 static int ip_addr_check
__P((u_int32_t
, struct wordlist
*));
152 static int scan_authfile
__P((FILE *, char *, char *, u_int32_t
, char *,
153 struct wordlist
**, char *));
154 static void free_wordlist
__P((struct wordlist
*));
155 static void auth_script
__P((char *));
156 static void set_allowed_addrs
__P((int, struct wordlist
*));
159 static int setupapfile
__P((char **));
163 * Authentication-related options.
165 option_t auth_options
[] = {
166 { "require-pap", o_bool
, &lcp_wantoptions
[0].neg_upap
,
167 "Require PAP authentication from peer", 1, &auth_required
},
168 { "+pap", o_bool
, &lcp_wantoptions
[0].neg_upap
,
169 "Require PAP authentication from peer", 1, &auth_required
},
170 { "refuse-pap", o_bool
, &refuse_pap
,
171 "Don't agree to auth to peer with PAP", 1 },
172 { "-pap", o_bool
, &refuse_pap
,
173 "Don't allow UPAP authentication with peer", 1 },
174 { "require-chap", o_bool
, &lcp_wantoptions
[0].neg_chap
,
175 "Require CHAP authentication from peer", 1, &auth_required
},
176 { "+chap", o_bool
, &lcp_wantoptions
[0].neg_chap
,
177 "Require CHAP authentication from peer", 1, &auth_required
},
178 { "refuse-chap", o_bool
, &refuse_chap
,
179 "Don't agree to auth to peer with CHAP", 1 },
180 { "-chap", o_bool
, &refuse_chap
,
181 "Don't allow CHAP authentication with peer", 1 },
182 { "name", o_string
, our_name
,
183 "Set local name for authentication",
184 OPT_PRIV
|OPT_STATIC
, NULL
, MAXNAMELEN
},
185 { "user", o_string
, user
,
186 "Set name for auth with peer", OPT_STATIC
, NULL
, MAXNAMELEN
},
187 { "usehostname", o_bool
, &usehostname
,
188 "Must use hostname for authentication", 1 },
189 { "remotename", o_string
, remote_name
,
190 "Set remote name for authentication", OPT_STATIC
, NULL
, MAXNAMELEN
},
191 { "auth", o_bool
, &auth_required
,
192 "Require authentication from peer", 1 },
193 { "noauth", o_bool
, &auth_required
,
194 "Don't require peer to authenticate", OPT_PRIV
, &allow_any_ip
},
195 { "login", o_bool
, &uselogin
,
196 "Use system password database for PAP", 1 },
197 { "papcrypt", o_bool
, &cryptpap
,
198 "PAP passwords are encrypted", 1 },
200 { "+ua", o_special
, setupapfile
,
201 "Get PAP user and password from file" },
208 * setupapfile - specifies UPAP info for authenticating with peer.
217 lcp_allowoptions
[0].neg_upap
= 1;
219 /* open user info file */
220 if ((ufile
= fopen(*argv
, "r")) == NULL
) {
221 option_error("unable to open user login data file %s", *argv
);
224 if (!readable(fileno(ufile
))) {
225 option_error("%s: access denied", *argv
);
228 check_access(ufile
, *argv
);
231 if (fgets(user
, MAXNAMELEN
- 1, ufile
) == NULL
232 || fgets(passwd
, MAXSECRETLEN
- 1, ufile
) == NULL
){
233 option_error("unable to read user login data file %s", *argv
);
238 /* get rid of newlines */
240 if (l
> 0 && user
[l
-1] == '\n')
243 if (l
> 0 && passwd
[l
-1] == '\n')
252 * An Open on LCP has requested a change from Dead to Establish phase.
253 * Do what's necessary to bring the physical layer up.
262 * LCP has terminated the link; go to the Dead phase and take the
263 * physical layer down.
266 link_terminated(unit
)
269 if (phase
== PHASE_DEAD
)
274 syslog(LOG_NOTICE
, "Connection terminated.");
278 * LCP has gone down; it will either die or try to re-establish.
285 struct protent
*protp
;
288 auth_script(_PATH_AUTHDOWN
);
291 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
) {
292 if (!protp
->enabled_flag
)
294 if (protp
->protocol
!= PPP_LCP
&& protp
->lowerdown
!= NULL
)
295 (*protp
->lowerdown
)(unit
);
296 if (protp
->protocol
< 0xC000 && protp
->close
!= NULL
)
297 (*protp
->close
)(unit
, "LCP down");
301 if (phase
!= PHASE_DEAD
)
302 phase
= PHASE_TERMINATE
;
306 * The link is established.
307 * Proceed to the Dead, Authenticate or Network phase as appropriate.
310 link_established(unit
)
314 lcp_options
*wo
= &lcp_wantoptions
[unit
];
315 lcp_options
*go
= &lcp_gotoptions
[unit
];
316 lcp_options
*ho
= &lcp_hisoptions
[unit
];
318 struct protent
*protp
;
321 * Tell higher-level protocols that LCP is up.
323 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
324 if (protp
->protocol
!= PPP_LCP
&& protp
->enabled_flag
325 && protp
->lowerup
!= NULL
)
326 (*protp
->lowerup
)(unit
);
328 if (auth_required
&& !(go
->neg_chap
|| go
->neg_upap
)) {
330 * We wanted the peer to authenticate itself, and it refused:
331 * treat it as though it authenticated with PAP using a username
332 * of "" and a password of "". If that's not OK, boot it out.
334 if (!wo
->neg_upap
|| !null_login(unit
)) {
335 syslog(LOG_WARNING
, "peer refused to authenticate");
336 lcp_close(unit
, "peer refused to authenticate");
341 phase
= PHASE_AUTHENTICATE
;
344 ChapAuthPeer(unit
, our_name
, go
->chap_mdtype
);
346 } else if (go
->neg_upap
) {
351 ChapAuthWithPeer(unit
, user
, ho
->chap_mdtype
);
352 auth
|= CHAP_WITHPEER
;
353 } else if (ho
->neg_upap
) {
354 if (passwd
[0] == 0) {
355 passwd_from_file
= 1;
356 if (!get_pap_passwd(passwd
))
357 syslog(LOG_ERR
, "No secret found for PAP login");
359 upap_authwithpeer(unit
, user
, passwd
);
360 auth
|= PAP_WITHPEER
;
362 auth_pending
[unit
] = auth
;
369 * Proceed to the network phase.
376 struct protent
*protp
;
377 lcp_options
*go
= &lcp_gotoptions
[unit
];
380 * If the peer had to authenticate, run the auth-up script now.
382 if ((go
->neg_chap
|| go
->neg_upap
) && !did_authup
) {
383 auth_script(_PATH_AUTHUP
);
389 * If we negotiated callback, do it now.
392 phase
= PHASE_CALLBACK
;
393 (*cbcp_protent
.open
)(unit
);
398 phase
= PHASE_NETWORK
;
401 set_filters(&pass_filter
, &active_filter
);
403 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
404 if (protp
->protocol
< 0xC000 && protp
->enabled_flag
405 && protp
->open
!= NULL
) {
406 (*protp
->open
)(unit
);
407 if (protp
->protocol
!= PPP_CCP
)
411 if (num_np_open
== 0)
413 lcp_close(0, "No network protocols running");
417 * The peer has failed to authenticate himself using `protocol'.
420 auth_peer_fail(unit
, protocol
)
424 * Authentication failure: take the link down
426 lcp_close(unit
, "Authentication failed");
430 * The peer has been successfully authenticated using `protocol'.
433 auth_peer_success(unit
, protocol
, name
, namelen
)
448 syslog(LOG_WARNING
, "auth_peer_success: unknown protocol %x",
454 * Save the authenticated name of the peer for later.
456 if (namelen
> sizeof(peer_authname
) - 1)
457 namelen
= sizeof(peer_authname
) - 1;
458 BCOPY(name
, peer_authname
, namelen
);
459 peer_authname
[namelen
] = 0;
460 script_setenv("PEERNAME", peer_authname
);
463 * If there is no more authentication still to be done,
464 * proceed to the network (or callback) phase.
466 if ((auth_pending
[unit
] &= ~bit
) == 0)
471 * We have failed to authenticate ourselves to the peer using `protocol'.
474 auth_withpeer_fail(unit
, protocol
)
477 if (passwd_from_file
)
478 BZERO(passwd
, MAXSECRETLEN
);
480 * We've failed to authenticate ourselves to our peer.
481 * He'll probably take the link down, and there's not much
482 * we can do except wait for that.
487 * We have successfully authenticated ourselves with the peer using `protocol'.
490 auth_withpeer_success(unit
, protocol
)
500 if (passwd_from_file
)
501 BZERO(passwd
, MAXSECRETLEN
);
505 syslog(LOG_WARNING
, "auth_peer_success: unknown protocol %x",
511 * If there is no more authentication still being done,
512 * proceed to the network (or callback) phase.
514 if ((auth_pending
[unit
] &= ~bit
) == 0)
520 * np_up - a network protocol has come up.
526 if (num_np_up
== 0) {
528 * At this point we consider that the link has come up successfully.
532 if (idle_time_limit
> 0)
533 TIMEOUT(check_idle
, NULL
, idle_time_limit
);
536 * Set a timeout to close the connection once the maximum
537 * connect time has expired.
540 TIMEOUT(connect_time_expired
, 0, maxconnect
);
543 * Detach now, if the updetach option was given.
545 if (updetach
&& !nodetach
)
552 * np_down - a network protocol has gone down.
558 if (--num_np_up
== 0 && idle_time_limit
> 0) {
559 UNTIMEOUT(check_idle
, NULL
);
564 * np_finished - a network protocol has finished using the link.
567 np_finished(unit
, proto
)
570 if (--num_np_open
<= 0) {
571 /* no further use for the link: shut up shop. */
572 lcp_close(0, "No network protocols running");
577 * check_idle - check whether the link has been idle for long
578 * enough that we can shut it down.
584 struct ppp_idle idle
;
587 if (!get_idle_time(0, &idle
))
589 itime
= MIN(idle
.xmit_idle
, idle
.recv_idle
);
590 if (itime
>= idle_time_limit
) {
591 /* link is idle: shut it down. */
592 syslog(LOG_INFO
, "Terminating connection due to lack of activity.");
593 lcp_close(0, "Link inactive");
595 TIMEOUT(check_idle
, NULL
, idle_time_limit
- itime
);
600 * connect_time_expired - log a message and close the connection.
603 connect_time_expired(arg
)
606 syslog(LOG_INFO
, "Connect time expired");
607 lcp_close(0, "Connect time expired"); /* Close connection */
611 * auth_check_options - called to check authentication options.
616 lcp_options
*wo
= &lcp_wantoptions
[0];
618 ipcp_options
*ipwo
= &ipcp_wantoptions
[0];
621 /* Default our_name to hostname, and user to our_name */
622 if (our_name
[0] == 0 || usehostname
)
623 strcpy(our_name
, hostname
);
625 strcpy(user
, our_name
);
627 /* If authentication is required, ask peer for CHAP or PAP. */
629 if (!wo
->neg_chap
&& !wo
->neg_upap
) {
639 * Check whether we have appropriate secrets to use
640 * to authenticate the peer.
642 can_auth
= wo
->neg_upap
&& (uselogin
|| have_pap_secret());
643 if (!can_auth
&& wo
->neg_chap
) {
644 remote
= ipwo
->accept_remote
? 0: ipwo
->hisaddr
;
645 can_auth
= have_chap_secret(remote_name
, our_name
, remote
);
648 if (auth_required
&& !can_auth
) {
649 option_error("peer authentication required but no suitable secret(s) found\n");
650 if (remote_name
[0] == 0)
651 option_error("for authenticating any peer to us (%s)\n", our_name
);
653 option_error("for authenticating peer %s to us (%s)\n",
654 remote_name
, our_name
);
659 * Check whether the user tried to override certain values
663 if ((connector
!= NULL
&& connector_info
.priv
== 0)
664 || (disconnector
!= NULL
&& disconnector_info
.priv
== 0)
665 || (welcomer
!= NULL
&& welcomer_info
.priv
== 0)) {
666 option_error("connect, disconnect and welcome options");
667 option_error("are privileged when noauth option is used");
674 * auth_reset - called when LCP is starting negotiations to recheck
675 * authentication options, i.e. whether we have appropriate secrets
676 * to use for authenticating ourselves and/or the peer.
682 lcp_options
*go
= &lcp_gotoptions
[unit
];
683 lcp_options
*ao
= &lcp_allowoptions
[0];
684 ipcp_options
*ipwo
= &ipcp_wantoptions
[0];
687 ao
->neg_upap
= !refuse_pap
&& (passwd
[0] != 0 || get_pap_passwd(NULL
));
688 ao
->neg_chap
= !refuse_chap
689 && have_chap_secret(user
, remote_name
, (u_int32_t
)0);
691 if (go
->neg_upap
&& !uselogin
&& !have_pap_secret())
694 remote
= ipwo
->accept_remote
? 0: ipwo
->hisaddr
;
695 if (!have_chap_secret(remote_name
, our_name
, remote
))
702 * check_passwd - Check the user name and passwd against the PAP secrets
703 * file. If requested, also check against the system password database,
704 * and login the user if OK.
707 * UPAP_AUTHNAK: Authentication failed.
708 * UPAP_AUTHACK: Authentication succeeded.
709 * In either case, msg points to an appropriate message.
712 check_passwd(unit
, auser
, userlen
, apasswd
, passwdlen
, msg
, msglen
)
724 struct wordlist
*addrs
;
726 ipcp_options
*ipwo
= &ipcp_wantoptions
[unit
];
727 char passwd
[256], user
[256];
728 char secret
[MAXWORDLEN
];
729 static int attempts
= 0;
732 * Make copies of apasswd and auser, then null-terminate them.
734 BCOPY(apasswd
, passwd
, passwdlen
);
735 passwd
[passwdlen
] = '\0';
736 BCOPY(auser
, user
, userlen
);
737 user
[userlen
] = '\0';
741 * Open the file of pap secrets and scan for a suitable secret
742 * for authenticating this user.
744 filename
= _PATH_UPAPFILE
;
747 f
= fopen(filename
, "r");
749 syslog(LOG_ERR
, "Can't open PAP password file %s: %m", filename
);
753 check_access(f
, filename
);
754 remote
= ipwo
->accept_remote
? 0: ipwo
->hisaddr
;
755 if (scan_authfile(f
, user
, our_name
, remote
,
756 secret
, &addrs
, filename
) < 0
757 || (secret
[0] != 0 && (cryptpap
|| strcmp(passwd
, secret
) != 0)
758 && strcmp(crypt(passwd
, secret
), secret
) != 0)) {
759 syslog(LOG_WARNING
, "PAP authentication failure for %s", user
);
765 if (uselogin
&& ret
== UPAP_AUTHACK
) {
766 ret
= plogin(user
, passwd
, msg
, msglen
);
767 if (ret
== UPAP_AUTHNAK
) {
768 syslog(LOG_WARNING
, "PAP login failure for %s", user
);
772 if (ret
== UPAP_AUTHNAK
) {
773 if (*msg
== (char *) 0)
774 *msg
= "Login incorrect";
775 *msglen
= strlen(*msg
);
777 * Frustrate passwd stealer programs.
778 * Allow 10 tries, but start backing off after 3 (stolen from login).
779 * On 10'th, drop the connection.
781 if (attempts
++ >= 10) {
782 syslog(LOG_WARNING
, "%d LOGIN FAILURES ON %s, %s",
783 attempts
, devnam
, user
);
787 sleep((u_int
) (attempts
- 3) * 5);
789 free_wordlist(addrs
);
792 attempts
= 0; /* Reset count */
793 if (*msg
== (char *) 0)
795 *msglen
= strlen(*msg
);
796 set_allowed_addrs(unit
, addrs
);
799 BZERO(passwd
, sizeof(passwd
));
800 BZERO(secret
, sizeof(secret
));
806 * This function is needed for PAM.
810 static char *PAM_username
= "";
811 static char *PAM_password
= "";
813 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
814 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
816 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
819 static int pam_conv (int num_msg
,
820 const struct pam_message
**msg
,
821 struct pam_response
**resp
,
824 int count
= 0, replies
= 0;
825 struct pam_response
*reply
= NULL
;
828 for (count
= 0; count
< num_msg
; count
++)
830 size
+= sizeof (struct pam_response
);
831 reply
= realloc (reply
, size
); /* ANSI: is malloc() if reply==NULL */
835 switch (msg
[count
]->msg_style
)
837 case PAM_PROMPT_ECHO_ON
:
838 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
839 reply
[replies
++].resp
= strdup(PAM_username
); /* never NULL */
842 case PAM_PROMPT_ECHO_OFF
:
843 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
844 reply
[replies
++].resp
= strdup(PAM_password
); /* never NULL */
848 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
849 reply
[replies
++].resp
= NULL
;
869 * plogin - Check the user name and password against the system
870 * password database, and login the user if OK.
873 * UPAP_AUTHNAK: Login failed.
874 * UPAP_AUTHACK: Login succeeded.
875 * In either case, msg points to an appropriate message.
879 plogin(user
, passwd
, msg
, msglen
)
888 struct pam_conv pam_conversation
;
892 * Fill the pam_conversion structure
894 memset (&pam_conversation
, '\0', sizeof (struct pam_conv
));
895 pam_conversation
.conv
= &pam_conv
;
897 pam_error
= pam_start ("ppp", user
, &pam_conversation
, &pamh
);
899 if (pam_error
!= PAM_SUCCESS
) {
900 *msg
= MY_PAM_STRERROR (pam_error
);
904 * Define the fields for the credintial validation
906 (void) pam_set_item (pamh
, PAM_TTY
, devnam
);
908 PAM_password
= passwd
;
912 pam_error
= pam_authenticate (pamh
, PAM_SILENT
);
913 if (pam_error
== PAM_SUCCESS
) {
914 pam_error
= pam_acct_mgmt (pamh
, PAM_SILENT
);
916 /* start a session for this user. Session closed when link ends. */
917 if (pam_error
== PAM_SUCCESS
)
918 (void) pam_open_session (pamh
, PAM_SILENT
);
921 *msg
= MY_PAM_STRERROR (pam_error
);
928 (void) pam_end (pamh
, pam_error
);
930 if (pam_error
!= PAM_SUCCESS
)
933 * Use the non-PAM methods directly
935 #else /* #ifdef USE_PAM */
942 struct spwd
*getspnam();
948 return (UPAP_AUTHNAK
);
952 spwd
= getspnam(user
);
955 /* check the age of the password entry */
956 long now
= time(NULL
) / 86400L;
958 if ((spwd
->sp_expire
> 0 && now
>= spwd
->sp_expire
)
959 || ((spwd
->sp_max
>= 0 && spwd
->sp_max
< 10000)
960 && spwd
->sp_lstchg
>= 0
961 && now
>= spwd
->sp_lstchg
+ spwd
->sp_max
)) {
962 syslog(LOG_WARNING
, "Password for %s has expired", user
);
963 return (UPAP_AUTHNAK
);
965 pw
->pw_passwd
= spwd
->sp_pwdp
;
970 * If no passwd, don't let them login.
972 if (pw
->pw_passwd
== NULL
|| *pw
->pw_passwd
== '\0'
973 || strcmp(crypt(passwd
, pw
->pw_passwd
), pw
->pw_passwd
) != 0)
974 return (UPAP_AUTHNAK
);
976 /* These functions are not enabled for PAM. The reason for this is that */
977 /* there is not necessarily a "passwd" entry for this user. That is */
978 /* real purpose of 'PAM' -- to virtualize the account data from the */
979 /* application. If you want to do the same thing, write the entry in */
980 /* the 'session' hook. */
983 * Write a wtmp entry for this user.
987 if (strncmp(tty
, "/dev/", 5) == 0)
989 logwtmp(tty
, user
, remote_name
); /* Add wtmp login entry */
991 #if defined(_PATH_LASTLOG)
996 if ((fd
= open(_PATH_LASTLOG
, O_RDWR
, 0)) >= 0) {
997 (void)lseek(fd
, (off_t
)(pw
->pw_uid
* sizeof(ll
)), SEEK_SET
);
998 memset((void *)&ll
, 0, sizeof(ll
));
999 (void)time(&ll
.ll_time
);
1000 (void)strncpy(ll
.ll_line
, tty
, sizeof(ll
.ll_line
));
1001 (void)write(fd
, (char *)&ll
, sizeof(ll
));
1007 #endif /* #ifdef USE_PAM */
1009 syslog(LOG_INFO
, "user %s logged in", user
);
1012 return (UPAP_AUTHACK
);
1016 * plogout - Logout the user.
1022 struct pam_conv pam_conversation
;
1026 * Fill the pam_conversion structure. The PAM specification states that the
1027 * session must be able to be closed by a totally different handle from which
1028 * it was created. Hold the PAM group to their own specification!
1030 memset (&pam_conversation
, '\0', sizeof (struct pam_conv
));
1031 pam_conversation
.conv
= &pam_conv
;
1033 pam_error
= pam_start ("ppp", user
, &pam_conversation
, &pamh
);
1034 if (pam_error
== PAM_SUCCESS
) {
1035 (void) pam_set_item (pamh
, PAM_TTY
, devnam
);
1036 (void) pam_close_session (pamh
, PAM_SILENT
);
1037 (void) pam_end (pamh
, PAM_SUCCESS
);
1044 if (strncmp(tty
, "/dev/", 5) == 0)
1046 logwtmp(tty
, "", ""); /* Wipe out utmp logout entry */
1054 * null_login - Check if a username of "" and a password of "" are
1055 * acceptable, and iff so, set the list of acceptable IP addresses
1065 struct wordlist
*addrs
;
1066 char secret
[MAXWORDLEN
];
1069 * Open the file of pap secrets and scan for a suitable secret.
1070 * We don't accept a wildcard client.
1072 filename
= _PATH_UPAPFILE
;
1074 f
= fopen(filename
, "r");
1077 check_access(f
, filename
);
1079 i
= scan_authfile(f
, "", our_name
, (u_int32_t
)0, secret
, &addrs
, filename
);
1080 ret
= i
>= 0 && (i
& NONWILD_CLIENT
) != 0 && secret
[0] == 0;
1081 BZERO(secret
, sizeof(secret
));
1084 set_allowed_addrs(unit
, addrs
);
1086 free_wordlist(addrs
);
1094 * get_pap_passwd - get a password for authenticating ourselves with
1095 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1099 get_pap_passwd(passwd
)
1105 struct wordlist
*addrs
;
1106 char secret
[MAXWORDLEN
];
1108 filename
= _PATH_UPAPFILE
;
1110 f
= fopen(filename
, "r");
1113 check_access(f
, filename
);
1114 ret
= scan_authfile(f
, user
,
1115 remote_name
[0]? remote_name
: NULL
,
1116 (u_int32_t
)0, secret
, NULL
, filename
);
1120 if (passwd
!= NULL
) {
1121 strncpy(passwd
, secret
, MAXSECRETLEN
);
1122 passwd
[MAXSECRETLEN
-1] = 0;
1124 BZERO(secret
, sizeof(secret
));
1130 * have_pap_secret - check whether we have a PAP file with any
1131 * secrets that we could possibly use for authenticating the peer.
1139 ipcp_options
*ipwo
= &ipcp_wantoptions
[0];
1142 filename
= _PATH_UPAPFILE
;
1143 f
= fopen(filename
, "r");
1147 remote
= ipwo
->accept_remote
? 0: ipwo
->hisaddr
;
1148 ret
= scan_authfile(f
, NULL
, our_name
, remote
, NULL
, NULL
, filename
);
1158 * have_chap_secret - check whether we have a CHAP file with a
1159 * secret that we could possibly use for authenticating `client'
1160 * on `server'. Either can be the null string, meaning we don't
1161 * know the identity yet.
1164 have_chap_secret(client
, server
, remote
)
1173 filename
= _PATH_CHAPFILE
;
1174 f
= fopen(filename
, "r");
1180 else if (server
[0] == 0)
1183 ret
= scan_authfile(f
, client
, server
, remote
, NULL
, NULL
, filename
);
1193 * get_secret - open the CHAP secret file and return the secret
1194 * for authenticating the given client on the given server.
1195 * (We could be either client or server).
1198 get_secret(unit
, client
, server
, secret
, secret_len
, save_addrs
)
1209 struct wordlist
*addrs
;
1210 char secbuf
[MAXWORDLEN
];
1212 filename
= _PATH_CHAPFILE
;
1216 f
= fopen(filename
, "r");
1218 syslog(LOG_ERR
, "Can't open chap secret file %s: %m", filename
);
1221 check_access(f
, filename
);
1223 ret
= scan_authfile(f
, client
, server
, (u_int32_t
)0,
1224 secbuf
, &addrs
, filename
);
1230 set_allowed_addrs(unit
, addrs
);
1232 len
= strlen(secbuf
);
1233 if (len
> MAXSECRETLEN
) {
1234 syslog(LOG_ERR
, "Secret for %s on %s is too long", client
, server
);
1237 BCOPY(secbuf
, secret
, len
);
1238 BZERO(secbuf
, sizeof(secbuf
));
1245 * set_allowed_addrs() - set the list of allowed addresses.
1248 set_allowed_addrs(unit
, addrs
)
1250 struct wordlist
*addrs
;
1252 if (addresses
[unit
] != NULL
)
1253 free_wordlist(addresses
[unit
]);
1254 addresses
[unit
] = addrs
;
1257 * If there's only one authorized address we might as well
1258 * ask our peer for that one right away
1260 if (addrs
!= NULL
&& addrs
->next
== NULL
) {
1261 char *p
= addrs
->word
;
1262 struct ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
1266 if (*p
!= '!' && *p
!= '-' && *p
!= '*' && strchr(p
, '/') == NULL
) {
1267 hp
= gethostbyname(p
);
1268 if (hp
!= NULL
&& hp
->h_addrtype
== AF_INET
)
1269 a
= *(u_int32_t
*)hp
->h_addr
;
1272 if (a
!= (u_int32_t
) -1)
1279 * auth_ip_addr - check whether the peer is authorized to use
1280 * a given IP address. Returns 1 if authorized, 0 otherwise.
1283 auth_ip_addr(unit
, addr
)
1287 return ip_addr_check(addr
, addresses
[unit
]);
1291 ip_addr_check(addr
, addrs
)
1293 struct wordlist
*addrs
;
1295 u_int32_t a
, mask
, ah
;
1297 char *ptr_word
, *ptr_mask
;
1301 /* don't allow loopback or multicast address */
1302 if (bad_ip_adrs(addr
))
1305 if (addrs
== NULL
) {
1307 return 0; /* no addresses authorized */
1308 return allow_any_ip
|| !have_route_to(addr
);
1311 for (; addrs
!= NULL
; addrs
= addrs
->next
) {
1312 /* "-" means no addresses authorized, "*" means any address allowed */
1313 ptr_word
= addrs
->word
;
1314 if (strcmp(ptr_word
, "-") == 0)
1316 if (strcmp(ptr_word
, "*") == 0)
1320 if (*ptr_word
== '!') {
1325 mask
= ~ (u_int32_t
) 0;
1326 ptr_mask
= strchr (ptr_word
, '/');
1327 if (ptr_mask
!= NULL
) {
1330 bit_count
= (int) strtol (ptr_mask
+1, (char **) 0, 10);
1331 if (bit_count
<= 0 || bit_count
> 32) {
1332 syslog (LOG_WARNING
,
1333 "invalid address length %s in auth. address list",
1338 mask
<<= 32 - bit_count
;
1341 hp
= gethostbyname(ptr_word
);
1342 if (hp
!= NULL
&& hp
->h_addrtype
== AF_INET
) {
1343 a
= *(u_int32_t
*)hp
->h_addr
;
1345 np
= getnetbyname (ptr_word
);
1346 if (np
!= NULL
&& np
->n_addrtype
== AF_INET
) {
1347 a
= htonl (*(u_int32_t
*)np
->n_net
);
1348 if (ptr_mask
== NULL
) {
1349 /* calculate appropriate mask for net */
1352 mask
= IN_CLASSA_NET
;
1353 else if (IN_CLASSB(ah
))
1354 mask
= IN_CLASSB_NET
;
1355 else if (IN_CLASSC(ah
))
1356 mask
= IN_CLASSC_NET
;
1359 a
= inet_addr (ptr_word
);
1363 if (ptr_mask
!= NULL
)
1366 if (a
== (u_int32_t
)-1L)
1367 syslog (LOG_WARNING
,
1368 "unknown host %s in auth. address list",
1371 /* Here a and addr are in network byte order,
1372 and mask is in host order. */
1373 if (((addr
^ a
) & htonl(mask
)) == 0)
1376 return 0; /* not in list => can't have it */
1380 * bad_ip_adrs - return 1 if the IP address is one we don't want
1381 * to use, such as an address in the loopback net or a multicast address.
1382 * addr is in network byte order.
1389 return (addr
>> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
1390 || IN_MULTICAST(addr
) || IN_BADCLASS(addr
);
1394 * check_access - complain if a secret file has too-liberal permissions.
1397 check_access(f
, filename
)
1403 if (fstat(fileno(f
), &sbuf
) < 0) {
1404 syslog(LOG_WARNING
, "cannot stat secret file %s: %m", filename
);
1405 } else if ((sbuf
.st_mode
& (S_IRWXG
| S_IRWXO
)) != 0) {
1406 syslog(LOG_WARNING
, "Warning - secret file %s has world and/or group access", filename
);
1412 * scan_authfile - Scan an authorization file for a secret suitable
1413 * for authenticating `client' on `server'. The return value is -1
1414 * if no secret is found, otherwise >= 0. The return value has
1415 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1416 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1417 * Any following words on the line (i.e. address authorization
1418 * info) are placed in a wordlist and returned in *addrs.
1421 scan_authfile(f
, client
, server
, ipaddr
, secret
, addrs
, filename
)
1427 struct wordlist
**addrs
;
1431 int got_flag
, best_flag
;
1433 struct wordlist
*ap
, *addr_list
, *alist
, *alast
;
1434 char word
[MAXWORDLEN
];
1435 char atfile
[MAXWORDLEN
];
1436 char lsecret
[MAXWORDLEN
];
1441 if (!getword(f
, word
, &newline
, filename
))
1442 return -1; /* file is empty??? */
1447 * Skip until we find a word at the start of a line.
1449 while (!newline
&& getword(f
, word
, &newline
, filename
))
1452 break; /* got to end of file */
1455 * Got a client - check if it's a match or a wildcard.
1458 if (client
!= NULL
&& strcmp(word
, client
) != 0 && !ISWILD(word
)) {
1463 got_flag
= NONWILD_CLIENT
;
1466 * Now get a server and check if it matches.
1468 if (!getword(f
, word
, &newline
, filename
))
1472 if (server
!= NULL
&& strcmp(word
, server
) != 0 && !ISWILD(word
))
1475 got_flag
|= NONWILD_SERVER
;
1478 * Got some sort of a match - see if it's better than what
1481 if (got_flag
<= best_flag
)
1487 if (!getword(f
, word
, &newline
, filename
))
1493 * Special syntax: @filename means read secret from file.
1495 if (word
[0] == '@') {
1496 strcpy(atfile
, word
+1);
1497 if ((sf
= fopen(atfile
, "r")) == NULL
) {
1498 syslog(LOG_WARNING
, "can't open indirect secret file %s",
1502 check_access(sf
, atfile
);
1503 if (!getword(sf
, word
, &xxx
, atfile
)) {
1504 syslog(LOG_WARNING
, "no secret in indirect secret file %s",
1512 strcpy(lsecret
, word
);
1515 * Now read address authorization info and make a wordlist.
1517 alist
= alast
= NULL
;
1519 if (!getword(f
, word
, &newline
, filename
) || newline
)
1521 ap
= (struct wordlist
*) malloc(sizeof(struct wordlist
)
1524 novm("authorized addresses");
1526 strcpy(ap
->word
, word
);
1535 * Check if the given IP address is allowed by the wordlist.
1537 if (ipaddr
!= 0 && !ip_addr_check(ipaddr
, alist
)) {
1538 free_wordlist(alist
);
1543 * This is the best so far; remember it.
1545 best_flag
= got_flag
;
1547 free_wordlist(addr_list
);
1550 strcpy(secret
, lsecret
);
1558 else if (addr_list
!= NULL
)
1559 free_wordlist(addr_list
);
1565 * free_wordlist - release memory allocated for a wordlist.
1569 struct wordlist
*wp
;
1571 struct wordlist
*next
;
1573 while (wp
!= NULL
) {
1581 * auth_script - execute a script with arguments
1582 * interface-name peer-name real-user tty speed
1594 if ((pw
= getpwuid(getuid())) != NULL
&& pw
->pw_name
!= NULL
)
1595 user_name
= pw
->pw_name
;
1597 sprintf(struid
, "%d", getuid());
1600 sprintf(strspeed
, "%d", baud_rate
);
1604 argv
[2] = peer_authname
;
1605 argv
[3] = user_name
;
1610 authup_pid
= run_program(script
, argv
, 0, NULL
, NULL
);