2 * auth.c - PPP authentication and phase control.
4 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 1993 The Australian National University.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by the Australian National University. The name of the University
16 * may not be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 * Copyright (c) 1989 Carnegie Mellon University.
23 * All rights reserved.
25 * Redistribution and use in source and binary forms are permitted
26 * provided that the above copyright notice and this paragraph are
27 * duplicated in all such forms and that any documentation,
28 * advertising materials, and other materials related to such
29 * distribution and use acknowledge that the software was developed
30 * by Carnegie Mellon University. The name of the
31 * University may not be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
35 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
38 #define RCSID "$Id: auth.c,v 1.65 2000/04/15 01:27:10 masputra Exp $"
40 /* Pull in crypt() definition. */
41 #define __EXTENSIONS__
50 #include <sys/types.h>
52 #include <sys/socket.h>
55 #if defined(_PATH_LASTLOG) && (defined(_linux_) || defined(__linux__))
59 #if defined(_linux_) || defined(__linux__)
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
67 /* Backward compatibility with old Makefiles */
68 #if defined(USE_PAM) && !defined(ALLOW_PAM)
73 #include <security/pam_appl.h>
79 #define PW_PPP PW_LOGIN
92 #include "pathnames.h"
94 static const char rcsid
[] = RCSID
;
96 /* Bits in scan_authfile return value */
97 #define NONWILD_SERVER 1
98 #define NONWILD_CLIENT 2
100 #define ISWILD(word) (word[0] == '*' && word[1] == '\0')
102 /* The name by which the peer authenticated itself to us. */
103 char peer_authname
[MAXNAMELEN
];
105 /* Records which authentication operations haven't completed yet. */
106 static int auth_pending
[NUM_PPP
];
108 /* Set if we have successfully called plogin() */
109 static int logged_in
;
111 /* List of addresses which the peer may use. */
112 static struct permitted_ip
*addresses
[NUM_PPP
];
114 /* Wordlist giving addresses which the peer may use
115 without authenticating itself. */
116 static struct wordlist
*noauth_addrs
;
118 /* Extra options to apply, from the secrets file entry for the peer. */
119 static struct wordlist
*extra_options
;
121 /* Source of those extra options. */
122 static const char *extra_opt_filename
;
123 static int extra_opt_line
;
125 /* Number of network protocols which we have opened. */
126 static int num_np_open
;
128 /* Number of network protocols which have come up. */
129 static int num_np_up
;
131 /* Set if we got the contents of passwd[] from the pap-secrets file. */
132 static int passwd_from_file
;
134 /* Set if we require authentication only because we have a default route. */
135 static bool default_auth
;
137 /* Hook to enable a plugin to control the idle time limit */
138 int (*idle_time_hook
) __P((struct ppp_idle
*)) = NULL
;
140 /* Hook for a plugin to say whether we can possibly authenticate any peer */
141 int (*pap_check_hook
) __P((void)) = NULL
;
143 /* Hook for a plugin to check the PAP user and password */
144 int (*pap_auth_hook
) __P((char *user
, char *passwd
, char **msgp
,
145 struct wordlist
**paddrs
,
146 struct wordlist
**popts
)) = NULL
;
148 /* Hook for a plugin to know about the PAP user logout */
149 void (*pap_logout_hook
) __P((void)) = NULL
;
151 /* Hook for a plugin to get the PAP password for authenticating us */
152 int (*pap_passwd_hook
) __P((char *user
, char *passwd
)) = NULL
;
155 * This is used to ensure that we don't start an auth-up/down
156 * script while one is already running.
163 static enum script_state auth_state
= s_down
;
164 static enum script_state auth_script_state
= s_down
;
165 static pid_t auth_script_pid
= 0;
168 * This is set by scan_authfile if a client matches, but server doesn't
169 * (possible configuration error).
171 static char scan_server_match_failed
[MAXWORDLEN
];
176 bool uselogin
= 0; /* Use /etc/passwd for checking PAP */
177 bool cryptpap
= 0; /* Passwords in pap-secrets are encrypted */
178 bool refuse_pap
= 0; /* Don't wanna auth. ourselves with PAP */
179 bool refuse_chap
= 0; /* Don't wanna auth. ourselves with CHAP */
180 bool usehostname
= 0; /* Use hostname for our_name */
181 bool auth_required
= 0; /* Always require authentication from peer */
182 bool allow_any_ip
= 0; /* Allow peer to use any IP address */
183 bool explicit_remote
= 0; /* User specified explicit remote name */
184 char remote_name
[MAXNAMELEN
]; /* Peer's name for authentication */
187 bool refuse_mschap
= 0; /* Don't wanna auth. ourself with MS-CHAPv1 */
189 bool refuse_mschap
= 1; /* Never auth. ourself with MS-CHAPv1 */
192 bool refuse_mschapv2
= 0; /* Don't wanna auth. ourself with MS-CHAPv2 */
194 bool refuse_mschapv2
= 1; /* Never auth. ourself with MS-CHAPv2 */
198 bool use_pam
= 1; /* Enable use of PAM by default */
200 bool use_pam
= 0; /* Disable use of PAM by default */
203 /* Bits in auth_pending[] */
204 #define PAP_WITHPEER 1
206 #define CHAP_WITHPEER 4
209 /* Prototypes for procedures local to this file. */
211 static void network_phase
__P((int));
212 static void check_idle
__P((void *));
213 static void connect_time_expired
__P((void *));
214 static int plogin
__P((char *, char *, char **));
215 static void plogout
__P((void));
216 static int null_login
__P((int));
217 static int get_pap_passwd
__P((char *));
218 static int have_pap_secret
__P((int *));
219 static int have_chap_secret
__P((char *, char *, int, int *));
220 static int ip_addr_check
__P((u_int32_t
, struct permitted_ip
*));
221 static int scan_authfile
__P((FILE *, char *, char *, char *,
222 struct wordlist
**, struct wordlist
**,
224 static void free_wordlist
__P((struct wordlist
*));
225 static void auth_script
__P((char *));
226 static void auth_script_done
__P((void *, int));
227 static void set_allowed_addrs
__P((int, struct wordlist
*, struct wordlist
*));
228 static int some_ip_ok
__P((struct wordlist
*));
229 static int setupapfile
__P((char **, option_t
*));
230 static int privgroup
__P((char **, option_t
*));
231 static int set_noauth_addr
__P((char **, option_t
*));
232 static void check_access
__P((FILE *, char *));
235 * Authentication-related options.
237 option_t auth_options
[] = {
238 { "require-pap", o_bool
, &lcp_wantoptions
[0].neg_upap
,
239 "Require PAP authentication from peer", 1, &auth_required
},
240 { "+pap", o_bool
, &lcp_wantoptions
[0].neg_upap
,
241 "Require PAP authentication from peer", 1, &auth_required
},
242 { "refuse-pap", o_bool
, &refuse_pap
,
243 "Don't agree to auth to peer with PAP", 1 },
244 { "-pap", o_bool
, &refuse_pap
,
245 "Don't allow PAP authentication with peer", 1 },
246 { "require-chap", o_bool
, &lcp_wantoptions
[0].neg_chap
,
247 "Require CHAP authentication from peer", 1, &auth_required
},
248 { "+chap", o_bool
, &lcp_wantoptions
[0].neg_chap
,
249 "Require CHAP authentication from peer", 1, &auth_required
},
250 { "refuse-chap", o_bool
, &refuse_chap
,
251 "Don't agree to auth to peer with CHAP", 1 },
252 { "-chap", o_bool
, &refuse_chap
,
253 "Don't allow CHAP authentication with peer", 1 },
254 { "name", o_string
, our_name
,
255 "Set local name for authentication",
256 OPT_PRIV
|OPT_STATIC
, NULL
, MAXNAMELEN
},
257 { "user", o_string
, user
,
258 "Set name for auth with peer", OPT_STATIC
, NULL
, MAXNAMELEN
},
259 { "usehostname", o_bool
, &usehostname
,
260 "Must use hostname for authentication", 1 },
261 { "remotename", o_string
, remote_name
,
262 "Set remote name for authentication", OPT_STATIC
,
263 &explicit_remote
, MAXNAMELEN
},
264 { "auth", o_bool
, &auth_required
,
265 "Require authentication from peer", 1 },
266 { "noauth", o_bool
, &auth_required
,
267 "Don't require peer to authenticate", OPT_PRIV
, &allow_any_ip
},
268 { "login", o_bool
, &uselogin
,
269 "Use system password database for PAP", 1 },
270 { "papcrypt", o_bool
, &cryptpap
,
271 "PAP passwords are encrypted", 1 },
272 { "+ua", o_special
, (void *)setupapfile
,
273 "Get PAP user and password from file" },
274 { "password", o_string
, passwd
,
275 "Password for authenticating us to the peer", OPT_STATIC
,
276 NULL
, MAXSECRETLEN
},
277 { "privgroup", o_special
, (void *)privgroup
,
278 "Allow group members to use privileged options", OPT_PRIV
},
279 { "allow-ip", o_special
, (void *)set_noauth_addr
,
280 "Set peer IP address(es) usable without authentication",
283 { "require-mschap", o_bool
, &lcp_wantoptions
[0].neg_mschap
,
284 "Require MS-CHAPv1 authentication from peer", 1, &auth_required
},
285 { "refuse-mschap", o_bool
, &refuse_mschap
,
286 "Don't agree to authenticate to peer with MS-CHAPv1", 1 },
289 { "require-mschapv2", o_bool
, &lcp_wantoptions
[0].neg_mschapv2
,
290 "Require MS-CHAPv2 authentication from peer", 1, &auth_required
},
291 { "refuse-mschapv2", o_bool
, &refuse_mschapv2
,
292 "Don't agree to authenticate to peer with MS-CHAPv2", 1 },
295 { "pam", o_bool
, &use_pam
,
296 "Enable use of Pluggable Authentication Modules", OPT_PRIV
|1 },
297 { "nopam", o_bool
, &use_pam
,
298 "Disable use of Pluggable Authentication Modules", OPT_PRIV
|0 },
304 * setupapfile - specifies UPAP info for authenticating with peer.
308 setupapfile(argv
, opt
)
315 lcp_allowoptions
[0].neg_upap
= 1;
317 /* open user info file */
318 (void) seteuid(getuid());
319 ufile
= fopen(*argv
, "r");
322 option_error("unable to open user login data file %s", *argv
);
325 check_access(ufile
, *argv
);
328 if (fgets(user
, MAXNAMELEN
- 1, ufile
) == NULL
329 || fgets(passwd
, MAXSECRETLEN
- 1, ufile
) == NULL
){
330 option_error("unable to read user login data file %s", *argv
);
333 (void) fclose(ufile
);
335 /* get rid of newlines */
337 if (l
> 0 && user
[l
-1] == '\n')
340 if (l
> 0 && passwd
[l
-1] == '\n')
348 * privgroup - allow members of the group to have privileged access.
361 option_error("group %s is unknown", *argv
);
364 for (i
= 0; i
< ngroups
; ++i
) {
365 if (groups
[i
] == g
->gr_gid
) {
375 * set_noauth_addr - set address(es) that can be used without authentication.
376 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
380 set_noauth_addr(argv
, opt
)
385 int l
= strlen(addr
);
388 wp
= (struct wordlist
*) malloc(sizeof(struct wordlist
) + l
+ 1);
390 novm("allow-ip argument");
391 wp
->word
= (char *) (wp
+ 1);
392 wp
->next
= noauth_addrs
;
393 (void) strcpy(wp
->word
, addr
);
399 * An Open on LCP has requested a change from Dead to Establish phase.
400 * Do what's necessary to bring the physical layer up.
410 * LCP has terminated the link; go to the Dead phase and take the
411 * physical layer down.
415 link_terminated(unit
)
418 const char *pn1
, *pn2
;
420 if (phase
== PHASE_DEAD
)
422 if (pap_logout_hook
!= NULL
) {
423 (*pap_logout_hook
)();
428 new_phase(PHASE_DEAD
);
430 if ((pn1
= protocol_name(nak_auth_orig
)) == NULL
)
432 if ((pn2
= protocol_name(nak_auth_proto
)) == NULL
)
434 warn("Peer sent Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
435 nak_auth_orig
, pn1
, nak_auth_proto
, pn2
);
437 if (unsolicited_nak_auth
) {
438 if ((pn1
= protocol_name(unsolicit_auth_proto
)) == NULL
)
440 warn("Peer unexpectedly asked us to authenticate with 0x%x (%s)",
441 unsolicit_auth_proto
, pn1
);
443 if (peer_reject_auth
) {
444 if ((pn1
= protocol_name(reject_auth_proto
)) == NULL
)
446 warn("Peer rejected our demand for 0x%x (%s)",
447 reject_auth_proto
, pn1
);
449 if (naked_peers_auth
) {
450 if ((pn1
= protocol_name(naked_auth_orig
)) == NULL
)
452 if ((pn2
= protocol_name(naked_auth_proto
)) == NULL
)
454 warn("We set Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
455 naked_auth_orig
, pn1
, naked_auth_proto
, pn2
);
457 if (rejected_peers_auth
) {
458 if ((pn1
= protocol_name(rejected_auth_proto
)) == NULL
)
460 warn("We rejected the peer's demand for 0x%x (%s)",
461 rejected_auth_proto
, pn1
);
464 peer_nak_auth
= unsolicited_nak_auth
= peer_reject_auth
=
465 rejected_peers_auth
= naked_peers_auth
= 0;
466 nak_auth_proto
= nak_auth_orig
= unsolicit_auth_proto
= reject_auth_proto
=
467 rejected_auth_proto
= naked_auth_orig
= naked_auth_proto
= 0;
468 notice("Connection terminated.");
472 * LCP has gone down; it will either die or try to re-establish.
479 struct protent
*protp
;
482 if (auth_script_state
== s_up
&& auth_script_pid
== 0) {
483 update_link_stats(unit
);
484 auth_script_state
= s_down
;
485 auth_script(_PATH_AUTHDOWN
);
487 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
) {
488 if (!protp
->enabled_flag
)
490 if (protp
->protocol
!= PPP_LCP
&& protp
->lowerdown
!= NULL
)
491 (*protp
->lowerdown
)(unit
);
492 if (protp
->protocol
< 0xC000 && protp
->close
!= NULL
)
493 (*protp
->close
)(unit
, "LCP down");
497 if (phase
!= PHASE_DEAD
)
498 new_phase(PHASE_TERMINATE
);
502 * The link is established.
503 * Proceed to the Dead, Authenticate or Network phase as appropriate.
506 link_established(unit
)
510 lcp_options
*wo
= &lcp_wantoptions
[unit
];
511 lcp_options
*go
= &lcp_gotoptions
[unit
];
512 lcp_options
*ho
= &lcp_hisoptions
[unit
];
514 struct protent
*protp
;
517 * Tell higher-level protocols that LCP is up.
519 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
520 if (protp
->protocol
!= PPP_LCP
&& protp
->enabled_flag
521 && protp
->lowerup
!= NULL
)
522 (*protp
->lowerup
)(unit
);
524 if (auth_required
&& !(go
->neg_chap
|| go
->neg_mschap
||
525 go
->neg_mschapv2
|| go
->neg_upap
)) {
527 * We wanted the peer to authenticate itself, and it refused:
528 * if we have some address(es) it can use without auth, fine,
529 * otherwise treat it as though it authenticated with PAP using
530 * a username * of "" and a password of "". If that's not OK,
533 if (noauth_addrs
!= NULL
) {
534 set_allowed_addrs(unit
, noauth_addrs
, NULL
);
535 } else if (!wo
->neg_upap
|| !null_login(unit
)) {
536 warn("peer refused to authenticate: terminating link");
537 lcp_close(unit
, "peer refused to authenticate");
538 status
= EXIT_PEER_AUTH_FAILED
;
543 new_phase(PHASE_AUTHENTICATE
);
545 if (go
->neg_chap
|| go
->neg_mschap
|| go
->neg_mschapv2
) {
548 dbglog("Authenticating peer with standard CHAP");
549 go
->chap_mdtype
= CHAP_DIGEST_MD5
;
550 } else if (go
->neg_mschap
) {
552 dbglog("Authenticating peer with MS-CHAPv1");
553 go
->chap_mdtype
= CHAP_MICROSOFT
;
556 dbglog("Authenticating peer with MS-CHAPv2");
557 go
->chap_mdtype
= CHAP_MICROSOFT_V2
;
559 ChapAuthPeer(unit
, our_name
, go
->chap_mdtype
);
561 } else if (go
->neg_upap
) {
563 dbglog("Authenticating peer with PAP");
567 if (ho
->neg_chap
|| ho
->neg_mschap
|| ho
->neg_mschapv2
) {
568 switch (ho
->chap_mdtype
) {
569 case CHAP_DIGEST_MD5
:
571 dbglog("Authenticating to peer with standard CHAP");
575 dbglog("Authenticating to peer with MS-CHAPv1");
577 case CHAP_MICROSOFT_V2
:
579 dbglog("Authenticating to peer with MS-CHAPv2");
583 dbglog("Authenticating to peer with CHAP 0x%x", ho
->chap_mdtype
);
586 ChapAuthWithPeer(unit
, user
, ho
->chap_mdtype
);
587 auth
|= CHAP_WITHPEER
;
588 } else if (ho
->neg_upap
) {
589 if (passwd
[0] == '\0') {
590 passwd_from_file
= 1;
591 if (!get_pap_passwd(passwd
))
592 error("No secret found for PAP login");
595 dbglog("Authenticating to peer with PAP");
596 upap_authwithpeer(unit
, user
, passwd
);
597 auth
|= PAP_WITHPEER
;
599 auth_pending
[unit
] = auth
;
606 * Proceed to the network phase.
612 lcp_options
*go
= &lcp_gotoptions
[unit
];
615 * If the peer had to authenticate, run the auth-up script now.
617 if (go
->neg_chap
|| go
->neg_mschap
|| go
->neg_mschapv2
|| go
->neg_upap
) {
619 if (auth_script_state
== s_down
&& auth_script_pid
== 0) {
620 auth_script_state
= s_up
;
621 auth_script(_PATH_AUTHUP
);
626 * Process extra options from the secrets file
628 if (extra_options
!= NULL
) {
629 option_source
= (char *)extra_opt_filename
;
630 option_line
= extra_opt_line
;
631 (void) options_from_list(extra_options
, 1);
632 free_wordlist(extra_options
);
633 extra_options
= NULL
;
638 * If we negotiated callback, do it now.
641 new_phase(PHASE_CALLBACK
);
642 (*cbcp_protent
.open
)(unit
);
654 struct protent
*protp
;
656 new_phase(PHASE_NETWORK
);
658 #ifdef HAVE_MULTILINK
660 if (mp_join_bundle()) {
661 if (updetach
&& !nodetach
)
666 #endif /* HAVE_MULTILINK */
670 set_filters(&pass_filter
, &active_filter
);
672 for (i
= 0; (protp
= protocols
[i
]) != NULL
; ++i
)
673 if (protp
->protocol
< 0xC000 && protp
->enabled_flag
674 && protp
->open
!= NULL
) {
676 if (protp
->protocol
!= PPP_CCP
)
680 if (num_np_open
== 0)
682 lcp_close(0, "No network protocols running");
686 * The peer has failed to authenticate himself using `protocol'.
690 auth_peer_fail(unit
, protocol
)
694 * Authentication failure: take the link down
696 lcp_close(unit
, "Authentication failed");
697 status
= EXIT_PEER_AUTH_FAILED
;
701 * The peer has been successfully authenticated using `protocol'.
704 auth_peer_success(unit
, protocol
, name
, namelen
)
719 warn("auth_peer_success: unknown protocol %x", protocol
);
724 * Save the authenticated name of the peer for later.
726 if (namelen
> sizeof(peer_authname
) - 1)
727 namelen
= sizeof(peer_authname
) - 1;
728 BCOPY(name
, peer_authname
, namelen
);
729 peer_authname
[namelen
] = '\0';
730 script_setenv("PEERNAME", peer_authname
, 0);
733 * If there is no more authentication still to be done,
734 * proceed to the network (or callback) phase.
736 if ((auth_pending
[unit
] &= ~bit
) == 0)
741 * We have failed to authenticate ourselves to the peer using `protocol'.
745 auth_withpeer_fail(unit
, protocol
)
748 if (passwd_from_file
)
749 BZERO(passwd
, MAXSECRETLEN
);
751 * We've failed to authenticate ourselves to our peer.
752 * Some servers keep sending CHAP challenges, but there
753 * is no point in persisting without any way to get updated
754 * authentication secrets.
756 lcp_close(unit
, "Failed to authenticate ourselves to peer");
757 status
= EXIT_AUTH_TOPEER_FAILED
;
761 * We have successfully authenticated ourselves with the peer using `protocol'.
764 auth_withpeer_success(unit
, protocol
)
774 if (passwd_from_file
)
775 BZERO(passwd
, MAXSECRETLEN
);
779 warn("auth_withpeer_success: unknown protocol %x", protocol
);
784 * If there is no more authentication still being done,
785 * proceed to the network (or callback) phase.
787 if ((auth_pending
[unit
] &= ~bit
) == 0)
793 * np_up - a network protocol has come up.
802 if (num_np_up
== 0) {
804 * At this point we consider that the link has come up successfully.
809 peer_nak_auth
= unsolicited_nak_auth
= peer_reject_auth
=
810 rejected_peers_auth
= naked_peers_auth
= 0;
811 nak_auth_proto
= nak_auth_orig
= unsolicit_auth_proto
=
812 reject_auth_proto
= rejected_auth_proto
= naked_auth_orig
=
813 naked_auth_proto
= 0;
815 new_phase(PHASE_RUNNING
);
817 if (idle_time_hook
!= NULL
)
818 tlim
= (*idle_time_hook
)(NULL
);
820 tlim
= idle_time_limit
;
822 TIMEOUT(check_idle
, NULL
, tlim
);
825 * Set a timeout to close the connection once the maximum
826 * connect time has expired.
828 if (maxconnect
> 0) {
829 TIMEOUT(connect_time_expired
, &lcp_fsm
[unit
], maxconnect
);
832 * Tell LCP to send Time-Remaining packets. One should be
833 * sent out now, at maxconnect-300, at maxconnect-120, and
834 * again at maxconnect-30.
836 lcp_settimeremaining(unit
, maxconnect
, maxconnect
);
837 if (maxconnect
> 300)
838 lcp_settimeremaining(unit
, maxconnect
, 300);
839 if (maxconnect
> 120)
840 lcp_settimeremaining(unit
, maxconnect
, 120);
842 lcp_settimeremaining(unit
, maxconnect
, 30);
846 * Detach now, if the updetach option was given.
848 if (updetach
&& !nodetach
)
855 * np_down - a network protocol has gone down.
862 if (--num_np_up
== 0) {
863 UNTIMEOUT(check_idle
, NULL
);
864 new_phase(PHASE_NETWORK
);
869 * np_finished - a network protocol has finished using the link.
873 np_finished(unit
, proto
)
876 if (--num_np_open
<= 0) {
877 /* no further use for the link: shut up shop. */
878 lcp_close(0, "No network protocols running");
883 * check_idle - check whether the link has been idle for long
884 * enough that we can shut it down.
891 struct ppp_idle idle
;
895 if (!get_idle_time(0, &idle
))
897 if (idle_time_hook
!= NULL
) {
898 tlim
= (*idle_time_hook
)(&idle
);
900 itime
= MIN(idle
.xmit_idle
, idle
.recv_idle
);
901 tlim
= idle_time_limit
- itime
;
904 /* link is idle: shut it down. */
905 notice("Terminating connection due to lack of activity.");
906 lcp_close(0, "Link inactive");
908 status
= EXIT_IDLE_TIMEOUT
;
910 TIMEOUT(check_idle
, NULL
, tlim
);
915 * connect_time_expired - log a message and close the connection.
919 connect_time_expired(arg
)
924 info("Connect time expired");
925 lcp_close(f
->unit
, "Connect time expired"); /* Close connection */
926 status
= EXIT_CONNECT_TIME
;
930 * auth_check_options - called to check authentication options.
935 lcp_options
*wo
= &lcp_wantoptions
[0];
939 /* Default our_name to hostname, and user to our_name */
940 if (our_name
[0] == '\0' || usehostname
)
941 (void) strlcpy(our_name
, hostname
, sizeof(our_name
));
943 (void) strlcpy(user
, our_name
, sizeof(user
));
946 * If we have a default route, require the peer to authenticate
947 * unless the noauth option was given or the real user is root.
949 if (!auth_required
&& !allow_any_ip
&& have_route_to(0) && !privileged
) {
954 /* If authentication is required, ask peer for CHAP or PAP. */
956 if (!wo
->neg_chap
&& !wo
->neg_mschap
&& !wo
->neg_mschapv2
&&
963 wo
->neg_mschapv2
= 1;
965 wo
->chap_mdtype
= CHAP_DIGEST_MD5
;
971 wo
->neg_mschapv2
= 0;
976 * Check whether we have appropriate secrets to use
977 * to authenticate the peer.
980 can_auth
= wo
->neg_upap
&& (uselogin
|| have_pap_secret(&lacks_ip
));
981 if (!can_auth
&& (wo
->neg_chap
|| wo
->neg_mschap
|| wo
->neg_mschapv2
)) {
982 can_auth
= have_chap_secret((explicit_remote
? remote_name
: NULL
),
983 our_name
, 1, &lacks_ip
);
986 if (auth_required
&& !can_auth
&& noauth_addrs
== NULL
) {
989 "By default the remote system is required to authenticate itself");
991 "(because this system has a default route to the Internet)");
992 } else if (explicit_remote
)
994 "The remote system (%s) is required to authenticate itself",
998 "The remote system is required to authenticate itself");
1000 "but I couldn't find any suitable secret (password) for it to use to do so.");
1003 "(None of the available passwords would let it use an IP address.)");
1010 * auth_reset - called when LCP is starting negotiations to recheck
1011 * authentication options, i.e. whether we have appropriate secrets
1012 * to use for authenticating ourselves and/or the peer.
1018 lcp_options
*go
= &lcp_gotoptions
[unit
];
1019 lcp_options
*ao
= &lcp_allowoptions
[unit
];
1022 ao
->neg_upap
= !refuse_pap
&& (passwd
[0] != '\0' || get_pap_passwd(NULL
));
1024 havesecret
= passwd
[0] != '\0' ||
1025 have_chap_secret(user
, (explicit_remote
? remote_name
: NULL
), 0, NULL
);
1026 ao
->neg_chap
= !refuse_chap
&& havesecret
;
1027 ao
->neg_mschap
= !refuse_mschap
&& havesecret
;
1028 ao
->neg_mschapv2
= !refuse_mschapv2
&& havesecret
;
1030 ao
->chap_mdtype
= CHAP_DIGEST_MD5
;
1031 else if (ao
->neg_mschap
)
1032 ao
->chap_mdtype
= CHAP_MICROSOFT
;
1034 ao
->chap_mdtype
= CHAP_MICROSOFT_V2
;
1036 if (go
->neg_upap
&& !uselogin
&& !have_pap_secret(NULL
))
1038 if (go
->neg_chap
|| go
->neg_mschap
|| go
->neg_mschapv2
) {
1039 havesecret
= have_chap_secret((explicit_remote
? remote_name
: NULL
),
1042 go
->neg_chap
= go
->neg_mschap
= go
->neg_mschapv2
= 0;
1043 else if (go
->neg_chap
)
1044 go
->chap_mdtype
= CHAP_DIGEST_MD5
;
1045 else if (go
->neg_mschap
)
1046 go
->chap_mdtype
= CHAP_MICROSOFT
;
1048 go
->chap_mdtype
= CHAP_MICROSOFT_V2
;
1054 * check_passwd - Check the user name and passwd against the PAP secrets
1055 * file. If requested, also check against the system password database,
1056 * and login the user if OK.
1059 * UPAP_AUTHNAK: Authentication failed.
1060 * UPAP_AUTHACK: Authentication succeeded.
1061 * In either case, msg points to an appropriate message.
1064 check_passwd(unit
, auser
, userlen
, apasswd
, passwdlen
, msg
)
1075 struct wordlist
*addrs
= NULL
, *opts
= NULL
;
1076 char passwd
[256], user
[256];
1077 char secret
[MAXWORDLEN
];
1078 static int attempts
= 0;
1081 * Make copies of apasswd and auser, then null-terminate them.
1082 * If there are unprintable characters in the password, make
1085 (void) slprintf(passwd
, sizeof(passwd
), "%.*v", passwdlen
, apasswd
);
1086 (void) slprintf(user
, sizeof(user
), "%.*v", userlen
, auser
);
1090 * Check if a plugin wants to handle this.
1092 if (pap_auth_hook
!= NULL
) {
1093 /* Set a default and allow the plug-in to change it. */
1094 extra_opt_filename
= "plugin";
1096 ret
= (*pap_auth_hook
)(user
, passwd
, msg
, &addrs
, &opts
);
1099 set_allowed_addrs(unit
, addrs
, opts
);
1100 BZERO(passwd
, sizeof(passwd
));
1102 free_wordlist(addrs
);
1103 return ret
? UPAP_AUTHACK
: UPAP_AUTHNAK
;
1108 * Open the file of pap secrets and scan for a suitable secret
1109 * for authenticating this user.
1111 filename
= _PATH_UPAPFILE
;
1112 addrs
= opts
= NULL
;
1114 f
= fopen(filename
, "r");
1116 error("Can't open PAP password file %s: %m", filename
);
1119 check_access(f
, filename
);
1120 if (scan_authfile(f
, user
, our_name
, secret
, &addrs
, &opts
, filename
) < 0) {
1121 warn("no PAP secret found for %s", user
);
1122 if (scan_server_match_failed
[0] != '\0')
1123 warn("possible configuration error: local name is %q, but "
1124 "found %q instead", our_name
, scan_server_match_failed
);
1125 } else if (secret
[0] != '\0') {
1126 /* password given in pap-secrets - must match */
1127 if ((!cryptpap
&& strcmp(passwd
, secret
) == 0)
1128 || strcmp(crypt(passwd
, secret
), secret
) == 0)
1131 warn("PAP authentication failure for %s", user
);
1132 } else if (uselogin
) {
1133 /* empty password in pap-secrets and login option */
1134 ret
= plogin(user
, passwd
, msg
);
1135 if (ret
== UPAP_AUTHNAK
)
1136 warn("PAP login failure for %s", user
);
1138 /* empty password in pap-secrets and login option not used */
1144 if (ret
== UPAP_AUTHNAK
) {
1146 *msg
= "Login incorrect";
1148 * Frustrate passwd stealer programs.
1149 * Allow 10 tries, but start backing off after 3 (stolen from login).
1150 * On 10'th, drop the connection.
1152 if (attempts
++ >= 10) {
1153 warn("%d LOGIN FAILURES ON %s, %s", attempts
, devnam
, user
);
1154 lcp_close(unit
, "login failed");
1157 (void) sleep((u_int
) (attempts
- 3) * 5);
1159 free_wordlist(opts
);
1162 attempts
= 0; /* Reset count */
1165 set_allowed_addrs(unit
, addrs
, opts
);
1169 free_wordlist(addrs
);
1170 BZERO(passwd
, sizeof(passwd
));
1171 BZERO(secret
, sizeof(secret
));
1177 * This function is needed for PAM.
1181 /* Static variables used to communicate between the conversation function
1182 * and the server_login function
1184 static char *PAM_username
;
1185 static char *PAM_password
;
1186 static int PAM_error
= 0;
1187 static pam_handle_t
*pamh
= NULL
;
1189 /* PAM conversation function
1190 * Here we assume (for now, at least) that echo on means login name, and
1191 * echo off means password.
1195 static int PAM_conv (int num_msg
,
1199 struct pam_message
**msg
, struct pam_response
**resp
, void *appdata_ptr
)
1202 struct pam_response
*reply
= NULL
;
1204 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1206 reply
= malloc(sizeof(struct pam_response
) * num_msg
);
1208 return PAM_CONV_ERR
;
1210 for (replies
= 0; replies
< num_msg
; replies
++) {
1211 switch (msg
[replies
]->msg_style
) {
1212 case PAM_PROMPT_ECHO_ON
:
1213 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
1214 reply
[replies
].resp
= COPY_STRING(PAM_username
);
1215 /* PAM frees resp */
1217 case PAM_PROMPT_ECHO_OFF
:
1218 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
1219 reply
[replies
].resp
= COPY_STRING(PAM_password
);
1220 /* PAM frees resp */
1225 /* ignore it, but pam still wants a NULL response... */
1226 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
1227 reply
[replies
].resp
= NULL
;
1230 /* Must be an error of some sort... */
1233 return PAM_CONV_ERR
;
1240 static struct pam_conv PAM_conversation
= {
1243 #endif /* ALLOW_PAM */
1246 * plogin - Check the user name and password against the system
1247 * password database, and login the user if OK.
1250 * UPAP_AUTHNAK: Login failed.
1251 * UPAP_AUTHACK: Login succeeded.
1252 * In either case, msg points to an appropriate message.
1256 plogin(user
, passwd
, msg
)
1265 struct passwd
*pw
= NULL
;
1272 dbglog("using PAM for user authentication");
1273 pam_error
= pam_start ("ppp", user
, &PAM_conversation
, &pamh
);
1274 if (pam_error
!= PAM_SUCCESS
) {
1275 *msg
= (char *) pam_strerror (pamh
, pam_error
);
1277 return UPAP_AUTHNAK
;
1280 * Define the fields for the credential validation
1283 PAM_username
= user
;
1284 PAM_password
= passwd
;
1286 /* this might be useful to some modules; required for Solaris */
1290 (void) pam_set_item(pamh
, PAM_TTY
, tty
);
1292 (void) pam_set_item(pamh
, PAM_RHOST
, "");
1298 pam_error
= pam_authenticate (pamh
, PAM_SILENT
);
1299 if (pam_error
== PAM_SUCCESS
&& !PAM_error
) {
1300 pam_error
= pam_acct_mgmt (pamh
, PAM_SILENT
);
1301 if (pam_error
== PAM_SUCCESS
)
1302 (void) pam_open_session (pamh
, PAM_SILENT
);
1305 *msg
= (char *) pam_strerror (pamh
, pam_error
);
1310 reopen_log(); /* apparently the PAM stuff does closelog() */
1311 PAM_username
= NULL
;
1312 PAM_password
= NULL
;
1313 if (pam_error
!= PAM_SUCCESS
)
1314 return UPAP_AUTHNAK
;
1316 #endif /* ALLOW_PAM */
1321 dbglog("using passwd/shadow for user authentication");
1323 dbglog("using passwd for user authentication");
1327 * Use the non-PAM methods directly
1330 pw
= getpwnam(user
);
1334 return (UPAP_AUTHNAK
);
1337 spwd
= getspnam(user
);
1340 /* check the age of the password entry */
1341 long now
= time(NULL
) / 86400L;
1343 if ((spwd
->sp_expire
> 0 && now
>= spwd
->sp_expire
)
1344 || ((spwd
->sp_max
>= 0 && spwd
->sp_max
< 10000)
1345 && spwd
->sp_lstchg
>= 0
1346 && now
>= spwd
->sp_lstchg
+ spwd
->sp_max
)) {
1347 warn("Password for %s has expired", user
);
1348 return (UPAP_AUTHNAK
);
1350 pw
->pw_passwd
= spwd
->sp_pwdp
;
1355 * If no passwd, don't let them login.
1357 if (pw
->pw_passwd
== NULL
|| strlen(pw
->pw_passwd
) < 2 ||
1358 strcmp(crypt(passwd
, pw
->pw_passwd
), pw
->pw_passwd
) != 0)
1359 return (UPAP_AUTHNAK
);
1363 * Write a wtmp entry for this user.
1367 if (strncmp(tty
, "/dev/", 5) == 0)
1369 logwtmp(tty
, user
, remote_name
); /* Add wtmp login entry */
1371 info("user %s logged in", user
);
1374 return (UPAP_AUTHACK
);
1378 * plogout - Logout the user.
1390 pam_error
= pam_close_session (pamh
, PAM_SILENT
);
1391 (void) pam_end (pamh
, pam_error
);
1394 /* Apparently the pam stuff does closelog(). */
1397 #endif /* ALLOW_PAM */
1401 if (strncmp(tty
, "/dev/", 5) == 0)
1403 /* Wipe out utmp logout entry */
1404 logwtmp(tty
, "", "");
1411 * null_login - Check if a username of "" and a password of "" are
1412 * acceptable, and iff so, set the list of acceptable IP addresses
1422 struct wordlist
*addrs
, *opts
;
1423 char secret
[MAXWORDLEN
];
1426 * Open the file of pap secrets and scan for a suitable secret.
1428 filename
= _PATH_UPAPFILE
;
1430 f
= fopen(filename
, "r");
1433 check_access(f
, filename
);
1435 i
= scan_authfile(f
, "", our_name
, secret
, &addrs
, &opts
, filename
);
1436 ret
= i
>= 0 && secret
[0] == '\0';
1437 BZERO(secret
, sizeof(secret
));
1440 set_allowed_addrs(unit
, addrs
, opts
);
1441 else if (opts
!= NULL
)
1442 free_wordlist(opts
);
1444 free_wordlist(addrs
);
1452 * get_pap_passwd - get a password for authenticating ourselves with
1453 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1455 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1458 get_pap_passwd(passwd
)
1464 char secret
[MAXWORDLEN
];
1467 * Check whether a plugin wants to supply this.
1469 if (pap_passwd_hook
!= NULL
) {
1470 ret
= (*pap_passwd_hook
)(user
, passwd
);
1475 filename
= _PATH_UPAPFILE
;
1476 f
= fopen(filename
, "r");
1479 check_access(f
, filename
);
1480 ret
= scan_authfile(f
, user
,
1481 (remote_name
[0] != '\0' ? remote_name
: NULL
),
1482 secret
, NULL
, NULL
, filename
);
1487 (void) strlcpy(passwd
, secret
, MAXSECRETLEN
);
1488 BZERO(secret
, sizeof(secret
));
1494 * have_pap_secret - check whether we have a PAP file with any
1495 * secrets that we could possibly use for authenticating the peer.
1498 have_pap_secret(lacks_ipp
)
1504 struct wordlist
*addrs
;
1506 /* let the plugin decide, if there is one */
1507 if (pap_check_hook
!= NULL
) {
1508 ret
= (*pap_check_hook
)();
1513 filename
= _PATH_UPAPFILE
;
1514 f
= fopen(filename
, "r");
1518 ret
= scan_authfile(f
, (explicit_remote
? remote_name
: NULL
), our_name
,
1519 NULL
, &addrs
, NULL
, filename
);
1521 if (ret
>= 0 && !some_ip_ok(addrs
)) {
1522 if (lacks_ipp
!= NULL
)
1527 free_wordlist(addrs
);
1534 * have_chap_secret - check whether we have a CHAP file with a secret
1535 * that we could possibly use for authenticating `client' on `server'.
1536 * Either or both can be the null string, meaning we don't know the
1540 have_chap_secret(client
, server
, need_ip
, lacks_ipp
)
1549 struct wordlist
*addrs
;
1551 filename
= _PATH_CHAPFILE
;
1552 f
= fopen(filename
, "r");
1556 if (client
!= NULL
&& client
[0] == '\0')
1558 if (server
!= NULL
&& server
[0] == '\0')
1561 ret
= scan_authfile(f
, client
, server
, NULL
, &addrs
, NULL
, filename
);
1563 if (ret
>= 0 && need_ip
&& !some_ip_ok(addrs
)) {
1564 if (lacks_ipp
!= NULL
)
1569 free_wordlist(addrs
);
1576 * get_secret - open the CHAP secret file and return the secret
1577 * for authenticating the given client on the given server.
1579 * "am_server" means that we're the authenticator (demanding
1580 * identity from the peer).
1582 * "!am_server" means that we're the authenticatee (supplying
1583 * identity to the peer).
1586 get_secret(unit
, client
, server
, secret
, secret_len
, am_server
)
1597 struct wordlist
*addrs
, *opts
;
1598 char secbuf
[MAXWORDLEN
];
1601 * Support the 'password' option on authenticatee only in order to
1602 * avoid obvious security problem (authenticator and authenticatee
1603 * within a given implementation must never share secrets).
1605 if (!am_server
&& passwd
[0] != '\0') {
1606 (void) strlcpy(secbuf
, passwd
, sizeof(secbuf
));
1608 filename
= _PATH_CHAPFILE
;
1612 f
= fopen(filename
, "r");
1614 error("Can't open chap secret file %s: %m", filename
);
1617 check_access(f
, filename
);
1619 ret
= scan_authfile(f
, client
, server
, secbuf
, &addrs
, &opts
,
1623 if (scan_server_match_failed
[0] != '\0')
1624 warn("possible configuration error: local name is %q, but "
1625 "found %q instead", our_name
, scan_server_match_failed
);
1629 /* Only the authenticator cares about limiting peer addresses. */
1631 set_allowed_addrs(unit
, addrs
, opts
);
1632 else if (opts
!= NULL
)
1633 free_wordlist(opts
);
1635 free_wordlist(addrs
);
1638 len
= strlen(secbuf
);
1639 if (len
> MAXSECRETLEN
) {
1640 error("Secret for %s on %s is too long", client
, server
);
1643 /* Do not leave a temporary copy of the secret on the stack. */
1644 BCOPY(secbuf
, secret
, len
);
1645 BZERO(secbuf
, sizeof(secbuf
));
1652 * set_allowed_addrs() - set the list of allowed addresses.
1653 * The caller must also look for `--' indicating options to apply for
1654 * this peer and leaves the following words in extra_options.
1657 set_allowed_addrs(unit
, addrs
, opts
)
1659 struct wordlist
*addrs
;
1660 struct wordlist
*opts
;
1663 struct wordlist
*ap
, **pap
;
1664 struct permitted_ip
*ip
;
1665 char *ptr_word
, *ptr_mask
;
1668 u_int32_t a
, mask
, newmask
, ah
, offset
;
1669 struct ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
1670 u_int32_t suggested_ip
= 0;
1673 free(addresses
[unit
]);
1674 addresses
[unit
] = NULL
;
1675 if (extra_options
!= NULL
)
1676 free_wordlist(extra_options
);
1677 extra_options
= opts
;
1680 * Count the number of IP addresses given.
1682 for (n
= 0, pap
= &addrs
; (ap
= *pap
) != NULL
; pap
= &ap
->next
)
1686 ip
= (struct permitted_ip
*) malloc((n
+ 1) * sizeof(struct permitted_ip
));
1691 for (ap
= addrs
; ap
!= NULL
; ap
= ap
->next
) {
1692 /* "-" means no addresses authorized, "*" means any address allowed */
1693 ptr_word
= ap
->word
;
1694 if (strcmp(ptr_word
, "-") == 0)
1696 if (strcmp(ptr_word
, "*") == 0) {
1698 ip
[n
].base
= ip
[n
].mask
= 0;
1704 if (*ptr_word
== '!') {
1709 mask
= ~ (u_int32_t
) 0;
1711 ptr_mask
= strchr (ptr_word
, '/');
1712 if (ptr_mask
!= NULL
) {
1716 bit_count
= (int) strtol (ptr_mask
+1, &endp
, 10);
1717 if (bit_count
<= 0 || bit_count
> 32) {
1718 warn("invalid address length %v in authorized address list",
1722 bit_count
= 32 - bit_count
; /* # bits in host part */
1724 offset
= ifunit
+ 1;
1727 if (*endp
!= '\0') {
1728 warn("invalid address length syntax: %v", ptr_mask
+1);
1735 /* Try to interpret value as host name or numeric address first */
1736 hp
= getipnodebyname(ptr_word
, AF_INET
, 0, &err_num
);
1738 (void) memcpy(&a
, hp
->h_addr
, sizeof(a
));
1741 char *cp
= ptr_word
+ strlen(ptr_word
);
1745 offset
= ifunit
+ 1;
1748 np
= getnetbyname (ptr_word
);
1749 if (np
!= NULL
&& np
->n_addrtype
== AF_INET
) {
1751 newmask
= (u_int32_t
)~0;
1752 if ((ah
& 0xff000000ul
) == 0)
1753 ah
<<= 8, newmask
<<= 8;
1754 if ((ah
& 0xff000000ul
) == 0)
1755 ah
<<= 8, newmask
<<= 8;
1756 if ((ah
& 0xff000000ul
) == 0)
1757 ah
<<= 8, newmask
<<= 8;
1758 if (ptr_mask
== NULL
)
1764 if (ptr_mask
!= NULL
)
1767 if (a
== (u_int32_t
)-1L) {
1768 warn("unknown host %s in auth. address list", ap
->word
);
1772 if (offset
>= ~mask
) {
1773 warn("interface unit %d too large for subnet %v",
1777 a
= htonl((ntohl(a
) & mask
) + offset
);
1778 mask
= ~(u_int32_t
)0;
1780 ip
[n
].mask
= htonl(mask
);
1781 ip
[n
].base
= a
& ip
[n
].mask
;
1783 if (~mask
== 0 && suggested_ip
== 0)
1787 /* Sentinel value at end of list */
1788 ip
[n
].permit
= 0; /* make the last entry forbid all addresses */
1789 ip
[n
].base
= 0; /* to terminate the list */
1792 addresses
[unit
] = ip
;
1795 * If the address given for the peer isn't authorized, or if
1796 * the user hasn't given one, AND there is an authorized address
1797 * which is a single host, then use that if we find one.
1799 if (suggested_ip
!= 0
1800 && (wo
->hisaddr
== 0 || !auth_ip_addr(unit
, wo
->hisaddr
)))
1801 wo
->hisaddr
= suggested_ip
;
1805 * auth_ip_addr - check whether the peer is authorized to use
1806 * a given IP address. Returns 1 if authorized, 0 otherwise.
1809 auth_ip_addr(unit
, addr
)
1815 /* don't allow loopback or multicast address */
1816 if (bad_ip_adrs(addr
))
1819 if (addresses
[unit
] != NULL
) {
1820 ok
= ip_addr_check(addr
, addresses
[unit
]);
1825 return 0; /* no addresses authorized */
1826 return allow_any_ip
|| privileged
|| !have_route_to(addr
);
1830 ip_addr_check(addr
, addrs
)
1832 struct permitted_ip
*addrs
;
1834 /* This loop is safe because of the sentinel value in set_allowed_addrs */
1836 if ((addr
& addrs
->mask
) == addrs
->base
)
1837 return addrs
->permit
;
1841 * bad_ip_adrs - return 1 if the IP address is one we don't want
1842 * to use, such as an address in the loopback net or a multicast address.
1843 * addr is in network byte order.
1851 #ifndef ALLOW_127_NET
1852 (addr
>> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1855 ((addr
>> IN_CLASSA_NSHIFT
) == 0 && addr
!= 0) ||
1861 * some_ip_ok - check a wordlist to see if it authorizes any
1866 struct wordlist
*addrs
;
1868 for (; addrs
!= NULL
; addrs
= addrs
->next
) {
1869 if (addrs
->word
[0] == '-')
1871 if (addrs
->word
[0] != '!')
1872 return 1; /* some IP address is allowed */
1878 * check_access - complain if a secret file has too-liberal permissions.
1881 check_access(f
, filename
)
1887 if (fstat(fileno(f
), &sbuf
) < 0) {
1888 warn("cannot stat secret file %s: %m", filename
);
1889 } else if ((sbuf
.st_mode
& (S_IRWXG
| S_IRWXO
)) != 0) {
1890 warn("Warning - secret file %s has world and/or group access",
1897 * scan_authfile - Scan an authorization file for a secret suitable
1898 * for authenticating `client' on `server'. The return value is -1 if
1899 * no secret is found, otherwise >= 0. The return value has
1900 * NONWILD_CLIENT set if the secret didn't have "*" for the client,
1901 * and NONWILD_SERVER set if the secret didn't have "*" for the
1904 * Any following words on the line up to a "--" (i.e. address
1905 * authorization info) are placed in a wordlist and returned in
1906 * *addrs. Any following words (extra options) are placed in a
1907 * wordlist and returned in *opts. If opts is NULL, these are just
1908 * discarded. Otherwise, the extra_opt_* variables are set to
1909 * indicate the source of the options.
1911 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1914 scan_authfile(f
, client
, server
, secret
, addrs
, opts
, filename
)
1919 struct wordlist
**addrs
;
1920 struct wordlist
**opts
;
1923 int newline
, xxx
, sline
;
1924 int got_flag
, best_flag
;
1926 struct wordlist
*ap
, *addr_list
, *alist
, **app
;
1927 char word
[MAXWORDLEN
];
1928 char atfile
[MAXWORDLEN
];
1929 char lsecret
[MAXWORDLEN
];
1931 scan_server_match_failed
[0] = '\0';
1939 if (!getword(f
, word
, &newline
, filename
)) {
1941 dbglog("%s is apparently empty", filename
);
1942 return -1; /* file is empty??? */
1948 * Skip until we find a word at the start of a line.
1950 while (!newline
&& getword(f
, word
, &newline
, filename
))
1953 break; /* got to end of file */
1955 sline
= option_line
;
1957 * Got a client - check if it's a match or a wildcard.
1960 if (client
!= NULL
&& strcmp(word
, client
) != 0 && !ISWILD(word
)) {
1965 got_flag
= NONWILD_CLIENT
;
1968 * Now get a server and check if it matches.
1970 if (!getword(f
, word
, &newline
, filename
))
1974 if (!ISWILD(word
)) {
1975 if (server
!= NULL
&& strcmp(word
, server
) != 0) {
1976 (void) strcpy(scan_server_match_failed
, word
);
1979 got_flag
|= NONWILD_SERVER
;
1983 * Got some sort of a match - see if it's better than what
1986 if (got_flag
<= best_flag
)
1992 if (!getword(f
, word
, &newline
, filename
))
1998 * Special syntax: @filename means read secret from file.
1999 * Because the secrets files are modifiable only by root,
2000 * it's safe to open this file as root. One small addition --
2001 * if open fails, we try as the regular user; just in case
2002 * it's over NFS and not root-equivalent.
2004 if (word
[0] == '@') {
2005 (void) strlcpy(atfile
, word
+1, sizeof(atfile
));
2006 if ((sf
= fopen(atfile
, "r")) == NULL
) {
2007 (void) seteuid(getuid());
2008 sf
= fopen(atfile
, "r");
2012 warn("can't open indirect secret file %s: %m", atfile
);
2015 check_access(sf
, atfile
);
2016 if (!getword(sf
, word
, &xxx
, atfile
)) {
2017 warn("no secret in indirect secret file %s", atfile
);
2024 (void) strlcpy(lsecret
, word
, sizeof(lsecret
));
2027 * Now read address authorization info and make a wordlist.
2031 if (!getword(f
, word
, &newline
, filename
) || newline
)
2033 ap
= (struct wordlist
*) malloc(sizeof(struct wordlist
));
2035 novm("authorized addresses");
2036 ap
->word
= strdup(word
);
2037 if (ap
->word
== NULL
)
2038 novm("authorized addresses");
2045 * This is the best so far; remember it.
2047 best_flag
= got_flag
;
2048 if (addr_list
!= NULL
)
2049 free_wordlist(addr_list
);
2052 (void) strlcpy(secret
, lsecret
, MAXWORDLEN
);
2055 extra_opt_filename
= filename
;
2056 extra_opt_line
= sline
;
2063 /* scan for a -- word indicating the start of options */
2064 for (app
= &addr_list
; (ap
= *app
) != NULL
; app
= &ap
->next
)
2065 if (strcmp(ap
->word
, "--") == 0)
2067 /* ap = start of options */
2069 ap
= ap
->next
; /* first option */
2070 free(*app
); /* free the "--" word */
2071 *app
= NULL
; /* terminate addr list */
2075 else if (ap
!= NULL
)
2079 else if (addr_list
!= NULL
)
2080 free_wordlist(addr_list
);
2086 * free_wordlist - release memory allocated for a wordlist.
2090 struct wordlist
*wp
;
2092 struct wordlist
*next
;
2094 while (wp
!= NULL
) {
2102 * auth_script_done - called when the auth-up or auth-down script
2107 auth_script_done(arg
, status
)
2111 auth_script_pid
= 0;
2112 switch (auth_script_state
) {
2114 if (auth_state
== s_down
) {
2115 auth_script_state
= s_down
;
2116 auth_script(_PATH_AUTHDOWN
);
2120 if (auth_state
== s_up
) {
2121 auth_script_state
= s_up
;
2122 auth_script(_PATH_AUTHUP
);
2129 * auth_script - execute a script with arguments
2130 * interface-name peer-name real-user tty speed
2142 if ((pw
= getpwuid(getuid())) != NULL
&& pw
->pw_name
!= NULL
)
2143 user_name
= pw
->pw_name
;
2145 (void) slprintf(struid
, sizeof(struid
), "%d", getuid());
2148 (void) slprintf(strspeed
, sizeof(strspeed
), "%d", baud_rate
);
2152 argv
[2] = peer_authname
;
2153 argv
[3] = user_name
;
2158 auth_script_pid
= run_program(script
, argv
, 0, auth_script_done
, NULL
);