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.25 1996/07/01 01:10:24 paulus Exp $";
46 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
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 #if defined(sun) && defined(sparc)
87 /* Used for storing a sequence of words. Usually malloced. */
89 struct wordlist *next;
93 /* Bits in scan_authfile return value */
94 #define NONWILD_SERVER 1
95 #define NONWILD_CLIENT 2
97 #define ISWILD(word) (word[0] == '*' && word[1] == 0)
102 /* The name by which the peer authenticated itself to us. */
103 char peer_authname[MAXNAMELEN];
105 /* Records which authentication operations haven't completed yet. */
106 static int auth_pending[NUM_PPP];
108 /* Set if we have successfully called login() */
109 static int logged_in;
111 /* Set if we have run the /etc/ppp/auth-up script. */
112 static int did_authup;
114 /* List of addresses which the peer may use. */
115 static struct wordlist *addresses[NUM_PPP];
117 /* Number of network protocols which we have opened. */
118 static int num_np_open;
120 /* Number of network protocols which have come up. */
121 static int num_np_up;
123 /* Set if we got the contents of passwd[] from the pap-secrets file. */
124 static int passwd_from_file;
126 /* Bits in auth_pending[] */
127 #define PAP_WITHPEER 1
129 #define CHAP_WITHPEER 4
132 /* Prototypes for procedures local to this file. */
134 static void network_phase __P((int));
135 static void check_idle __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 *));
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 phase = PHASE_TERMINATE;
202 * The link is established.
203 * Proceed to the Dead, Authenticate or Network phase as appropriate.
206 link_established(unit)
210 lcp_options *wo = &lcp_wantoptions[unit];
211 lcp_options *go = &lcp_gotoptions[unit];
212 lcp_options *ho = &lcp_hisoptions[unit];
214 struct protent *protp;
217 * Tell higher-level protocols that LCP is up.
219 for (i = 0; (protp = protocols[i]) != NULL; ++i)
220 if (protp->protocol != PPP_LCP && protp->enabled_flag
221 && protp->lowerup != NULL)
222 (*protp->lowerup)(unit);
224 if (auth_required && !(go->neg_chap || go->neg_upap)) {
226 * We wanted the peer to authenticate itself, and it refused:
227 * treat it as though it authenticated with PAP using a username
228 * of "" and a password of "". If that's not OK, boot it out.
230 if (!wo->neg_upap || !null_login(unit)) {
231 syslog(LOG_WARNING, "peer refused to authenticate");
232 lcp_close(unit, "peer refused to authenticate");
237 phase = PHASE_AUTHENTICATE;
240 ChapAuthPeer(unit, our_name, go->chap_mdtype);
242 } else if (go->neg_upap) {
247 ChapAuthWithPeer(unit, our_name, ho->chap_mdtype);
248 auth |= CHAP_WITHPEER;
249 } else if (ho->neg_upap) {
250 if (passwd[0] == 0) {
251 passwd_from_file = 1;
252 if (!get_pap_passwd(passwd))
253 syslog(LOG_ERR, "No secret found for PAP login");
255 upap_authwithpeer(unit, user, passwd);
256 auth |= PAP_WITHPEER;
258 auth_pending[unit] = auth;
265 * Proceed to the network phase.
272 struct protent *protp;
273 lcp_options *go = &lcp_gotoptions[unit];
276 * If the peer had to authenticate, run the auth-up script now.
278 if ((go->neg_chap || go->neg_upap) && !did_authup) {
279 auth_script(_PATH_AUTHUP);
283 phase = PHASE_NETWORK;
286 set_filters(&pass_filter, &active_filter);
288 for (i = 0; (protp = protocols[i]) != NULL; ++i)
289 if (protp->protocol < 0xC000 && protp->enabled_flag
290 && protp->open != NULL) {
291 (*protp->open)(unit);
292 if (protp->protocol != PPP_CCP)
298 * The peer has failed to authenticate himself using `protocol'.
301 auth_peer_fail(unit, protocol)
305 * Authentication failure: take the link down
307 lcp_close(unit, "Authentication failed");
311 * The peer has been successfully authenticated using `protocol'.
314 auth_peer_success(unit, protocol, name, namelen)
329 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
335 * Save the authenticated name of the peer for later.
337 if (namelen > sizeof(peer_authname) - 1)
338 namelen = sizeof(peer_authname) - 1;
339 BCOPY(name, peer_authname, namelen);
340 peer_authname[namelen] = 0;
343 * If there is no more authentication still to be done,
344 * proceed to the network phase.
346 if ((auth_pending[unit] &= ~bit) == 0)
351 * We have failed to authenticate ourselves to the peer using `protocol'.
354 auth_withpeer_fail(unit, protocol)
357 if (passwd_from_file)
358 BZERO(passwd, MAXSECRETLEN);
360 * We've failed to authenticate ourselves to our peer.
361 * He'll probably take the link down, and there's not much
362 * we can do except wait for that.
367 * We have successfully authenticated ourselves with the peer using `protocol'.
370 auth_withpeer_success(unit, protocol)
380 if (passwd_from_file)
381 BZERO(passwd, MAXSECRETLEN);
385 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x",
391 * If there is no more authentication still being done,
392 * proceed to the network phase.
394 if ((auth_pending[unit] &= ~bit) == 0)
400 * np_up - a network protocol has come up.
406 if (num_np_up == 0 && idle_time_limit > 0) {
407 TIMEOUT(check_idle, NULL, idle_time_limit);
413 * np_down - a network protocol has gone down.
419 if (--num_np_up == 0 && idle_time_limit > 0) {
420 UNTIMEOUT(check_idle, NULL);
425 * np_finished - a network protocol has finished using the link.
428 np_finished(unit, proto)
431 if (--num_np_open <= 0) {
432 /* no further use for the link: shut up shop. */
433 lcp_close(0, "No network protocols running");
438 * check_idle - check whether the link has been idle for long
439 * enough that we can shut it down.
445 struct ppp_idle idle;
448 if (!get_idle_time(0, &idle))
450 itime = MIN(idle.xmit_idle, idle.recv_idle);
451 if (itime >= idle_time_limit) {
452 /* link is idle: shut it down. */
453 syslog(LOG_INFO, "Terminating connection due to lack of activity.");
454 lcp_close(0, "Link inactive");
456 TIMEOUT(check_idle, NULL, idle_time_limit - itime);
461 * auth_check_options - called to check authentication options.
466 lcp_options *wo = &lcp_wantoptions[0];
468 ipcp_options *ipwo = &ipcp_wantoptions[0];
471 /* Default our_name to hostname, and user to our_name */
472 if (our_name[0] == 0 || usehostname)
473 strcpy(our_name, hostname);
475 strcpy(user, our_name);
477 /* If authentication is required, ask peer for CHAP or PAP. */
478 if (auth_required && !wo->neg_chap && !wo->neg_upap) {
484 * Check whether we have appropriate secrets to use
485 * to authenticate the peer.
487 can_auth = wo->neg_upap && (uselogin || have_pap_secret());
488 if (!can_auth && wo->neg_chap) {
489 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
490 can_auth = have_chap_secret(remote_name, our_name, remote);
493 if (auth_required && !can_auth) {
495 pppd: peer authentication required but no suitable secret(s) found\n");
502 * auth_reset - called when LCP is starting negotiations to recheck
503 * authentication options, i.e. whether we have appropriate secrets
504 * to use for authenticating ourselves and/or the peer.
510 lcp_options *go = &lcp_gotoptions[unit];
511 lcp_options *ao = &lcp_allowoptions[0];
512 ipcp_options *ipwo = &ipcp_wantoptions[0];
515 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
516 ao->neg_chap = !refuse_chap
517 && have_chap_secret(our_name, remote_name, (u_int32_t)0);
519 if (go->neg_upap && !uselogin && !have_pap_secret())
522 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
523 if (!have_chap_secret(remote_name, our_name, remote))
531 * check_passwd - Check the user name and passwd against the PAP secrets
532 * file. If requested, also check against the system password database,
533 * and login the user if OK.
536 * UPAP_AUTHNAK: Authentication failed.
537 * UPAP_AUTHACK: Authentication succeeded.
538 * In either case, msg points to an appropriate message.
541 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen)
553 struct wordlist *addrs;
555 ipcp_options *ipwo = &ipcp_wantoptions[unit];
556 char passwd[256], user[256];
557 char secret[MAXWORDLEN];
558 static int attempts = 0;
561 * Make copies of apasswd and auser, then null-terminate them.
563 BCOPY(apasswd, passwd, passwdlen);
564 passwd[passwdlen] = '\0';
565 BCOPY(auser, user, userlen);
566 user[userlen] = '\0';
570 * Open the file of pap secrets and scan for a suitable secret
571 * for authenticating this user.
573 filename = _PATH_UPAPFILE;
576 f = fopen(filename, "r");
579 syslog(LOG_ERR, "Can't open PAP password file %s: %m", filename);
584 check_access(f, filename);
585 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
586 if (scan_authfile(f, user, our_name, remote,
587 secret, &addrs, filename) < 0
588 || (secret[0] != 0 && (cryptpap || strcmp(passwd, secret) != 0)
589 && strcmp(crypt(passwd, secret), secret) != 0)) {
590 syslog(LOG_WARNING, "PAP authentication failure for %s", user);
596 if (uselogin && ret == UPAP_AUTHACK) {
597 ret = login(user, passwd, msg, msglen);
598 if (ret == UPAP_AUTHNAK) {
599 syslog(LOG_WARNING, "PAP login failure for %s", user);
603 if (ret == UPAP_AUTHNAK) {
604 if (*msg == (char *) 0)
605 *msg = "Login incorrect";
606 *msglen = strlen(*msg);
608 * Frustrate passwd stealer programs.
609 * Allow 10 tries, but start backing off after 3 (stolen from login).
610 * On 10'th, drop the connection.
612 if (attempts++ >= 10) {
613 syslog(LOG_WARNING, "%d LOGIN FAILURES ON %s, %s",
614 attempts, devnam, user);
618 sleep((u_int) (attempts - 3) * 5);
620 free_wordlist(addrs);
623 attempts = 0; /* Reset count */
624 if (*msg == (char *) 0)
626 *msglen = strlen(*msg);
627 if (addresses[unit] != NULL)
628 free_wordlist(addresses[unit]);
629 addresses[unit] = addrs;
632 BZERO(passwd, sizeof(passwd));
633 BZERO(secret, sizeof(secret));
640 * This function was lifted from the shadow-3.3.2 version by John Haugh II.
641 * It is included because the function was not in the standard libshadow
642 * library. If it is included in the library then I can remove it from here.
645 #define DAY (24L*3600L)
647 * isexpired - determine if account is expired yet
649 * isexpired calculates the expiration date based on the
650 * password expiration criteria.
661 clock = time ((time_t *) 0) / DAY;
664 * Quick and easy - there is an expired account field
665 * along with an inactive account field. Do the expired
666 * one first since it is worse.
669 if (sp->sp_expire > 0 && sp->sp_expire < clock)
672 if (sp->sp_inact > 0 && sp->sp_lstchg > 0 && sp->sp_max > 0 &&
673 sp->sp_inact + sp->sp_lstchg + sp->sp_max < clock)
677 * The last and max fields must be present for an account
678 * to have an expired password. A maximum of >10000 days
679 * is considered to be infinite.
682 if (sp->sp_lstchg == -1 ||
683 sp->sp_max == -1 || sp->sp_max >= 10000L)
687 * Calculate today's day and the day on which the password
688 * is going to expire. If that date has already passed,
689 * the password has expired.
692 if (sp->sp_lstchg + sp->sp_max < clock)
701 * This function is needed for PAM. However, it should not be called.
702 * If it is, return the error code.
706 static int pam_conv(int num_msg, const struct pam_message **msg,
707 struct pam_response **resp, void *appdata_ptr)
714 * login - Check the user name and password against the system
715 * password database, and login the user if OK.
718 * UPAP_AUTHNAK: Login failed.
719 * UPAP_AUTHACK: Login succeeded.
720 * In either case, msg points to an appropriate message.
724 login(user, passwd, msg, msglen)
733 struct pam_conv pam_conversation;
739 * Fill the pam_conversion structure
741 memset (&pam_conversation, '\0', sizeof (struct pam_conv));
742 pam_conversation.conv = &pam_conv;
744 pam_error = pam_start ("ppp", user, &pam_conversation, &pamh);
745 if (pam_error != PAM_SUCCESS) {
746 *msg = (char *) pam_strerror (pam_error);
750 * Define the fields for the credintial validation
752 (void) pam_set_item (pamh, PAM_AUTHTOK, passwd);
753 (void) pam_set_item (pamh, PAM_TTY, devnam);
757 pam_error = pam_authenticate (pamh, PAM_SILENT);
758 if (pam_error == PAM_SUCCESS)
759 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
761 *msg = (char *) pam_strerror (pam_error);
765 (void) pam_end (pamh, pam_error);
767 if (pam_error != PAM_SUCCESS)
770 * Use the non-PAM methods directly
772 #else /* #ifdef USE_PAM */
779 struct spwd *getspnam();
782 if ((pw = getpwnam(user)) == NULL) {
783 return (UPAP_AUTHNAK);
787 if ((spwd = getspnam(user)) == NULL) {
790 pw->pw_passwd = spwd->sp_pwdp;
795 * XXX If no passwd, let them login without one.
797 if (pw->pw_passwd == '\0') {
798 return (UPAP_AUTHACK);
803 if (pw->pw_passwd[0] == '@') {
804 if (pw_auth (pw->pw_passwd+1, pw->pw_name, PW_PPP, NULL)) {
805 return (UPAP_AUTHNAK);
808 epasswd = pw_encrypt(passwd, pw->pw_passwd);
809 if (strcmp(epasswd, pw->pw_passwd)) {
810 return (UPAP_AUTHNAK);
813 /* check the age of the password entry */
814 if (spwd && (isexpired (pw, spwd) != 0)) {
815 return (UPAP_AUTHNAK);
819 epasswd = crypt(passwd, pw->pw_passwd);
820 if (strcmp(epasswd, pw->pw_passwd)) {
821 return (UPAP_AUTHNAK);
824 #endif /* #ifdef USE_PAM */
826 syslog(LOG_INFO, "user %s logged in", user);
829 * Write a wtmp entry for this user.
832 if (strncmp(tty, "/dev/", 5) == 0)
834 logwtmp(tty, user, remote_name); /* Add wtmp login entry */
837 return (UPAP_AUTHACK);
841 * logout - Logout the user.
849 if (strncmp(tty, "/dev/", 5) == 0)
851 logwtmp(tty, "", ""); /* Wipe out wtmp logout entry */
857 * null_login - Check if a username of "" and a password of "" are
858 * acceptable, and iff so, set the list of acceptable IP addresses
868 struct wordlist *addrs;
869 char secret[MAXWORDLEN];
872 * Open the file of pap secrets and scan for a suitable secret.
873 * We don't accept a wildcard client.
875 filename = _PATH_UPAPFILE;
877 f = fopen(filename, "r");
880 check_access(f, filename);
882 i = scan_authfile(f, "", our_name, (u_int32_t)0, secret, &addrs, filename);
883 ret = i >= 0 && (i & NONWILD_CLIENT) != 0 && secret[0] == 0;
884 BZERO(secret, sizeof(secret));
887 if (addresses[unit] != NULL)
888 free_wordlist(addresses[unit]);
889 addresses[unit] = addrs;
898 * get_pap_passwd - get a password for authenticating ourselves with
899 * our peer using PAP. Returns 1 on success, 0 if no suitable password
903 get_pap_passwd(char *passwd)
907 struct wordlist *addrs;
908 char secret[MAXWORDLEN];
910 filename = _PATH_UPAPFILE;
912 f = fopen(filename, "r");
915 check_access(f, filename);
916 if (scan_authfile(f, user,
917 remote_name[0]? remote_name: NULL,
918 (u_int32_t)0, secret, NULL, filename) < 0)
920 if (passwd != NULL) {
921 strncpy(passwd, secret, MAXSECRETLEN);
922 passwd[MAXSECRETLEN-1] = 0;
924 BZERO(secret, sizeof(secret));
930 * have_pap_secret - check whether we have a PAP file with any
931 * secrets that we could possibly use for authenticating the peer.
939 ipcp_options *ipwo = &ipcp_wantoptions[0];
942 filename = _PATH_UPAPFILE;
943 f = fopen(filename, "r");
947 remote = ipwo->accept_remote? 0: ipwo->hisaddr;
948 ret = scan_authfile(f, NULL, our_name, remote, NULL, NULL, filename);
958 * have_chap_secret - check whether we have a CHAP file with a
959 * secret that we could possibly use for authenticating `client'
960 * on `server'. Either can be the null string, meaning we don't
961 * know the identity yet.
964 have_chap_secret(client, server, remote)
973 filename = _PATH_CHAPFILE;
974 f = fopen(filename, "r");
980 else if (server[0] == 0)
983 ret = scan_authfile(f, client, server, remote, NULL, NULL, filename);
993 * get_secret - open the CHAP secret file and return the secret
994 * for authenticating the given client on the given server.
995 * (We could be either client or server).
998 get_secret(unit, client, server, secret, secret_len, save_addrs)
1009 struct wordlist *addrs;
1010 char secbuf[MAXWORDLEN];
1012 filename = _PATH_CHAPFILE;
1016 f = fopen(filename, "r");
1018 syslog(LOG_ERR, "Can't open chap secret file %s: %m", filename);
1021 check_access(f, filename);
1023 ret = scan_authfile(f, client, server, (u_int32_t)0,
1024 secbuf, &addrs, filename);
1030 if (addresses[unit] != NULL)
1031 free_wordlist(addresses[unit]);
1032 addresses[unit] = addrs;
1035 len = strlen(secbuf);
1036 if (len > MAXSECRETLEN) {
1037 syslog(LOG_ERR, "Secret for %s on %s is too long", client, server);
1040 BCOPY(secbuf, secret, len);
1041 BZERO(secbuf, sizeof(secbuf));
1048 * auth_ip_addr - check whether the peer is authorized to use
1049 * a given IP address. Returns 1 if authorized, 0 otherwise.
1052 auth_ip_addr(unit, addr)
1056 return ip_addr_check(addr, addresses[unit]);
1060 ip_addr_check(addr, addrs)
1062 struct wordlist *addrs;
1064 u_int32_t a, mask, ah;
1066 char *ptr_word, *ptr_mask;
1070 /* don't allow loopback or multicast address */
1071 if (bad_ip_adrs(addr))
1075 return 1; /* no restriction */
1077 for (; addrs != NULL; addrs = addrs->next) {
1078 /* "-" means no addresses authorized */
1079 ptr_word = addrs->word;
1080 if (strcmp(ptr_word, "-") == 0)
1084 if (*ptr_word == '!') {
1089 mask = ~ (u_int32_t) 0;
1090 ptr_mask = strchr (ptr_word, '/');
1091 if (ptr_mask != NULL) {
1094 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10);
1095 if (bit_count <= 0 || bit_count > 32) {
1096 syslog (LOG_WARNING,
1097 "invalid address length %s in auth. address list",
1102 mask <<= 32 - bit_count;
1105 hp = gethostbyname(ptr_word);
1106 if (hp != NULL && hp->h_addrtype == AF_INET) {
1107 a = *(u_int32_t *)hp->h_addr;
1108 mask = ~ (u_int32_t) 0; /* are we sure we want this? */
1110 np = getnetbyname (ptr_word);
1111 if (np != NULL && np->n_addrtype == AF_INET)
1112 a = htonl (*(u_int32_t *)np->n_net);
1114 a = inet_addr (ptr_word);
1115 if (ptr_mask == NULL) {
1116 /* calculate appropriate mask for net */
1119 mask = IN_CLASSA_NET;
1120 else if (IN_CLASSB(ah))
1121 mask = IN_CLASSB_NET;
1122 else if (IN_CLASSC(ah))
1123 mask = IN_CLASSC_NET;
1127 if (ptr_mask != NULL)
1131 syslog (LOG_WARNING,
1132 "unknown host %s in auth. address list",
1135 if (((addr ^ a) & htonl(mask)) == 0)
1138 return 0; /* not in list => can't have it */
1142 * bad_ip_adrs - return 1 if the IP address is one we don't want
1143 * to use, such as an address in the loopback net or a multicast address.
1144 * addr is in network byte order.
1151 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
1152 || IN_MULTICAST(addr) || IN_BADCLASS(addr);
1156 * check_access - complain if a secret file has too-liberal permissions.
1159 check_access(f, filename)
1165 if (fstat(fileno(f), &sbuf) < 0) {
1166 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename);
1167 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
1168 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename);
1174 * scan_authfile - Scan an authorization file for a secret suitable
1175 * for authenticating `client' on `server'. The return value is -1
1176 * if no secret is found, otherwise >= 0. The return value has
1177 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
1178 * NONWILD_SERVER set if the secret didn't have "*" for the server.
1179 * Any following words on the line (i.e. address authorization
1180 * info) are placed in a wordlist and returned in *addrs.
1183 scan_authfile(f, client, server, ipaddr, secret, addrs, filename)
1189 struct wordlist **addrs;
1193 int got_flag, best_flag;
1195 struct wordlist *ap, *addr_list, *alist, *alast;
1196 char word[MAXWORDLEN];
1197 char atfile[MAXWORDLEN];
1198 char lsecret[MAXWORDLEN];
1203 if (!getword(f, word, &newline, filename))
1204 return -1; /* file is empty??? */
1209 * Skip until we find a word at the start of a line.
1211 while (!newline && getword(f, word, &newline, filename))
1214 break; /* got to end of file */
1217 * Got a client - check if it's a match or a wildcard.
1220 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
1225 got_flag = NONWILD_CLIENT;
1228 * Now get a server and check if it matches.
1230 if (!getword(f, word, &newline, filename))
1234 if (server != NULL && strcmp(word, server) != 0 && !ISWILD(word))
1237 got_flag |= NONWILD_SERVER;
1240 * Got some sort of a match - see if it's better than what
1243 if (got_flag <= best_flag)
1249 if (!getword(f, word, &newline, filename))
1255 * Special syntax: @filename means read secret from file.
1257 if (word[0] == '@') {
1258 strcpy(atfile, word+1);
1259 if ((sf = fopen(atfile, "r")) == NULL) {
1260 syslog(LOG_WARNING, "can't open indirect secret file %s",
1264 check_access(sf, atfile);
1265 if (!getword(sf, word, &xxx, atfile)) {
1266 syslog(LOG_WARNING, "no secret in indirect secret file %s",
1274 strcpy(lsecret, word);
1277 * Now read address authorization info and make a wordlist.
1279 alist = alast = NULL;
1281 if (!getword(f, word, &newline, filename) || newline)
1283 ap = (struct wordlist *) malloc(sizeof(struct wordlist)
1286 novm("authorized addresses");
1288 strcpy(ap->word, word);
1297 * Check if the given IP address is allowed by the wordlist.
1299 if (ipaddr != 0 && !ip_addr_check(ipaddr, alist)) {
1300 free_wordlist(alist);
1305 * This is the best so far; remember it.
1307 best_flag = got_flag;
1309 free_wordlist(addr_list);
1312 strcpy(secret, lsecret);
1320 else if (addr_list != NULL)
1321 free_wordlist(addr_list);
1327 * free_wordlist - release memory allocated for a wordlist.
1331 struct wordlist *wp;
1333 struct wordlist *next;
1335 while (wp != NULL) {
1343 * auth_script - execute a script with arguments
1344 * interface-name peer-name real-user tty speed
1356 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
1357 user_name = pw->pw_name;
1359 sprintf(struid, "%d", getuid());
1362 sprintf(strspeed, "%d", baud_rate);
1366 argv[2] = peer_authname;
1367 argv[3] = user_name;
1372 run_program(script, argv, 0);