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.
35 #define RCSID "$Id: auth.c,v 1.89 2002/11/02 19:48:12 carlsonj Exp $"
44 #include <sys/types.h>
46 #include <sys/socket.h>
49 #if defined(_PATH_LASTLOG) && defined(__linux__)
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
58 #include <security/pam_appl.h>
64 #define PW_PPP PW_LOGIN
81 #include "pathnames.h"
83 static const char rcsid[] = RCSID;
85 /* Bits in scan_authfile return value */
86 #define NONWILD_SERVER 1
87 #define NONWILD_CLIENT 2
89 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
91 /* The name by which the peer authenticated itself to us. */
92 char peer_authname[MAXNAMELEN];
94 /* Records which authentication operations haven't completed yet. */
95 static int auth_pending[NUM_PPP];
97 /* Records which authentication operations have been completed. */
98 int auth_done[NUM_PPP];
100 /* Set if we have successfully called plogin() */
101 static int logged_in;
103 /* List of addresses which the peer may use. */
104 static struct permitted_ip *addresses[NUM_PPP];
106 /* Wordlist giving addresses which the peer may use
107 without authenticating itself. */
108 static struct wordlist *noauth_addrs;
110 /* Remote telephone number, if available */
111 char remote_number[MAXNAMELEN];
113 /* Wordlist giving remote telephone numbers which may connect. */
114 static struct wordlist *permitted_numbers;
116 /* Extra options to apply, from the secrets file entry for the peer. */
117 static struct wordlist *extra_options;
119 /* Number of network protocols which we have opened. */
120 static int num_np_open;
122 /* Number of network protocols which have come up. */
123 static int num_np_up;
125 /* Set if we got the contents of passwd[] from the pap-secrets file. */
126 static int passwd_from_file;
128 /* Set if we require authentication only because we have a default route. */
129 static bool default_auth;
131 /* Hook to enable a plugin to control the idle time limit */
132 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
134 /* Hook for a plugin to say whether we can possibly authenticate any peer */
135 int (*pap_check_hook) __P((void)) = NULL;
137 /* Hook for a plugin to check the PAP user and password */
138 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
139 struct wordlist **paddrs,
140 struct wordlist **popts)) = NULL;
142 /* Hook for a plugin to know about the PAP user logout */
143 void (*pap_logout_hook) __P((void)) = NULL;
145 /* Hook for a plugin to get the PAP password for authenticating us */
146 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
148 /* Hook for a plugin to say whether it is OK if the peer
149 refuses to authenticate. */
150 int (*null_auth_hook) __P((struct wordlist **paddrs,
151 struct wordlist **popts)) = NULL;
153 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
155 /* A notifier for when the peer has authenticated itself,
156 and we are proceeding to the network phase. */
157 struct notifier *auth_up_notifier = NULL;
159 /* A notifier for when the link goes down. */
160 struct notifier *link_down_notifier = NULL;
163 * This is used to ensure that we don't start an auth-up/down
164 * script while one is already running.
171 static enum script_state auth_state = s_down;
172 static enum script_state auth_script_state = s_down;
173 static pid_t auth_script_pid = 0;
175 static int used_login; /* peer authenticated against login database */
180 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
181 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
182 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
183 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
184 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
186 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
187 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
189 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
190 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
192 bool usehostname = 0; /* Use hostname for our_name */
193 bool auth_required = 0; /* Always require authentication from peer */
194 bool allow_any_ip = 0; /* Allow peer to use any IP address */
195 bool explicit_remote = 0; /* User specified explicit remote name */
196 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
198 static char *uafname; /* name of most recent +ua file */
200 extern char *crypt __P((const char *, const char *));
202 /* Prototypes for procedures local to this file. */
204 static void network_phase __P((int));
205 static void check_idle __P((void *));
206 static void connect_time_expired __P((void *));
207 static int plogin __P((char *, char *, char **));
208 static void plogout __P((void));
209 static int null_login __P((int));
210 static int get_pap_passwd __P((char *));
211 static int have_pap_secret __P((int *));
212 static int have_chap_secret __P((char *, char *, int, int *));
213 static int have_srp_secret __P((char *client, char *server, int need_ip,
215 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
216 static int scan_authfile __P((FILE *, char *, char *, char *,
217 struct wordlist **, struct wordlist **,
219 static void free_wordlist __P((struct wordlist *));
220 static void auth_script __P((char *));
221 static void auth_script_done __P((void *));
222 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
223 static int some_ip_ok __P((struct wordlist *));
224 static int setupapfile __P((char **));
225 static int privgroup __P((char **));
226 static int set_noauth_addr __P((char **));
227 static int set_permitted_number __P((char **));
228 static void check_access __P((FILE *, char *));
229 static int wordlist_count __P((struct wordlist *));
232 static void check_maxoctets __P((void *));
236 * Authentication-related options.
238 option_t auth_options[] = {
239 { "auth", o_bool, &auth_required,
240 "Require authentication from peer", OPT_PRIO | 1 },
241 { "noauth", o_bool, &auth_required,
242 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
244 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
245 "Require PAP authentication from peer",
246 OPT_PRIOSUB | 1, &auth_required },
247 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
248 "Require PAP authentication from peer",
249 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
250 { "require-chap", o_bool, &auth_required,
251 "Require CHAP authentication from peer",
252 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
253 &lcp_wantoptions[0].chap_mdtype },
254 { "+chap", o_bool, &auth_required,
255 "Require CHAP authentication from peer",
256 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
257 &lcp_wantoptions[0].chap_mdtype },
259 { "require-mschap", o_bool, &auth_required,
260 "Require MS-CHAP authentication from peer",
261 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
262 &lcp_wantoptions[0].chap_mdtype },
263 { "+mschap", o_bool, &auth_required,
264 "Require MS-CHAP authentication from peer",
265 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
266 &lcp_wantoptions[0].chap_mdtype },
267 { "require-mschap-v2", o_bool, &auth_required,
268 "Require MS-CHAPv2 authentication from peer",
269 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
270 &lcp_wantoptions[0].chap_mdtype },
271 { "+mschap-v2", o_bool, &auth_required,
272 "Require MS-CHAPv2 authentication from peer",
273 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
274 &lcp_wantoptions[0].chap_mdtype },
277 { "refuse-pap", o_bool, &refuse_pap,
278 "Don't agree to auth to peer with PAP", 1 },
279 { "-pap", o_bool, &refuse_pap,
280 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
281 { "refuse-chap", o_bool, &refuse_chap,
282 "Don't agree to auth to peer with CHAP",
283 OPT_A2CLRB | MDTYPE_MD5,
284 &lcp_allowoptions[0].chap_mdtype },
285 { "-chap", o_bool, &refuse_chap,
286 "Don't allow CHAP authentication with peer",
287 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
288 &lcp_allowoptions[0].chap_mdtype },
290 { "refuse-mschap", o_bool, &refuse_mschap,
291 "Don't agree to auth to peer with MS-CHAP",
292 OPT_A2CLRB | MDTYPE_MICROSOFT,
293 &lcp_allowoptions[0].chap_mdtype },
294 { "-mschap", o_bool, &refuse_mschap,
295 "Don't allow MS-CHAP authentication with peer",
296 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
297 &lcp_allowoptions[0].chap_mdtype },
298 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
299 "Don't agree to auth to peer with MS-CHAPv2",
300 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
301 &lcp_allowoptions[0].chap_mdtype },
302 { "-mschap-v2", o_bool, &refuse_mschap_v2,
303 "Don't allow MS-CHAPv2 authentication with peer",
304 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
305 &lcp_allowoptions[0].chap_mdtype },
308 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
309 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
311 { "refuse-eap", o_bool, &refuse_eap,
312 "Don't agree to authenticate to peer with EAP", 1 },
314 { "name", o_string, our_name,
315 "Set local name for authentication",
316 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
318 { "+ua", o_special, (void *)setupapfile,
319 "Get PAP user and password from file",
320 OPT_PRIO | OPT_A2STRVAL, &uafname },
322 { "user", o_string, user,
323 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
325 { "password", o_string, passwd,
326 "Password for authenticating us to the peer",
327 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
329 { "usehostname", o_bool, &usehostname,
330 "Must use hostname for authentication", 1 },
332 { "remotename", o_string, remote_name,
333 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
334 &explicit_remote, MAXNAMELEN },
336 { "login", o_bool, &uselogin,
337 "Use system password database for PAP", 1 },
339 { "papcrypt", o_bool, &cryptpap,
340 "PAP passwords are encrypted", 1 },
342 { "privgroup", o_special, (void *)privgroup,
343 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
345 { "allow-ip", o_special, (void *)set_noauth_addr,
346 "Set IP address(es) which can be used without authentication",
347 OPT_PRIV | OPT_A2LIST },
349 { "remotenumber", o_string, remote_number,
350 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
353 { "allow-number", o_special, (void *)set_permitted_number,
354 "Set telephone number(s) which are allowed to connect",
355 OPT_PRIV | OPT_A2LIST },
361 * setupapfile - specifies UPAP info for authenticating with peer.
369 char u[MAXNAMELEN], p[MAXSECRETLEN];
372 lcp_allowoptions[0].neg_upap = 1;
374 /* open user info file */
375 fname = strdup(*argv);
377 novm("+ua file name");
379 ufile = fopen(fname, "r");
382 option_error("unable to open user login data file %s", fname);
385 check_access(ufile, fname);
389 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
390 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL){
391 option_error("unable to read user login data file %s", fname);
396 /* get rid of newlines */
398 if (l > 0 && u[l-1] == '\n')
401 if (l > 0 && p[l-1] == '\n')
404 if (override_value("user", option_priority, fname))
405 strlcpy(user, u, sizeof(user));
406 if (override_value("passwd", option_priority, fname))
407 strlcpy(passwd, p, sizeof(passwd));
414 * privgroup - allow members of the group to have privileged access.
425 option_error("group %s is unknown", *argv);
428 for (i = 0; i < ngroups; ++i) {
429 if (groups[i] == g->gr_gid) {
439 * set_noauth_addr - set address(es) that can be used without authentication.
440 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
443 set_noauth_addr(argv)
447 int l = strlen(addr) + 1;
450 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
452 novm("allow-ip argument");
453 wp->word = (char *) (wp + 1);
454 wp->next = noauth_addrs;
455 BCOPY(addr, wp->word, l);
462 * set_permitted_number - set remote telephone number(s) that may connect.
465 set_permitted_number(argv)
468 char *number = *argv;
469 int l = strlen(number) + 1;
472 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
474 novm("allow-number argument");
475 wp->word = (char *) (wp + 1);
476 wp->next = permitted_numbers;
477 BCOPY(number, wp->word, l);
478 permitted_numbers = wp;
484 * An Open on LCP has requested a change from Dead to Establish phase.
485 * Do what's necessary to bring the physical layer up.
494 * LCP has terminated the link; go to the Dead phase and take the
495 * physical layer down.
498 link_terminated(unit)
501 if (phase == PHASE_DEAD)
503 if (pap_logout_hook) {
509 new_phase(PHASE_DEAD);
510 notice("Connection terminated.");
514 * LCP has gone down; it will either die or try to re-establish.
521 struct protent *protp;
523 notify(link_down_notifier, 0);
525 if (auth_script_state == s_up && auth_script_pid == 0) {
526 update_link_stats(unit);
527 auth_script_state = s_down;
528 auth_script(_PATH_AUTHDOWN);
530 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
531 if (!protp->enabled_flag)
533 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
534 (*protp->lowerdown)(unit);
535 if (protp->protocol < 0xC000 && protp->close != NULL)
536 (*protp->close)(unit, "LCP down");
540 if (phase != PHASE_DEAD)
541 new_phase(PHASE_ESTABLISH);
545 * The link is established.
546 * Proceed to the Dead, Authenticate or Network phase as appropriate.
549 link_established(unit)
553 lcp_options *wo = &lcp_wantoptions[unit];
554 lcp_options *go = &lcp_gotoptions[unit];
555 lcp_options *ho = &lcp_hisoptions[unit];
557 struct protent *protp;
560 * Tell higher-level protocols that LCP is up.
562 for (i = 0; (protp = protocols[i]) != NULL; ++i)
563 if (protp->protocol != PPP_LCP && protp->enabled_flag
564 && protp->lowerup != NULL)
565 (*protp->lowerup)(unit);
567 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
569 * We wanted the peer to authenticate itself, and it refused:
570 * if we have some address(es) it can use without auth, fine,
571 * otherwise treat it as though it authenticated with PAP using
572 * a username of "" and a password of "". If that's not OK,
575 if (noauth_addrs != NULL) {
576 set_allowed_addrs(unit, NULL, NULL);
577 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
578 warn("peer refused to authenticate: terminating link");
579 lcp_close(unit, "peer refused to authenticate");
580 status = EXIT_PEER_AUTH_FAILED;
585 new_phase(PHASE_AUTHENTICATE);
589 eap_authpeer(unit, our_name);
591 } else if (go->neg_chap) {
592 ChapAuthPeer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
594 } else if (go->neg_upap) {
599 eap_authwithpeer(unit, user);
600 auth |= EAP_WITHPEER;
601 } else if (ho->neg_chap) {
602 ChapAuthWithPeer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
603 auth |= CHAP_WITHPEER;
604 } else if (ho->neg_upap) {
605 if (passwd[0] == 0) {
606 passwd_from_file = 1;
607 if (!get_pap_passwd(passwd))
608 error("No secret found for PAP login");
610 upap_authwithpeer(unit, user, passwd);
611 auth |= PAP_WITHPEER;
613 auth_pending[unit] = auth;
621 * Proceed to the network phase.
627 lcp_options *go = &lcp_gotoptions[unit];
629 /* Log calling number. */
631 notice("peer from calling number %q authorized", remote_number);
634 * If the peer had to authenticate, run the auth-up script now.
636 if (go->neg_chap || go->neg_upap || go->neg_eap) {
637 notify(auth_up_notifier, 0);
639 if (auth_script_state == s_down && auth_script_pid == 0) {
640 auth_script_state = s_up;
641 auth_script(_PATH_AUTHUP);
647 * If we negotiated callback, do it now.
650 new_phase(PHASE_CALLBACK);
651 (*cbcp_protent.open)(unit);
657 * Process extra options from the secrets file
660 options_from_list(extra_options, 1);
661 free_wordlist(extra_options);
664 start_networks(unit);
672 struct protent *protp;
673 int ecp_required, mppe_required;
675 new_phase(PHASE_NETWORK);
677 #ifdef HAVE_MULTILINK
679 if (mp_join_bundle()) {
680 if (updetach && !nodetach)
685 #endif /* HAVE_MULTILINK */
689 set_filters(&pass_filter, &active_filter);
691 /* Start CCP and ECP */
692 for (i = 0; (protp = protocols[i]) != NULL; ++i)
693 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
694 && protp->enabled_flag && protp->open != NULL)
698 * Bring up other network protocols iff encryption is not required.
700 ecp_required = ecp_gotoptions[unit].required;
701 mppe_required = ccp_gotoptions[unit].mppe;
702 if (!ecp_required && !mppe_required)
703 continue_networks(unit);
707 continue_networks(unit)
711 struct protent *protp;
714 * Start the "real" network protocols.
716 for (i = 0; (protp = protocols[i]) != NULL; ++i)
717 if (protp->protocol < 0xC000
718 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
719 && protp->enabled_flag && protp->open != NULL) {
724 if (num_np_open == 0)
726 lcp_close(0, "No network protocols running");
730 * The peer has failed to authenticate himself using `protocol'.
733 auth_peer_fail(unit, protocol)
737 * Authentication failure: take the link down
739 lcp_close(unit, "Authentication failed");
740 status = EXIT_PEER_AUTH_FAILED;
744 * The peer has been successfully authenticated using `protocol'.
747 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
748 int unit, protocol, prot_flavor;
757 switch (prot_flavor) {
758 case CHAP_DIGEST_MD5:
759 bit |= CHAP_MD5_PEER;
765 case CHAP_MICROSOFT_V2:
766 bit |= CHAP_MS2_PEER;
778 warn("auth_peer_success: unknown protocol %x", protocol);
783 * Save the authenticated name of the peer for later.
785 if (namelen > sizeof(peer_authname) - 1)
786 namelen = sizeof(peer_authname) - 1;
787 BCOPY(name, peer_authname, namelen);
788 peer_authname[namelen] = 0;
789 script_setenv("PEERNAME", peer_authname, 0);
791 /* Save the authentication method for later. */
792 auth_done[unit] |= bit;
795 * If there is no more authentication still to be done,
796 * proceed to the network (or callback) phase.
798 if ((auth_pending[unit] &= ~bit) == 0)
803 * We have failed to authenticate ourselves to the peer using `protocol'.
806 auth_withpeer_fail(unit, protocol)
809 if (passwd_from_file)
810 BZERO(passwd, MAXSECRETLEN);
812 * We've failed to authenticate ourselves to our peer.
813 * Some servers keep sending CHAP challenges, but there
814 * is no point in persisting without any way to get updated
815 * authentication secrets.
817 lcp_close(unit, "Failed to authenticate ourselves to peer");
818 status = EXIT_AUTH_TOPEER_FAILED;
822 * We have successfully authenticated ourselves with the peer using `protocol'.
825 auth_withpeer_success(unit, protocol, prot_flavor)
826 int unit, protocol, prot_flavor;
833 switch (prot_flavor) {
834 case CHAP_DIGEST_MD5:
835 bit |= CHAP_MD5_WITHPEER;
839 bit |= CHAP_MS_WITHPEER;
841 case CHAP_MICROSOFT_V2:
842 bit |= CHAP_MS2_WITHPEER;
848 if (passwd_from_file)
849 BZERO(passwd, MAXSECRETLEN);
856 warn("auth_withpeer_success: unknown protocol %x", protocol);
860 /* Save the authentication method for later. */
861 auth_done[unit] |= bit;
864 * If there is no more authentication still being done,
865 * proceed to the network (or callback) phase.
867 if ((auth_pending[unit] &= ~bit) == 0)
873 * np_up - a network protocol has come up.
881 if (num_np_up == 0) {
883 * At this point we consider that the link has come up successfully.
887 new_phase(PHASE_RUNNING);
889 if (idle_time_hook != 0)
890 tlim = (*idle_time_hook)(NULL);
892 tlim = idle_time_limit;
894 TIMEOUT(check_idle, NULL, tlim);
897 * Set a timeout to close the connection once the maximum
898 * connect time has expired.
901 TIMEOUT(connect_time_expired, 0, maxconnect);
905 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
909 * Detach now, if the updetach option was given.
911 if (updetach && !nodetach)
918 * np_down - a network protocol has gone down.
924 if (--num_np_up == 0) {
925 UNTIMEOUT(check_idle, NULL);
926 UNTIMEOUT(connect_time_expired, NULL);
928 UNTIMEOUT(check_maxoctets, NULL);
930 new_phase(PHASE_NETWORK);
935 * np_finished - a network protocol has finished using the link.
938 np_finished(unit, proto)
941 if (--num_np_open <= 0) {
942 /* no further use for the link: shut up shop. */
943 lcp_close(0, "No network protocols running");
955 update_link_stats(ifunit);
958 switch(maxoctets_dir) {
959 case PPP_OCTETS_DIRECTION_IN:
960 used = link_stats.bytes_in;
962 case PPP_OCTETS_DIRECTION_OUT:
963 used = link_stats.bytes_out;
965 case PPP_OCTETS_DIRECTION_MAXOVERAL:
966 case PPP_OCTETS_DIRECTION_MAXSESSION:
967 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
970 used = link_stats.bytes_in+link_stats.bytes_out;
973 diff = maxoctets - used;
975 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
976 lcp_close(0, "Traffic limit");
978 status = EXIT_TRAFFIC_LIMIT;
980 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
986 * check_idle - check whether the link has been idle for long
987 * enough that we can shut it down.
993 struct ppp_idle idle;
997 if (!get_idle_time(0, &idle))
999 if (idle_time_hook != 0) {
1000 tlim = idle_time_hook(&idle);
1002 itime = MIN(idle.xmit_idle, idle.recv_idle);
1003 tlim = idle_time_limit - itime;
1006 /* link is idle: shut it down. */
1007 notice("Terminating connection due to lack of activity.");
1008 lcp_close(0, "Link inactive");
1010 status = EXIT_IDLE_TIMEOUT;
1012 TIMEOUT(check_idle, NULL, tlim);
1017 * connect_time_expired - log a message and close the connection.
1020 connect_time_expired(arg)
1023 info("Connect time expired");
1024 lcp_close(0, "Connect time expired"); /* Close connection */
1025 status = EXIT_CONNECT_TIME;
1029 * auth_check_options - called to check authentication options.
1032 auth_check_options()
1034 lcp_options *wo = &lcp_wantoptions[0];
1038 /* Default our_name to hostname, and user to our_name */
1039 if (our_name[0] == 0 || usehostname)
1040 strlcpy(our_name, hostname, sizeof(our_name));
1042 strlcpy(user, our_name, sizeof(user));
1045 * If we have a default route, require the peer to authenticate
1046 * unless the noauth option was given or the real user is root.
1048 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1053 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1054 if (wo->chap_mdtype)
1057 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1058 if (auth_required) {
1060 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1061 wo->neg_chap = 1; wo->chap_mdtype = MDTYPE_ALL;
1066 wo->neg_chap = 0; wo->chap_mdtype = MDTYPE_NONE;
1072 * Check whether we have appropriate secrets to use
1073 * to authenticate the peer. Note that EAP can authenticate by way
1074 * of a CHAP-like exchanges as well as SRP.
1077 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1078 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1079 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1080 our_name, 1, &lacks_ip);
1082 if (!can_auth && wo->neg_eap) {
1083 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1084 our_name, 1, &lacks_ip);
1087 if (auth_required && !can_auth && noauth_addrs == NULL) {
1090 "By default the remote system is required to authenticate itself");
1092 "(because this system has a default route to the internet)");
1093 } else if (explicit_remote)
1095 "The remote system (%s) is required to authenticate itself",
1099 "The remote system is required to authenticate itself");
1101 "but I couldn't find any suitable secret (password) for it to use to do so.");
1104 "(None of the available passwords would let it use an IP address.)");
1111 * auth_reset - called when LCP is starting negotiations to recheck
1112 * authentication options, i.e. whether we have appropriate secrets
1113 * to use for authenticating ourselves and/or the peer.
1119 lcp_options *go = &lcp_gotoptions[unit];
1120 lcp_options *ao = &lcp_allowoptions[unit];
1124 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1125 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1126 && (passwd[0] != 0 ||
1127 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1129 ao->neg_eap = !refuse_eap && (
1131 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1132 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1133 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1136 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1139 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1140 our_name, 1, NULL)))
1144 (hadchap == 0 || (hadchap == -1 &&
1145 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1147 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1154 * check_passwd - Check the user name and passwd against the PAP secrets
1155 * file. If requested, also check against the system password database,
1156 * and login the user if OK.
1159 * UPAP_AUTHNAK: Authentication failed.
1160 * UPAP_AUTHACK: Authentication succeeded.
1161 * In either case, msg points to an appropriate message.
1164 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1175 struct wordlist *addrs = NULL, *opts = NULL;
1176 char passwd[256], user[256];
1177 char secret[MAXWORDLEN];
1178 static int attempts = 0;
1181 * Make copies of apasswd and auser, then null-terminate them.
1182 * If there are unprintable characters in the password, make
1185 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1186 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1190 * Check if a plugin wants to handle this.
1192 if (pap_auth_hook) {
1193 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1196 set_allowed_addrs(unit, addrs, opts);
1197 BZERO(passwd, sizeof(passwd));
1199 free_wordlist(addrs);
1201 free_wordlist(opts);
1203 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1208 * Open the file of pap secrets and scan for a suitable secret
1209 * for authenticating this user.
1211 filename = _PATH_UPAPFILE;
1212 addrs = opts = NULL;
1214 f = fopen(filename, "r");
1216 error("Can't open PAP password file %s: %m", filename);
1219 check_access(f, filename);
1220 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1221 warn("no PAP secret found for %s", user);
1224 * If the secret is "@login", it means to check
1225 * the password against the login database.
1227 int login_secret = strcmp(secret, "@login") == 0;
1229 if (uselogin || login_secret) {
1230 /* login option or secret is @login */
1231 if ((ret = plogin(user, passwd, msg)) == UPAP_AUTHACK)
1234 if (secret[0] != 0 && !login_secret) {
1235 /* password given in pap-secrets - must match */
1236 if ((cryptpap || strcmp(passwd, secret) != 0)
1237 && strcmp(crypt(passwd, secret), secret) != 0)
1244 if (ret == UPAP_AUTHNAK) {
1246 *msg = "Login incorrect";
1248 * XXX can we ever get here more than once??
1249 * Frustrate passwd stealer programs.
1250 * Allow 10 tries, but start backing off after 3 (stolen from login).
1251 * On 10'th, drop the connection.
1253 if (attempts++ >= 10) {
1254 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1255 lcp_close(unit, "login failed");
1258 sleep((u_int) (attempts - 3) * 5);
1260 free_wordlist(opts);
1263 attempts = 0; /* Reset count */
1266 set_allowed_addrs(unit, addrs, opts);
1270 free_wordlist(addrs);
1271 BZERO(passwd, sizeof(passwd));
1272 BZERO(secret, sizeof(secret));
1278 * This function is needed for PAM.
1282 /* Static variables used to communicate between the conversation function
1283 * and the server_login function
1285 static char *PAM_username;
1286 static char *PAM_password;
1287 static int PAM_error = 0;
1288 static pam_handle_t *pamh = NULL;
1290 /* PAM conversation function
1291 * Here we assume (for now, at least) that echo on means login name, and
1292 * echo off means password.
1295 static int PAM_conv (int num_msg,
1299 struct pam_message **msg,
1300 struct pam_response **resp, void *appdata_ptr)
1303 struct pam_response *reply = NULL;
1305 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1307 reply = malloc(sizeof(struct pam_response) * num_msg);
1308 if (!reply) return PAM_CONV_ERR;
1310 for (replies = 0; replies < num_msg; replies++) {
1311 switch (msg[replies]->msg_style) {
1312 case PAM_PROMPT_ECHO_ON:
1313 reply[replies].resp_retcode = PAM_SUCCESS;
1314 reply[replies].resp = COPY_STRING(PAM_username);
1315 /* PAM frees resp */
1317 case PAM_PROMPT_ECHO_OFF:
1318 reply[replies].resp_retcode = PAM_SUCCESS;
1319 reply[replies].resp = COPY_STRING(PAM_password);
1320 /* PAM frees resp */
1325 /* ignore it, but pam still wants a NULL response... */
1326 reply[replies].resp_retcode = PAM_SUCCESS;
1327 reply[replies].resp = NULL;
1330 /* Must be an error of some sort... */
1333 return PAM_CONV_ERR;
1340 static struct pam_conv PAM_conversation = {
1344 #endif /* USE_PAM */
1347 * plogin - Check the user name and password against the system
1348 * password database, and login the user if OK.
1351 * UPAP_AUTHNAK: Login failed.
1352 * UPAP_AUTHACK: Login succeeded.
1353 * In either case, msg points to an appropriate message.
1357 plogin(user, passwd, msg)
1367 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1368 if (pam_error != PAM_SUCCESS) {
1369 *msg = (char *) pam_strerror (pamh, pam_error);
1371 return UPAP_AUTHNAK;
1374 * Define the fields for the credential validation
1377 PAM_username = user;
1378 PAM_password = passwd;
1380 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1385 pam_error = pam_authenticate (pamh, PAM_SILENT);
1386 if (pam_error == PAM_SUCCESS && !PAM_error) {
1387 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1388 if (pam_error == PAM_SUCCESS)
1389 pam_error = pam_open_session (pamh, PAM_SILENT);
1392 *msg = (char *) pam_strerror (pamh, pam_error);
1397 reopen_log(); /* apparently the PAM stuff does closelog() */
1398 PAM_username = NULL;
1399 PAM_password = NULL;
1400 if (pam_error != PAM_SUCCESS)
1401 return UPAP_AUTHNAK;
1402 #else /* #ifdef USE_PAM */
1405 * Use the non-PAM methods directly
1410 struct spwd *getspnam();
1412 struct passwd *pw = getpwnam(user);
1416 return (UPAP_AUTHNAK);
1419 spwd = getspnam(user);
1422 /* check the age of the password entry */
1423 long now = time(NULL) / 86400L;
1425 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1426 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1427 && spwd->sp_lstchg >= 0
1428 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1429 warn("Password for %s has expired", user);
1430 return (UPAP_AUTHNAK);
1432 pw->pw_passwd = spwd->sp_pwdp;
1437 * If no passwd, don't let them login.
1439 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1440 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1441 return (UPAP_AUTHNAK);
1443 #endif /* #ifdef USE_PAM */
1446 * Write a wtmp entry for this user.
1450 if (strncmp(tty, "/dev/", 5) == 0)
1452 logwtmp(tty, user, ifname); /* Add wtmp login entry */
1454 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1455 if (pw != (struct passwd *)NULL) {
1459 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1460 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1461 memset((void *)&ll, 0, sizeof(ll));
1462 (void)time(&ll.ll_time);
1463 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1464 (void)write(fd, (char *)&ll, sizeof(ll));
1468 #endif /* _PATH_LASTLOG and not USE_PAM */
1470 info("user %s logged in", user);
1473 return (UPAP_AUTHACK);
1477 * plogout - Logout the user.
1486 pam_error = pam_close_session (pamh, PAM_SILENT);
1487 pam_end (pamh, pam_error);
1490 /* Apparently the pam stuff does closelog(). */
1492 #else /* ! USE_PAM */
1496 if (strncmp(tty, "/dev/", 5) == 0)
1498 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1499 #endif /* ! USE_PAM */
1505 * null_login - Check if a username of "" and a password of "" are
1506 * acceptable, and iff so, set the list of acceptable IP addresses
1516 struct wordlist *addrs, *opts;
1517 char secret[MAXWORDLEN];
1520 * Check if a plugin wants to handle this.
1524 ret = (*null_auth_hook)(&addrs, &opts);
1527 * Open the file of pap secrets and scan for a suitable secret.
1530 filename = _PATH_UPAPFILE;
1532 f = fopen(filename, "r");
1535 check_access(f, filename);
1537 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1538 ret = i >= 0 && secret[0] == 0;
1539 BZERO(secret, sizeof(secret));
1544 set_allowed_addrs(unit, addrs, opts);
1546 free_wordlist(opts);
1548 free_wordlist(addrs);
1555 * get_pap_passwd - get a password for authenticating ourselves with
1556 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1558 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1561 get_pap_passwd(passwd)
1567 char secret[MAXWORDLEN];
1570 * Check whether a plugin wants to supply this.
1572 if (pap_passwd_hook) {
1573 ret = (*pap_passwd_hook)(user, passwd);
1578 filename = _PATH_UPAPFILE;
1579 f = fopen(filename, "r");
1582 check_access(f, filename);
1583 ret = scan_authfile(f, user,
1584 (remote_name[0]? remote_name: NULL),
1585 secret, NULL, NULL, filename, 0);
1590 strlcpy(passwd, secret, MAXSECRETLEN);
1591 BZERO(secret, sizeof(secret));
1597 * have_pap_secret - check whether we have a PAP file with any
1598 * secrets that we could possibly use for authenticating the peer.
1601 have_pap_secret(lacks_ipp)
1607 struct wordlist *addrs;
1609 /* let the plugin decide, if there is one */
1610 if (pap_check_hook) {
1611 ret = (*pap_check_hook)();
1616 filename = _PATH_UPAPFILE;
1617 f = fopen(filename, "r");
1621 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1622 NULL, &addrs, NULL, filename, 0);
1624 if (ret >= 0 && !some_ip_ok(addrs)) {
1630 free_wordlist(addrs);
1637 * have_chap_secret - check whether we have a CHAP file with a
1638 * secret that we could possibly use for authenticating `client'
1639 * on `server'. Either can be the null string, meaning we don't
1640 * know the identity yet.
1643 have_chap_secret(client, server, need_ip, lacks_ipp)
1652 struct wordlist *addrs;
1654 if (chap_check_hook) {
1655 ret = (*chap_check_hook)();
1661 filename = _PATH_CHAPFILE;
1662 f = fopen(filename, "r");
1666 if (client != NULL && client[0] == 0)
1668 else if (server != NULL && server[0] == 0)
1671 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1673 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1679 free_wordlist(addrs);
1686 * have_srp_secret - check whether we have a SRP file with a
1687 * secret that we could possibly use for authenticating `client'
1688 * on `server'. Either can be the null string, meaning we don't
1689 * know the identity yet.
1692 have_srp_secret(client, server, need_ip, lacks_ipp)
1701 struct wordlist *addrs;
1703 filename = _PATH_SRPFILE;
1704 f = fopen(filename, "r");
1708 if (client != NULL && client[0] == 0)
1710 else if (server != NULL && server[0] == 0)
1713 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1715 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1721 free_wordlist(addrs);
1728 * get_secret - open the CHAP secret file and return the secret
1729 * for authenticating the given client on the given server.
1730 * (We could be either client or server).
1733 get_secret(unit, client, server, secret, secret_len, am_server)
1744 struct wordlist *addrs, *opts;
1745 char secbuf[MAXWORDLEN];
1747 if (!am_server && passwd[0] != 0) {
1748 strlcpy(secbuf, passwd, sizeof(secbuf));
1749 } else if (!am_server && chap_passwd_hook) {
1750 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1751 error("Unable to obtain CHAP password for %s on %s from plugin",
1756 filename = _PATH_CHAPFILE;
1760 f = fopen(filename, "r");
1762 error("Can't open chap secret file %s: %m", filename);
1765 check_access(f, filename);
1767 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1773 set_allowed_addrs(unit, addrs, opts);
1775 free_wordlist(opts);
1777 free_wordlist(addrs);
1780 len = strlen(secbuf);
1781 if (len > MAXSECRETLEN) {
1782 error("Secret for %s on %s is too long", client, server);
1785 BCOPY(secbuf, secret, len);
1786 BZERO(secbuf, sizeof(secbuf));
1794 * get_srp_secret - open the SRP secret file and return the secret
1795 * for authenticating the given client on the given server.
1796 * (We could be either client or server).
1799 get_srp_secret(unit, client, server, secret, am_server)
1809 struct wordlist *addrs, *opts;
1811 if (!am_server && passwd[0] != '\0') {
1812 strlcpy(secret, passwd, MAXWORDLEN);
1814 filename = _PATH_SRPFILE;
1817 fp = fopen(filename, "r");
1819 error("Can't open srp secret file %s: %m", filename);
1822 check_access(fp, filename);
1825 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
1826 filename, am_server);
1832 set_allowed_addrs(unit, addrs, opts);
1833 else if (opts != NULL)
1834 free_wordlist(opts);
1836 free_wordlist(addrs);
1843 * set_allowed_addrs() - set the list of allowed addresses.
1844 * Also looks for `--' indicating options to apply for this peer
1845 * and leaves the following words in extra_options.
1848 set_allowed_addrs(unit, addrs, opts)
1850 struct wordlist *addrs;
1851 struct wordlist *opts;
1854 struct wordlist *ap, **plink;
1855 struct permitted_ip *ip;
1856 char *ptr_word, *ptr_mask;
1859 u_int32_t a, mask, ah, offset;
1860 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1861 u_int32_t suggested_ip = 0;
1863 if (addresses[unit] != NULL)
1864 free(addresses[unit]);
1865 addresses[unit] = NULL;
1866 if (extra_options != NULL)
1867 free_wordlist(extra_options);
1868 extra_options = opts;
1871 * Count the number of IP addresses given.
1873 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1876 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1880 /* temporarily append the noauth_addrs list to addrs */
1881 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1883 *plink = noauth_addrs;
1886 for (ap = addrs; ap != NULL; ap = ap->next) {
1887 /* "-" means no addresses authorized, "*" means any address allowed */
1888 ptr_word = ap->word;
1889 if (strcmp(ptr_word, "-") == 0)
1891 if (strcmp(ptr_word, "*") == 0) {
1893 ip[n].base = ip[n].mask = 0;
1899 if (*ptr_word == '!') {
1904 mask = ~ (u_int32_t) 0;
1906 ptr_mask = strchr (ptr_word, '/');
1907 if (ptr_mask != NULL) {
1911 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1912 if (bit_count <= 0 || bit_count > 32) {
1913 warn("invalid address length %v in auth. address list",
1917 bit_count = 32 - bit_count; /* # bits in host part */
1919 offset = ifunit + 1;
1923 warn("invalid address length syntax: %v", ptr_mask+1);
1930 hp = gethostbyname(ptr_word);
1931 if (hp != NULL && hp->h_addrtype == AF_INET) {
1932 a = *(u_int32_t *)hp->h_addr;
1934 np = getnetbyname (ptr_word);
1935 if (np != NULL && np->n_addrtype == AF_INET) {
1936 a = htonl (*(u_int32_t *)np->n_net);
1937 if (ptr_mask == NULL) {
1938 /* calculate appropriate mask for net */
1941 mask = IN_CLASSA_NET;
1942 else if (IN_CLASSB(ah))
1943 mask = IN_CLASSB_NET;
1944 else if (IN_CLASSC(ah))
1945 mask = IN_CLASSC_NET;
1948 a = inet_addr (ptr_word);
1952 if (ptr_mask != NULL)
1955 if (a == (u_int32_t)-1L) {
1956 warn("unknown host %s in auth. address list", ap->word);
1960 if (offset >= ~mask) {
1961 warn("interface unit %d too large for subnet %v",
1965 a = htonl((ntohl(a) & mask) + offset);
1966 mask = ~(u_int32_t)0;
1968 ip[n].mask = htonl(mask);
1969 ip[n].base = a & ip[n].mask;
1971 if (~mask == 0 && suggested_ip == 0)
1976 ip[n].permit = 0; /* make the last entry forbid all addresses */
1977 ip[n].base = 0; /* to terminate the list */
1980 addresses[unit] = ip;
1983 * If the address given for the peer isn't authorized, or if
1984 * the user hasn't given one, AND there is an authorized address
1985 * which is a single host, then use that if we find one.
1987 if (suggested_ip != 0
1988 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1989 wo->hisaddr = suggested_ip;
1991 * Do we insist on this address? No, if there are other
1992 * addresses authorized than the suggested one.
1995 wo->accept_remote = 1;
2000 * auth_ip_addr - check whether the peer is authorized to use
2001 * a given IP address. Returns 1 if authorized, 0 otherwise.
2004 auth_ip_addr(unit, addr)
2010 /* don't allow loopback or multicast address */
2011 if (bad_ip_adrs(addr))
2014 if (allowed_address_hook) {
2015 ok = allowed_address_hook(addr);
2016 if (ok >= 0) return ok;
2019 if (addresses[unit] != NULL) {
2020 ok = ip_addr_check(addr, addresses[unit]);
2026 return 0; /* no addresses authorized */
2027 return allow_any_ip || privileged || !have_route_to(addr);
2031 ip_addr_check(addr, addrs)
2033 struct permitted_ip *addrs;
2036 if ((addr & addrs->mask) == addrs->base)
2037 return addrs->permit;
2041 * bad_ip_adrs - return 1 if the IP address is one we don't want
2042 * to use, such as an address in the loopback net or a multicast address.
2043 * addr is in network byte order.
2050 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2051 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2055 * some_ip_ok - check a wordlist to see if it authorizes any
2060 struct wordlist *addrs;
2062 for (; addrs != 0; addrs = addrs->next) {
2063 if (addrs->word[0] == '-')
2065 if (addrs->word[0] != '!')
2066 return 1; /* some IP address is allowed */
2072 * auth_number - check whether the remote number is allowed to connect.
2073 * Returns 1 if authorized, 0 otherwise.
2078 struct wordlist *wp = permitted_numbers;
2081 /* Allow all if no authorization list. */
2085 /* Allow if we have a match in the authorization list. */
2087 /* trailing '*' wildcard */
2088 l = strlen(wp->word);
2089 if ((wp->word)[l - 1] == '*')
2091 if (!strncasecmp(wp->word, remote_number, l))
2100 * check_access - complain if a secret file has too-liberal permissions.
2103 check_access(f, filename)
2109 if (fstat(fileno(f), &sbuf) < 0) {
2110 warn("cannot stat secret file %s: %m", filename);
2111 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2112 warn("Warning - secret file %s has world and/or group access",
2119 * scan_authfile - Scan an authorization file for a secret suitable
2120 * for authenticating `client' on `server'. The return value is -1
2121 * if no secret is found, otherwise >= 0. The return value has
2122 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2123 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2124 * Any following words on the line up to a "--" (i.e. address authorization
2125 * info) are placed in a wordlist and returned in *addrs. Any
2126 * following words (extra options) are placed in a wordlist and
2127 * returned in *opts.
2128 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2129 * Flags are non-zero if we need two colons in the secret in order to
2133 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2138 struct wordlist **addrs;
2139 struct wordlist **opts;
2144 int got_flag, best_flag;
2146 struct wordlist *ap, *addr_list, *alist, **app;
2147 char word[MAXWORDLEN];
2148 char atfile[MAXWORDLEN];
2149 char lsecret[MAXWORDLEN];
2157 if (!getword(f, word, &newline, filename))
2158 return -1; /* file is empty??? */
2163 * Skip until we find a word at the start of a line.
2165 while (!newline && getword(f, word, &newline, filename))
2168 break; /* got to end of file */
2171 * Got a client - check if it's a match or a wildcard.
2174 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2179 got_flag = NONWILD_CLIENT;
2182 * Now get a server and check if it matches.
2184 if (!getword(f, word, &newline, filename))
2188 if (!ISWILD(word)) {
2189 if (server != NULL && strcmp(word, server) != 0)
2191 got_flag |= NONWILD_SERVER;
2195 * Got some sort of a match - see if it's better than what
2198 if (got_flag <= best_flag)
2204 if (!getword(f, word, &newline, filename))
2210 * SRP-SHA1 authenticator should never be reading secrets from
2211 * a file. (Authenticatee may, though.)
2213 if (flags && ((cp = strchr(word, ':')) == NULL ||
2214 strchr(cp + 1, ':') == NULL))
2217 if (secret != NULL) {
2219 * Special syntax: @/pathname means read secret from file.
2221 if (word[0] == '@' && word[1] == '/') {
2222 strlcpy(atfile, word+1, sizeof(atfile));
2223 if ((sf = fopen(atfile, "r")) == NULL) {
2224 warn("can't open indirect secret file %s", atfile);
2227 check_access(sf, atfile);
2228 if (!getword(sf, word, &xxx, atfile)) {
2229 warn("no secret in indirect secret file %s", atfile);
2235 strlcpy(lsecret, word, sizeof(lsecret));
2239 * Now read address authorization info and make a wordlist.
2243 if (!getword(f, word, &newline, filename) || newline)
2245 ap = (struct wordlist *)
2246 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2248 novm("authorized addresses");
2249 ap->word = (char *) (ap + 1);
2250 strcpy(ap->word, word);
2257 * This is the best so far; remember it.
2259 best_flag = got_flag;
2261 free_wordlist(addr_list);
2264 strlcpy(secret, lsecret, MAXWORDLEN);
2270 /* scan for a -- word indicating the start of options */
2271 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2272 if (strcmp(ap->word, "--") == 0)
2274 /* ap = start of options */
2276 ap = ap->next; /* first option */
2277 free(*app); /* free the "--" word */
2278 *app = NULL; /* terminate addr list */
2282 else if (ap != NULL)
2286 else if (addr_list != NULL)
2287 free_wordlist(addr_list);
2293 * wordlist_count - return the number of items in a wordlist
2297 struct wordlist *wp;
2301 for (n = 0; wp != NULL; wp = wp->next)
2307 * free_wordlist - release memory allocated for a wordlist.
2311 struct wordlist *wp;
2313 struct wordlist *next;
2315 while (wp != NULL) {
2323 * auth_script_done - called when the auth-up or auth-down script
2327 auth_script_done(arg)
2330 auth_script_pid = 0;
2331 switch (auth_script_state) {
2333 if (auth_state == s_down) {
2334 auth_script_state = s_down;
2335 auth_script(_PATH_AUTHDOWN);
2339 if (auth_state == s_up) {
2340 auth_script_state = s_up;
2341 auth_script(_PATH_AUTHUP);
2348 * auth_script - execute a script with arguments
2349 * interface-name peer-name real-user tty speed
2361 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2362 user_name = pw->pw_name;
2364 slprintf(struid, sizeof(struid), "%d", getuid());
2367 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2371 argv[2] = peer_authname;
2372 argv[3] = user_name;
2377 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);