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.30 1997/03/04 03:37:21 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
54 #if defined(SVR4) || defined(_linux_)
57 #if defined(SUNOS4) || defined(ULTRIX)
63 #include <security/pam_appl.h>
64 #include <security/pam_modules.h>
69 #include <shadow/pwauth.h>
71 #define PW_PPP PW_LOGIN
84 #include "pathnames.h"
86 /* Used for storing a sequence of words. Usually malloced. */
88 struct wordlist *next;
92 /* Bits in scan_authfile return value */
93 #define NONWILD_SERVER 1
94 #define NONWILD_CLIENT 2
96 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
101 /* The name by which the peer authenticated itself to us. */
102 char peer_authname[MAXNAMELEN];
104 /* Records which authentication operations haven't completed yet. */
105 static int auth_pending[NUM_PPP];
107 /* Set if we have successfully called login() */
108 static int logged_in;
110 /* Set if we have run the /etc/ppp/auth-up script. */
111 static int did_authup;
113 /* List of addresses which the peer may use. */
114 static struct wordlist *addresses[NUM_PPP];
116 /* Number of network protocols which we have opened. */
117 static int num_np_open;
119 /* Number of network protocols which have come up. */
120 static int num_np_up;
122 /* Set if we got the contents of passwd[] from the pap-secrets file. */
123 static int passwd_from_file;
125 /* Bits in auth_pending[] */
126 #define PAP_WITHPEER 1
128 #define CHAP_WITHPEER 4
131 /* Prototypes for procedures local to this file. */
133 static void network_phase __P((int));
134 static void check_idle __P((caddr_t));
135 static void connect_time_expired __P((caddr_t));
136 static int login __P((char *, char *, char **, int *));
137 static void logout __P((void));
138 static int null_login __P((int));
139 static int get_pap_passwd __P((char *));
140 static int have_pap_secret __P((void));
141 static int have_chap_secret __P((char *, char *, u_int32_t));
142 static int ip_addr_check __P((u_int32_t, struct wordlist *));
143 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
144 struct wordlist **, char *));
145 static void free_wordlist __P((struct wordlist *));
146 static void auth_script __P((char *));
147 static void set_allowed_addrs __P((int, struct wordlist *));
149 static void callback_phase __P((int));
153 * An Open on LCP has requested a change from Dead to Establish phase.
154 * Do what's necessary to bring the physical layer up.
163 * LCP has terminated the link; go to the Dead phase and take the
164 * physical layer down.
167 link_terminated(unit)
170 if (phase == PHASE_DEAD)
175 syslog(LOG_NOTICE, "Connection terminated.");
179 * LCP has gone down; it will either die or try to re-establish.
186 struct protent *protp;
189 auth_script(_PATH_AUTHDOWN);
192 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
193 if (!protp->enabled_flag)
195 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
196 (*protp->lowerdown)(unit);
197 if (protp->protocol < 0xC000 && protp->close != NULL)
198 (*protp->close)(unit, "LCP down");
202 if (phase != PHASE_DEAD)
203 phase = PHASE_TERMINATE;
207 * The link is established.
208 * Proceed to the Dead, Authenticate or Network phase as appropriate.
211 link_established(unit)
215 lcp_options *wo = &lcp_wantoptions[unit];
216 lcp_options *go = &lcp_gotoptions[unit];
217 lcp_options *ho = &lcp_hisoptions[unit];
219 struct protent *protp;
222 * Tell higher-level protocols that LCP is up.
224 for (i = 0; (protp = protocols[i]) != NULL; ++i)
225 if (protp->protocol != PPP_LCP && protp->enabled_flag
226 && protp->lowerup != NULL)
227 (*protp->lowerup)(unit);
229 if (auth_required && !(go->neg_chap || go->neg_upap)) {
231 * We wanted the peer to authenticate itself, and it refused:
232 * treat it as though it authenticated with PAP using a username
233 * of "" and a password of "". If that's not OK, boot it out.
235 if (!wo->neg_upap || !null_login(unit)) {
236 syslog(LOG_WARNING, "peer refused to authenticate");
237 lcp_close(unit, "peer refused to authenticate");
242 phase = PHASE_AUTHENTICATE;
245 ChapAuthPeer(unit, our_name, go->chap_mdtype);
247 } else if (go->neg_upap) {
252 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
253 auth |= CHAP_WITHPEER;
254 } else if (ho->neg_upap) {
255 if (passwd[0] == 0) {
256 passwd_from_file = 1;
257 if (!get_pap_passwd(passwd))
258 syslog(LOG_ERR, "No secret found for PAP login");
260 upap_authwithpeer(unit, user, passwd);
261 auth |= PAP_WITHPEER;
263 auth_pending[unit] = auth;
270 * Proceed to the network phase.
277 struct protent *protp;
278 lcp_options *go = &lcp_gotoptions[unit];
281 * If the peer had to authenticate, run the auth-up script now.
283 if ((go->neg_chap || go->neg_upap) && !did_authup) {
284 auth_script(_PATH_AUTHUP);
290 * If we negotiated callback, do it now.
293 phase = PHASE_CALLBACK;
294 (*cbcp_protent.open)(unit);
299 phase = PHASE_NETWORK;
302 set_filters(&pass_filter, &active_filter);
304 for (i = 0; (protp = protocols[i]) != NULL; ++i)
305 if (protp->protocol < 0xC000 && protp->enabled_flag
306 && protp->open != NULL) {
307 (*protp->open)(unit);
308 if (protp->protocol != PPP_CCP)
314 * The peer has failed to authenticate himself using `protocol'.
317 auth_peer_fail(unit, protocol)
321 * Authentication failure: take the link down
323 lcp_close(unit, "Authentication failed");
327 * The peer has been successfully authenticated using `protocol'.
330 auth_peer_success(unit, protocol, name, namelen)
345 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
351 * Save the authenticated name of the peer for later.
353 if (namelen > sizeof(peer_authname) - 1)
354 namelen = sizeof(peer_authname) - 1;
355 BCOPY(name, peer_authname, namelen);
356 peer_authname[namelen] = 0;
359 * If there is no more authentication still to be done,
360 * proceed to the network (or callback) phase.
362 if ((auth_pending[unit] &= ~bit) == 0)
367 * We have failed to authenticate ourselves to the peer using `protocol'.
370 auth_withpeer_fail(unit, protocol)
373 if (passwd_from_file)
374 BZERO(passwd, MAXSECRETLEN);
376 * We've failed to authenticate ourselves to our peer.
377 * He'll probably take the link down, and there's not much
378 * we can do except wait for that.
383 * We have successfully authenticated ourselves with the peer using `protocol'.
386 auth_withpeer_success(unit, protocol)
396 if (passwd_from_file)
397 BZERO(passwd, MAXSECRETLEN);
401 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
407 * If there is no more authentication still being done,
408 * proceed to the network (or callback) phase.
410 if ((auth_pending[unit] &= ~bit) == 0)
416 * np_up - a network protocol has come up.
422 if (num_np_up == 0 && idle_time_limit > 0) {
423 TIMEOUT(check_idle, NULL, idle_time_limit);
426 * Set a timeout to close the connection once the maximum
427 * connect time has expired.
430 TIMEOUT(connect_time_expired, 0, maxconnect);
436 * np_down - a network protocol has gone down.
442 if (--num_np_up == 0 && idle_time_limit > 0) {
443 UNTIMEOUT(check_idle, NULL);
448 * np_finished - a network protocol has finished using the link.
451 np_finished(unit, proto)
454 if (--num_np_open <= 0) {
455 /* no further use for the link: shut up shop. */
456 lcp_close(0, "No network protocols running");
461 * check_idle - check whether the link has been idle for long
462 * enough that we can shut it down.
468 struct ppp_idle idle;
471 if (!get_idle_time(0, &idle))
473 itime = MIN(idle.xmit_idle, idle.recv_idle);
474 if (itime >= idle_time_limit) {
475 /* link is idle: shut it down. */
476 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
478 lcp_close(0, "Link inactive");
480 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
485 * connect_time_expired - log a message and close the connection.
488 connect_time_expired(arg)
491 syslog(LOG_INFO, "Connect time expired");
492 lcp_close(0, "Connect time expired"); /* Close connection */
496 * auth_check_options - called to check authentication options.
501 lcp_options *wo = &lcp_wantoptions[0];
503 ipcp_options *ipwo = &ipcp_wantoptions[0];
506 /* Default our_name to hostname, and user to our_name */
507 if (our_name[0] == 0 || usehostname)
508 strcpy(our_name, hostname);
510 strcpy(user, our_name);
512 /* If authentication is required, ask peer for CHAP or PAP. */
513 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
519 * Check whether we have appropriate secrets to use
520 * to authenticate the peer.
522 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
523 if (!can_auth && wo->neg_chap) {
524 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
525 can_auth = have_chap_secret(remote_name, our_name, remote);
528 if (auth_required && !can_auth) {
529 option_error("peer authentication required but no suitable secret(s) found\n");
530 if (remote_name[0] == 0)
531 option_error("for authenticating any peer to us (%s)\n", our_name);
533 option_error("for authenticating peer %s to us (%s)\n",
534 remote_name, our_name);
539 * Check whether the user tried to override certain values
542 if (!auth_required && auth_req_info.priv > 0) {
543 if (!default_device && devnam_info.priv == 0) {
544 option_error("can't override device name when noauth option used");
547 if (connector != NULL && connector_info.priv == 0
548 || disconnector != NULL && disconnector_info.priv == 0
549 || welcomer != NULL && welcomer_info.priv == 0) {
550 option_error("can't override connect, disconnect or welcome");
551 option_error("option values when noauth option used");
558 * auth_reset - called when LCP is starting negotiations to recheck
559 * authentication options, i.e. whether we have appropriate secrets
560 * to use for authenticating ourselves and/or the peer.
566 lcp_options *go = &lcp_gotoptions[unit];
567 lcp_options *ao = &lcp_allowoptions[0];
568 ipcp_options *ipwo = &ipcp_wantoptions[0];
571 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
572 ao->neg_chap = !refuse_chap
573 && have_chap_secret(user, remote_name, (u_int32_t)0);
575 if (go->neg_upap && !uselogin && !have_pap_secret())
578 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
579 if (!have_chap_secret(remote_name, our_name, remote))
587 * check_passwd - Check the user name and passwd against the PAP secrets
588 * file. If requested, also check against the system password database,
589 * and login the user if OK.
592 * UPAP_AUTHNAK: Authentication failed.
593 * UPAP_AUTHACK: Authentication succeeded.
594 * In either case, msg points to an appropriate message.
597 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
609 struct wordlist *addrs;
611 ipcp_options *ipwo = &ipcp_wantoptions[unit];
612 char passwd[256], user[256];
613 char secret[MAXWORDLEN];
614 static int attempts = 0;
617 * Make copies of apasswd and auser, then null-terminate them.
619 BCOPY(apasswd, passwd, passwdlen);
620 passwd[passwdlen] = '\0';
621 BCOPY(auser, user, userlen);
622 user[userlen] = '\0';
626 * Open the file of pap secrets and scan for a suitable secret
627 * for authenticating this user.
629 filename = _PATH_UPAPFILE;
632 f = fopen(filename, "r");
634 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
638 check_access(f, filename);
639 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
640 if (scan_authfile(f, user, our_name, remote,
641 secret, &addrs, filename) < 0
642 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
643 && strcmp(crypt(passwd, secret), secret) != 0)) {
644 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
650 if (uselogin && ret == UPAP_AUTHACK) {
651 ret = login(user, passwd, msg, msglen);
652 if (ret == UPAP_AUTHNAK) {
653 syslog(LOG_WARNING, "PAP login failure for %s", user);
657 if (ret == UPAP_AUTHNAK) {
658 if (*msg == (char *) 0)
659 *msg = "Login incorrect";
660 *msglen = strlen(*msg);
662 * Frustrate passwd stealer programs.
663 * Allow 10 tries, but start backing off after 3 (stolen from login).
664 * On 10'th, drop the connection.
666 if (attempts++ >= 10) {
667 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
668 attempts, devnam, user);
672 sleep((u_int) (attempts - 3) * 5);
674 free_wordlist(addrs);
677 attempts = 0; /* Reset count */
678 if (*msg == (char *) 0)
680 *msglen = strlen(*msg);
681 set_allowed_addrs(unit, addrs);
684 BZERO(passwd, sizeof(passwd));
685 BZERO(secret, sizeof(secret));
691 * This function is needed for PAM. However, it should not be called.
692 * If it is, return the error code.
696 static int pam_conv(int num_msg, const struct pam_message **msg,
697 struct pam_response **resp, void *appdata_ptr)
704 * login - Check the user name and password against the system
705 * password database, and login the user if OK.
708 * UPAP_AUTHNAK: Login failed.
709 * UPAP_AUTHACK: Login succeeded.
710 * In either case, msg points to an appropriate message.
714 login(user, passwd, msg, msglen)
723 struct pam_conv pam_conversation;
729 * Fill the pam_conversion structure
731 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
732 pam_conversation.conv = &pam_conv;
734 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
735 if (pam_error != PAM_SUCCESS) {
736 *msg = (char *) pam_strerror (pam_error);
740 * Define the fields for the credintial validation
742 (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
743 (void) pam_set_item (pamh, PAM_TTY, devnam);
747 pam_error = pam_authenticate (pamh, PAM_SILENT);
748 if (pam_error == PAM_SUCCESS)
749 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
751 *msg = (char *) pam_strerror (pam_error);
755 (void) pam_end (pamh, pam_error);
757 if (pam_error != PAM_SUCCESS)
760 * Use the non-PAM methods directly
762 #else /* #ifdef USE_PAM */
769 struct spwd *getspnam();
770 extern int isexpired (struct passwd *, struct spwd *); /* in libshadow.a */
775 return (UPAP_AUTHNAK);
779 spwd = getspnam(user);
782 /* check the age of the password entry */
783 if (isexpired(pw, spwd)) {
784 syslog(LOG_WARNING,"Expired password for %s",user);
785 return (UPAP_AUTHNAK);
787 pw->pw_passwd = spwd->sp_pwdp;
792 * If no passwd, don't let them login.
794 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
795 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
796 return (UPAP_AUTHNAK);
798 #endif /* #ifdef USE_PAM */
800 syslog(LOG_INFO, "user %s logged in", user);
803 * Write a wtmp entry for this user.
806 if (strncmp(tty, "/dev/", 5) == 0)
808 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
811 return (UPAP_AUTHACK);
815 * logout - Logout the user.
823 if (strncmp(tty, "/dev/", 5) == 0)
825 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
831 * null_login - Check if a username of "" and a password of "" are
832 * acceptable, and iff so, set the list of acceptable IP addresses
842 struct wordlist *addrs;
843 char secret[MAXWORDLEN];
846 * Open the file of pap secrets and scan for a suitable secret.
847 * We don't accept a wildcard client.
849 filename = _PATH_UPAPFILE;
851 f = fopen(filename, "r");
854 check_access(f, filename);
856 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
857 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
858 BZERO(secret, sizeof(secret));
861 set_allowed_addrs(unit, addrs);
863 free_wordlist(addrs);
871 * get_pap_passwd - get a password for authenticating ourselves with
872 * our peer using PAP. Returns 1 on success, 0 if no suitable password
876 get_pap_passwd(passwd)
881 struct wordlist *addrs;
882 char secret[MAXWORDLEN];
884 filename = _PATH_UPAPFILE;
886 f = fopen(filename, "r");
889 check_access(f, filename);
890 if (scan_authfile(f, user,
891 remote_name[0]? remote_name: NULL,
892 (u_int32_t)0, secret, NULL, filename) < 0)
894 if (passwd != NULL) {
895 strncpy(passwd, secret, MAXSECRETLEN);
896 passwd[MAXSECRETLEN-1] = 0;
898 BZERO(secret, sizeof(secret));
904 * have_pap_secret - check whether we have a PAP file with any
905 * secrets that we could possibly use for authenticating the peer.
913 ipcp_options *ipwo = &ipcp_wantoptions[0];
916 filename = _PATH_UPAPFILE;
917 f = fopen(filename, "r");
921 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
922 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
932 * have_chap_secret - check whether we have a CHAP file with a
933 * secret that we could possibly use for authenticating `client'
934 * on `server'. Either can be the null string, meaning we don't
935 * know the identity yet.
938 have_chap_secret(client, server, remote)
947 filename = _PATH_CHAPFILE;
948 f = fopen(filename, "r");
954 else if (server[0] == 0)
957 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
967 * get_secret - open the CHAP secret file and return the secret
968 * for authenticating the given client on the given server.
969 * (We could be either client or server).
972 get_secret(unit, client, server, secret, secret_len, save_addrs)
983 struct wordlist *addrs;
984 char secbuf[MAXWORDLEN];
986 filename = _PATH_CHAPFILE;
990 f = fopen(filename, "r");
992 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
995 check_access(f, filename);
997 ret = scan_authfile(f, client, server, (u_int32_t)0,
998 secbuf, &addrs, filename);
1004 set_allowed_addrs(unit, addrs);
1006 len = strlen(secbuf);
1007 if (len > MAXSECRETLEN) {
1008 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1011 BCOPY(secbuf, secret, len);
1012 BZERO(secbuf, sizeof(secbuf));
1019 * set_allowed_addrs() - set the list of allowed addresses.
1022 set_allowed_addrs(unit, addrs)
1024 struct wordlist *addrs;
1026 if (addresses[unit] != NULL)
1027 free_wordlist(addresses[unit]);
1028 addresses[unit] = addrs;
1031 * If there's only one authorized address we might as well
1032 * ask our peer for that one right away
1034 if (addrs != NULL && addrs->next == NULL) {
1035 char *p = addrs->word;
1036 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1040 if (wo->hisaddr == 0 && *p != '!' && *p != '-'
1041 && strchr(p, '/') == NULL) {
1042 hp = gethostbyname(p);
1043 if (hp != NULL && hp->h_addrtype == AF_INET)
1044 a = *(u_int32_t *)hp->h_addr;
1047 if (a != (u_int32_t) -1)
1054 * auth_ip_addr - check whether the peer is authorized to use
1055 * a given IP address. Returns 1 if authorized, 0 otherwise.
1058 auth_ip_addr(unit, addr)
1062 return ip_addr_check(addr, addresses[unit]);
1066 ip_addr_check(addr, addrs)
1068 struct wordlist *addrs;
1070 u_int32_t a, mask, ah;
1072 char *ptr_word, *ptr_mask;
1076 /* don't allow loopback or multicast address */
1077 if (bad_ip_adrs(addr))
1081 return !auth_required; /* no addresses authorized */
1083 for (; addrs != NULL; addrs = addrs->next) {
1084 /* "-" means no addresses authorized, "*" means any address allowed */
1085 ptr_word = addrs->word;
1086 if (strcmp(ptr_word, "-") == 0)
1088 if (strcmp(ptr_word, "*") == 0)
1092 if (*ptr_word == '!') {
1097 mask = ~ (u_int32_t) 0;
1098 ptr_mask = strchr (ptr_word, '/');
1099 if (ptr_mask != NULL) {
1102 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1103 if (bit_count <= 0 || bit_count > 32) {
1104 syslog (LOG_WARNING,
1105 "invalid address length %s in auth. address list",
1110 mask <<= 32 - bit_count;
1113 hp = gethostbyname(ptr_word);
1114 if (hp != NULL && hp->h_addrtype == AF_INET) {
1115 a = *(u_int32_t *)hp->h_addr;
1117 np = getnetbyname (ptr_word);
1118 if (np != NULL && np->n_addrtype == AF_INET) {
1119 a = htonl (*(u_int32_t *)np->n_net);
1120 if (ptr_mask == NULL) {
1121 /* calculate appropriate mask for net */
1124 mask = IN_CLASSA_NET;
1125 else if (IN_CLASSB(ah))
1126 mask = IN_CLASSB_NET;
1127 else if (IN_CLASSC(ah))
1128 mask = IN_CLASSC_NET;
1131 a = inet_addr (ptr_word);
1135 if (ptr_mask != NULL)
1139 syslog (LOG_WARNING,
1140 "unknown host %s in auth. address list",
1143 /* Here a and addr are in network byte order,
1144 and mask is in host order. */
1145 if (((addr ^ a) & htonl(mask)) == 0)
1148 return 0; /* not in list => can't have it */
1152 * bad_ip_adrs - return 1 if the IP address is one we don't want
1153 * to use, such as an address in the loopback net or a multicast address.
1154 * addr is in network byte order.
1161 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1162 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1166 * check_access - complain if a secret file has too-liberal permissions.
1169 check_access(f, filename)
1175 if (fstat(fileno(f), &sbuf) < 0) {
1176 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1177 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1178 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1184 * scan_authfile - Scan an authorization file for a secret suitable
1185 * for authenticating `client' on `server'. The return value is -1
1186 * if no secret is found, otherwise >= 0. The return value has
1187 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1188 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1189 * Any following words on the line (i.e. address authorization
1190 * info) are placed in a wordlist and returned in *addrs.
1193 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1199 struct wordlist **addrs;
1203 int got_flag, best_flag;
1205 struct wordlist *ap, *addr_list, *alist, *alast;
1206 char word[MAXWORDLEN];
1207 char atfile[MAXWORDLEN];
1208 char lsecret[MAXWORDLEN];
1213 if (!getword(f, word, &newline, filename))
1214 return -1; /* file is empty??? */
1219 * Skip until we find a word at the start of a line.
1221 while (!newline && getword(f, word, &newline, filename))
1224 break; /* got to end of file */
1227 * Got a client - check if it's a match or a wildcard.
1230 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1235 got_flag = NONWILD_CLIENT;
1238 * Now get a server and check if it matches.
1240 if (!getword(f, word, &newline, filename))
1244 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1247 got_flag |= NONWILD_SERVER;
1250 * Got some sort of a match - see if it's better than what
1253 if (got_flag <= best_flag)
1259 if (!getword(f, word, &newline, filename))
1265 * Special syntax: @filename means read secret from file.
1267 if (word[0] == '@') {
1268 strcpy(atfile, word+1);
1269 if ((sf = fopen(atfile, "r")) == NULL) {
1270 syslog(LOG_WARNING, "can't open indirect secret file %s",
1274 check_access(sf, atfile);
1275 if (!getword(sf, word, &xxx, atfile)) {
1276 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1284 strcpy(lsecret, word);
1287 * Now read address authorization info and make a wordlist.
1289 alist = alast = NULL;
1291 if (!getword(f, word, &newline, filename) || newline)
1293 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1296 novm("authorized addresses");
1298 strcpy(ap->word, word);
1307 * Check if the given IP address is allowed by the wordlist.
1309 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1310 free_wordlist(alist);
1315 * This is the best so far; remember it.
1317 best_flag = got_flag;
1319 free_wordlist(addr_list);
1322 strcpy(secret, lsecret);
1330 else if (addr_list != NULL)
1331 free_wordlist(addr_list);
1337 * free_wordlist - release memory allocated for a wordlist.
1341 struct wordlist *wp;
1343 struct wordlist *next;
1345 while (wp != NULL) {
1353 * auth_script - execute a script with arguments
1354 * interface-name peer-name real-user tty speed
1366 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1367 user_name = pw->pw_name;
1369 sprintf(struid, "%d", getuid());
1372 sprintf(strspeed, "%d", baud_rate);
1376 argv[2] = peer_authname;
1377 argv[3] = user_name;
1382 run_program(script, argv, 0);