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.74 2002/03/01 14:39:18 dfs 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
78 #include "pathnames.h"
80 static const char rcsid[] = RCSID;
82 /* Bits in scan_authfile return value */
83 #define NONWILD_SERVER 1
84 #define NONWILD_CLIENT 2
86 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
88 /* The name by which the peer authenticated itself to us. */
89 char peer_authname[MAXNAMELEN];
91 /* Records which authentication operations haven't completed yet. */
92 static int auth_pending[NUM_PPP];
94 /* Set if we have successfully called plogin() */
97 /* List of addresses which the peer may use. */
98 static struct permitted_ip *addresses[NUM_PPP];
100 /* Wordlist giving addresses which the peer may use
101 without authenticating itself. */
102 static struct wordlist *noauth_addrs;
104 /* Extra options to apply, from the secrets file entry for the peer. */
105 static struct wordlist *extra_options;
107 /* Number of network protocols which we have opened. */
108 static int num_np_open;
110 /* Number of network protocols which have come up. */
111 static int num_np_up;
113 /* Set if we got the contents of passwd[] from the pap-secrets file. */
114 static int passwd_from_file;
116 /* Set if we require authentication only because we have a default route. */
117 static bool default_auth;
119 /* Hook to enable a plugin to control the idle time limit */
120 int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
122 /* Hook for a plugin to say whether we can possibly authenticate any peer */
123 int (*pap_check_hook) __P((void)) = NULL;
125 /* Hook for a plugin to check the PAP user and password */
126 int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
127 struct wordlist **paddrs,
128 struct wordlist **popts)) = NULL;
130 /* Hook for a plugin to know about the PAP user logout */
131 void (*pap_logout_hook) __P((void)) = NULL;
133 /* Hook for a plugin to get the PAP password for authenticating us */
134 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
136 /* Hook for a plugin to say whether it is OK if the peer
137 refuses to authenticate. */
138 int (*null_auth_hook) __P((struct wordlist **paddrs,
139 struct wordlist **popts)) = NULL;
141 int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
143 /* A notifier for when the peer has authenticated itself,
144 and we are proceeding to the network phase. */
145 struct notifier *auth_up_notifier = NULL;
147 /* A notifier for when the link goes down. */
148 struct notifier *link_down_notifier = NULL;
151 * This is used to ensure that we don't start an auth-up/down
152 * script while one is already running.
159 static enum script_state auth_state = s_down;
160 static enum script_state auth_script_state = s_down;
161 static pid_t auth_script_pid = 0;
163 static int used_login; /* peer authenticated against login database */
168 bool uselogin = 0; /* Use /etc/passwd for checking PAP */
169 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
170 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
171 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
173 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
175 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
177 bool usehostname = 0; /* Use hostname for our_name */
178 bool auth_required = 0; /* Always require authentication from peer */
179 bool allow_any_ip = 0; /* Allow peer to use any IP address */
180 bool explicit_remote = 0; /* User specified explicit remote name */
181 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
183 static char *uafname; /* name of most recent +ua file */
185 /* Bits in auth_pending[] */
186 #define PAP_WITHPEER 1
188 #define CHAP_WITHPEER 4
191 extern char *crypt __P((const char *, const char *));
193 /* Prototypes for procedures local to this file. */
195 static void network_phase __P((int));
196 static void check_idle __P((void *));
197 static void connect_time_expired __P((void *));
198 static int plogin __P((char *, char *, char **));
199 static void plogout __P((void));
200 static int null_login __P((int));
201 static int get_pap_passwd __P((char *));
202 static int have_pap_secret __P((int *));
203 static int have_chap_secret __P((char *, char *, int, int *));
204 static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
205 static int scan_authfile __P((FILE *, char *, char *, char *,
206 struct wordlist **, struct wordlist **,
208 static void free_wordlist __P((struct wordlist *));
209 static void auth_script __P((char *));
210 static void auth_script_done __P((void *));
211 static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
212 static int some_ip_ok __P((struct wordlist *));
213 static int setupapfile __P((char **));
214 static int privgroup __P((char **));
215 static int set_noauth_addr __P((char **));
216 static void check_access __P((FILE *, char *));
217 static int wordlist_count __P((struct wordlist *));
220 * Authentication-related options.
222 option_t auth_options[] = {
223 { "auth", o_bool, &auth_required,
224 "Require authentication from peer", OPT_PRIO | 1 },
225 { "noauth", o_bool, &auth_required,
226 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV | OPT_A2COPY,
228 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
229 "Require PAP authentication from peer",
230 OPT_PRIOSUB | OPT_A2COPY | 1, &auth_required },
231 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
232 "Require PAP authentication from peer",
233 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | 1, &auth_required },
234 { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
235 "Require CHAP authentication from peer",
236 OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MD5,
237 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
238 { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
239 "Require CHAP authentication from peer",
240 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MD5,
241 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
243 { "require-mschap", o_bool, &lcp_wantoptions[0].neg_chap,
244 "Require MS-CHAP authentication from peer",
245 OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT,
246 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
247 { "+mschap", o_bool, &lcp_wantoptions[0].neg_chap,
248 "Require MS-CHAP authentication from peer",
249 OPT_ALIAS | OPT_PRIOSUB | OPT_A2COPY | OPT_A3OR | MDTYPE_MICROSOFT,
250 &auth_required, 0, 0, NULL, 0, 0, &lcp_wantoptions[0].chap_mdtype },
253 { "refuse-pap", o_bool, &refuse_pap,
254 "Don't agree to auth to peer with PAP", 1 },
255 { "-pap", o_bool, &refuse_pap,
256 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
257 { "refuse-chap", o_bool, &refuse_chap,
258 "Don't agree to auth to peer with CHAP", OPT_A2CLRB | MDTYPE_MD5,
259 &lcp_allowoptions[0].chap_mdtype },
260 { "-chap", o_bool, &refuse_chap,
261 "Don't allow CHAP authentication with peer",
262 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
263 &lcp_allowoptions[0].chap_mdtype },
265 { "refuse-mschap", o_bool, &refuse_mschap,
266 "Don't agree to auth to peer with MS-CHAP", OPT_A2CLRB | MDTYPE_MICROSOFT,
267 &lcp_allowoptions[0].chap_mdtype },
268 { "-mschap", o_bool, &refuse_mschap,
269 "Don't allow MS-CHAP authentication with peer",
270 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
271 &lcp_allowoptions[0].chap_mdtype },
274 { "name", o_string, our_name,
275 "Set local name for authentication",
276 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
278 { "+ua", o_special, (void *)setupapfile,
279 "Get PAP user and password from file",
280 OPT_PRIO | OPT_A2STRVAL, &uafname },
282 { "user", o_string, user,
283 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN },
285 { "password", o_string, passwd,
286 "Password for authenticating us to the peer",
287 OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN },
289 { "usehostname", o_bool, &usehostname,
290 "Must use hostname for authentication", 1 },
292 { "remotename", o_string, remote_name,
293 "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
294 &explicit_remote, MAXNAMELEN },
296 { "login", o_bool, &uselogin,
297 "Use system password database for PAP", 1 },
299 { "papcrypt", o_bool, &cryptpap,
300 "PAP passwords are encrypted", 1 },
302 { "privgroup", o_special, (void *)privgroup,
303 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
305 { "allow-ip", o_special, (void *)set_noauth_addr,
306 "Set IP address(es) which can be used without authentication",
307 OPT_PRIV | OPT_A2LIST },
313 * setupapfile - specifies UPAP info for authenticating with peer.
321 char u[MAXNAMELEN], p[MAXSECRETLEN];
324 lcp_allowoptions[0].neg_upap = 1;
326 /* open user info file */
327 fname = strdup(*argv);
329 novm("+ua file name");
331 ufile = fopen(fname, "r");
334 option_error("unable to open user login data file %s", fname);
337 check_access(ufile, fname);
341 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
342 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL){
343 option_error("unable to read user login data file %s", fname);
348 /* get rid of newlines */
350 if (l > 0 && u[l-1] == '\n')
353 if (l > 0 && p[l-1] == '\n')
356 if (override_value("user", option_priority, fname))
357 strlcpy(user, u, sizeof(user));
358 if (override_value("passwd", option_priority, fname))
359 strlcpy(passwd, p, sizeof(passwd));
366 * privgroup - allow members of the group to have privileged access.
377 option_error("group %s is unknown", *argv);
380 for (i = 0; i < ngroups; ++i) {
381 if (groups[i] == g->gr_gid) {
391 * set_noauth_addr - set address(es) that can be used without authentication.
392 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
395 set_noauth_addr(argv)
399 int l = strlen(addr) + 1;
402 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
404 novm("allow-ip argument");
405 wp->word = (char *) (wp + 1);
406 wp->next = noauth_addrs;
407 BCOPY(addr, wp->word, l);
414 * An Open on LCP has requested a change from Dead to Establish phase.
415 * Do what's necessary to bring the physical layer up.
424 * LCP has terminated the link; go to the Dead phase and take the
425 * physical layer down.
428 link_terminated(unit)
431 if (phase == PHASE_DEAD)
433 if (pap_logout_hook) {
439 new_phase(PHASE_DEAD);
440 notice("Connection terminated.");
444 * LCP has gone down; it will either die or try to re-establish.
451 struct protent *protp;
453 notify(link_down_notifier, 0);
455 if (auth_script_state == s_up && auth_script_pid == 0) {
456 update_link_stats(unit);
457 auth_script_state = s_down;
458 auth_script(_PATH_AUTHDOWN);
460 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
461 if (!protp->enabled_flag)
463 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
464 (*protp->lowerdown)(unit);
465 if (protp->protocol < 0xC000 && protp->close != NULL)
466 (*protp->close)(unit, "LCP down");
470 if (phase != PHASE_DEAD)
471 new_phase(PHASE_ESTABLISH);
475 * The link is established.
476 * Proceed to the Dead, Authenticate or Network phase as appropriate.
479 link_established(unit)
483 lcp_options *wo = &lcp_wantoptions[unit];
484 lcp_options *go = &lcp_gotoptions[unit];
485 lcp_options *ho = &lcp_hisoptions[unit];
487 struct protent *protp;
490 * Tell higher-level protocols that LCP is up.
492 for (i = 0; (protp = protocols[i]) != NULL; ++i)
493 if (protp->protocol != PPP_LCP && protp->enabled_flag
494 && protp->lowerup != NULL)
495 (*protp->lowerup)(unit);
497 if (auth_required && !(go->neg_upap || go->neg_chap)) {
499 * We wanted the peer to authenticate itself, and it refused:
500 * if we have some address(es) it can use without auth, fine,
501 * otherwise treat it as though it authenticated with PAP using
502 * a username of "" and a password of "". If that's not OK,
505 if (noauth_addrs != NULL) {
506 set_allowed_addrs(unit, NULL, NULL);
507 } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
508 warn("peer refused to authenticate: terminating link");
509 lcp_close(unit, "peer refused to authenticate");
510 status = EXIT_PEER_AUTH_FAILED;
515 new_phase(PHASE_AUTHENTICATE);
519 ChapAuthPeer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
521 } else if (go->neg_upap) {
526 ChapAuthWithPeer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
527 auth |= CHAP_WITHPEER;
528 } else if (ho->neg_upap) {
529 if (passwd[0] == 0) {
530 passwd_from_file = 1;
531 if (!get_pap_passwd(passwd))
532 error("No secret found for PAP login");
534 upap_authwithpeer(unit, user, passwd);
535 auth |= PAP_WITHPEER;
537 auth_pending[unit] = auth;
544 * Proceed to the network phase.
550 lcp_options *go = &lcp_gotoptions[unit];
553 * If the peer had to authenticate, run the auth-up script now.
555 if (go->neg_chap || go->neg_upap) {
556 notify(auth_up_notifier, 0);
558 if (auth_script_state == s_down && auth_script_pid == 0) {
559 auth_script_state = s_up;
560 auth_script(_PATH_AUTHUP);
566 * If we negotiated callback, do it now.
569 new_phase(PHASE_CALLBACK);
570 (*cbcp_protent.open)(unit);
576 * Process extra options from the secrets file
579 options_from_list(extra_options, 1);
580 free_wordlist(extra_options);
590 struct protent *protp;
592 new_phase(PHASE_NETWORK);
594 #ifdef HAVE_MULTILINK
596 if (mp_join_bundle()) {
597 if (updetach && !nodetach)
602 #endif /* HAVE_MULTILINK */
606 set_filters(&pass_filter, &active_filter);
608 for (i = 0; (protp = protocols[i]) != NULL; ++i)
609 if (protp->protocol < 0xC000 && protp->enabled_flag
610 && protp->open != NULL) {
612 if (protp->protocol != PPP_CCP)
616 if (num_np_open == 0)
618 lcp_close(0, "No network protocols running");
622 * The peer has failed to authenticate himself using `protocol'.
625 auth_peer_fail(unit, protocol)
629 * Authentication failure: take the link down
631 lcp_close(unit, "Authentication failed");
632 status = EXIT_PEER_AUTH_FAILED;
636 * The peer has been successfully authenticated using `protocol'.
639 auth_peer_success(unit, protocol, name, namelen)
654 warn("auth_peer_success: unknown protocol %x", protocol);
659 * Save the authenticated name of the peer for later.
661 if (namelen > sizeof(peer_authname) - 1)
662 namelen = sizeof(peer_authname) - 1;
663 BCOPY(name, peer_authname, namelen);
664 peer_authname[namelen] = 0;
665 script_setenv("PEERNAME", peer_authname, 0);
668 * If there is no more authentication still to be done,
669 * proceed to the network (or callback) phase.
671 if ((auth_pending[unit] &= ~bit) == 0)
676 * We have failed to authenticate ourselves to the peer using `protocol'.
679 auth_withpeer_fail(unit, protocol)
682 if (passwd_from_file)
683 BZERO(passwd, MAXSECRETLEN);
685 * We've failed to authenticate ourselves to our peer.
686 * Some servers keep sending CHAP challenges, but there
687 * is no point in persisting without any way to get updated
688 * authentication secrets.
690 lcp_close(unit, "Failed to authenticate ourselves to peer");
691 status = EXIT_AUTH_TOPEER_FAILED;
695 * We have successfully authenticated ourselves with the peer using `protocol'.
698 auth_withpeer_success(unit, protocol)
708 if (passwd_from_file)
709 BZERO(passwd, MAXSECRETLEN);
713 warn("auth_withpeer_success: unknown protocol %x", protocol);
718 * If there is no more authentication still being done,
719 * proceed to the network (or callback) phase.
721 if ((auth_pending[unit] &= ~bit) == 0)
727 * np_up - a network protocol has come up.
735 if (num_np_up == 0) {
737 * At this point we consider that the link has come up successfully.
741 new_phase(PHASE_RUNNING);
743 if (idle_time_hook != 0)
744 tlim = (*idle_time_hook)(NULL);
746 tlim = idle_time_limit;
748 TIMEOUT(check_idle, NULL, tlim);
751 * Set a timeout to close the connection once the maximum
752 * connect time has expired.
755 TIMEOUT(connect_time_expired, 0, maxconnect);
758 * Detach now, if the updetach option was given.
760 if (updetach && !nodetach)
767 * np_down - a network protocol has gone down.
773 if (--num_np_up == 0) {
774 UNTIMEOUT(check_idle, NULL);
775 UNTIMEOUT(connect_time_expired, NULL);
776 new_phase(PHASE_NETWORK);
781 * np_finished - a network protocol has finished using the link.
784 np_finished(unit, proto)
787 if (--num_np_open <= 0) {
788 /* no further use for the link: shut up shop. */
789 lcp_close(0, "No network protocols running");
794 * check_idle - check whether the link has been idle for long
795 * enough that we can shut it down.
801 struct ppp_idle idle;
805 if (!get_idle_time(0, &idle))
807 if (idle_time_hook != 0) {
808 tlim = idle_time_hook(&idle);
810 itime = MIN(idle.xmit_idle, idle.recv_idle);
811 tlim = idle_time_limit - itime;
814 /* link is idle: shut it down. */
815 notice("Terminating connection due to lack of activity.");
816 lcp_close(0, "Link inactive");
818 status = EXIT_IDLE_TIMEOUT;
820 TIMEOUT(check_idle, NULL, tlim);
825 * connect_time_expired - log a message and close the connection.
828 connect_time_expired(arg)
831 info("Connect time expired");
832 lcp_close(0, "Connect time expired"); /* Close connection */
833 status = EXIT_CONNECT_TIME;
837 * auth_check_options - called to check authentication options.
842 lcp_options *wo = &lcp_wantoptions[0];
846 /* Default our_name to hostname, and user to our_name */
847 if (our_name[0] == 0 || usehostname)
848 strlcpy(our_name, hostname, sizeof(our_name));
850 strlcpy(user, our_name, sizeof(user));
853 * If we have a default route, require the peer to authenticate
854 * unless the noauth option was given or the real user is root.
856 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
861 /* If authentication is required, ask peer for CHAP or PAP. */
864 if (!wo->neg_chap && !wo->neg_upap) {
865 wo->neg_chap = 1; wo->chap_mdtype = MDTYPE_ALL;
869 wo->neg_chap = 0; wo->chap_mdtype = MDTYPE_NONE;
874 * Check whether we have appropriate secrets to use
875 * to authenticate the peer.
878 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
879 if (!can_auth && (wo->neg_chap)) {
880 can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
881 our_name, 1, &lacks_ip);
884 if (auth_required && !can_auth && noauth_addrs == NULL) {
887 "By default the remote system is required to authenticate itself");
889 "(because this system has a default route to the internet)");
890 } else if (explicit_remote)
892 "The remote system (%s) is required to authenticate itself",
896 "The remote system is required to authenticate itself");
898 "but I couldn't find any suitable secret (password) for it to use to do so.");
901 "(None of the available passwords would let it use an IP address.)");
908 * auth_reset - called when LCP is starting negotiations to recheck
909 * authentication options, i.e. whether we have appropriate secrets
910 * to use for authenticating ourselves and/or the peer.
916 lcp_options *go = &lcp_gotoptions[unit];
917 lcp_options *ao = &lcp_allowoptions[0];
919 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
920 ao->neg_chap = (!refuse_chap || !refuse_mschap)
922 || have_chap_secret(user, (explicit_remote? remote_name: NULL),
925 if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
928 if (!have_chap_secret((explicit_remote? remote_name: NULL),
936 * check_passwd - Check the user name and passwd against the PAP secrets
937 * file. If requested, also check against the system password database,
938 * and login the user if OK.
941 * UPAP_AUTHNAK: Authentication failed.
942 * UPAP_AUTHACK: Authentication succeeded.
943 * In either case, msg points to an appropriate message.
946 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
957 struct wordlist *addrs = NULL, *opts = NULL;
958 char passwd[256], user[256];
959 char secret[MAXWORDLEN];
960 static int attempts = 0;
963 * Make copies of apasswd and auser, then null-terminate them.
964 * If there are unprintable characters in the password, make
967 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
968 slprintf(user, sizeof(user), "%.*v", userlen, auser);
972 * Check if a plugin wants to handle this.
975 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
978 set_allowed_addrs(unit, addrs, opts);
979 BZERO(passwd, sizeof(passwd));
981 free_wordlist(addrs);
985 return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
990 * Open the file of pap secrets and scan for a suitable secret
991 * for authenticating this user.
993 filename = _PATH_UPAPFILE;
996 f = fopen(filename, "r");
998 error("Can't open PAP password file %s: %m", filename);
1001 check_access(f, filename);
1002 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
1003 warn("no PAP secret found for %s", user);
1006 * If the secret is "@login", it means to check
1007 * the password against the login database.
1009 int login_secret = strcmp(secret, "@login") == 0;
1011 if (uselogin || login_secret) {
1012 /* login option or secret is @login */
1013 ret = plogin(user, passwd, msg);
1014 if (ret == UPAP_AUTHNAK)
1015 warn("PAP login failure for %s", user);
1019 if (secret[0] != 0 && !login_secret) {
1020 /* password given in pap-secrets - must match */
1021 if ((cryptpap || strcmp(passwd, secret) != 0)
1022 && strcmp(crypt(passwd, secret), secret) != 0) {
1024 warn("PAP authentication failure for %s", user);
1031 if (ret == UPAP_AUTHNAK) {
1033 *msg = "Login incorrect";
1035 * XXX can we ever get here more than once??
1036 * Frustrate passwd stealer programs.
1037 * Allow 10 tries, but start backing off after 3 (stolen from login).
1038 * On 10'th, drop the connection.
1040 if (attempts++ >= 10) {
1041 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
1042 lcp_close(unit, "login failed");
1045 sleep((u_int) (attempts - 3) * 5);
1047 free_wordlist(opts);
1050 attempts = 0; /* Reset count */
1053 set_allowed_addrs(unit, addrs, opts);
1057 free_wordlist(addrs);
1058 BZERO(passwd, sizeof(passwd));
1059 BZERO(secret, sizeof(secret));
1065 * This function is needed for PAM.
1069 /* Static variables used to communicate between the conversation function
1070 * and the server_login function
1072 static char *PAM_username;
1073 static char *PAM_password;
1074 static int PAM_error = 0;
1075 static pam_handle_t *pamh = NULL;
1077 /* PAM conversation function
1078 * Here we assume (for now, at least) that echo on means login name, and
1079 * echo off means password.
1082 static int PAM_conv (int num_msg, const struct pam_message **msg,
1083 struct pam_response **resp, void *appdata_ptr)
1086 struct pam_response *reply = NULL;
1088 #define COPY_STRING(s) (s) ? strdup(s) : NULL
1090 reply = malloc(sizeof(struct pam_response) * num_msg);
1091 if (!reply) return PAM_CONV_ERR;
1093 for (replies = 0; replies < num_msg; replies++) {
1094 switch (msg[replies]->msg_style) {
1095 case PAM_PROMPT_ECHO_ON:
1096 reply[replies].resp_retcode = PAM_SUCCESS;
1097 reply[replies].resp = COPY_STRING(PAM_username);
1098 /* PAM frees resp */
1100 case PAM_PROMPT_ECHO_OFF:
1101 reply[replies].resp_retcode = PAM_SUCCESS;
1102 reply[replies].resp = COPY_STRING(PAM_password);
1103 /* PAM frees resp */
1108 /* ignore it, but pam still wants a NULL response... */
1109 reply[replies].resp_retcode = PAM_SUCCESS;
1110 reply[replies].resp = NULL;
1113 /* Must be an error of some sort... */
1116 return PAM_CONV_ERR;
1123 static struct pam_conv PAM_conversation = {
1127 #endif /* USE_PAM */
1130 * plogin - Check the user name and password against the system
1131 * password database, and login the user if OK.
1134 * UPAP_AUTHNAK: Login failed.
1135 * UPAP_AUTHACK: Login succeeded.
1136 * In either case, msg points to an appropriate message.
1140 plogin(user, passwd, msg)
1150 pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
1151 if (pam_error != PAM_SUCCESS) {
1152 *msg = (char *) pam_strerror (pamh, pam_error);
1154 return UPAP_AUTHNAK;
1157 * Define the fields for the credential validation
1160 PAM_username = user;
1161 PAM_password = passwd;
1163 pam_set_item (pamh, PAM_TTY, devnam); /* this might be useful to some modules */
1168 pam_error = pam_authenticate (pamh, PAM_SILENT);
1169 if (pam_error == PAM_SUCCESS && !PAM_error) {
1170 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
1171 if (pam_error == PAM_SUCCESS)
1172 pam_error = pam_open_session (pamh, PAM_SILENT);
1175 *msg = (char *) pam_strerror (pamh, pam_error);
1180 reopen_log(); /* apparently the PAM stuff does closelog() */
1181 PAM_username = NULL;
1182 PAM_password = NULL;
1183 if (pam_error != PAM_SUCCESS)
1184 return UPAP_AUTHNAK;
1185 #else /* #ifdef USE_PAM */
1188 * Use the non-PAM methods directly
1193 struct spwd *getspnam();
1195 struct passwd *pw = getpwnam(user);
1199 return (UPAP_AUTHNAK);
1202 spwd = getspnam(user);
1205 /* check the age of the password entry */
1206 long now = time(NULL) / 86400L;
1208 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
1209 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
1210 && spwd->sp_lstchg >= 0
1211 && now >= spwd->sp_lstchg + spwd->sp_max)) {
1212 warn("Password for %s has expired", user);
1213 return (UPAP_AUTHNAK);
1215 pw->pw_passwd = spwd->sp_pwdp;
1220 * If no passwd, don't let them login.
1222 if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2
1223 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
1224 return (UPAP_AUTHNAK);
1226 #endif /* #ifdef USE_PAM */
1229 * Write a wtmp entry for this user.
1233 if (strncmp(tty, "/dev/", 5) == 0)
1235 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
1237 #if defined(_PATH_LASTLOG) && !defined(USE_PAM)
1238 if (pw != (struct passwd *)NULL) {
1242 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1243 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
1244 memset((void *)&ll, 0, sizeof(ll));
1245 (void)time(&ll.ll_time);
1246 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1247 (void)write(fd, (char *)&ll, sizeof(ll));
1251 #endif /* _PATH_LASTLOG and not USE_PAM */
1253 info("user %s logged in", user);
1256 return (UPAP_AUTHACK);
1260 * plogout - Logout the user.
1269 pam_error = pam_close_session (pamh, PAM_SILENT);
1270 pam_end (pamh, pam_error);
1273 /* Apparently the pam stuff does closelog(). */
1275 #else /* ! USE_PAM */
1279 if (strncmp(tty, "/dev/", 5) == 0)
1281 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
1282 #endif /* ! USE_PAM */
1288 * null_login - Check if a username of "" and a password of "" are
1289 * acceptable, and iff so, set the list of acceptable IP addresses
1299 struct wordlist *addrs, *opts;
1300 char secret[MAXWORDLEN];
1303 * Check if a plugin wants to handle this.
1307 ret = (*null_auth_hook)(&addrs, &opts);
1310 * Open the file of pap secrets and scan for a suitable secret.
1313 filename = _PATH_UPAPFILE;
1315 f = fopen(filename, "r");
1318 check_access(f, filename);
1320 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
1321 ret = i >= 0 && secret[0] == 0;
1322 BZERO(secret, sizeof(secret));
1327 set_allowed_addrs(unit, addrs, opts);
1329 free_wordlist(opts);
1331 free_wordlist(addrs);
1338 * get_pap_passwd - get a password for authenticating ourselves with
1339 * our peer using PAP. Returns 1 on success, 0 if no suitable password
1341 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
1344 get_pap_passwd(passwd)
1350 char secret[MAXWORDLEN];
1353 * Check whether a plugin wants to supply this.
1355 if (pap_passwd_hook) {
1356 ret = (*pap_passwd_hook)(user, passwd);
1361 filename = _PATH_UPAPFILE;
1362 f = fopen(filename, "r");
1365 check_access(f, filename);
1366 ret = scan_authfile(f, user,
1367 (remote_name[0]? remote_name: NULL),
1368 secret, NULL, NULL, filename);
1373 strlcpy(passwd, secret, MAXSECRETLEN);
1374 BZERO(secret, sizeof(secret));
1380 * have_pap_secret - check whether we have a PAP file with any
1381 * secrets that we could possibly use for authenticating the peer.
1384 have_pap_secret(lacks_ipp)
1390 struct wordlist *addrs;
1392 /* let the plugin decide, if there is one */
1393 if (pap_check_hook) {
1394 ret = (*pap_check_hook)();
1399 filename = _PATH_UPAPFILE;
1400 f = fopen(filename, "r");
1404 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
1405 NULL, &addrs, NULL, filename);
1407 if (ret >= 0 && !some_ip_ok(addrs)) {
1413 free_wordlist(addrs);
1420 * have_chap_secret - check whether we have a CHAP file with a
1421 * secret that we could possibly use for authenticating `client'
1422 * on `server'. Either can be the null string, meaning we don't
1423 * know the identity yet.
1426 have_chap_secret(client, server, need_ip, lacks_ipp)
1435 struct wordlist *addrs;
1437 if (chap_check_hook) {
1438 ret = (*chap_check_hook)();
1444 filename = _PATH_CHAPFILE;
1445 f = fopen(filename, "r");
1449 if (client != NULL && client[0] == 0)
1451 else if (server != NULL && server[0] == 0)
1454 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
1456 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
1462 free_wordlist(addrs);
1469 * get_secret - open the CHAP secret file and return the secret
1470 * for authenticating the given client on the given server.
1471 * (We could be either client or server).
1474 get_secret(unit, client, server, secret, secret_len, am_server)
1485 struct wordlist *addrs, *opts;
1486 char secbuf[MAXWORDLEN];
1488 if (!am_server && passwd[0] != 0) {
1489 strlcpy(secbuf, passwd, sizeof(secbuf));
1490 } else if (!am_server && chap_passwd_hook) {
1491 if ( (*chap_passwd_hook)(client, secbuf) < 0) {
1492 error("Unable to obtain CHAP password for %s on %s from plugin",
1497 filename = _PATH_CHAPFILE;
1501 f = fopen(filename, "r");
1503 error("Can't open chap secret file %s: %m", filename);
1506 check_access(f, filename);
1508 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename);
1514 set_allowed_addrs(unit, addrs, opts);
1516 free_wordlist(opts);
1518 free_wordlist(addrs);
1521 len = strlen(secbuf);
1522 if (len > MAXSECRETLEN) {
1523 error("Secret for %s on %s is too long", client, server);
1526 BCOPY(secbuf, secret, len);
1527 BZERO(secbuf, sizeof(secbuf));
1534 * set_allowed_addrs() - set the list of allowed addresses.
1535 * Also looks for `--' indicating options to apply for this peer
1536 * and leaves the following words in extra_options.
1539 set_allowed_addrs(unit, addrs, opts)
1541 struct wordlist *addrs;
1542 struct wordlist *opts;
1545 struct wordlist *ap, **plink;
1546 struct permitted_ip *ip;
1547 char *ptr_word, *ptr_mask;
1550 u_int32_t a, mask, ah, offset;
1551 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1552 u_int32_t suggested_ip = 0;
1554 if (addresses[unit] != NULL)
1555 free(addresses[unit]);
1556 addresses[unit] = NULL;
1557 if (extra_options != NULL)
1558 free_wordlist(extra_options);
1559 extra_options = opts;
1562 * Count the number of IP addresses given.
1564 n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
1567 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
1571 /* temporarily append the noauth_addrs list to addrs */
1572 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
1574 *plink = noauth_addrs;
1577 for (ap = addrs; ap != NULL; ap = ap->next) {
1578 /* "-" means no addresses authorized, "*" means any address allowed */
1579 ptr_word = ap->word;
1580 if (strcmp(ptr_word, "-") == 0)
1582 if (strcmp(ptr_word, "*") == 0) {
1584 ip[n].base = ip[n].mask = 0;
1590 if (*ptr_word == '!') {
1595 mask = ~ (u_int32_t) 0;
1597 ptr_mask = strchr (ptr_word, '/');
1598 if (ptr_mask != NULL) {
1602 bit_count = (int) strtol (ptr_mask+1, &endp, 10);
1603 if (bit_count <= 0 || bit_count > 32) {
1604 warn("invalid address length %v in auth. address list",
1608 bit_count = 32 - bit_count; /* # bits in host part */
1610 offset = ifunit + 1;
1614 warn("invalid address length syntax: %v", ptr_mask+1);
1621 hp = gethostbyname(ptr_word);
1622 if (hp != NULL && hp->h_addrtype == AF_INET) {
1623 a = *(u_int32_t *)hp->h_addr;
1625 np = getnetbyname (ptr_word);
1626 if (np != NULL && np->n_addrtype == AF_INET) {
1627 a = htonl (*(u_int32_t *)np->n_net);
1628 if (ptr_mask == NULL) {
1629 /* calculate appropriate mask for net */
1632 mask = IN_CLASSA_NET;
1633 else if (IN_CLASSB(ah))
1634 mask = IN_CLASSB_NET;
1635 else if (IN_CLASSC(ah))
1636 mask = IN_CLASSC_NET;
1639 a = inet_addr (ptr_word);
1643 if (ptr_mask != NULL)
1646 if (a == (u_int32_t)-1L) {
1647 warn("unknown host %s in auth. address list", ap->word);
1651 if (offset >= ~mask) {
1652 warn("interface unit %d too large for subnet %v",
1656 a = htonl((ntohl(a) & mask) + offset);
1657 mask = ~(u_int32_t)0;
1659 ip[n].mask = htonl(mask);
1660 ip[n].base = a & ip[n].mask;
1662 if (~mask == 0 && suggested_ip == 0)
1667 ip[n].permit = 0; /* make the last entry forbid all addresses */
1668 ip[n].base = 0; /* to terminate the list */
1671 addresses[unit] = ip;
1674 * If the address given for the peer isn't authorized, or if
1675 * the user hasn't given one, AND there is an authorized address
1676 * which is a single host, then use that if we find one.
1678 if (suggested_ip != 0
1679 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
1680 wo->hisaddr = suggested_ip;
1682 * Do we insist on this address? No, if there are other
1683 * addresses authorized than the suggested one.
1686 wo->accept_remote = 1;
1691 * auth_ip_addr - check whether the peer is authorized to use
1692 * a given IP address. Returns 1 if authorized, 0 otherwise.
1695 auth_ip_addr(unit, addr)
1701 /* don't allow loopback or multicast address */
1702 if (bad_ip_adrs(addr))
1705 if (allowed_address_hook) {
1706 ok = allowed_address_hook(addr);
1707 if (ok >= 0) return ok;
1710 if (addresses[unit] != NULL) {
1711 ok = ip_addr_check(addr, addresses[unit]);
1717 return 0; /* no addresses authorized */
1718 return allow_any_ip || privileged || !have_route_to(addr);
1722 ip_addr_check(addr, addrs)
1724 struct permitted_ip *addrs;
1727 if ((addr & addrs->mask) == addrs->base)
1728 return addrs->permit;
1732 * bad_ip_adrs - return 1 if the IP address is one we don't want
1733 * to use, such as an address in the loopback net or a multicast address.
1734 * addr is in network byte order.
1741 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1742 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1746 * some_ip_ok - check a wordlist to see if it authorizes any
1751 struct wordlist *addrs;
1753 for (; addrs != 0; addrs = addrs->next) {
1754 if (addrs->word[0] == '-')
1756 if (addrs->word[0] != '!')
1757 return 1; /* some IP address is allowed */
1763 * check_access - complain if a secret file has too-liberal permissions.
1766 check_access(f, filename)
1772 if (fstat(fileno(f), &sbuf) < 0) {
1773 warn("cannot stat secret file %s: %m", filename);
1774 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1775 warn("Warning - secret file %s has world and/or group access",
1782 * scan_authfile - Scan an authorization file for a secret suitable
1783 * for authenticating `client' on `server'. The return value is -1
1784 * if no secret is found, otherwise >= 0. The return value has
1785 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1786 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1787 * Any following words on the line up to a "--" (i.e. address authorization
1788 * info) are placed in a wordlist and returned in *addrs. Any
1789 * following words (extra options) are placed in a wordlist and
1790 * returned in *opts.
1791 * We assume secret is NULL or points to MAXWORDLEN bytes of space.
1794 scan_authfile(f, client, server, secret, addrs, opts, filename)
1799 struct wordlist **addrs;
1800 struct wordlist **opts;
1804 int got_flag, best_flag;
1806 struct wordlist *ap, *addr_list, *alist, **app;
1807 char word[MAXWORDLEN];
1808 char atfile[MAXWORDLEN];
1809 char lsecret[MAXWORDLEN];
1816 if (!getword(f, word, &newline, filename))
1817 return -1; /* file is empty??? */
1822 * Skip until we find a word at the start of a line.
1824 while (!newline && getword(f, word, &newline, filename))
1827 break; /* got to end of file */
1830 * Got a client - check if it's a match or a wildcard.
1833 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1838 got_flag = NONWILD_CLIENT;
1841 * Now get a server and check if it matches.
1843 if (!getword(f, word, &newline, filename))
1847 if (!ISWILD(word)) {
1848 if (server != NULL && strcmp(word, server) != 0)
1850 got_flag |= NONWILD_SERVER;
1854 * Got some sort of a match - see if it's better than what
1857 if (got_flag <= best_flag)
1863 if (!getword(f, word, &newline, filename))
1868 if (secret != NULL) {
1870 * Special syntax: @/pathname means read secret from file.
1872 if (word[0] == '@' && word[1] == '/') {
1873 strlcpy(atfile, word+1, sizeof(atfile));
1874 if ((sf = fopen(atfile, "r")) == NULL) {
1875 warn("can't open indirect secret file %s", atfile);
1878 check_access(sf, atfile);
1879 if (!getword(sf, word, &xxx, atfile)) {
1880 warn("no secret in indirect secret file %s", atfile);
1886 strlcpy(lsecret, word, sizeof(lsecret));
1890 * Now read address authorization info and make a wordlist.
1894 if (!getword(f, word, &newline, filename) || newline)
1896 ap = (struct wordlist *)
1897 malloc(sizeof(struct wordlist) + strlen(word) + 1);
1899 novm("authorized addresses");
1900 ap->word = (char *) (ap + 1);
1901 strcpy(ap->word, word);
1908 * This is the best so far; remember it.
1910 best_flag = got_flag;
1912 free_wordlist(addr_list);
1915 strlcpy(secret, lsecret, MAXWORDLEN);
1921 /* scan for a -- word indicating the start of options */
1922 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
1923 if (strcmp(ap->word, "--") == 0)
1925 /* ap = start of options */
1927 ap = ap->next; /* first option */
1928 free(*app); /* free the "--" word */
1929 *app = NULL; /* terminate addr list */
1933 else if (ap != NULL)
1937 else if (addr_list != NULL)
1938 free_wordlist(addr_list);
1944 * wordlist_count - return the number of items in a wordlist
1948 struct wordlist *wp;
1952 for (n = 0; wp != NULL; wp = wp->next)
1958 * free_wordlist - release memory allocated for a wordlist.
1962 struct wordlist *wp;
1964 struct wordlist *next;
1966 while (wp != NULL) {
1974 * auth_script_done - called when the auth-up or auth-down script
1978 auth_script_done(arg)
1981 auth_script_pid = 0;
1982 switch (auth_script_state) {
1984 if (auth_state == s_down) {
1985 auth_script_state = s_down;
1986 auth_script(_PATH_AUTHDOWN);
1990 if (auth_state == s_up) {
1991 auth_script_state = s_up;
1992 auth_script(_PATH_AUTHUP);
1999 * auth_script - execute a script with arguments
2000 * interface-name peer-name real-user tty speed
2012 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
2013 user_name = pw->pw_name;
2015 slprintf(struid, sizeof(struid), "%d", getuid());
2018 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2022 argv[2] = peer_authname;
2023 argv[3] = user_name;
2028 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);