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.32 1997/07/14 03:52:33 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <security/pam_appl.h>
56 #include <security/pam_modules.h>
62 #include <shadow/pwauth.h>
65 #define PW_PPP PW_LOGIN
78 #include "pathnames.h"
80 /* Used for storing a sequence of words. Usually malloced. */
82 struct wordlist *next;
86 /* Bits in scan_authfile return value */
87 #define NONWILD_SERVER 1
88 #define NONWILD_CLIENT 2
90 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
95 /* The name by which the peer authenticated itself to us. */
96 char peer_authname[MAXNAMELEN];
98 /* Records which authentication operations haven't completed yet. */
99 static int auth_pending[NUM_PPP];
101 /* Set if we have successfully called login() */
102 static int logged_in;
104 /* Set if we have run the /etc/ppp/auth-up script. */
105 static int did_authup;
107 /* List of addresses which the peer may use. */
108 static struct wordlist *addresses[NUM_PPP];
110 /* Number of network protocols which we have opened. */
111 static int num_np_open;
113 /* Number of network protocols which have come up. */
114 static int num_np_up;
116 /* Set if we got the contents of passwd[] from the pap-secrets file. */
117 static int passwd_from_file;
119 /* Bits in auth_pending[] */
120 #define PAP_WITHPEER 1
122 #define CHAP_WITHPEER 4
125 extern char *crypt __P((const char *, const char *));
127 /* Prototypes for procedures local to this file. */
129 static void network_phase __P((int));
130 static void check_idle __P((void *));
131 static void connect_time_expired __P((void *));
132 static int login __P((char *, char *, char **, int *));
133 static void logout __P((void));
134 static int null_login __P((int));
135 static int get_pap_passwd __P((char *));
136 static int have_pap_secret __P((void));
137 static int have_chap_secret __P((char *, char *, u_int32_t));
138 static int ip_addr_check __P((u_int32_t, struct wordlist *));
139 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
140 struct wordlist **, char *));
141 static void free_wordlist __P((struct wordlist *));
142 static void auth_script __P((char *));
143 static void set_allowed_addrs __P((int, struct wordlist *));
145 static void callback_phase __P((int));
149 * An Open on LCP has requested a change from Dead to Establish phase.
150 * Do what's necessary to bring the physical layer up.
159 * LCP has terminated the link; go to the Dead phase and take the
160 * physical layer down.
163 link_terminated(unit)
166 if (phase == PHASE_DEAD)
171 syslog(LOG_NOTICE, "Connection terminated.");
175 * LCP has gone down; it will either die or try to re-establish.
182 struct protent *protp;
185 auth_script(_PATH_AUTHDOWN);
188 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
189 if (!protp->enabled_flag)
191 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
192 (*protp->lowerdown)(unit);
193 if (protp->protocol < 0xC000 && protp->close != NULL)
194 (*protp->close)(unit, "LCP down");
198 if (phase != PHASE_DEAD)
199 phase = PHASE_TERMINATE;
203 * The link is established.
204 * Proceed to the Dead, Authenticate or Network phase as appropriate.
207 link_established(unit)
211 lcp_options *wo = &lcp_wantoptions[unit];
212 lcp_options *go = &lcp_gotoptions[unit];
213 lcp_options *ho = &lcp_hisoptions[unit];
215 struct protent *protp;
218 * Tell higher-level protocols that LCP is up.
220 for (i = 0; (protp = protocols[i]) != NULL; ++i)
221 if (protp->protocol != PPP_LCP && protp->enabled_flag
222 && protp->lowerup != NULL)
223 (*protp->lowerup)(unit);
225 if (auth_required && !(go->neg_chap || go->neg_upap)) {
227 * We wanted the peer to authenticate itself, and it refused:
228 * treat it as though it authenticated with PAP using a username
229 * of "" and a password of "". If that's not OK, boot it out.
231 if (!wo->neg_upap || !null_login(unit)) {
232 syslog(LOG_WARNING, "peer refused to authenticate");
233 lcp_close(unit, "peer refused to authenticate");
238 phase = PHASE_AUTHENTICATE;
241 ChapAuthPeer(unit, our_name, go->chap_mdtype);
243 } else if (go->neg_upap) {
248 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
249 auth |= CHAP_WITHPEER;
250 } else if (ho->neg_upap) {
251 if (passwd[0] == 0) {
252 passwd_from_file = 1;
253 if (!get_pap_passwd(passwd))
254 syslog(LOG_ERR, "No secret found for PAP login");
256 upap_authwithpeer(unit, user, passwd);
257 auth |= PAP_WITHPEER;
259 auth_pending[unit] = auth;
266 * Proceed to the network phase.
273 struct protent *protp;
274 lcp_options *go = &lcp_gotoptions[unit];
277 * If the peer had to authenticate, run the auth-up script now.
279 if ((go->neg_chap || go->neg_upap) && !did_authup) {
280 auth_script(_PATH_AUTHUP);
286 * If we negotiated callback, do it now.
289 phase = PHASE_CALLBACK;
290 (*cbcp_protent.open)(unit);
295 phase = PHASE_NETWORK;
298 set_filters(&pass_filter, &active_filter);
300 for (i = 0; (protp = protocols[i]) != NULL; ++i)
301 if (protp->protocol < 0xC000 && protp->enabled_flag
302 && protp->open != NULL) {
303 (*protp->open)(unit);
304 if (protp->protocol != PPP_CCP)
308 if (num_np_open == 0)
310 lcp_close(0, "No network protocols running");
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) {
424 * At this point we consider that the link has come up successfully.
428 if (idle_time_limit > 0)
429 TIMEOUT(check_idle, NULL, idle_time_limit);
432 * Set a timeout to close the connection once the maximum
433 * connect time has expired.
436 TIMEOUT(connect_time_expired, 0, maxconnect);
442 * np_down - a network protocol has gone down.
448 if (--num_np_up == 0 && idle_time_limit > 0) {
449 UNTIMEOUT(check_idle, NULL);
454 * np_finished - a network protocol has finished using the link.
457 np_finished(unit, proto)
460 if (--num_np_open <= 0) {
461 /* no further use for the link: shut up shop. */
462 lcp_close(0, "No network protocols running");
467 * check_idle - check whether the link has been idle for long
468 * enough that we can shut it down.
474 struct ppp_idle idle;
477 if (!get_idle_time(0, &idle))
479 itime = MIN(idle.xmit_idle, idle.recv_idle);
480 if (itime >= idle_time_limit) {
481 /* link is idle: shut it down. */
482 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
483 lcp_close(0, "Link inactive");
485 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
490 * connect_time_expired - log a message and close the connection.
493 connect_time_expired(arg)
496 syslog(LOG_INFO, "Connect time expired");
497 lcp_close(0, "Connect time expired"); /* Close connection */
501 * auth_check_options - called to check authentication options.
506 lcp_options *wo = &lcp_wantoptions[0];
508 ipcp_options *ipwo = &ipcp_wantoptions[0];
511 /* Default our_name to hostname, and user to our_name */
512 if (our_name[0] == 0 || usehostname)
513 strcpy(our_name, hostname);
515 strcpy(user, our_name);
517 /* If authentication is required, ask peer for CHAP or PAP. */
518 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
524 * Check whether we have appropriate secrets to use
525 * to authenticate the peer.
527 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
528 if (!can_auth && wo->neg_chap) {
529 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
530 can_auth = have_chap_secret(remote_name, our_name, remote);
533 if (auth_required && !can_auth) {
534 option_error("peer authentication required but no suitable secret(s) found\n");
535 if (remote_name[0] == 0)
536 option_error("for authenticating any peer to us (%s)\n", our_name);
538 option_error("for authenticating peer %s to us (%s)\n",
539 remote_name, our_name);
544 * Check whether the user tried to override certain values
547 if (!auth_required && auth_req_info.priv > 0) {
548 if (!default_device && devnam_info.priv == 0) {
549 option_error("can't override device name when noauth option used");
552 if ((connector != NULL && connector_info.priv == 0)
553 || (disconnector != NULL && disconnector_info.priv == 0)
554 || (welcomer != NULL && welcomer_info.priv == 0)) {
555 option_error("can't override connect, disconnect or welcome");
556 option_error("option values when noauth option used");
563 * auth_reset - called when LCP is starting negotiations to recheck
564 * authentication options, i.e. whether we have appropriate secrets
565 * to use for authenticating ourselves and/or the peer.
571 lcp_options *go = &lcp_gotoptions[unit];
572 lcp_options *ao = &lcp_allowoptions[0];
573 ipcp_options *ipwo = &ipcp_wantoptions[0];
576 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
577 ao->neg_chap = !refuse_chap
578 && have_chap_secret(user, remote_name, (u_int32_t)0);
580 if (go->neg_upap && !uselogin && !have_pap_secret())
583 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
584 if (!have_chap_secret(remote_name, our_name, remote))
592 * check_passwd - Check the user name and passwd against the PAP secrets
593 * file. If requested, also check against the system password database,
594 * and login the user if OK.
597 * UPAP_AUTHNAK: Authentication failed.
598 * UPAP_AUTHACK: Authentication succeeded.
599 * In either case, msg points to an appropriate message.
602 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
614 struct wordlist *addrs;
616 ipcp_options *ipwo = &ipcp_wantoptions[unit];
617 char passwd[256], user[256];
618 char secret[MAXWORDLEN];
619 static int attempts = 0;
622 * Make copies of apasswd and auser, then null-terminate them.
624 BCOPY(apasswd, passwd, passwdlen);
625 passwd[passwdlen] = '\0';
626 BCOPY(auser, user, userlen);
627 user[userlen] = '\0';
631 * Open the file of pap secrets and scan for a suitable secret
632 * for authenticating this user.
634 filename = _PATH_UPAPFILE;
637 f = fopen(filename, "r");
639 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
643 check_access(f, filename);
644 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
645 if (scan_authfile(f, user, our_name, remote,
646 secret, &addrs, filename) < 0
647 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
648 && strcmp(crypt(passwd, secret), secret) != 0)) {
649 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
655 if (uselogin && ret == UPAP_AUTHACK) {
656 ret = login(user, passwd, msg, msglen);
657 if (ret == UPAP_AUTHNAK) {
658 syslog(LOG_WARNING, "PAP login failure for %s", user);
662 if (ret == UPAP_AUTHNAK) {
663 if (*msg == (char *) 0)
664 *msg = "Login incorrect";
665 *msglen = strlen(*msg);
667 * Frustrate passwd stealer programs.
668 * Allow 10 tries, but start backing off after 3 (stolen from login).
669 * On 10'th, drop the connection.
671 if (attempts++ >= 10) {
672 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
673 attempts, devnam, user);
677 sleep((u_int) (attempts - 3) * 5);
679 free_wordlist(addrs);
682 attempts = 0; /* Reset count */
683 if (*msg == (char *) 0)
685 *msglen = strlen(*msg);
686 set_allowed_addrs(unit, addrs);
689 BZERO(passwd, sizeof(passwd));
690 BZERO(secret, sizeof(secret));
696 * This function is needed for PAM. However, it should not be called.
697 * If it is, return the error code.
701 static int pam_conv(int num_msg, const struct pam_message **msg,
702 struct pam_response **resp, void *appdata_ptr)
709 * login - Check the user name and password against the system
710 * password database, and login the user if OK.
713 * UPAP_AUTHNAK: Login failed.
714 * UPAP_AUTHACK: Login succeeded.
715 * In either case, msg points to an appropriate message.
719 login(user, passwd, msg, msglen)
728 struct pam_conv pam_conversation;
734 * Fill the pam_conversion structure
736 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
737 pam_conversation.conv = &pam_conv;
739 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
740 if (pam_error != PAM_SUCCESS) {
741 *msg = (char *) pam_strerror (pam_error);
745 * Define the fields for the credintial validation
747 (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
748 (void) pam_set_item (pamh, PAM_TTY, devnam);
752 pam_error = pam_authenticate (pamh, PAM_SILENT);
753 if (pam_error == PAM_SUCCESS)
754 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
756 *msg = (char *) pam_strerror (pam_error);
760 (void) pam_end (pamh, pam_error);
762 if (pam_error != PAM_SUCCESS)
765 * Use the non-PAM methods directly
767 #else /* #ifdef USE_PAM */
773 struct spwd *getspnam();
774 extern int isexpired (struct passwd *, struct spwd *); /* in libshadow.a */
779 return (UPAP_AUTHNAK);
783 spwd = getspnam(user);
786 /* check the age of the password entry */
787 if (isexpired(pw, spwd)) {
788 syslog(LOG_WARNING,"Expired password for %s",user);
789 return (UPAP_AUTHNAK);
791 pw->pw_passwd = spwd->sp_pwdp;
796 * If no passwd, don't let them login.
798 if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0'
799 || strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
800 return (UPAP_AUTHNAK);
802 #endif /* #ifdef USE_PAM */
804 syslog(LOG_INFO, "user %s logged in", user);
807 * Write a wtmp entry for this user.
810 if (strncmp(tty, "/dev/", 5) == 0)
812 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
815 return (UPAP_AUTHACK);
819 * logout - Logout the user.
827 if (strncmp(tty, "/dev/", 5) == 0)
829 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
835 * null_login - Check if a username of "" and a password of "" are
836 * acceptable, and iff so, set the list of acceptable IP addresses
846 struct wordlist *addrs;
847 char secret[MAXWORDLEN];
850 * Open the file of pap secrets and scan for a suitable secret.
851 * We don't accept a wildcard client.
853 filename = _PATH_UPAPFILE;
855 f = fopen(filename, "r");
858 check_access(f, filename);
860 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
861 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
862 BZERO(secret, sizeof(secret));
865 set_allowed_addrs(unit, addrs);
867 free_wordlist(addrs);
875 * get_pap_passwd - get a password for authenticating ourselves with
876 * our peer using PAP. Returns 1 on success, 0 if no suitable password
880 get_pap_passwd(passwd)
886 struct wordlist *addrs;
887 char secret[MAXWORDLEN];
889 filename = _PATH_UPAPFILE;
891 f = fopen(filename, "r");
894 check_access(f, filename);
895 ret = scan_authfile(f, user,
896 remote_name[0]? remote_name: NULL,
897 (u_int32_t)0, secret, NULL, filename);
901 if (passwd != NULL) {
902 strncpy(passwd, secret, MAXSECRETLEN);
903 passwd[MAXSECRETLEN-1] = 0;
905 BZERO(secret, sizeof(secret));
911 * have_pap_secret - check whether we have a PAP file with any
912 * secrets that we could possibly use for authenticating the peer.
920 ipcp_options *ipwo = &ipcp_wantoptions[0];
923 filename = _PATH_UPAPFILE;
924 f = fopen(filename, "r");
928 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
929 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
939 * have_chap_secret - check whether we have a CHAP file with a
940 * secret that we could possibly use for authenticating `client'
941 * on `server'. Either can be the null string, meaning we don't
942 * know the identity yet.
945 have_chap_secret(client, server, remote)
954 filename = _PATH_CHAPFILE;
955 f = fopen(filename, "r");
961 else if (server[0] == 0)
964 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
974 * get_secret - open the CHAP secret file and return the secret
975 * for authenticating the given client on the given server.
976 * (We could be either client or server).
979 get_secret(unit, client, server, secret, secret_len, save_addrs)
990 struct wordlist *addrs;
991 char secbuf[MAXWORDLEN];
993 filename = _PATH_CHAPFILE;
997 f = fopen(filename, "r");
999 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1002 check_access(f, filename);
1004 ret = scan_authfile(f, client, server, (u_int32_t)0,
1005 secbuf, &addrs, filename);
1011 set_allowed_addrs(unit, addrs);
1013 len = strlen(secbuf);
1014 if (len > MAXSECRETLEN) {
1015 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1018 BCOPY(secbuf, secret, len);
1019 BZERO(secbuf, sizeof(secbuf));
1026 * set_allowed_addrs() - set the list of allowed addresses.
1029 set_allowed_addrs(unit, addrs)
1031 struct wordlist *addrs;
1033 if (addresses[unit] != NULL)
1034 free_wordlist(addresses[unit]);
1035 addresses[unit] = addrs;
1038 * If there's only one authorized address we might as well
1039 * ask our peer for that one right away
1041 if (addrs != NULL && addrs->next == NULL) {
1042 char *p = addrs->word;
1043 struct ipcp_options *wo = &ipcp_wantoptions[unit];
1047 if (wo->hisaddr == 0 && *p != '!' && *p != '-'
1048 && strchr(p, '/') == NULL) {
1049 hp = gethostbyname(p);
1050 if (hp != NULL && hp->h_addrtype == AF_INET)
1051 a = *(u_int32_t *)hp->h_addr;
1054 if (a != (u_int32_t) -1)
1061 * auth_ip_addr - check whether the peer is authorized to use
1062 * a given IP address. Returns 1 if authorized, 0 otherwise.
1065 auth_ip_addr(unit, addr)
1069 return ip_addr_check(addr, addresses[unit]);
1073 ip_addr_check(addr, addrs)
1075 struct wordlist *addrs;
1077 u_int32_t a, mask, ah;
1079 char *ptr_word, *ptr_mask;
1083 /* don't allow loopback or multicast address */
1084 if (bad_ip_adrs(addr))
1088 return !auth_required; /* no addresses authorized */
1090 for (; addrs != NULL; addrs = addrs->next) {
1091 /* "-" means no addresses authorized, "*" means any address allowed */
1092 ptr_word = addrs->word;
1093 if (strcmp(ptr_word, "-") == 0)
1095 if (strcmp(ptr_word, "*") == 0)
1099 if (*ptr_word == '!') {
1104 mask = ~ (u_int32_t) 0;
1105 ptr_mask = strchr (ptr_word, '/');
1106 if (ptr_mask != NULL) {
1109 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1110 if (bit_count <= 0 || bit_count > 32) {
1111 syslog (LOG_WARNING,
1112 "invalid address length %s in auth. address list",
1117 mask <<= 32 - bit_count;
1120 hp = gethostbyname(ptr_word);
1121 if (hp != NULL && hp->h_addrtype == AF_INET) {
1122 a = *(u_int32_t *)hp->h_addr;
1124 np = getnetbyname (ptr_word);
1125 if (np != NULL && np->n_addrtype == AF_INET) {
1126 a = htonl (*(u_int32_t *)np->n_net);
1127 if (ptr_mask == NULL) {
1128 /* calculate appropriate mask for net */
1131 mask = IN_CLASSA_NET;
1132 else if (IN_CLASSB(ah))
1133 mask = IN_CLASSB_NET;
1134 else if (IN_CLASSC(ah))
1135 mask = IN_CLASSC_NET;
1138 a = inet_addr (ptr_word);
1142 if (ptr_mask != NULL)
1145 if (a == (u_int32_t)-1L)
1146 syslog (LOG_WARNING,
1147 "unknown host %s in auth. address list",
1150 /* Here a and addr are in network byte order,
1151 and mask is in host order. */
1152 if (((addr ^ a) & htonl(mask)) == 0)
1155 return 0; /* not in list => can't have it */
1159 * bad_ip_adrs - return 1 if the IP address is one we don't want
1160 * to use, such as an address in the loopback net or a multicast address.
1161 * addr is in network byte order.
1168 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1169 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1173 * check_access - complain if a secret file has too-liberal permissions.
1176 check_access(f, filename)
1182 if (fstat(fileno(f), &sbuf) < 0) {
1183 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1184 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1185 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1191 * scan_authfile - Scan an authorization file for a secret suitable
1192 * for authenticating `client' on `server'. The return value is -1
1193 * if no secret is found, otherwise >= 0. The return value has
1194 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1195 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1196 * Any following words on the line (i.e. address authorization
1197 * info) are placed in a wordlist and returned in *addrs.
1200 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1206 struct wordlist **addrs;
1210 int got_flag, best_flag;
1212 struct wordlist *ap, *addr_list, *alist, *alast;
1213 char word[MAXWORDLEN];
1214 char atfile[MAXWORDLEN];
1215 char lsecret[MAXWORDLEN];
1220 if (!getword(f, word, &newline, filename))
1221 return -1; /* file is empty??? */
1226 * Skip until we find a word at the start of a line.
1228 while (!newline && getword(f, word, &newline, filename))
1231 break; /* got to end of file */
1234 * Got a client - check if it's a match or a wildcard.
1237 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1242 got_flag = NONWILD_CLIENT;
1245 * Now get a server and check if it matches.
1247 if (!getword(f, word, &newline, filename))
1251 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1254 got_flag |= NONWILD_SERVER;
1257 * Got some sort of a match - see if it's better than what
1260 if (got_flag <= best_flag)
1266 if (!getword(f, word, &newline, filename))
1272 * Special syntax: @filename means read secret from file.
1274 if (word[0] == '@') {
1275 strcpy(atfile, word+1);
1276 if ((sf = fopen(atfile, "r")) == NULL) {
1277 syslog(LOG_WARNING, "can't open indirect secret file %s",
1281 check_access(sf, atfile);
1282 if (!getword(sf, word, &xxx, atfile)) {
1283 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1291 strcpy(lsecret, word);
1294 * Now read address authorization info and make a wordlist.
1296 alist = alast = NULL;
1298 if (!getword(f, word, &newline, filename) || newline)
1300 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1303 novm("authorized addresses");
1305 strcpy(ap->word, word);
1314 * Check if the given IP address is allowed by the wordlist.
1316 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1317 free_wordlist(alist);
1322 * This is the best so far; remember it.
1324 best_flag = got_flag;
1326 free_wordlist(addr_list);
1329 strcpy(secret, lsecret);
1337 else if (addr_list != NULL)
1338 free_wordlist(addr_list);
1344 * free_wordlist - release memory allocated for a wordlist.
1348 struct wordlist *wp;
1350 struct wordlist *next;
1352 while (wp != NULL) {
1360 * auth_script - execute a script with arguments
1361 * interface-name peer-name real-user tty speed
1373 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1374 user_name = pw->pw_name;
1376 sprintf(struid, "%d", getuid());
1379 sprintf(strspeed, "%d", baud_rate);
1383 argv[2] = peer_authname;
1384 argv[3] = user_name;
1389 run_program(script, argv, 0);