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.27 1996/09/26 06:20:35 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
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
81 #include "pathnames.h"
83 /* Used for storing a sequence of words. Usually malloced. */
85 struct wordlist *next;
89 /* Bits in scan_authfile return value */
90 #define NONWILD_SERVER 1
91 #define NONWILD_CLIENT 2
93 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
98 /* The name by which the peer authenticated itself to us. */
99 char peer_authname[MAXNAMELEN];
101 /* Records which authentication operations haven't completed yet. */
102 static int auth_pending[NUM_PPP];
104 /* Set if we have successfully called login() */
105 static int logged_in;
107 /* Set if we have run the /etc/ppp/auth-up script. */
108 static int did_authup;
110 /* List of addresses which the peer may use. */
111 static struct wordlist *addresses[NUM_PPP];
113 /* Number of network protocols which we have opened. */
114 static int num_np_open;
116 /* Number of network protocols which have come up. */
117 static int num_np_up;
119 /* Set if we got the contents of passwd[] from the pap-secrets file. */
120 static int passwd_from_file;
122 /* Bits in auth_pending[] */
123 #define PAP_WITHPEER 1
125 #define CHAP_WITHPEER 4
128 /* Prototypes for procedures local to this file. */
130 static void network_phase __P((int));
131 static void check_idle __P((caddr_t));
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 *));
145 * An Open on LCP has requested a change from Dead to Establish phase.
146 * Do what's necessary to bring the physical layer up.
155 * LCP has terminated the link; go to the Dead phase and take the
156 * physical layer down.
159 link_terminated(unit)
162 if (phase == PHASE_DEAD)
167 syslog(LOG_NOTICE, "Connection terminated.");
171 * LCP has gone down; it will either die or try to re-establish.
178 struct protent *protp;
181 auth_script(_PATH_AUTHDOWN);
184 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
185 if (!protp->enabled_flag)
187 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
188 (*protp->lowerdown)(unit);
189 if (protp->protocol < 0xC000 && protp->close != NULL)
190 (*protp->close)(unit, "LCP down");
194 phase = PHASE_TERMINATE;
198 * The link is established.
199 * Proceed to the Dead, Authenticate or Network phase as appropriate.
202 link_established(unit)
206 lcp_options *wo = &lcp_wantoptions[unit];
207 lcp_options *go = &lcp_gotoptions[unit];
208 lcp_options *ho = &lcp_hisoptions[unit];
210 struct protent *protp;
213 * Tell higher-level protocols that LCP is up.
215 for (i = 0; (protp = protocols[i]) != NULL; ++i)
216 if (protp->protocol != PPP_LCP && protp->enabled_flag
217 && protp->lowerup != NULL)
218 (*protp->lowerup)(unit);
220 if (auth_required && !(go->neg_chap || go->neg_upap)) {
222 * We wanted the peer to authenticate itself, and it refused:
223 * treat it as though it authenticated with PAP using a username
224 * of "" and a password of "". If that's not OK, boot it out.
226 if (!wo->neg_upap || !null_login(unit)) {
227 syslog(LOG_WARNING, "peer refused to authenticate");
228 lcp_close(unit, "peer refused to authenticate");
233 phase = PHASE_AUTHENTICATE;
236 ChapAuthPeer(unit, our_name, go->chap_mdtype);
238 } else if (go->neg_upap) {
243 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
244 auth |= CHAP_WITHPEER;
245 } else if (ho->neg_upap) {
246 if (passwd[0] == 0) {
247 passwd_from_file = 1;
248 if (!get_pap_passwd(passwd))
249 syslog(LOG_ERR, "No secret found for PAP login");
251 upap_authwithpeer(unit, user, passwd);
252 auth |= PAP_WITHPEER;
254 auth_pending[unit] = auth;
261 * Proceed to the network phase.
268 struct protent *protp;
269 lcp_options *go = &lcp_gotoptions[unit];
272 * If the peer had to authenticate, run the auth-up script now.
274 if ((go->neg_chap || go->neg_upap) && !did_authup) {
275 auth_script(_PATH_AUTHUP);
279 phase = PHASE_NETWORK;
282 set_filters(&pass_filter, &active_filter);
284 for (i = 0; (protp = protocols[i]) != NULL; ++i)
285 if (protp->protocol < 0xC000 && protp->enabled_flag
286 && protp->open != NULL) {
287 (*protp->open)(unit);
288 if (protp->protocol != PPP_CCP)
294 * The peer has failed to authenticate himself using `protocol'.
297 auth_peer_fail(unit, protocol)
301 * Authentication failure: take the link down
303 lcp_close(unit, "Authentication failed");
307 * The peer has been successfully authenticated using `protocol'.
310 auth_peer_success(unit, protocol, name, namelen)
325 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
331 * Save the authenticated name of the peer for later.
333 if (namelen > sizeof(peer_authname) - 1)
334 namelen = sizeof(peer_authname) - 1;
335 BCOPY(name, peer_authname, namelen);
336 peer_authname[namelen] = 0;
339 * If there is no more authentication still to be done,
340 * proceed to the network phase.
342 if ((auth_pending[unit] &= ~bit) == 0)
347 * We have failed to authenticate ourselves to the peer using `protocol'.
350 auth_withpeer_fail(unit, protocol)
353 if (passwd_from_file)
354 BZERO(passwd, MAXSECRETLEN);
356 * We've failed to authenticate ourselves to our peer.
357 * He'll probably take the link down, and there's not much
358 * we can do except wait for that.
363 * We have successfully authenticated ourselves with the peer using `protocol'.
366 auth_withpeer_success(unit, protocol)
376 if (passwd_from_file)
377 BZERO(passwd, MAXSECRETLEN);
381 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
387 * If there is no more authentication still being done,
388 * proceed to the network phase.
390 if ((auth_pending[unit] &= ~bit) == 0)
396 * np_up - a network protocol has come up.
402 if (num_np_up == 0 && idle_time_limit > 0) {
403 TIMEOUT(check_idle, NULL, idle_time_limit);
409 * np_down - a network protocol has gone down.
415 if (--num_np_up == 0 && idle_time_limit > 0) {
416 UNTIMEOUT(check_idle, NULL);
421 * np_finished - a network protocol has finished using the link.
424 np_finished(unit, proto)
427 if (--num_np_open <= 0) {
428 /* no further use for the link: shut up shop. */
429 lcp_close(0, "No network protocols running");
434 * check_idle - check whether the link has been idle for long
435 * enough that we can shut it down.
441 struct ppp_idle idle;
444 if (!get_idle_time(0, &idle))
446 itime = MIN(idle.xmit_idle, idle.recv_idle);
447 if (itime >= idle_time_limit) {
448 /* link is idle: shut it down. */
449 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
451 lcp_close(0, "Link inactive");
453 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
458 * auth_check_options - called to check authentication options.
463 lcp_options *wo = &lcp_wantoptions[0];
465 ipcp_options *ipwo = &ipcp_wantoptions[0];
468 /* Check that we are running as root. */
469 if (geteuid() != 0) {
470 option_error("must be run with root privileges");
474 /* Default our_name to hostname, and user to our_name */
475 if (our_name[0] == 0 || usehostname)
476 strcpy(our_name, hostname);
478 strcpy(user, our_name);
480 /* If authentication is required, ask peer for CHAP or PAP. */
481 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
487 * Check whether we have appropriate secrets to use
488 * to authenticate the peer.
490 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
491 if (!can_auth && wo->neg_chap) {
492 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
493 can_auth = have_chap_secret(remote_name, our_name, remote);
496 if (auth_required && !can_auth) {
497 option_error("peer authentication required but no suitable secret(s) found\n");
502 * Check whether the user tried to override certain values
505 if (!auth_required && auth_req_info.priv > 0) {
506 if (!default_device && devnam_info.priv == 0) {
507 option_error("can't override device name when noauth option used");
510 if (connector != NULL && connector_info.priv == 0
511 || disconnector != NULL && disconnector_info.priv == 0
512 || welcomer != NULL && welcomer_info.priv == 0) {
513 option_error("can't override connect, disconnect or welcome");
514 option_error("option values when noauth option used");
521 * auth_reset - called when LCP is starting negotiations to recheck
522 * authentication options, i.e. whether we have appropriate secrets
523 * to use for authenticating ourselves and/or the peer.
529 lcp_options *go = &lcp_gotoptions[unit];
530 lcp_options *ao = &lcp_allowoptions[0];
531 ipcp_options *ipwo = &ipcp_wantoptions[0];
534 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
535 ao->neg_chap = !refuse_chap
536 && have_chap_secret(user, remote_name, (u_int32_t)0);
538 if (go->neg_upap && !uselogin && !have_pap_secret())
541 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
542 if (!have_chap_secret(remote_name, our_name, remote))
550 * check_passwd - Check the user name and passwd against the PAP secrets
551 * file. If requested, also check against the system password database,
552 * and login the user if OK.
555 * UPAP_AUTHNAK: Authentication failed.
556 * UPAP_AUTHACK: Authentication succeeded.
557 * In either case, msg points to an appropriate message.
560 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
572 struct wordlist *addrs;
574 ipcp_options *ipwo = &ipcp_wantoptions[unit];
575 char passwd[256], user[256];
576 char secret[MAXWORDLEN];
577 static int attempts = 0;
580 * Make copies of apasswd and auser, then null-terminate them.
582 BCOPY(apasswd, passwd, passwdlen);
583 passwd[passwdlen] = '\0';
584 BCOPY(auser, user, userlen);
585 user[userlen] = '\0';
589 * Open the file of pap secrets and scan for a suitable secret
590 * for authenticating this user.
592 filename = _PATH_UPAPFILE;
595 f = fopen(filename, "r");
598 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
603 check_access(f, filename);
604 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
605 if (scan_authfile(f, user, our_name, remote,
606 secret, &addrs, filename) < 0
607 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
608 && strcmp(crypt(passwd, secret), secret) != 0)) {
609 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
615 if (uselogin && ret == UPAP_AUTHACK) {
616 ret = login(user, passwd, msg, msglen);
617 if (ret == UPAP_AUTHNAK) {
618 syslog(LOG_WARNING, "PAP login failure for %s", user);
622 if (ret == UPAP_AUTHNAK) {
623 if (*msg == (char *) 0)
624 *msg = "Login incorrect";
625 *msglen = strlen(*msg);
627 * Frustrate passwd stealer programs.
628 * Allow 10 tries, but start backing off after 3 (stolen from login).
629 * On 10'th, drop the connection.
631 if (attempts++ >= 10) {
632 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
633 attempts, devnam, user);
637 sleep((u_int) (attempts - 3) * 5);
639 free_wordlist(addrs);
642 attempts = 0; /* Reset count */
643 if (*msg == (char *) 0)
645 *msglen = strlen(*msg);
646 if (addresses[unit] != NULL)
647 free_wordlist(addresses[unit]);
648 addresses[unit] = addrs;
651 BZERO(passwd, sizeof(passwd));
652 BZERO(secret, sizeof(secret));
659 * This function was lifted from the shadow-3.3.2 version by John Haugh II.
660 * It is included because the function was not in the standard libshadow
661 * library. If it is included in the library then I can remove it from here.
664 #define DAY (24L*3600L)
666 * isexpired - determine if account is expired yet
668 * isexpired calculates the expiration date based on the
669 * password expiration criteria.
680 clock = time ((time_t *) 0) / DAY;
683 * Quick and easy - there is an expired account field
684 * along with an inactive account field. Do the expired
685 * one first since it is worse.
688 if (sp->sp_expire > 0 && sp->sp_expire < clock)
691 if (sp->sp_inact > 0 && sp->sp_lstchg > 0 && sp->sp_max > 0 &&
692 sp->sp_inact + sp->sp_lstchg + sp->sp_max < clock)
696 * The last and max fields must be present for an account
697 * to have an expired password. A maximum of >10000 days
698 * is considered to be infinite.
701 if (sp->sp_lstchg == -1 ||
702 sp->sp_max == -1 || sp->sp_max >= 10000L)
706 * Calculate today's day and the day on which the password
707 * is going to expire. If that date has already passed,
708 * the password has expired.
711 if (sp->sp_lstchg + sp->sp_max < clock)
720 * This function is needed for PAM. However, it should not be called.
721 * If it is, return the error code.
725 static int pam_conv(int num_msg, const struct pam_message **msg,
726 struct pam_response **resp, void *appdata_ptr)
733 * login - Check the user name and password against the system
734 * password database, and login the user if OK.
737 * UPAP_AUTHNAK: Login failed.
738 * UPAP_AUTHACK: Login succeeded.
739 * In either case, msg points to an appropriate message.
743 login(user, passwd, msg, msglen)
752 struct pam_conv pam_conversation;
758 * Fill the pam_conversion structure
760 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
761 pam_conversation.conv = &pam_conv;
763 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
764 if (pam_error != PAM_SUCCESS) {
765 *msg = (char *) pam_strerror (pam_error);
769 * Define the fields for the credintial validation
771 (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
772 (void) pam_set_item (pamh, PAM_TTY, devnam);
776 pam_error = pam_authenticate (pamh, PAM_SILENT);
777 if (pam_error == PAM_SUCCESS)
778 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
780 *msg = (char *) pam_strerror (pam_error);
784 (void) pam_end (pamh, pam_error);
786 if (pam_error != PAM_SUCCESS)
789 * Use the non-PAM methods directly
791 #else /* #ifdef USE_PAM */
798 struct spwd *getspnam();
801 if ((pw = getpwnam(user)) == NULL) {
802 return (UPAP_AUTHNAK);
806 if ((spwd = getspnam(user)) == NULL) {
809 pw->pw_passwd = spwd->sp_pwdp;
814 * XXX If no passwd, let them login without one.
816 if (pw->pw_passwd == '\0') {
817 return (UPAP_AUTHACK);
822 if (pw->pw_passwd[0] == '@') {
823 if (pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL)) {
824 return (UPAP_AUTHNAK);
827 epasswd = pw_encrypt(passwd, pw->pw_passwd);
828 if (strcmp(epasswd, pw->pw_passwd)) {
829 return (UPAP_AUTHNAK);
832 /* check the age of the password entry */
833 if (spwd && (isexpired (pw, spwd) != 0)) {
834 return (UPAP_AUTHNAK);
838 epasswd = crypt(passwd, pw->pw_passwd);
839 if (strcmp(epasswd, pw->pw_passwd)) {
840 return (UPAP_AUTHNAK);
843 #endif /* #ifdef USE_PAM */
845 syslog(LOG_INFO, "user %s logged in", user);
848 * Write a wtmp entry for this user.
851 if (strncmp(tty, "/dev/", 5) == 0)
853 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
856 return (UPAP_AUTHACK);
860 * logout - Logout the user.
868 if (strncmp(tty, "/dev/", 5) == 0)
870 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
876 * null_login - Check if a username of "" and a password of "" are
877 * acceptable, and iff so, set the list of acceptable IP addresses
887 struct wordlist *addrs;
888 char secret[MAXWORDLEN];
891 * Open the file of pap secrets and scan for a suitable secret.
892 * We don't accept a wildcard client.
894 filename = _PATH_UPAPFILE;
896 f = fopen(filename, "r");
899 check_access(f, filename);
901 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
902 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
903 BZERO(secret, sizeof(secret));
906 if (addresses[unit] != NULL)
907 free_wordlist(addresses[unit]);
908 addresses[unit] = addrs;
917 * get_pap_passwd - get a password for authenticating ourselves with
918 * our peer using PAP. Returns 1 on success, 0 if no suitable password
922 get_pap_passwd(passwd)
927 struct wordlist *addrs;
928 char secret[MAXWORDLEN];
930 filename = _PATH_UPAPFILE;
932 f = fopen(filename, "r");
935 check_access(f, filename);
936 if (scan_authfile(f, user,
937 remote_name[0]? remote_name: NULL,
938 (u_int32_t)0, secret, NULL, filename) < 0)
940 if (passwd != NULL) {
941 strncpy(passwd, secret, MAXSECRETLEN);
942 passwd[MAXSECRETLEN-1] = 0;
944 BZERO(secret, sizeof(secret));
950 * have_pap_secret - check whether we have a PAP file with any
951 * secrets that we could possibly use for authenticating the peer.
959 ipcp_options *ipwo = &ipcp_wantoptions[0];
962 filename = _PATH_UPAPFILE;
963 f = fopen(filename, "r");
967 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
968 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
978 * have_chap_secret - check whether we have a CHAP file with a
979 * secret that we could possibly use for authenticating `client'
980 * on `server'. Either can be the null string, meaning we don't
981 * know the identity yet.
984 have_chap_secret(client, server, remote)
993 filename = _PATH_CHAPFILE;
994 f = fopen(filename, "r");
1000 else if (server[0] == 0)
1003 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1013 * get_secret - open the CHAP secret file and return the secret
1014 * for authenticating the given client on the given server.
1015 * (We could be either client or server).
1018 get_secret(unit, client, server, secret, secret_len, save_addrs)
1029 struct wordlist *addrs;
1030 char secbuf[MAXWORDLEN];
1032 filename = _PATH_CHAPFILE;
1036 f = fopen(filename, "r");
1038 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1041 check_access(f, filename);
1043 ret = scan_authfile(f, client, server, (u_int32_t)0,
1044 secbuf, &addrs, filename);
1050 if (addresses[unit] != NULL)
1051 free_wordlist(addresses[unit]);
1052 addresses[unit] = addrs;
1055 len = strlen(secbuf);
1056 if (len > MAXSECRETLEN) {
1057 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1060 BCOPY(secbuf, secret, len);
1061 BZERO(secbuf, sizeof(secbuf));
1068 * auth_ip_addr - check whether the peer is authorized to use
1069 * a given IP address. Returns 1 if authorized, 0 otherwise.
1072 auth_ip_addr(unit, addr)
1076 return ip_addr_check(addr, addresses[unit]);
1080 ip_addr_check(addr, addrs)
1082 struct wordlist *addrs;
1084 u_int32_t a, mask, ah;
1086 char *ptr_word, *ptr_mask;
1090 /* don't allow loopback or multicast address */
1091 if (bad_ip_adrs(addr))
1095 return 1; /* no restriction */
1097 for (; addrs != NULL; addrs = addrs->next) {
1098 /* "-" means no addresses authorized */
1099 ptr_word = addrs->word;
1100 if (strcmp(ptr_word, "-") == 0)
1104 if (*ptr_word == '!') {
1109 mask = ~ (u_int32_t) 0;
1110 ptr_mask = strchr (ptr_word, '/');
1111 if (ptr_mask != NULL) {
1114 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1115 if (bit_count <= 0 || bit_count > 32) {
1116 syslog (LOG_WARNING,
1117 "invalid address length %s in auth. address list",
1122 mask <<= 32 - bit_count;
1125 hp = gethostbyname(ptr_word);
1126 if (hp != NULL && hp->h_addrtype == AF_INET) {
1127 a = *(u_int32_t *)hp->h_addr;
1128 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
1130 np = getnetbyname (ptr_word);
1131 if (np != NULL && np->n_addrtype == AF_INET)
1132 a = htonl (*(u_int32_t *)np->n_net);
1134 a = inet_addr (ptr_word);
1135 if (ptr_mask == NULL) {
1136 /* calculate appropriate mask for net */
1139 mask = IN_CLASSA_NET;
1140 else if (IN_CLASSB(ah))
1141 mask = IN_CLASSB_NET;
1142 else if (IN_CLASSC(ah))
1143 mask = IN_CLASSC_NET;
1147 if (ptr_mask != NULL)
1151 syslog (LOG_WARNING,
1152 "unknown host %s in auth. address list",
1155 if (((addr ^ a) & htonl(mask)) == 0)
1158 return 0; /* not in list => can't have it */
1162 * bad_ip_adrs - return 1 if the IP address is one we don't want
1163 * to use, such as an address in the loopback net or a multicast address.
1164 * addr is in network byte order.
1171 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1172 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1176 * check_access - complain if a secret file has too-liberal permissions.
1179 check_access(f, filename)
1185 if (fstat(fileno(f), &sbuf) < 0) {
1186 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1187 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1188 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1194 * scan_authfile - Scan an authorization file for a secret suitable
1195 * for authenticating `client' on `server'. The return value is -1
1196 * if no secret is found, otherwise >= 0. The return value has
1197 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1198 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1199 * Any following words on the line (i.e. address authorization
1200 * info) are placed in a wordlist and returned in *addrs.
1203 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1209 struct wordlist **addrs;
1213 int got_flag, best_flag;
1215 struct wordlist *ap, *addr_list, *alist, *alast;
1216 char word[MAXWORDLEN];
1217 char atfile[MAXWORDLEN];
1218 char lsecret[MAXWORDLEN];
1223 if (!getword(f, word, &newline, filename))
1224 return -1; /* file is empty??? */
1229 * Skip until we find a word at the start of a line.
1231 while (!newline && getword(f, word, &newline, filename))
1234 break; /* got to end of file */
1237 * Got a client - check if it's a match or a wildcard.
1240 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1245 got_flag = NONWILD_CLIENT;
1248 * Now get a server and check if it matches.
1250 if (!getword(f, word, &newline, filename))
1254 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1257 got_flag |= NONWILD_SERVER;
1260 * Got some sort of a match - see if it's better than what
1263 if (got_flag <= best_flag)
1269 if (!getword(f, word, &newline, filename))
1275 * Special syntax: @filename means read secret from file.
1277 if (word[0] == '@') {
1278 strcpy(atfile, word+1);
1279 if ((sf = fopen(atfile, "r")) == NULL) {
1280 syslog(LOG_WARNING, "can't open indirect secret file %s",
1284 check_access(sf, atfile);
1285 if (!getword(sf, word, &xxx, atfile)) {
1286 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1294 strcpy(lsecret, word);
1297 * Now read address authorization info and make a wordlist.
1299 alist = alast = NULL;
1301 if (!getword(f, word, &newline, filename) || newline)
1303 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1306 novm("authorized addresses");
1308 strcpy(ap->word, word);
1317 * Check if the given IP address is allowed by the wordlist.
1319 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1320 free_wordlist(alist);
1325 * This is the best so far; remember it.
1327 best_flag = got_flag;
1329 free_wordlist(addr_list);
1332 strcpy(secret, lsecret);
1340 else if (addr_list != NULL)
1341 free_wordlist(addr_list);
1347 * free_wordlist - release memory allocated for a wordlist.
1351 struct wordlist *wp;
1353 struct wordlist *next;
1355 while (wp != NULL) {
1363 * auth_script - execute a script with arguments
1364 * interface-name peer-name real-user tty speed
1376 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1377 user_name = pw->pw_name;
1379 sprintf(struid, "%d", getuid());
1382 sprintf(strspeed, "%d", baud_rate);
1386 argv[2] = peer_authname;
1387 argv[3] = user_name;
1392 run_program(script, argv, 0);