2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993-2002 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. The name(s) of the authors of this software must not be used to
14 * endorse or promote products derived from this software without
15 * prior written permission.
17 * 3. Redistributions of any form whatsoever must retain the following
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * Derived from main.c, which is:
32 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
46 * 3. The name "Carnegie Mellon University" must not be used to
47 * endorse or promote products derived from this software without
48 * prior written permission. For permission or any legal
49 * details, please contact
50 * Office of Technology Transfer
51 * Carnegie Mellon University
53 * Pittsburgh, PA 15213-3890
54 * (412) 268-4387, fax: (412) 268-7395
55 * tech-transfer@andrew.cmu.edu
57 * 4. Redistributions of any form whatsoever must retain the following
59 * "This product includes software developed by Computing Services
60 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
62 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
63 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
65 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
67 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
68 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 #define RCSID "$Id: auth.c,v 1.106 2005/07/13 10:41:58 paulus Exp $"
81 #include <sys/types.h>
83 #include <sys/socket.h>
86 #if defined(_PATH_LASTLOG) && defined(__linux__)
91 #include <netinet/in.h>
92 #include <arpa/inet.h>
95 #include <security/pam_appl.h>
101 #define PW_PPP PW_LOGIN
113 #include "chap-new.h"
118 #include "pathnames.h"
120 static const char rcsid[] = RCSID;
122 /* Bits in scan_authfile return value */
123 #define NONWILD_SERVER 1
124 #define NONWILD_CLIENT 2
126 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
128 /* The name by which the peer authenticated itself to us. */
129 char peer_authname[MAXNAMELEN];
131 /* Records which authentication operations haven't completed yet. */
132 static int auth_pending[NUM_PPP];
134 /* Records which authentication operations have been completed. */
135 int auth_done[NUM_PPP];
137 /* Set if we have successfully called plogin() */
138 static int logged_in;
140 /* List of addresses which the peer may use. */
141 static struct permitted_ip *addresses[NUM_PPP];
143 /* Wordlist giving addresses which the peer may use
144 without authenticating itself. */
145 static struct wordlist *noauth_addrs;
147 /* Remote telephone number, if available */
148 char remote_number[MAXNAMELEN];
150 /* Wordlist giving remote telephone numbers which may connect. */
151 static struct wordlist *permitted_numbers;
153 /* Extra options to apply, from the secrets file entry for the peer. */
154 static struct wordlist *extra_options;
156 /* Number of network protocols which we have opened. */
157 static int num_np_open;
159 /* Number of network protocols which have come up. */
160 static int num_np_up;
162 /* Set if we got the contents of passwd[] from the pap-secrets file. */
163 static int passwd_from_file;
165 /* Set if we require authentication only because we have a default route. */
166 static bool default_auth;
168 /* Hook to enable a plugin to control the idle time limit */
169 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
171 /* Hook for a plugin to say whether we can possibly authenticate any peer */
172 int (*pap_check_hook) __P((void)) = NULL;
174 /* Hook for a plugin to check the PAP user and password */
175 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
176 struct wordlist **paddrs,
177 struct wordlist **popts)) = NULL;
179 /* Hook for a plugin to know about the PAP user logout */
180 void (*pap_logout_hook) __P((void)) = NULL;
182 /* Hook for a plugin to get the PAP password for authenticating us */
183 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
185 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
186 int (*chap_check_hook) __P((void)) = NULL;
188 /* Hook for a plugin to get the CHAP password for authenticating us */
189 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
191 /* Hook for a plugin to say whether it is OK if the peer
192 refuses to authenticate. */
193 int (*null_auth_hook) __P((struct wordlist **paddrs,
194 struct wordlist **popts)) = NULL;
196 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
198 /* A notifier for when the peer has authenticated itself,
199 and we are proceeding to the network phase. */
200 struct notifier *auth_up_notifier = NULL;
202 /* A notifier for when the link goes down. */
203 struct notifier *link_down_notifier = NULL;
206 * This is used to ensure that we don't start an auth-up/down
207 * script while one is already running.
214 static enum script_state auth_state = s_down;
215 static enum script_state auth_script_state = s_down;
216 static pid_t auth_script_pid = 0;
218 static int used_login; /* peer authenticated against login database */
223 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
224 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
225 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
226 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
227 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
229 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
230 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
232 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
233 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
235 bool usehostname = 0; /* Use hostname for our_name */
236 bool auth_required = 0; /* Always require authentication from peer */
237 bool allow_any_ip = 0; /* Allow peer to use any IP address */
238 bool explicit_remote = 0; /* User specified explicit remote name */
239 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
241 static char *uafname; /* name of most recent +ua file */
243 extern char *crypt __P((const char *, const char *));
245 /* Prototypes for procedures local to this file. */
247 static void network_phase __P((int));
248 static void check_idle __P((void *));
249 static void connect_time_expired __P((void *));
250 static int plogin __P((char *, char *, char **));
251 static void plogout __P((void));
252 static int null_login __P((int));
253 static int get_pap_passwd __P((char *));
254 static int have_pap_secret __P((int *));
255 static int have_chap_secret __P((char *, char *, int, int *));
256 static int have_srp_secret __P((char *client, char *server, int need_ip,
258 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
259 static int scan_authfile __P((FILE *, char *, char *, char *,
260 struct wordlist **, struct wordlist **,
262 static void free_wordlist __P((struct wordlist *));
263 static void auth_script __P((char *));
264 static void auth_script_done __P((void *));
265 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
266 static int some_ip_ok __P((struct wordlist *));
267 static int setupapfile __P((char **));
268 static int privgroup __P((char **));
269 static int set_noauth_addr __P((char **));
270 static int set_permitted_number __P((char **));
271 static void check_access __P((FILE *, char *));
272 static int wordlist_count __P((struct wordlist *));
275 static void check_maxoctets __P((void *));
279 * Authentication-related options.
281 option_t auth_options[] = {
282 { "auth", o_bool, &auth_required,
283 "Require authentication from peer", OPT_PRIO | 1 },
284 { "noauth", o_bool, &auth_required,
285 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
287 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
288 "Require PAP authentication from peer",
289 OPT_PRIOSUB | 1, &auth_required },
290 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
291 "Require PAP authentication from peer",
292 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
293 { "require-chap", o_bool, &auth_required,
294 "Require CHAP authentication from peer",
295 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
296 &lcp_wantoptions[0].chap_mdtype },
297 { "+chap", o_bool, &auth_required,
298 "Require CHAP authentication from peer",
299 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
300 &lcp_wantoptions[0].chap_mdtype },
302 { "require-mschap", o_bool, &auth_required,
303 "Require MS-CHAP authentication from peer",
304 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
305 &lcp_wantoptions[0].chap_mdtype },
306 { "+mschap", o_bool, &auth_required,
307 "Require MS-CHAP authentication from peer",
308 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
309 &lcp_wantoptions[0].chap_mdtype },
310 { "require-mschap-v2", o_bool, &auth_required,
311 "Require MS-CHAPv2 authentication from peer",
312 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
313 &lcp_wantoptions[0].chap_mdtype },
314 { "+mschap-v2", o_bool, &auth_required,
315 "Require MS-CHAPv2 authentication from peer",
316 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
317 &lcp_wantoptions[0].chap_mdtype },
320 { "refuse-pap", o_bool, &refuse_pap,
321 "Don't agree to auth to peer with PAP", 1 },
322 { "-pap", o_bool, &refuse_pap,
323 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
324 { "refuse-chap", o_bool, &refuse_chap,
325 "Don't agree to auth to peer with CHAP",
326 OPT_A2CLRB | MDTYPE_MD5,
327 &lcp_allowoptions[0].chap_mdtype },
328 { "-chap", o_bool, &refuse_chap,
329 "Don't allow CHAP authentication with peer",
330 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
331 &lcp_allowoptions[0].chap_mdtype },
333 { "refuse-mschap", o_bool, &refuse_mschap,
334 "Don't agree to auth to peer with MS-CHAP",
335 OPT_A2CLRB | MDTYPE_MICROSOFT,
336 &lcp_allowoptions[0].chap_mdtype },
337 { "-mschap", o_bool, &refuse_mschap,
338 "Don't allow MS-CHAP authentication with peer",
339 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
340 &lcp_allowoptions[0].chap_mdtype },
341 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
342 "Don't agree to auth to peer with MS-CHAPv2",
343 OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
344 &lcp_allowoptions[0].chap_mdtype },
345 { "-mschap-v2", o_bool, &refuse_mschap_v2,
346 "Don't allow MS-CHAPv2 authentication with peer",
347 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
348 &lcp_allowoptions[0].chap_mdtype },
351 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
352 "Require EAP authentication from peer", OPT_PRIOSUB | 1,
354 { "refuse-eap", o_bool, &refuse_eap,
355 "Don't agree to authenticate to peer with EAP", 1 },
357 { "name", o_string, our_name,
358 "Set local name for authentication",
359 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
361 { "+ua", o_special, (void *)setupapfile,
362 "Get PAP user and password from file",
363 OPT_PRIO | OPT_A2STRVAL, &uafname },
365 { "user", o_string, user,
366 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
368 { "password", o_string, passwd,
369 "Password for authenticating us to the peer",
370 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
372 { "usehostname", o_bool, &usehostname,
373 "Must use hostname for authentication", 1 },
375 { "remotename", o_string, remote_name,
376 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
377 &explicit_remote, MAXNAMELEN },
379 { "login", o_bool, &uselogin,
380 "Use system password database for PAP", 1 },
382 { "papcrypt", o_bool, &cryptpap,
383 "PAP passwords are encrypted", 1 },
385 { "privgroup", o_special, (void *)privgroup,
386 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
388 { "allow-ip", o_special, (void *)set_noauth_addr,
389 "Set IP address(es) which can be used without authentication",
390 OPT_PRIV | OPT_A2LIST },
392 { "remotenumber", o_string, remote_number,
393 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
396 { "allow-number", o_special, (void *)set_permitted_number,
397 "Set telephone number(s) which are allowed to connect",
398 OPT_PRIV | OPT_A2LIST },
404 * setupapfile - specifies UPAP info for authenticating with peer.
412 char u[MAXNAMELEN], p[MAXSECRETLEN];
415 lcp_allowoptions[0].neg_upap = 1;
417 /* open user info file */
418 fname = strdup(*argv);
420 novm("+ua file name");
422 ufile = fopen(fname, "r");
425 option_error("unable to open user login data file %s", fname);
428 check_access(ufile, fname);
432 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
433 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
435 option_error("unable to read user login data file %s", fname);
440 /* get rid of newlines */
442 if (l > 0 && u[l-1] == '\n')
445 if (l > 0 && p[l-1] == '\n')
448 if (override_value("user", option_priority, fname))
449 strlcpy(user, u, sizeof(user));
450 if (override_value("passwd", option_priority, fname))
451 strlcpy(passwd, p, sizeof(passwd));
458 * privgroup - allow members of the group to have privileged access.
469 option_error("group %s is unknown", *argv);
472 for (i = 0; i < ngroups; ++i) {
473 if (groups[i] == g->gr_gid) {
483 * set_noauth_addr - set address(es) that can be used without authentication.
484 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
487 set_noauth_addr(argv)
491 int l = strlen(addr) + 1;
494 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
496 novm("allow-ip argument");
497 wp->word = (char *) (wp + 1);
498 wp->next = noauth_addrs;
499 BCOPY(addr, wp->word, l);
506 * set_permitted_number - set remote telephone number(s) that may connect.
509 set_permitted_number(argv)
512 char *number = *argv;
513 int l = strlen(number) + 1;
516 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
518 novm("allow-number argument");
519 wp->word = (char *) (wp + 1);
520 wp->next = permitted_numbers;
521 BCOPY(number, wp->word, l);
522 permitted_numbers = wp;
528 * An Open on LCP has requested a change from Dead to Establish phase.
537 * Bring the link up to the point of being able to do ppp.
539 void start_link(unit)
544 new_phase(PHASE_SERIALCONN);
546 devfd = the_channel->connect();
547 msg = "Connect script failed";
551 /* set up the serial device as a ppp interface */
553 * N.B. we used to do tdb_writelock/tdb_writeunlock around this
554 * (from establish_ppp to set_ifunit). However, we won't be
555 * doing the set_ifunit in multilink mode, which is the only time
556 * we need the atomicity that the tdb_writelock/tdb_writeunlock
557 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
559 fd_ppp = the_channel->establish_ppp(devfd);
560 msg = "ppp establishment failed";
562 status = EXIT_FATAL_ERROR;
566 if (!demand && ifunit >= 0)
570 * Start opening the connection and wait for
571 * incoming events (reply, timeout, etc.).
574 notice("Connect: %s <--> %s", ifname, ppp_devnam);
576 notice("Starting negotiation on %s", ppp_devnam);
579 status = EXIT_NEGOTIATION_FAILED;
580 new_phase(PHASE_ESTABLISH);
586 new_phase(PHASE_DISCONNECT);
587 if (the_channel->disconnect)
588 the_channel->disconnect();
591 new_phase(PHASE_DEAD);
592 if (the_channel->cleanup)
593 (*the_channel->cleanup)();
597 * LCP has terminated the link; go to the Dead phase and take the
598 * physical layer down.
601 link_terminated(unit)
604 if (phase == PHASE_DEAD || phase == PHASE_MASTER)
606 new_phase(PHASE_DISCONNECT);
608 if (pap_logout_hook) {
615 if (!doing_multilink) {
616 notice("Connection terminated.");
619 notice("Link terminated.");
622 * Delete pid files before disestablishing ppp. Otherwise it
623 * can happen that another pppd gets the same unit and then
624 * we delete its pid file.
626 if (!doing_multilink && !demand)
630 * If we may want to bring the link up again, transfer
631 * the ppp unit back to the loopback. Set the
632 * real serial device back to its normal mode of operation.
637 the_channel->disestablish_ppp(devfd);
644 if (!doing_multilink && !demand)
645 script_unsetenv("IFNAME");
648 * Run disconnector script, if requested.
649 * XXX we may not be able to do this if the line has hung up!
651 if (devfd >= 0 && the_channel->disconnect) {
652 the_channel->disconnect();
656 if (doing_multilink && multilink_master) {
657 if (!bundle_terminating)
658 new_phase(PHASE_MASTER);
660 mp_bundle_terminated();
662 new_phase(PHASE_DEAD);
666 * LCP has gone down; it will either die or try to re-establish.
672 if (auth_state != s_down) {
673 notify(link_down_notifier, 0);
675 if (auth_script_state == s_up && auth_script_pid == 0) {
676 update_link_stats(unit);
677 auth_script_state = s_down;
678 auth_script(_PATH_AUTHDOWN);
681 if (!doing_multilink) {
682 upper_layers_down(unit);
683 if (phase != PHASE_DEAD && phase != PHASE_MASTER)
684 new_phase(PHASE_ESTABLISH);
686 /* XXX if doing_multilink, should do something to stop
687 network-layer traffic on the link */
690 void upper_layers_down(int unit)
693 struct protent *protp;
695 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
696 if (!protp->enabled_flag)
698 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
699 (*protp->lowerdown)(unit);
700 if (protp->protocol < 0xC000 && protp->close != NULL)
701 (*protp->close)(unit, "LCP down");
708 * The link is established.
709 * Proceed to the Dead, Authenticate or Network phase as appropriate.
712 link_established(unit)
716 lcp_options *wo = &lcp_wantoptions[unit];
717 lcp_options *go = &lcp_gotoptions[unit];
718 lcp_options *ho = &lcp_hisoptions[unit];
720 struct protent *protp;
723 * Tell higher-level protocols that LCP is up.
725 if (!doing_multilink) {
726 for (i = 0; (protp = protocols[i]) != NULL; ++i)
727 if (protp->protocol != PPP_LCP && protp->enabled_flag
728 && protp->lowerup != NULL)
729 (*protp->lowerup)(unit);
732 if (!auth_required && noauth_addrs != NULL)
733 set_allowed_addrs(unit, NULL, NULL);
735 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
737 * We wanted the peer to authenticate itself, and it refused:
738 * if we have some address(es) it can use without auth, fine,
739 * otherwise treat it as though it authenticated with PAP using
740 * a username of "" and a password of "". If that's not OK,
743 if (noauth_addrs != NULL) {
744 set_allowed_addrs(unit, NULL, NULL);
745 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
746 warn("peer refused to authenticate: terminating link");
747 lcp_close(unit, "peer refused to authenticate");
748 status = EXIT_PEER_AUTH_FAILED;
753 new_phase(PHASE_AUTHENTICATE);
757 eap_authpeer(unit, our_name);
759 } else if (go->neg_chap) {
760 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
762 } else if (go->neg_upap) {
767 eap_authwithpeer(unit, user);
768 auth |= EAP_WITHPEER;
769 } else if (ho->neg_chap) {
770 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
771 auth |= CHAP_WITHPEER;
772 } else if (ho->neg_upap) {
773 if (passwd[0] == 0) {
774 passwd_from_file = 1;
775 if (!get_pap_passwd(passwd))
776 error("No secret found for PAP login");
778 upap_authwithpeer(unit, user, passwd);
779 auth |= PAP_WITHPEER;
781 auth_pending[unit] = auth;
789 * Proceed to the network phase.
795 lcp_options *go = &lcp_gotoptions[unit];
797 /* Log calling number. */
799 notice("peer from calling number %q authorized", remote_number);
802 * If the peer had to authenticate, run the auth-up script now.
804 if (go->neg_chap || go->neg_upap || go->neg_eap) {
805 notify(auth_up_notifier, 0);
807 if (auth_script_state == s_down && auth_script_pid == 0) {
808 auth_script_state = s_up;
809 auth_script(_PATH_AUTHUP);
815 * If we negotiated callback, do it now.
818 new_phase(PHASE_CALLBACK);
819 (*cbcp_protent.open)(unit);
825 * Process extra options from the secrets file
828 options_from_list(extra_options, 1);
829 free_wordlist(extra_options);
832 start_networks(unit);
840 struct protent *protp;
841 int ecp_required, mppe_required;
843 new_phase(PHASE_NETWORK);
845 #ifdef HAVE_MULTILINK
847 if (mp_join_bundle()) {
848 if (updetach && !nodetach)
853 #endif /* HAVE_MULTILINK */
857 set_filters(&pass_filter, &active_filter);
859 /* Start CCP and ECP */
860 for (i = 0; (protp = protocols[i]) != NULL; ++i)
861 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
862 && protp->enabled_flag && protp->open != NULL)
866 * Bring up other network protocols iff encryption is not required.
868 ecp_required = ecp_gotoptions[unit].required;
869 mppe_required = ccp_gotoptions[unit].mppe;
870 if (!ecp_required && !mppe_required)
871 continue_networks(unit);
875 continue_networks(unit)
879 struct protent *protp;
882 * Start the "real" network protocols.
884 for (i = 0; (protp = protocols[i]) != NULL; ++i)
885 if (protp->protocol < 0xC000
886 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
887 && protp->enabled_flag && protp->open != NULL) {
892 if (num_np_open == 0)
894 lcp_close(0, "No network protocols running");
898 * The peer has failed to authenticate himself using `protocol'.
901 auth_peer_fail(unit, protocol)
905 * Authentication failure: take the link down
907 lcp_close(unit, "Authentication failed");
908 status = EXIT_PEER_AUTH_FAILED;
912 * The peer has been successfully authenticated using `protocol'.
915 auth_peer_success(unit, protocol, prot_flavor, name, namelen)
916 int unit, protocol, prot_flavor;
925 switch (prot_flavor) {
927 bit |= CHAP_MD5_PEER;
933 case CHAP_MICROSOFT_V2:
934 bit |= CHAP_MS2_PEER;
946 warn("auth_peer_success: unknown protocol %x", protocol);
951 * Save the authenticated name of the peer for later.
953 if (namelen > sizeof(peer_authname) - 1)
954 namelen = sizeof(peer_authname) - 1;
955 BCOPY(name, peer_authname, namelen);
956 peer_authname[namelen] = 0;
957 script_setenv("PEERNAME", peer_authname, 0);
959 /* Save the authentication method for later. */
960 auth_done[unit] |= bit;
963 * If there is no more authentication still to be done,
964 * proceed to the network (or callback) phase.
966 if ((auth_pending[unit] &= ~bit) == 0)
971 * We have failed to authenticate ourselves to the peer using `protocol'.
974 auth_withpeer_fail(unit, protocol)
977 if (passwd_from_file)
978 BZERO(passwd, MAXSECRETLEN);
980 * We've failed to authenticate ourselves to our peer.
981 * Some servers keep sending CHAP challenges, but there
982 * is no point in persisting without any way to get updated
983 * authentication secrets.
985 lcp_close(unit, "Failed to authenticate ourselves to peer");
986 status = EXIT_AUTH_TOPEER_FAILED;
990 * We have successfully authenticated ourselves with the peer using `protocol'.
993 auth_withpeer_success(unit, protocol, prot_flavor)
994 int unit, protocol, prot_flavor;
997 const char *prot = "";
1001 bit = CHAP_WITHPEER;
1003 switch (prot_flavor) {
1005 bit |= CHAP_MD5_WITHPEER;
1008 case CHAP_MICROSOFT:
1009 bit |= CHAP_MS_WITHPEER;
1011 case CHAP_MICROSOFT_V2:
1012 bit |= CHAP_MS2_WITHPEER;
1018 if (passwd_from_file)
1019 BZERO(passwd, MAXSECRETLEN);
1028 warn("auth_withpeer_success: unknown protocol %x", protocol);
1032 notice("%s authentication succeeded", prot);
1034 /* Save the authentication method for later. */
1035 auth_done[unit] |= bit;
1038 * If there is no more authentication still being done,
1039 * proceed to the network (or callback) phase.
1041 if ((auth_pending[unit] &= ~bit) == 0)
1042 network_phase(unit);
1047 * np_up - a network protocol has come up.
1055 if (num_np_up == 0) {
1057 * At this point we consider that the link has come up successfully.
1061 new_phase(PHASE_RUNNING);
1063 if (idle_time_hook != 0)
1064 tlim = (*idle_time_hook)(NULL);
1066 tlim = idle_time_limit;
1068 TIMEOUT(check_idle, NULL, tlim);
1071 * Set a timeout to close the connection once the maximum
1072 * connect time has expired.
1075 TIMEOUT(connect_time_expired, 0, maxconnect);
1079 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1083 * Detach now, if the updetach option was given.
1085 if (updetach && !nodetach)
1092 * np_down - a network protocol has gone down.
1095 np_down(unit, proto)
1098 if (--num_np_up == 0) {
1099 UNTIMEOUT(check_idle, NULL);
1100 UNTIMEOUT(connect_time_expired, NULL);
1102 UNTIMEOUT(check_maxoctets, NULL);
1104 new_phase(PHASE_NETWORK);
1109 * np_finished - a network protocol has finished using the link.
1112 np_finished(unit, proto)
1115 if (--num_np_open <= 0) {
1116 /* no further use for the link: shut up shop. */
1117 lcp_close(0, "No network protocols running");
1123 check_maxoctets(arg)
1129 update_link_stats(ifunit);
1132 switch(maxoctets_dir) {
1133 case PPP_OCTETS_DIRECTION_IN:
1134 used = link_stats.bytes_in;
1136 case PPP_OCTETS_DIRECTION_OUT:
1137 used = link_stats.bytes_out;
1139 case PPP_OCTETS_DIRECTION_MAXOVERAL:
1140 case PPP_OCTETS_DIRECTION_MAXSESSION:
1141 used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
1144 used = link_stats.bytes_in+link_stats.bytes_out;
1147 diff = maxoctets - used;
1149 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
1150 lcp_close(0, "Traffic limit");
1152 status = EXIT_TRAFFIC_LIMIT;
1154 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
1160 * check_idle - check whether the link has been idle for long
1161 * enough that we can shut it down.
1167 struct ppp_idle idle;
1171 if (!get_idle_time(0, &idle))
1173 if (idle_time_hook != 0) {
1174 tlim = idle_time_hook(&idle);
1176 itime = MIN(idle.xmit_idle, idle.recv_idle);
1177 tlim = idle_time_limit - itime;
1180 /* link is idle: shut it down. */
1181 notice("Terminating connection due to lack of activity.");
1182 lcp_close(0, "Link inactive");
1184 status = EXIT_IDLE_TIMEOUT;
1186 TIMEOUT(check_idle, NULL, tlim);
1191 * connect_time_expired - log a message and close the connection.
1194 connect_time_expired(arg)
1197 info("Connect time expired");
1198 status = EXIT_CONNECT_TIME;
1199 lcp_close(0, "Connect time expired"); /* Close connection */
1203 * auth_check_options - called to check authentication options.
1206 auth_check_options()
1208 lcp_options *wo = &lcp_wantoptions[0];
1212 /* Default our_name to hostname, and user to our_name */
1213 if (our_name[0] == 0 || usehostname)
1214 strlcpy(our_name, hostname, sizeof(our_name));
1216 strlcpy(user, our_name, sizeof(user));
1219 * If we have a default route, require the peer to authenticate
1220 * unless the noauth option was given or the real user is root.
1222 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
1227 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
1228 if (wo->chap_mdtype)
1231 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
1232 if (auth_required) {
1234 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
1235 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
1236 wo->chap_mdtype = chap_mdtype_all;
1242 wo->chap_mdtype = MDTYPE_NONE;
1248 * Check whether we have appropriate secrets to use
1249 * to authenticate the peer. Note that EAP can authenticate by way
1250 * of a CHAP-like exchanges as well as SRP.
1253 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
1254 if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
1255 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
1256 our_name, 1, &lacks_ip);
1258 if (!can_auth && wo->neg_eap) {
1259 can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
1260 our_name, 1, &lacks_ip);
1263 if (auth_required && !can_auth && noauth_addrs == NULL) {
1266 "By default the remote system is required to authenticate itself");
1268 "(because this system has a default route to the internet)");
1269 } else if (explicit_remote)
1271 "The remote system (%s) is required to authenticate itself",
1275 "The remote system is required to authenticate itself");
1277 "but I couldn't find any suitable secret (password) for it to use to do so.");
1280 "(None of the available passwords would let it use an IP address.)");
1286 * Early check for remote number authorization.
1288 if (!auth_number()) {
1289 warn("calling number %q is not authorized", remote_number);
1290 exit(EXIT_CNID_AUTH_FAILED);
1295 * auth_reset - called when LCP is starting negotiations to recheck
1296 * authentication options, i.e. whether we have appropriate secrets
1297 * to use for authenticating ourselves and/or the peer.
1303 lcp_options *go = &lcp_gotoptions[unit];
1304 lcp_options *ao = &lcp_allowoptions[unit];
1308 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
1309 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
1310 && (passwd[0] != 0 ||
1311 (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
1313 ao->neg_eap = !refuse_eap && (
1315 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
1316 (explicit_remote? remote_name: NULL), 0, NULL))) ||
1317 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
1320 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
1323 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
1324 our_name, 1, NULL)))
1328 (hadchap == 0 || (hadchap == -1 &&
1329 !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
1331 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
1338 * check_passwd - Check the user name and passwd against the PAP secrets
1339 * file. If requested, also check against the system password database,
1340 * and login the user if OK.
1343 * UPAP_AUTHNAK: Authentication failed.
1344 * UPAP_AUTHACK: Authentication succeeded.
1345 * In either case, msg points to an appropriate message.
1348 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
1359 struct wordlist *addrs = NULL, *opts = NULL;
1360 char passwd[256], user[256];
1361 char secret[MAXWORDLEN];
1362 static int attempts = 0;
1365 * Make copies of apasswd and auser, then null-terminate them.
1366 * If there are unprintable characters in the password, make
1369 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
1370 slprintf(user, sizeof(user), "%.*v", userlen, auser);
1374 * Check if a plugin wants to handle this.
1376 if (pap_auth_hook) {
1377 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
1379 /* note: set_allowed_addrs() saves opts (but not addrs):
1382 set_allowed_addrs(unit, addrs, opts);
1384 free_wordlist(opts);
1386 free_wordlist(addrs);
1387 BZERO(passwd, sizeof(passwd));
1388 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
1393 * Open the file of pap secrets and scan for a suitable secret
1394 * for authenticating this user.
1396 filename = _PATH_UPAPFILE;
1397 addrs = opts = NULL;
1399 f = fopen(filename, "r");
1401 error("Can't open PAP password file %s: %m", filename);
1404 check_access(f, filename);
1405 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
1406 warn("no PAP secret found for %s", user);
1409 * If the secret is "@login", it means to check
1410 * the password against the login database.
1412 int login_secret = strcmp(secret, "@login") == 0;
1414 if (uselogin || login_secret) {
1415 /* login option or secret is @login */
1416 if ((ret = plogin(user, passwd, msg)) == UPAP_AUTHACK)
1419 if (secret[0] != 0 && !login_secret) {
1420 /* password given in pap-secrets - must match */
1421 if ((cryptpap || strcmp(passwd, secret) != 0)
1422 && strcmp(crypt(passwd, secret), secret) != 0)
1429 if (ret == UPAP_AUTHNAK) {
1431 *msg = "Login incorrect";
1433 * XXX can we ever get here more than once??
1434 * Frustrate passwd stealer programs.
1435 * Allow 10 tries, but start backing off after 3 (stolen from login).
1436 * On 10'th, drop the connection.
1438 if (attempts++ >= 10) {
1439 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1440 lcp_close(unit, "login failed");
1443 sleep((u_int) (attempts - 3) * 5);
1445 free_wordlist(opts);
1448 attempts = 0; /* Reset count */
1451 set_allowed_addrs(unit, addrs, opts);
1455 free_wordlist(addrs);
1456 BZERO(passwd, sizeof(passwd));
1457 BZERO(secret, sizeof(secret));
1463 * This function is needed for PAM.
1467 /* Static variables used to communicate between the conversation function
1468 * and the server_login function
1470 static char *PAM_username;
1471 static char *PAM_password;
1472 static int PAM_error = 0;
1473 static pam_handle_t *pamh = NULL;
1475 /* PAM conversation function
1476 * Here we assume (for now, at least) that echo on means login name, and
1477 * echo off means password.
1480 static int PAM_conv (int num_msg,
1484 struct pam_message **msg,
1485 struct pam_response **resp, void *appdata_ptr)
1488 struct pam_response *reply = NULL;
1490 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1492 reply = malloc(sizeof(struct pam_response) * num_msg);
1493 if (!reply) return PAM_CONV_ERR;
1495 for (replies = 0; replies < num_msg; replies++) {
1496 switch (msg[replies]->msg_style) {
1497 case PAM_PROMPT_ECHO_ON:
1498 reply[replies].resp_retcode = PAM_SUCCESS;
1499 reply[replies].resp = COPY_STRING(PAM_username);
1500 /* PAM frees resp */
1502 case PAM_PROMPT_ECHO_OFF:
1503 reply[replies].resp_retcode = PAM_SUCCESS;
1504 reply[replies].resp = COPY_STRING(PAM_password);
1505 /* PAM frees resp */
1510 /* ignore it, but pam still wants a NULL response... */
1511 reply[replies].resp_retcode = PAM_SUCCESS;
1512 reply[replies].resp = NULL;
1515 /* Must be an error of some sort... */
1518 return PAM_CONV_ERR;
1525 static struct pam_conv PAM_conversation = {
1529 #endif /* USE_PAM */
1532 * plogin - Check the user name and password against the system
1533 * password database, and login the user if OK.
1536 * UPAP_AUTHNAK: Login failed.
1537 * UPAP_AUTHACK: Login succeeded.
1538 * In either case, msg points to an appropriate message.
1542 plogin(user, passwd, msg)
1552 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1553 if (pam_error != PAM_SUCCESS) {
1554 *msg = (char *) pam_strerror (pamh, pam_error);
1556 return UPAP_AUTHNAK;
1559 * Define the fields for the credential validation
1562 PAM_username = user;
1563 PAM_password = passwd;
1565 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1570 pam_error = pam_authenticate (pamh, PAM_SILENT);
1571 if (pam_error == PAM_SUCCESS && !PAM_error) {
1572 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1573 if (pam_error == PAM_SUCCESS)
1574 pam_error = pam_open_session (pamh, PAM_SILENT);
1577 *msg = (char *) pam_strerror (pamh, pam_error);
1582 reopen_log(); /* apparently the PAM stuff does closelog() */
1583 PAM_username = NULL;
1584 PAM_password = NULL;
1585 if (pam_error != PAM_SUCCESS)
1586 return UPAP_AUTHNAK;
1587 #else /* #ifdef USE_PAM */
1590 * Use the non-PAM methods directly
1595 struct spwd *getspnam();
1597 struct passwd *pw = getpwnam(user);
1601 return (UPAP_AUTHNAK);
1604 spwd = getspnam(user);
1607 /* check the age of the password entry */
1608 long now = time(NULL) / 86400L;
1610 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1611 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1612 && spwd->sp_lstchg >= 0
1613 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1614 warn("Password for %s has expired", user);
1615 return (UPAP_AUTHNAK);
1617 pw->pw_passwd = spwd->sp_pwdp;
1622 * If no passwd, don't let them login.
1624 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1625 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1626 return (UPAP_AUTHNAK);
1628 #endif /* #ifdef USE_PAM */
1631 * Write a wtmp entry for this user.
1635 if (strncmp(tty, "/dev/", 5) == 0)
1637 logwtmp(tty, user, ifname); /* Add wtmp login entry */
1639 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1640 if (pw != (struct passwd *)NULL) {
1644 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1645 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1646 memset((void *)&ll, 0, sizeof(ll));
1647 (void)time(&ll.ll_time);
1648 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1649 (void)write(fd, (char *)&ll, sizeof(ll));
1653 #endif /* _PATH_LASTLOG and not USE_PAM */
1655 info("user %s logged in", user);
1658 return (UPAP_AUTHACK);
1662 * plogout - Logout the user.
1672 pam_error = pam_close_session (pamh, PAM_SILENT);
1673 pam_end (pamh, pam_error);
1676 /* Apparently the pam stuff does closelog(). */
1678 #endif /* USE_PAM */
1681 if (strncmp(tty, "/dev/", 5) == 0)
1683 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1689 * null_login - Check if a username of "" and a password of "" are
1690 * acceptable, and iff so, set the list of acceptable IP addresses
1700 struct wordlist *addrs, *opts;
1701 char secret[MAXWORDLEN];
1704 * Check if a plugin wants to handle this.
1708 ret = (*null_auth_hook)(&addrs, &opts);
1711 * Open the file of pap secrets and scan for a suitable secret.
1714 filename = _PATH_UPAPFILE;
1716 f = fopen(filename, "r");
1719 check_access(f, filename);
1721 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
1722 ret = i >= 0 && secret[0] == 0;
1723 BZERO(secret, sizeof(secret));
1728 set_allowed_addrs(unit, addrs, opts);
1730 free_wordlist(opts);
1732 free_wordlist(addrs);
1739 * get_pap_passwd - get a password for authenticating ourselves with
1740 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1742 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1745 get_pap_passwd(passwd)
1751 char secret[MAXWORDLEN];
1754 * Check whether a plugin wants to supply this.
1756 if (pap_passwd_hook) {
1757 ret = (*pap_passwd_hook)(user, passwd);
1762 filename = _PATH_UPAPFILE;
1763 f = fopen(filename, "r");
1766 check_access(f, filename);
1767 ret = scan_authfile(f, user,
1768 (remote_name[0]? remote_name: NULL),
1769 secret, NULL, NULL, filename, 0);
1774 strlcpy(passwd, secret, MAXSECRETLEN);
1775 BZERO(secret, sizeof(secret));
1781 * have_pap_secret - check whether we have a PAP file with any
1782 * secrets that we could possibly use for authenticating the peer.
1785 have_pap_secret(lacks_ipp)
1791 struct wordlist *addrs;
1793 /* let the plugin decide, if there is one */
1794 if (pap_check_hook) {
1795 ret = (*pap_check_hook)();
1800 filename = _PATH_UPAPFILE;
1801 f = fopen(filename, "r");
1805 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1806 NULL, &addrs, NULL, filename, 0);
1808 if (ret >= 0 && !some_ip_ok(addrs)) {
1814 free_wordlist(addrs);
1821 * have_chap_secret - check whether we have a CHAP file with a
1822 * secret that we could possibly use for authenticating `client'
1823 * on `server'. Either can be the null string, meaning we don't
1824 * know the identity yet.
1827 have_chap_secret(client, server, need_ip, lacks_ipp)
1836 struct wordlist *addrs;
1838 if (chap_check_hook) {
1839 ret = (*chap_check_hook)();
1845 filename = _PATH_CHAPFILE;
1846 f = fopen(filename, "r");
1850 if (client != NULL && client[0] == 0)
1852 else if (server != NULL && server[0] == 0)
1855 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1857 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1863 free_wordlist(addrs);
1870 * have_srp_secret - check whether we have a SRP file with a
1871 * secret that we could possibly use for authenticating `client'
1872 * on `server'. Either can be the null string, meaning we don't
1873 * know the identity yet.
1876 have_srp_secret(client, server, need_ip, lacks_ipp)
1885 struct wordlist *addrs;
1887 filename = _PATH_SRPFILE;
1888 f = fopen(filename, "r");
1892 if (client != NULL && client[0] == 0)
1894 else if (server != NULL && server[0] == 0)
1897 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
1899 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1905 free_wordlist(addrs);
1912 * get_secret - open the CHAP secret file and return the secret
1913 * for authenticating the given client on the given server.
1914 * (We could be either client or server).
1917 get_secret(unit, client, server, secret, secret_len, am_server)
1928 struct wordlist *addrs, *opts;
1929 char secbuf[MAXWORDLEN];
1931 if (!am_server && passwd[0] != 0) {
1932 strlcpy(secbuf, passwd, sizeof(secbuf));
1933 } else if (!am_server && chap_passwd_hook) {
1934 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1935 error("Unable to obtain CHAP password for %s on %s from plugin",
1940 filename = _PATH_CHAPFILE;
1944 f = fopen(filename, "r");
1946 error("Can't open chap secret file %s: %m", filename);
1949 check_access(f, filename);
1951 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
1957 set_allowed_addrs(unit, addrs, opts);
1959 free_wordlist(opts);
1961 free_wordlist(addrs);
1964 len = strlen(secbuf);
1965 if (len > MAXSECRETLEN) {
1966 error("Secret for %s on %s is too long", client, server);
1969 BCOPY(secbuf, secret, len);
1970 BZERO(secbuf, sizeof(secbuf));
1978 * get_srp_secret - open the SRP secret file and return the secret
1979 * for authenticating the given client on the given server.
1980 * (We could be either client or server).
1983 get_srp_secret(unit, client, server, secret, am_server)
1993 struct wordlist *addrs, *opts;
1995 if (!am_server && passwd[0] != '\0') {
1996 strlcpy(secret, passwd, MAXWORDLEN);
1998 filename = _PATH_SRPFILE;
2001 fp = fopen(filename, "r");
2003 error("Can't open srp secret file %s: %m", filename);
2006 check_access(fp, filename);
2009 ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
2010 filename, am_server);
2016 set_allowed_addrs(unit, addrs, opts);
2017 else if (opts != NULL)
2018 free_wordlist(opts);
2020 free_wordlist(addrs);
2027 * set_allowed_addrs() - set the list of allowed addresses.
2028 * Also looks for `--' indicating options to apply for this peer
2029 * and leaves the following words in extra_options.
2032 set_allowed_addrs(unit, addrs, opts)
2034 struct wordlist *addrs;
2035 struct wordlist *opts;
2038 struct wordlist *ap, **plink;
2039 struct permitted_ip *ip;
2040 char *ptr_word, *ptr_mask;
2043 u_int32_t a, mask, ah, offset;
2044 struct ipcp_options *wo = &ipcp_wantoptions[unit];
2045 u_int32_t suggested_ip = 0;
2047 if (addresses[unit] != NULL)
2048 free(addresses[unit]);
2049 addresses[unit] = NULL;
2050 if (extra_options != NULL)
2051 free_wordlist(extra_options);
2052 extra_options = opts;
2055 * Count the number of IP addresses given.
2057 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
2060 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
2064 /* temporarily append the noauth_addrs list to addrs */
2065 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
2067 *plink = noauth_addrs;
2070 for (ap = addrs; ap != NULL; ap = ap->next) {
2071 /* "-" means no addresses authorized, "*" means any address allowed */
2072 ptr_word = ap->word;
2073 if (strcmp(ptr_word, "-") == 0)
2075 if (strcmp(ptr_word, "*") == 0) {
2077 ip[n].base = ip[n].mask = 0;
2083 if (*ptr_word == '!') {
2088 mask = ~ (u_int32_t) 0;
2090 ptr_mask = strchr (ptr_word, '/');
2091 if (ptr_mask != NULL) {
2095 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
2096 if (bit_count <= 0 || bit_count > 32) {
2097 warn("invalid address length %v in auth. address list",
2101 bit_count = 32 - bit_count; /* # bits in host part */
2103 offset = ifunit + 1;
2107 warn("invalid address length syntax: %v", ptr_mask+1);
2114 hp = gethostbyname(ptr_word);
2115 if (hp != NULL && hp->h_addrtype == AF_INET) {
2116 a = *(u_int32_t *)hp->h_addr;
2118 np = getnetbyname (ptr_word);
2119 if (np != NULL && np->n_addrtype == AF_INET) {
2120 a = htonl ((u_int32_t)np->n_net);
2121 if (ptr_mask == NULL) {
2122 /* calculate appropriate mask for net */
2125 mask = IN_CLASSA_NET;
2126 else if (IN_CLASSB(ah))
2127 mask = IN_CLASSB_NET;
2128 else if (IN_CLASSC(ah))
2129 mask = IN_CLASSC_NET;
2132 a = inet_addr (ptr_word);
2136 if (ptr_mask != NULL)
2139 if (a == (u_int32_t)-1L) {
2140 warn("unknown host %s in auth. address list", ap->word);
2144 if (offset >= ~mask) {
2145 warn("interface unit %d too large for subnet %v",
2149 a = htonl((ntohl(a) & mask) + offset);
2150 mask = ~(u_int32_t)0;
2152 ip[n].mask = htonl(mask);
2153 ip[n].base = a & ip[n].mask;
2155 if (~mask == 0 && suggested_ip == 0)
2160 ip[n].permit = 0; /* make the last entry forbid all addresses */
2161 ip[n].base = 0; /* to terminate the list */
2164 addresses[unit] = ip;
2167 * If the address given for the peer isn't authorized, or if
2168 * the user hasn't given one, AND there is an authorized address
2169 * which is a single host, then use that if we find one.
2171 if (suggested_ip != 0
2172 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
2173 wo->hisaddr = suggested_ip;
2175 * Do we insist on this address? No, if there are other
2176 * addresses authorized than the suggested one.
2179 wo->accept_remote = 1;
2184 * auth_ip_addr - check whether the peer is authorized to use
2185 * a given IP address. Returns 1 if authorized, 0 otherwise.
2188 auth_ip_addr(unit, addr)
2194 /* don't allow loopback or multicast address */
2195 if (bad_ip_adrs(addr))
2198 if (allowed_address_hook) {
2199 ok = allowed_address_hook(addr);
2200 if (ok >= 0) return ok;
2203 if (addresses[unit] != NULL) {
2204 ok = ip_addr_check(addr, addresses[unit]);
2210 return 0; /* no addresses authorized */
2211 return allow_any_ip || privileged || !have_route_to(addr);
2215 ip_addr_check(addr, addrs)
2217 struct permitted_ip *addrs;
2220 if ((addr & addrs->mask) == addrs->base)
2221 return addrs->permit;
2225 * bad_ip_adrs - return 1 if the IP address is one we don't want
2226 * to use, such as an address in the loopback net or a multicast address.
2227 * addr is in network byte order.
2234 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
2235 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
2239 * some_ip_ok - check a wordlist to see if it authorizes any
2244 struct wordlist *addrs;
2246 for (; addrs != 0; addrs = addrs->next) {
2247 if (addrs->word[0] == '-')
2249 if (addrs->word[0] != '!')
2250 return 1; /* some IP address is allowed */
2256 * auth_number - check whether the remote number is allowed to connect.
2257 * Returns 1 if authorized, 0 otherwise.
2262 struct wordlist *wp = permitted_numbers;
2265 /* Allow all if no authorization list. */
2269 /* Allow if we have a match in the authorization list. */
2271 /* trailing '*' wildcard */
2272 l = strlen(wp->word);
2273 if ((wp->word)[l - 1] == '*')
2275 if (!strncasecmp(wp->word, remote_number, l))
2284 * check_access - complain if a secret file has too-liberal permissions.
2287 check_access(f, filename)
2293 if (fstat(fileno(f), &sbuf) < 0) {
2294 warn("cannot stat secret file %s: %m", filename);
2295 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
2296 warn("Warning - secret file %s has world and/or group access",
2303 * scan_authfile - Scan an authorization file for a secret suitable
2304 * for authenticating `client' on `server'. The return value is -1
2305 * if no secret is found, otherwise >= 0. The return value has
2306 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
2307 * NONWILD_SERVER set if the secret didn't have "*" for the server.
2308 * Any following words on the line up to a "--" (i.e. address authorization
2309 * info) are placed in a wordlist and returned in *addrs. Any
2310 * following words (extra options) are placed in a wordlist and
2311 * returned in *opts.
2312 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
2313 * Flags are non-zero if we need two colons in the secret in order to
2317 scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
2322 struct wordlist **addrs;
2323 struct wordlist **opts;
2328 int got_flag, best_flag;
2330 struct wordlist *ap, *addr_list, *alist, **app;
2331 char word[MAXWORDLEN];
2332 char atfile[MAXWORDLEN];
2333 char lsecret[MAXWORDLEN];
2341 if (!getword(f, word, &newline, filename))
2342 return -1; /* file is empty??? */
2347 * Skip until we find a word at the start of a line.
2349 while (!newline && getword(f, word, &newline, filename))
2352 break; /* got to end of file */
2355 * Got a client - check if it's a match or a wildcard.
2358 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
2363 got_flag = NONWILD_CLIENT;
2366 * Now get a server and check if it matches.
2368 if (!getword(f, word, &newline, filename))
2372 if (!ISWILD(word)) {
2373 if (server != NULL && strcmp(word, server) != 0)
2375 got_flag |= NONWILD_SERVER;
2379 * Got some sort of a match - see if it's better than what
2382 if (got_flag <= best_flag)
2388 if (!getword(f, word, &newline, filename))
2394 * SRP-SHA1 authenticator should never be reading secrets from
2395 * a file. (Authenticatee may, though.)
2397 if (flags && ((cp = strchr(word, ':')) == NULL ||
2398 strchr(cp + 1, ':') == NULL))
2401 if (secret != NULL) {
2403 * Special syntax: @/pathname means read secret from file.
2405 if (word[0] == '@' && word[1] == '/') {
2406 strlcpy(atfile, word+1, sizeof(atfile));
2407 if ((sf = fopen(atfile, "r")) == NULL) {
2408 warn("can't open indirect secret file %s", atfile);
2411 check_access(sf, atfile);
2412 if (!getword(sf, word, &xxx, atfile)) {
2413 warn("no secret in indirect secret file %s", atfile);
2419 strlcpy(lsecret, word, sizeof(lsecret));
2423 * Now read address authorization info and make a wordlist.
2427 if (!getword(f, word, &newline, filename) || newline)
2429 ap = (struct wordlist *)
2430 malloc(sizeof(struct wordlist) + strlen(word) + 1);
2432 novm("authorized addresses");
2433 ap->word = (char *) (ap + 1);
2434 strcpy(ap->word, word);
2441 * This is the best so far; remember it.
2443 best_flag = got_flag;
2445 free_wordlist(addr_list);
2448 strlcpy(secret, lsecret, MAXWORDLEN);
2454 /* scan for a -- word indicating the start of options */
2455 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
2456 if (strcmp(ap->word, "--") == 0)
2458 /* ap = start of options */
2460 ap = ap->next; /* first option */
2461 free(*app); /* free the "--" word */
2462 *app = NULL; /* terminate addr list */
2466 else if (ap != NULL)
2470 else if (addr_list != NULL)
2471 free_wordlist(addr_list);
2477 * wordlist_count - return the number of items in a wordlist
2481 struct wordlist *wp;
2485 for (n = 0; wp != NULL; wp = wp->next)
2491 * free_wordlist - release memory allocated for a wordlist.
2495 struct wordlist *wp;
2497 struct wordlist *next;
2499 while (wp != NULL) {
2507 * auth_script_done - called when the auth-up or auth-down script
2511 auth_script_done(arg)
2514 auth_script_pid = 0;
2515 switch (auth_script_state) {
2517 if (auth_state == s_down) {
2518 auth_script_state = s_down;
2519 auth_script(_PATH_AUTHDOWN);
2523 if (auth_state == s_up) {
2524 auth_script_state = s_up;
2525 auth_script(_PATH_AUTHUP);
2532 * auth_script - execute a script with arguments
2533 * interface-name peer-name real-user tty speed
2545 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2546 user_name = pw->pw_name;
2548 slprintf(struid, sizeof(struid), "%d", getuid());
2551 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2555 argv[2] = peer_authname;
2556 argv[3] = user_name;
2561 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);