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.28 1996/10/08 04:35:02 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
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 int login __P((char *, char *, char **, int *));
136 static void logout __P((void));
137 static int null_login __P((int));
138 static int get_pap_passwd __P((char *));
139 static int have_pap_secret __P((void));
140 static int have_chap_secret __P((char *, char *, u_int32_t));
141 static int ip_addr_check __P((u_int32_t, struct wordlist *));
142 static int scan_authfile __P((FILE *, char *, char *, u_int32_t, char *,
143 struct wordlist **, char *));
144 static void free_wordlist __P((struct wordlist *));
145 static void auth_script __P((char *));
147 static void callback_phase __P((int));
151 * An Open on LCP has requested a change from Dead to Establish phase.
152 * Do what's necessary to bring the physical layer up.
161 * LCP has terminated the link; go to the Dead phase and take the
162 * physical layer down.
165 link_terminated(unit)
168 if (phase == PHASE_DEAD)
173 syslog(LOG_NOTICE, "Connection terminated.");
177 * LCP has gone down; it will either die or try to re-establish.
184 struct protent *protp;
187 auth_script(_PATH_AUTHDOWN);
190 for (i = 0; (protp = protocols[i]) != NULL; ++i) {
191 if (!protp->enabled_flag)
193 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
194 (*protp->lowerdown)(unit);
195 if (protp->protocol < 0xC000 && protp->close != NULL)
196 (*protp->close)(unit, "LCP down");
200 phase = PHASE_TERMINATE;
204 * The link is established.
205 * Proceed to the Dead, Authenticate or Network phase as appropriate.
208 link_established(unit)
212 lcp_options *wo = &lcp_wantoptions[unit];
213 lcp_options *go = &lcp_gotoptions[unit];
214 lcp_options *ho = &lcp_hisoptions[unit];
216 struct protent *protp;
219 * Tell higher-level protocols that LCP is up.
221 for (i = 0; (protp = protocols[i]) != NULL; ++i)
222 if (protp->protocol != PPP_LCP && protp->enabled_flag
223 && protp->lowerup != NULL)
224 (*protp->lowerup)(unit);
226 if (auth_required && !(go->neg_chap || go->neg_upap)) {
228 * We wanted the peer to authenticate itself, and it refused:
229 * treat it as though it authenticated with PAP using a username
230 * of "" and a password of "". If that's not OK, boot it out.
232 if (!wo->neg_upap || !null_login(unit)) {
233 syslog(LOG_WARNING, "peer refused to authenticate");
234 lcp_close(unit, "peer refused to authenticate");
239 phase = PHASE_AUTHENTICATE;
242 ChapAuthPeer(unit, our_name, go->chap_mdtype);
244 } else if (go->neg_upap) {
249 ChapAuthWithPeer(unit, user, ho->chap_mdtype);
250 auth |= CHAP_WITHPEER;
251 } else if (ho->neg_upap) {
252 if (passwd[0] == 0) {
253 passwd_from_file = 1;
254 if (!get_pap_passwd(passwd))
255 syslog(LOG_ERR, "No secret found for PAP login");
257 upap_authwithpeer(unit, user, passwd);
258 auth |= PAP_WITHPEER;
260 auth_pending[unit] = auth;
267 * Proceed to the network phase.
274 struct protent *protp;
275 lcp_options *go = &lcp_gotoptions[unit];
278 * If the peer had to authenticate, run the auth-up script now.
280 if ((go->neg_chap || go->neg_upap) && !did_authup) {
281 auth_script(_PATH_AUTHUP);
287 * If we negotiated callback, do it now.
290 phase = PHASE_CALLBACK;
291 (*cbcp_protent.open)(unit);
296 phase = PHASE_NETWORK;
299 set_filters(&pass_filter, &active_filter);
301 for (i = 0; (protp = protocols[i]) != NULL; ++i)
302 if (protp->protocol < 0xC000 && protp->enabled_flag
303 && protp->open != NULL) {
304 (*protp->open)(unit);
305 if (protp->protocol != PPP_CCP)
311 * The peer has failed to authenticate himself using `protocol'.
314 auth_peer_fail(unit, protocol)
318 * Authentication failure: take the link down
320 lcp_close(unit, "Authentication failed");
324 * The peer has been successfully authenticated using `protocol'.
327 auth_peer_success(unit, protocol, name, namelen)
342 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
348 * Save the authenticated name of the peer for later.
350 if (namelen > sizeof(peer_authname) - 1)
351 namelen = sizeof(peer_authname) - 1;
352 BCOPY(name, peer_authname, namelen);
353 peer_authname[namelen] = 0;
356 * If there is no more authentication still to be done,
357 * proceed to the network (or callback) phase.
359 if ((auth_pending[unit] &= ~bit) == 0)
364 * We have failed to authenticate ourselves to the peer using `protocol'.
367 auth_withpeer_fail(unit, protocol)
370 if (passwd_from_file)
371 BZERO(passwd, MAXSECRETLEN);
373 * We've failed to authenticate ourselves to our peer.
374 * He'll probably take the link down, and there's not much
375 * we can do except wait for that.
380 * We have successfully authenticated ourselves with the peer using `protocol'.
383 auth_withpeer_success(unit, protocol)
393 if (passwd_from_file)
394 BZERO(passwd, MAXSECRETLEN);
398 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
404 * If there is no more authentication still being done,
405 * proceed to the network (or callback) phase.
407 if ((auth_pending[unit] &= ~bit) == 0)
413 * np_up - a network protocol has come up.
419 if (num_np_up == 0 && idle_time_limit > 0) {
420 TIMEOUT(check_idle, NULL, idle_time_limit);
426 * np_down - a network protocol has gone down.
432 if (--num_np_up == 0 && idle_time_limit > 0) {
433 UNTIMEOUT(check_idle, NULL);
438 * np_finished - a network protocol has finished using the link.
441 np_finished(unit, proto)
444 if (--num_np_open <= 0) {
445 /* no further use for the link: shut up shop. */
446 lcp_close(0, "No network protocols running");
451 * check_idle - check whether the link has been idle for long
452 * enough that we can shut it down.
458 struct ppp_idle idle;
461 if (!get_idle_time(0, &idle))
463 itime = MIN(idle.xmit_idle, idle.recv_idle);
464 if (itime >= idle_time_limit) {
465 /* link is idle: shut it down. */
466 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
468 lcp_close(0, "Link inactive");
470 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
475 * auth_check_options - called to check authentication options.
480 lcp_options *wo = &lcp_wantoptions[0];
482 ipcp_options *ipwo = &ipcp_wantoptions[0];
485 /* Check that we are running as root. */
486 if (geteuid() != 0) {
487 option_error("must be run with root privileges");
491 /* Default our_name to hostname, and user to our_name */
492 if (our_name[0] == 0 || usehostname)
493 strcpy(our_name, hostname);
495 strcpy(user, our_name);
497 /* If authentication is required, ask peer for CHAP or PAP. */
498 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
504 * Check whether we have appropriate secrets to use
505 * to authenticate the peer.
507 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
508 if (!can_auth && wo->neg_chap) {
509 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
510 can_auth = have_chap_secret(remote_name, our_name, remote);
513 if (auth_required && !can_auth) {
514 option_error("peer authentication required but no suitable secret(s) found\n");
519 * Check whether the user tried to override certain values
522 if (!auth_required && auth_req_info.priv > 0) {
523 if (!default_device && devnam_info.priv == 0) {
524 option_error("can't override device name when noauth option used");
527 if (connector != NULL && connector_info.priv == 0
528 || disconnector != NULL && disconnector_info.priv == 0
529 || welcomer != NULL && welcomer_info.priv == 0) {
530 option_error("can't override connect, disconnect or welcome");
531 option_error("option values when noauth option used");
538 * auth_reset - called when LCP is starting negotiations to recheck
539 * authentication options, i.e. whether we have appropriate secrets
540 * to use for authenticating ourselves and/or the peer.
546 lcp_options *go = &lcp_gotoptions[unit];
547 lcp_options *ao = &lcp_allowoptions[0];
548 ipcp_options *ipwo = &ipcp_wantoptions[0];
551 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
552 ao->neg_chap = !refuse_chap
553 && have_chap_secret(user, remote_name, (u_int32_t)0);
555 if (go->neg_upap && !uselogin && !have_pap_secret())
558 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
559 if (!have_chap_secret(remote_name, our_name, remote))
567 * check_passwd - Check the user name and passwd against the PAP secrets
568 * file. If requested, also check against the system password database,
569 * and login the user if OK.
572 * UPAP_AUTHNAK: Authentication failed.
573 * UPAP_AUTHACK: Authentication succeeded.
574 * In either case, msg points to an appropriate message.
577 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
589 struct wordlist *addrs;
591 ipcp_options *ipwo = &ipcp_wantoptions[unit];
592 char passwd[256], user[256];
593 char secret[MAXWORDLEN];
594 static int attempts = 0;
597 * Make copies of apasswd and auser, then null-terminate them.
599 BCOPY(apasswd, passwd, passwdlen);
600 passwd[passwdlen] = '\0';
601 BCOPY(auser, user, userlen);
602 user[userlen] = '\0';
606 * Open the file of pap secrets and scan for a suitable secret
607 * for authenticating this user.
609 filename = _PATH_UPAPFILE;
612 f = fopen(filename, "r");
615 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
620 check_access(f, filename);
621 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
622 if (scan_authfile(f, user, our_name, remote,
623 secret, &addrs, filename) < 0
624 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
625 && strcmp(crypt(passwd, secret), secret) != 0)) {
626 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
632 if (uselogin && ret == UPAP_AUTHACK) {
633 ret = login(user, passwd, msg, msglen);
634 if (ret == UPAP_AUTHNAK) {
635 syslog(LOG_WARNING, "PAP login failure for %s", user);
639 if (ret == UPAP_AUTHNAK) {
640 if (*msg == (char *) 0)
641 *msg = "Login incorrect";
642 *msglen = strlen(*msg);
644 * Frustrate passwd stealer programs.
645 * Allow 10 tries, but start backing off after 3 (stolen from login).
646 * On 10'th, drop the connection.
648 if (attempts++ >= 10) {
649 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
650 attempts, devnam, user);
654 sleep((u_int) (attempts - 3) * 5);
656 free_wordlist(addrs);
659 attempts = 0; /* Reset count */
660 if (*msg == (char *) 0)
662 *msglen = strlen(*msg);
663 if (addresses[unit] != NULL)
664 free_wordlist(addresses[unit]);
665 addresses[unit] = addrs;
668 BZERO(passwd, sizeof(passwd));
669 BZERO(secret, sizeof(secret));
676 * This function was lifted from the shadow-3.3.2 version by John Haugh II.
677 * It is included because the function was not in the standard libshadow
678 * library. If it is included in the library then I can remove it from here.
681 #define DAY (24L*3600L)
683 * isexpired - determine if account is expired yet
685 * isexpired calculates the expiration date based on the
686 * password expiration criteria.
697 clock = time ((time_t *) 0) / DAY;
700 * Quick and easy - there is an expired account field
701 * along with an inactive account field. Do the expired
702 * one first since it is worse.
705 if (sp->sp_expire > 0 && sp->sp_expire < clock)
708 if (sp->sp_inact > 0 && sp->sp_lstchg > 0 && sp->sp_max > 0 &&
709 sp->sp_inact + sp->sp_lstchg + sp->sp_max < clock)
713 * The last and max fields must be present for an account
714 * to have an expired password. A maximum of >10000 days
715 * is considered to be infinite.
718 if (sp->sp_lstchg == -1 ||
719 sp->sp_max == -1 || sp->sp_max >= 10000L)
723 * Calculate today's day and the day on which the password
724 * is going to expire. If that date has already passed,
725 * the password has expired.
728 if (sp->sp_lstchg + sp->sp_max < clock)
737 * This function is needed for PAM. However, it should not be called.
738 * If it is, return the error code.
742 static int pam_conv(int num_msg, const struct pam_message **msg,
743 struct pam_response **resp, void *appdata_ptr)
750 * login - Check the user name and password against the system
751 * password database, and login the user if OK.
754 * UPAP_AUTHNAK: Login failed.
755 * UPAP_AUTHACK: Login succeeded.
756 * In either case, msg points to an appropriate message.
760 login(user, passwd, msg, msglen)
769 struct pam_conv pam_conversation;
775 * Fill the pam_conversion structure
777 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
778 pam_conversation.conv = &pam_conv;
780 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
781 if (pam_error != PAM_SUCCESS) {
782 *msg = (char *) pam_strerror (pam_error);
786 * Define the fields for the credintial validation
788 (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
789 (void) pam_set_item (pamh, PAM_TTY, devnam);
793 pam_error = pam_authenticate (pamh, PAM_SILENT);
794 if (pam_error == PAM_SUCCESS)
795 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
797 *msg = (char *) pam_strerror (pam_error);
801 (void) pam_end (pamh, pam_error);
803 if (pam_error != PAM_SUCCESS)
806 * Use the non-PAM methods directly
808 #else /* #ifdef USE_PAM */
815 struct spwd *getspnam();
818 if ((pw = getpwnam(user)) == NULL) {
819 return (UPAP_AUTHNAK);
823 if ((spwd = getspnam(user)) == NULL) {
826 pw->pw_passwd = spwd->sp_pwdp;
831 * XXX If no passwd, let them login without one.
833 if (pw->pw_passwd == '\0') {
834 return (UPAP_AUTHACK);
839 if (pw->pw_passwd[0] == '@') {
840 if (pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL)) {
841 return (UPAP_AUTHNAK);
844 epasswd = pw_encrypt(passwd, pw->pw_passwd);
845 if (strcmp(epasswd, pw->pw_passwd)) {
846 return (UPAP_AUTHNAK);
849 /* check the age of the password entry */
850 if (spwd && (isexpired (pw, spwd) != 0)) {
851 return (UPAP_AUTHNAK);
855 epasswd = crypt(passwd, pw->pw_passwd);
856 if (strcmp(epasswd, pw->pw_passwd)) {
857 return (UPAP_AUTHNAK);
860 #endif /* #ifdef USE_PAM */
862 syslog(LOG_INFO, "user %s logged in", user);
865 * Write a wtmp entry for this user.
868 if (strncmp(tty, "/dev/", 5) == 0)
870 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
873 return (UPAP_AUTHACK);
877 * logout - Logout the user.
885 if (strncmp(tty, "/dev/", 5) == 0)
887 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
893 * null_login - Check if a username of "" and a password of "" are
894 * acceptable, and iff so, set the list of acceptable IP addresses
904 struct wordlist *addrs;
905 char secret[MAXWORDLEN];
908 * Open the file of pap secrets and scan for a suitable secret.
909 * We don't accept a wildcard client.
911 filename = _PATH_UPAPFILE;
913 f = fopen(filename, "r");
916 check_access(f, filename);
918 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
919 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
920 BZERO(secret, sizeof(secret));
923 if (addresses[unit] != NULL)
924 free_wordlist(addresses[unit]);
925 addresses[unit] = addrs;
934 * get_pap_passwd - get a password for authenticating ourselves with
935 * our peer using PAP. Returns 1 on success, 0 if no suitable password
939 get_pap_passwd(passwd)
944 struct wordlist *addrs;
945 char secret[MAXWORDLEN];
947 filename = _PATH_UPAPFILE;
949 f = fopen(filename, "r");
952 check_access(f, filename);
953 if (scan_authfile(f, user,
954 remote_name[0]? remote_name: NULL,
955 (u_int32_t)0, secret, NULL, filename) < 0)
957 if (passwd != NULL) {
958 strncpy(passwd, secret, MAXSECRETLEN);
959 passwd[MAXSECRETLEN-1] = 0;
961 BZERO(secret, sizeof(secret));
967 * have_pap_secret - check whether we have a PAP file with any
968 * secrets that we could possibly use for authenticating the peer.
976 ipcp_options *ipwo = &ipcp_wantoptions[0];
979 filename = _PATH_UPAPFILE;
980 f = fopen(filename, "r");
984 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
985 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
995 * have_chap_secret - check whether we have a CHAP file with a
996 * secret that we could possibly use for authenticating `client'
997 * on `server'. Either can be the null string, meaning we don't
998 * know the identity yet.
1001 have_chap_secret(client, server, remote)
1010 filename = _PATH_CHAPFILE;
1011 f = fopen(filename, "r");
1017 else if (server[0] == 0)
1020 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
1030 * get_secret - open the CHAP secret file and return the secret
1031 * for authenticating the given client on the given server.
1032 * (We could be either client or server).
1035 get_secret(unit, client, server, secret, secret_len, save_addrs)
1046 struct wordlist *addrs;
1047 char secbuf[MAXWORDLEN];
1049 filename = _PATH_CHAPFILE;
1053 f = fopen(filename, "r");
1055 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1058 check_access(f, filename);
1060 ret = scan_authfile(f, client, server, (u_int32_t)0,
1061 secbuf, &addrs, filename);
1067 if (addresses[unit] != NULL)
1068 free_wordlist(addresses[unit]);
1069 addresses[unit] = addrs;
1072 len = strlen(secbuf);
1073 if (len > MAXSECRETLEN) {
1074 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1077 BCOPY(secbuf, secret, len);
1078 BZERO(secbuf, sizeof(secbuf));
1085 * auth_ip_addr - check whether the peer is authorized to use
1086 * a given IP address. Returns 1 if authorized, 0 otherwise.
1089 auth_ip_addr(unit, addr)
1093 return ip_addr_check(addr, addresses[unit]);
1097 ip_addr_check(addr, addrs)
1099 struct wordlist *addrs;
1101 u_int32_t a, mask, ah;
1103 char *ptr_word, *ptr_mask;
1107 /* don't allow loopback or multicast address */
1108 if (bad_ip_adrs(addr))
1112 return 1; /* no restriction */
1114 for (; addrs != NULL; addrs = addrs->next) {
1115 /* "-" means no addresses authorized */
1116 ptr_word = addrs->word;
1117 if (strcmp(ptr_word, "-") == 0)
1121 if (*ptr_word == '!') {
1126 mask = ~ (u_int32_t) 0;
1127 ptr_mask = strchr (ptr_word, '/');
1128 if (ptr_mask != NULL) {
1131 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1132 if (bit_count <= 0 || bit_count > 32) {
1133 syslog (LOG_WARNING,
1134 "invalid address length %s in auth. address list",
1139 mask <<= 32 - bit_count;
1142 hp = gethostbyname(ptr_word);
1143 if (hp != NULL && hp->h_addrtype == AF_INET) {
1144 a = *(u_int32_t *)hp->h_addr;
1145 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
1147 np = getnetbyname (ptr_word);
1148 if (np != NULL && np->n_addrtype == AF_INET)
1149 a = htonl (*(u_int32_t *)np->n_net);
1151 a = inet_addr (ptr_word);
1152 if (ptr_mask == NULL) {
1153 /* calculate appropriate mask for net */
1156 mask = IN_CLASSA_NET;
1157 else if (IN_CLASSB(ah))
1158 mask = IN_CLASSB_NET;
1159 else if (IN_CLASSC(ah))
1160 mask = IN_CLASSC_NET;
1164 if (ptr_mask != NULL)
1168 syslog (LOG_WARNING,
1169 "unknown host %s in auth. address list",
1172 if (((addr ^ a) & htonl(mask)) == 0)
1175 return 0; /* not in list => can't have it */
1179 * bad_ip_adrs - return 1 if the IP address is one we don't want
1180 * to use, such as an address in the loopback net or a multicast address.
1181 * addr is in network byte order.
1188 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1189 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1193 * check_access - complain if a secret file has too-liberal permissions.
1196 check_access(f, filename)
1202 if (fstat(fileno(f), &sbuf) < 0) {
1203 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1204 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1205 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1211 * scan_authfile - Scan an authorization file for a secret suitable
1212 * for authenticating `client' on `server'. The return value is -1
1213 * if no secret is found, otherwise >= 0. The return value has
1214 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1215 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1216 * Any following words on the line (i.e. address authorization
1217 * info) are placed in a wordlist and returned in *addrs.
1220 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1226 struct wordlist **addrs;
1230 int got_flag, best_flag;
1232 struct wordlist *ap, *addr_list, *alist, *alast;
1233 char word[MAXWORDLEN];
1234 char atfile[MAXWORDLEN];
1235 char lsecret[MAXWORDLEN];
1240 if (!getword(f, word, &newline, filename))
1241 return -1; /* file is empty??? */
1246 * Skip until we find a word at the start of a line.
1248 while (!newline && getword(f, word, &newline, filename))
1251 break; /* got to end of file */
1254 * Got a client - check if it's a match or a wildcard.
1257 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1262 got_flag = NONWILD_CLIENT;
1265 * Now get a server and check if it matches.
1267 if (!getword(f, word, &newline, filename))
1271 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1274 got_flag |= NONWILD_SERVER;
1277 * Got some sort of a match - see if it's better than what
1280 if (got_flag <= best_flag)
1286 if (!getword(f, word, &newline, filename))
1292 * Special syntax: @filename means read secret from file.
1294 if (word[0] == '@') {
1295 strcpy(atfile, word+1);
1296 if ((sf = fopen(atfile, "r")) == NULL) {
1297 syslog(LOG_WARNING, "can't open indirect secret file %s",
1301 check_access(sf, atfile);
1302 if (!getword(sf, word, &xxx, atfile)) {
1303 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1311 strcpy(lsecret, word);
1314 * Now read address authorization info and make a wordlist.
1316 alist = alast = NULL;
1318 if (!getword(f, word, &newline, filename) || newline)
1320 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1323 novm("authorized addresses");
1325 strcpy(ap->word, word);
1334 * Check if the given IP address is allowed by the wordlist.
1336 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1337 free_wordlist(alist);
1342 * This is the best so far; remember it.
1344 best_flag = got_flag;
1346 free_wordlist(addr_list);
1349 strcpy(secret, lsecret);
1357 else if (addr_list != NULL)
1358 free_wordlist(addr_list);
1364 * free_wordlist - release memory allocated for a wordlist.
1368 struct wordlist *wp;
1370 struct wordlist *next;
1372 while (wp != NULL) {
1380 * auth_script - execute a script with arguments
1381 * interface-name peer-name real-user tty speed
1393 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1394 user_name = pw->pw_name;
1396 sprintf(struid, "%d", getuid());
1399 sprintf(strspeed, "%d", baud_rate);
1403 argv[2] = peer_authname;
1404 argv[3] = user_name;
1409 run_program(script, argv, 0);