2 * auth.c - PPP authentication and phase control.
4 * Copyright (c) 1993 The Australian National University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Copyright (c) 1989 Carnegie Mellon University.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by Carnegie Mellon University. The name of the
28 * University may not be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
32 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36 static char rcsid[] = "$Id: auth.c,v 1.36 1998/03/25 01:26:03 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #if defined(_PATH_LASTLOG) && defined(_linux_)
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
60 #include <security/pam_appl.h>
66 #define PW_PPP PW_LOGIN
79 #include "pathnames.h"
81 /* Used for storing a sequence of words. Usually malloced. */
83 struct wordlist *next;
87 /* Bits in scan_authfile return value */
88 #define NONWILD_SERVER 1
89 #define NONWILD_CLIENT 2
91 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
96 /* The name by which the peer authenticated itself to us. */
97 char peer_authname[MAXNAMELEN];
99 /* Records which authentication operations haven't completed yet. */
100 static int auth_pending[NUM_PPP];
102 /* Set if we have successfully called plogin() */
103 static int logged_in;
105 /* Set if we have run the /etc/ppp/auth-up script. */
106 static int did_authup;
108 /* List of addresses which the peer may use. */
109 static struct wordlist *addresses[NUM_PPP];
111 /* Number of network protocols which we have opened. */
112 static int num_np_open;
114 /* Number of network protocols which have come up. */
115 static int num_np_up;
117 /* Set if we got the contents of passwd[] from the pap-secrets file. */
118 static int passwd_from_file;
120 /* Bits in auth_pending[] */
121 #define PAP_WITHPEER 1
123 #define CHAP_WITHPEER 4
126 extern char *crypt __P((const char *, const char *));
128 /* Prototypes for procedures local to this file. */
130 static void network_phase __P((int));
131 static void check_idle __P((void *));
132 static void connect_time_expired __P((void *));
133 static int plogin __P((char *, char *, char **, int *));
134 static void plogout __P((void));
135 static int null_login __P((int));
136 static int get_pap_passwd __P((char *));
137 static int have_pap_secret __P((void));
138 static int have_chap_secret __P((char *, char *, u_int32_t));
139 static int ip_addr_check __P((u_int32_t, struct wordlist *));
140 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
141 struct wordlist **, char *));
142 static void free_wordlist __P((struct wordlist *));
143 static void auth_script __P((char *));
144 static void set_allowed_addrs __P((int, struct wordlist *));
147 * An Open on LCP has requested a change from Dead to Establish phase.
148 * Do what's necessary to bring the physical layer up.
157 * LCP has terminated the link; go to the Dead phase and take the
158 * physical layer down.
161 link_terminated(unit)
164 if (phase == PHASE_DEAD)
169 syslog(LOG_NOTICE, "Connection terminated.");
173 * LCP has gone down; it will either die or try to re-establish.
180 struct protent *protp;
183 auth_script(_PATH_AUTHDOWN);
186 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
187 if (!protp->enabled_flag)
189 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
190 (*protp->lowerdown)(unit);
191 if (protp->protocol < 0xC000 && protp->close != NULL)
192 (*protp->close)(unit, "LCP down");
196 if (phase != PHASE_DEAD)
197 phase = PHASE_TERMINATE;
201 * The link is established.
202 * Proceed to the Dead, Authenticate or Network phase as appropriate.
205 link_established(unit)
209 lcp_options *wo = &lcp_wantoptions[unit];
210 lcp_options *go = &lcp_gotoptions[unit];
211 lcp_options *ho = &lcp_hisoptions[unit];
213 struct protent *protp;
216 * Tell higher-level protocols that LCP is up.
218 for (i = 0; (protp = protocols[i]) != NULL; ++i)
219 if (protp->protocol != PPP_LCP && protp->enabled_flag
220 && protp->lowerup != NULL)
221 (*protp->lowerup)(unit);
223 if (auth_required && !(go->neg_chap || go->neg_upap)) {
225 * We wanted the peer to authenticate itself, and it refused:
226 * treat it as though it authenticated with PAP using a username
227 * of "" and a password of "". If that's not OK, boot it out.
229 if (!wo->neg_upap || !null_login(unit)) {
230 syslog(LOG_WARNING, "peer refused to authenticate");
231 lcp_close(unit, "peer refused to authenticate");
236 phase = PHASE_AUTHENTICATE;
239 ChapAuthPeer(unit, our_name, go->chap_mdtype);
241 } else if (go->neg_upap) {
246 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
247 auth |= CHAP_WITHPEER;
248 } else if (ho->neg_upap) {
249 if (passwd[0] == 0) {
250 passwd_from_file = 1;
251 if (!get_pap_passwd(passwd))
252 syslog(LOG_ERR, "No secret found for PAP login");
254 upap_authwithpeer(unit, user, passwd);
255 auth |= PAP_WITHPEER;
257 auth_pending[unit] = auth;
264 * Proceed to the network phase.
271 struct protent *protp;
272 lcp_options *go = &lcp_gotoptions[unit];
275 * If the peer had to authenticate, run the auth-up script now.
277 if ((go->neg_chap || go->neg_upap) && !did_authup) {
278 auth_script(_PATH_AUTHUP);
284 * If we negotiated callback, do it now.
287 phase = PHASE_CALLBACK;
288 (*cbcp_protent.open)(unit);
293 phase = PHASE_NETWORK;
296 set_filters(&pass_filter, &active_filter);
298 for (i = 0; (protp = protocols[i]) != NULL; ++i)
299 if (protp->protocol < 0xC000 && protp->enabled_flag
300 && protp->open != NULL) {
301 (*protp->open)(unit);
302 if (protp->protocol != PPP_CCP)
306 if (num_np_open == 0)
308 lcp_close(0, "No network protocols running");
312 * The peer has failed to authenticate himself using `protocol'.
315 auth_peer_fail(unit, protocol)
319 * Authentication failure: take the link down
321 lcp_close(unit, "Authentication failed");
325 * The peer has been successfully authenticated using `protocol'.
328 auth_peer_success(unit, protocol, name, namelen)
343 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
349 * Save the authenticated name of the peer for later.
351 if (namelen > sizeof(peer_authname) - 1)
352 namelen = sizeof(peer_authname) - 1;
353 BCOPY(name, peer_authname, namelen);
354 peer_authname[namelen] = 0;
355 script_setenv("PEERNAME", peer_authname);
358 * If there is no more authentication still to be done,
359 * proceed to the network (or callback) phase.
361 if ((auth_pending[unit] &= ~bit) == 0)
366 * We have failed to authenticate ourselves to the peer using `protocol'.
369 auth_withpeer_fail(unit, protocol)
372 if (passwd_from_file)
373 BZERO(passwd, MAXSECRETLEN);
375 * We've failed to authenticate ourselves to our peer.
376 * He'll probably take the link down, and there's not much
377 * we can do except wait for that.
382 * We have successfully authenticated ourselves with the peer using `protocol'.
385 auth_withpeer_success(unit, protocol)
395 if (passwd_from_file)
396 BZERO(passwd, MAXSECRETLEN);
400 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
406 * If there is no more authentication still being done,
407 * proceed to the network (or callback) phase.
409 if ((auth_pending[unit] &= ~bit) == 0)
415 * np_up - a network protocol has come up.
421 if (num_np_up == 0) {
423 * At this point we consider that the link has come up successfully.
427 if (idle_time_limit > 0)
428 TIMEOUT(check_idle, NULL, idle_time_limit);
431 * Set a timeout to close the connection once the maximum
432 * connect time has expired.
435 TIMEOUT(connect_time_expired, 0, maxconnect);
441 * np_down - a network protocol has gone down.
447 if (--num_np_up == 0 && idle_time_limit > 0) {
448 UNTIMEOUT(check_idle, NULL);
453 * np_finished - a network protocol has finished using the link.
456 np_finished(unit, proto)
459 if (--num_np_open <= 0) {
460 /* no further use for the link: shut up shop. */
461 lcp_close(0, "No network protocols running");
466 * check_idle - check whether the link has been idle for long
467 * enough that we can shut it down.
473 struct ppp_idle idle;
476 if (!get_idle_time(0, &idle))
478 itime = MIN(idle.xmit_idle, idle.recv_idle);
479 if (itime >= idle_time_limit) {
480 /* link is idle: shut it down. */
481 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
482 lcp_close(0, "Link inactive");
484 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
489 * connect_time_expired - log a message and close the connection.
492 connect_time_expired(arg)
495 syslog(LOG_INFO, "Connect time expired");
496 lcp_close(0, "Connect time expired"); /* Close connection */
500 * auth_check_options - called to check authentication options.
505 lcp_options *wo = &lcp_wantoptions[0];
507 ipcp_options *ipwo = &ipcp_wantoptions[0];
510 /* Default our_name to hostname, and user to our_name */
511 if (our_name[0] == 0 || usehostname)
512 strcpy(our_name, hostname);
514 strcpy(user, our_name);
516 /* If authentication is required, ask peer for CHAP or PAP. */
517 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
523 * Check whether we have appropriate secrets to use
524 * to authenticate the peer.
526 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
527 if (!can_auth && wo->neg_chap) {
528 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
529 can_auth = have_chap_secret(remote_name, our_name, remote);
532 if (auth_required && !can_auth) {
533 option_error("peer authentication required but no suitable secret(s) found\n");
534 if (remote_name[0] == 0)
535 option_error("for authenticating any peer to us (%s)\n", our_name);
537 option_error("for authenticating peer %s to us (%s)\n",
538 remote_name, our_name);
543 * Check whether the user tried to override certain values
546 if (!auth_required && auth_req_info.priv > 0) {
547 if (!default_device && devnam_info.priv == 0) {
548 option_error("can't override device name when noauth option used");
551 if ((connector != NULL && connector_info.priv == 0)
552 || (disconnector != NULL && disconnector_info.priv == 0)
553 || (welcomer != NULL && welcomer_info.priv == 0)) {
554 option_error("can't override connect, disconnect or welcome");
555 option_error("option values when noauth option used");
562 * auth_reset - called when LCP is starting negotiations to recheck
563 * authentication options, i.e. whether we have appropriate secrets
564 * to use for authenticating ourselves and/or the peer.
570 lcp_options *go = &lcp_gotoptions[unit];
571 lcp_options *ao = &lcp_allowoptions[0];
572 ipcp_options *ipwo = &ipcp_wantoptions[0];
575 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
576 ao->neg_chap = !refuse_chap
577 && have_chap_secret(user, remote_name, (u_int32_t)0);
579 if (go->neg_upap && !uselogin && !have_pap_secret())
582 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
583 if (!have_chap_secret(remote_name, our_name, remote))
590 * check_passwd - Check the user name and passwd against the PAP secrets
591 * file. If requested, also check against the system password database,
592 * and login the user if OK.
595 * UPAP_AUTHNAK: Authentication failed.
596 * UPAP_AUTHACK: Authentication succeeded.
597 * In either case, msg points to an appropriate message.
600 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
612 struct wordlist *addrs;
614 ipcp_options *ipwo = &ipcp_wantoptions[unit];
615 char passwd[256], user[256];
616 char secret[MAXWORDLEN];
617 static int attempts = 0;
620 * Make copies of apasswd and auser, then null-terminate them.
622 BCOPY(apasswd, passwd, passwdlen);
623 passwd[passwdlen] = '\0';
624 BCOPY(auser, user, userlen);
625 user[userlen] = '\0';
629 * Open the file of pap secrets and scan for a suitable secret
630 * for authenticating this user.
632 filename = _PATH_UPAPFILE;
635 f = fopen(filename, "r");
637 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
641 check_access(f, filename);
642 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
643 if (scan_authfile(f, user, our_name, remote,
644 secret, &addrs, filename) < 0
645 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
646 && strcmp(crypt(passwd, secret), secret) != 0)) {
647 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
653 if (uselogin && ret == UPAP_AUTHACK) {
654 ret = plogin(user, passwd, msg, msglen);
655 if (ret == UPAP_AUTHNAK) {
656 syslog(LOG_WARNING, "PAP login failure for %s", user);
660 if (ret == UPAP_AUTHNAK) {
661 if (*msg == (char *) 0)
662 *msg = "Login incorrect";
663 *msglen = strlen(*msg);
665 * Frustrate passwd stealer programs.
666 * Allow 10 tries, but start backing off after 3 (stolen from login).
667 * On 10'th, drop the connection.
669 if (attempts++ >= 10) {
670 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
671 attempts, devnam, user);
675 sleep((u_int) (attempts - 3) * 5);
677 free_wordlist(addrs);
680 attempts = 0; /* Reset count */
681 if (*msg == (char *) 0)
683 *msglen = strlen(*msg);
684 set_allowed_addrs(unit, addrs);
687 BZERO(passwd, sizeof(passwd));
688 BZERO(secret, sizeof(secret));
694 * This function is needed for PAM.
698 static char *PAM_username = "";
699 static char *PAM_password = "";
701 #ifdef PAM_ESTABLISH_CRED /* new PAM defines :(^ */
702 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(pamh,err_code)
704 #define MY_PAM_STRERROR(err_code) (char *) pam_strerror(err_code)
707 static int pam_conv (int num_msg,
708 const struct pam_message **msg,
709 struct pam_response **resp,
712 int count = 0, replies = 0;
713 struct pam_response *reply = NULL;
716 for (count = 0; count < num_msg; count++)
718 size += sizeof (struct pam_response);
719 reply = realloc (reply, size); /* ANSI: is malloc() if reply==NULL */
723 switch (msg[count]->msg_style)
725 case PAM_PROMPT_ECHO_ON:
726 reply[replies].resp_retcode = PAM_SUCCESS;
727 reply[replies++].resp = strdup(PAM_username); /* never NULL */
730 case PAM_PROMPT_ECHO_OFF:
731 reply[replies].resp_retcode = PAM_SUCCESS;
732 reply[replies++].resp = strdup(PAM_password); /* never NULL */
736 reply[replies].resp_retcode = PAM_SUCCESS;
737 reply[replies++].resp = NULL;
757 * plogin - Check the user name and password against the system
758 * password database, and login the user if OK.
761 * UPAP_AUTHNAK: Login failed.
762 * UPAP_AUTHACK: Login succeeded.
763 * In either case, msg points to an appropriate message.
767 plogin(user, passwd, msg, msglen)
776 struct pam_conv pam_conversation;
780 * Fill the pam_conversion structure
782 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
783 pam_conversation.conv = &pam_conv;
785 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
787 if (pam_error != PAM_SUCCESS) {
788 *msg = MY_PAM_STRERROR (pam_error);
792 * Define the fields for the credintial validation
794 (void) pam_set_item (pamh, PAM_TTY, devnam);
796 PAM_password = passwd;
800 pam_error = pam_authenticate (pamh, PAM_SILENT);
801 if (pam_error == PAM_SUCCESS) {
802 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
804 /* start a session for this user. Session closed when link ends. */
805 if (pam_error == PAM_SUCCESS)
806 (void) pam_open_session (pamh, PAM_SILENT);
809 *msg = MY_PAM_STRERROR (pam_error);
816 (void) pam_end (pamh, pam_error);
818 if (pam_error != PAM_SUCCESS)
821 * Use the non-PAM methods directly
823 #else /* #ifdef USE_PAM */
830 struct spwd *getspnam();
836 return (UPAP_AUTHNAK);
840 spwd = getspnam(user);
843 /* check the age of the password entry */
844 long now = time(NULL) / 86400L;
846 if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
847 || ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
848 && spwd->sp_lstchg >= 0
849 && now >= spwd->sp_lstchg + spwd->sp_max)) {
850 syslog(LOG_WARNING, "Password for %s has expired", user);
851 return (UPAP_AUTHNAK);
853 pw->pw_passwd = spwd->sp_pwdp;
858 * If no passwd, don't let them login.
860 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
861 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
862 return (UPAP_AUTHNAK);
864 /* These functions are not enabled for PAM. The reason for this is that */
865 /* there is not necessarily a "passwd" entry for this user. That is */
866 /* real purpose of 'PAM' -- to virtualize the account data from the */
867 /* application. If you want to do the same thing, write the entry in */
868 /* the 'session' hook. */
871 * Write a wtmp entry for this user.
875 if (strncmp(tty, "/dev/", 5) == 0)
877 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
879 #if defined(_PATH_LASTLOG)
884 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
885 (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
886 memset((void *)&ll, 0, sizeof(ll));
887 (void)time(&ll.ll_time);
888 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
889 (void)write(fd, (char *)&ll, sizeof(ll));
895 #endif /* #ifdef USE_PAM */
897 syslog(LOG_INFO, "user %s logged in", user);
900 return (UPAP_AUTHACK);
904 * plogout - Logout the user.
910 struct pam_conv pam_conversation;
914 * Fill the pam_conversion structure. The PAM specification states that the
915 * session must be able to be closed by a totally different handle from which
916 * it was created. Hold the PAM group to their own specification!
918 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
919 pam_conversation.conv = &pam_conv;
921 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
922 if (pam_error == PAM_SUCCESS) {
923 (void) pam_set_item (pamh, PAM_TTY, devnam);
924 (void) pam_close_session (pamh, PAM_SILENT);
925 (void) pam_end (pamh, PAM_SUCCESS);
932 if (strncmp(tty, "/dev/", 5) == 0)
934 logwtmp(tty, "", ""); /* Wipe out utmp logout entry */
942 * null_login - Check if a username of "" and a password of "" are
943 * acceptable, and iff so, set the list of acceptable IP addresses
953 struct wordlist *addrs;
954 char secret[MAXWORDLEN];
957 * Open the file of pap secrets and scan for a suitable secret.
958 * We don't accept a wildcard client.
960 filename = _PATH_UPAPFILE;
962 f = fopen(filename, "r");
965 check_access(f, filename);
967 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
968 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
969 BZERO(secret, sizeof(secret));
972 set_allowed_addrs(unit, addrs);
974 free_wordlist(addrs);
982 * get_pap_passwd - get a password for authenticating ourselves with
983 * our peer using PAP. Returns 1 on success, 0 if no suitable password
987 get_pap_passwd(passwd)
993 struct wordlist *addrs;
994 char secret[MAXWORDLEN];
996 filename = _PATH_UPAPFILE;
998 f = fopen(filename, "r");
1001 check_access(f, filename);
1002 ret = scan_authfile(f, user,
1003 remote_name[0]? remote_name: NULL,
1004 (u_int32_t)0, secret, NULL, filename);
1008 if (passwd != NULL) {
1009 strncpy(passwd, secret, MAXSECRETLEN);
1010 passwd[MAXSECRETLEN-1] = 0;
1012 BZERO(secret, sizeof(secret));
1018 * have_pap_secret - check whether we have a PAP file with any
1019 * secrets that we could possibly use for authenticating the peer.
1027 ipcp_options *ipwo = &ipcp_wantoptions[0];
1030 filename = _PATH_UPAPFILE;
1031 f = fopen(filename, "r");
1035 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
1036 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
1046 * have_chap_secret - check whether we have a CHAP file with a
1047 * secret that we could possibly use for authenticating `client'
1048 * on `server'. Either can be the null string, meaning we don't
1049 * know the identity yet.
1052 have_chap_secret(client, server, remote)
1061 filename = _PATH_CHAPFILE;
1062 f = fopen(filename, "r");
1068 else if (server[0] == 0)
1071 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1081 * get_secret - open the CHAP secret file and return the secret
1082 * for authenticating the given client on the given server.
1083 * (We could be either client or server).
1086 get_secret(unit, client, server, secret, secret_len, save_addrs)
1097 struct wordlist *addrs;
1098 char secbuf[MAXWORDLEN];
1100 filename = _PATH_CHAPFILE;
1104 f = fopen(filename, "r");
1106 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1109 check_access(f, filename);
1111 ret = scan_authfile(f, client, server, (u_int32_t)0,
1112 secbuf, &addrs, filename);
1118 set_allowed_addrs(unit, addrs);
1120 len = strlen(secbuf);
1121 if (len > MAXSECRETLEN) {
1122 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1125 BCOPY(secbuf, secret, len);
1126 BZERO(secbuf, sizeof(secbuf));
1133 * set_allowed_addrs() - set the list of allowed addresses.
1136 set_allowed_addrs(unit, addrs)
1138 struct wordlist *addrs;
1140 if (addresses[unit] != NULL)
1141 free_wordlist(addresses[unit]);
1142 addresses[unit] = addrs;
1145 * If there's only one authorized address we might as well
1146 * ask our peer for that one right away
1148 if (addrs != NULL && addrs->next == NULL) {
1149 char *p = addrs->word;
1150 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1154 if (*p != '!' && *p != '-' && strchr(p, '/') == NULL) {
1155 hp = gethostbyname(p);
1156 if (hp != NULL && hp->h_addrtype == AF_INET)
1157 a = *(u_int32_t *)hp->h_addr;
1160 if (a != (u_int32_t) -1)
1167 * auth_ip_addr - check whether the peer is authorized to use
1168 * a given IP address. Returns 1 if authorized, 0 otherwise.
1171 auth_ip_addr(unit, addr)
1175 return ip_addr_check(addr, addresses[unit]);
1179 ip_addr_check(addr, addrs)
1181 struct wordlist *addrs;
1183 u_int32_t a, mask, ah;
1185 char *ptr_word, *ptr_mask;
1189 /* don't allow loopback or multicast address */
1190 if (bad_ip_adrs(addr))
1194 return !auth_required; /* no addresses authorized */
1196 for (; addrs != NULL; addrs = addrs->next) {
1197 /* "-" means no addresses authorized, "*" means any address allowed */
1198 ptr_word = addrs->word;
1199 if (strcmp(ptr_word, "-") == 0)
1201 if (strcmp(ptr_word, "*") == 0)
1205 if (*ptr_word == '!') {
1210 mask = ~ (u_int32_t) 0;
1211 ptr_mask = strchr (ptr_word, '/');
1212 if (ptr_mask != NULL) {
1215 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1216 if (bit_count <= 0 || bit_count > 32) {
1217 syslog (LOG_WARNING,
1218 "invalid address length %s in auth. address list",
1223 mask <<= 32 - bit_count;
1226 hp = gethostbyname(ptr_word);
1227 if (hp != NULL && hp->h_addrtype == AF_INET) {
1228 a = *(u_int32_t *)hp->h_addr;
1230 np = getnetbyname (ptr_word);
1231 if (np != NULL && np->n_addrtype == AF_INET) {
1232 a = htonl (*(u_int32_t *)np->n_net);
1233 if (ptr_mask == NULL) {
1234 /* calculate appropriate mask for net */
1237 mask = IN_CLASSA_NET;
1238 else if (IN_CLASSB(ah))
1239 mask = IN_CLASSB_NET;
1240 else if (IN_CLASSC(ah))
1241 mask = IN_CLASSC_NET;
1244 a = inet_addr (ptr_word);
1248 if (ptr_mask != NULL)
1251 if (a == (u_int32_t)-1L)
1252 syslog (LOG_WARNING,
1253 "unknown host %s in auth. address list",
1256 /* Here a and addr are in network byte order,
1257 and mask is in host order. */
1258 if (((addr ^ a) & htonl(mask)) == 0)
1261 return 0; /* not in list => can't have it */
1265 * bad_ip_adrs - return 1 if the IP address is one we don't want
1266 * to use, such as an address in the loopback net or a multicast address.
1267 * addr is in network byte order.
1274 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1275 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1279 * check_access - complain if a secret file has too-liberal permissions.
1282 check_access(f, filename)
1288 if (fstat(fileno(f), &sbuf) < 0) {
1289 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1290 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1291 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1297 * scan_authfile - Scan an authorization file for a secret suitable
1298 * for authenticating `client' on `server'. The return value is -1
1299 * if no secret is found, otherwise >= 0. The return value has
1300 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1301 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1302 * Any following words on the line (i.e. address authorization
1303 * info) are placed in a wordlist and returned in *addrs.
1306 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1312 struct wordlist **addrs;
1316 int got_flag, best_flag;
1318 struct wordlist *ap, *addr_list, *alist, *alast;
1319 char word[MAXWORDLEN];
1320 char atfile[MAXWORDLEN];
1321 char lsecret[MAXWORDLEN];
1326 if (!getword(f, word, &newline, filename))
1327 return -1; /* file is empty??? */
1332 * Skip until we find a word at the start of a line.
1334 while (!newline && getword(f, word, &newline, filename))
1337 break; /* got to end of file */
1340 * Got a client - check if it's a match or a wildcard.
1343 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1348 got_flag = NONWILD_CLIENT;
1351 * Now get a server and check if it matches.
1353 if (!getword(f, word, &newline, filename))
1357 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1360 got_flag |= NONWILD_SERVER;
1363 * Got some sort of a match - see if it's better than what
1366 if (got_flag <= best_flag)
1372 if (!getword(f, word, &newline, filename))
1378 * Special syntax: @filename means read secret from file.
1380 if (word[0] == '@') {
1381 strcpy(atfile, word+1);
1382 if ((sf = fopen(atfile, "r")) == NULL) {
1383 syslog(LOG_WARNING, "can't open indirect secret file %s",
1387 check_access(sf, atfile);
1388 if (!getword(sf, word, &xxx, atfile)) {
1389 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1397 strcpy(lsecret, word);
1400 * Now read address authorization info and make a wordlist.
1402 alist = alast = NULL;
1404 if (!getword(f, word, &newline, filename) || newline)
1406 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1409 novm("authorized addresses");
1411 strcpy(ap->word, word);
1420 * Check if the given IP address is allowed by the wordlist.
1422 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1423 free_wordlist(alist);
1428 * This is the best so far; remember it.
1430 best_flag = got_flag;
1432 free_wordlist(addr_list);
1435 strcpy(secret, lsecret);
1443 else if (addr_list != NULL)
1444 free_wordlist(addr_list);
1450 * free_wordlist - release memory allocated for a wordlist.
1454 struct wordlist *wp;
1456 struct wordlist *next;
1458 while (wp != NULL) {
1466 * auth_script - execute a script with arguments
1467 * interface-name peer-name real-user tty speed
1479 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1480 user_name = pw->pw_name;
1482 sprintf(struid, "%d", getuid());
1485 sprintf(strspeed, "%d", baud_rate);
1489 argv[2] = peer_authname;
1490 argv[3] = user_name;
1495 run_program(script, argv, 0);