2 * Copyright (c) 1998-2005 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: daemon.c,v 8.658 2005/02/02 18:19:28 ca Exp $")
18 #if defined(SOCK_STREAM) || defined(__GNU_LIBRARY__)
19 # define USE_SOCK_STREAM 1
20 #endif /* defined(SOCK_STREAM) || defined(__GNU_LIBRARY__) */
22 #if defined(USE_SOCK_STREAM)
23 # if NETINET || NETINET6
24 # include <arpa/inet.h>
25 # endif /* NETINET || NETINET6 */
28 # define NO_DATA NO_ADDRESS
29 # endif /* ! NO_DATA */
30 # endif /* NAMED_BIND */
31 #endif /* defined(USE_SOCK_STREAM) */
34 # include <openssl/rand.h>
39 #if IP_SRCROUTE && NETINET
40 # include <netinet/in_systm.h>
41 # include <netinet/ip.h>
43 # include <netinet/in.h>
45 # define IPOPTION ip_opts
46 # define IP_LIST ip_opts
47 # define IP_DST ip_dst
48 # endif /* ! IPOPTION */
50 # include <netinet/ip_var.h>
52 # define IPOPTION ipoption
53 # define IP_LIST ipopt_list
54 # define IP_DST ipopt_dst
55 # endif /* ! IPOPTION */
56 # endif /* HAS_IN_H */
57 #endif /* IP_SRCROUTE && NETINET */
61 /* structure to describe a daemon or a client */
64 int d_socket; /* fd for socket */
65 SOCKADDR d_addr; /* socket for incoming */
66 unsigned short d_port; /* port number */
67 int d_listenqueue; /* size of listen queue */
68 int d_tcprcvbufsize; /* size of TCP receive buffer */
69 int d_tcpsndbufsize; /* size of TCP send buffer */
70 time_t d_refuse_connections_until;
73 BITMAP256 d_flags; /* flags; see sendmail.h */
74 char *d_mflags; /* flags for use in macro */
75 char *d_name; /* user-supplied name */
77 char *d_inputfilterlist;
78 struct milter *d_inputfilters[MAXFILTERS];
80 #if _FFR_SS_PER_DAEMON
82 #endif /* _FFR_SS_PER_DAEMON */
83 #if _FFR_DM_PER_DAEMON
84 int d_dm; /* DeliveryMode */
85 #endif /* _FFR_DM_PER_DAEMON */
88 typedef struct daemon DAEMON_T;
90 #define SAFE_NOTSET (-1) /* SuperSafe (per daemon) option not set */
91 /* see also sendmail.h: SuperSafe values */
93 #define DM_NOTSET (-1) /* DeliveryMode (per daemon) option not set */
94 /* see also sendmail.h: values for e_sendmode -- send modes */
96 static void connecttimeout __P((int));
97 static int opendaemonsocket __P((DAEMON_T *, bool));
98 static unsigned short setupdaemon __P((SOCKADDR *));
99 static void getrequests_checkdiskspace __P((ENVELOPE *e));
100 static void setsockaddroptions __P((char *, DAEMON_T *));
101 static void printdaemonflags __P((DAEMON_T *));
102 static int addr_family __P((char *));
103 static int addrcmp __P((struct hostent *, char *, SOCKADDR *));
104 static void authtimeout __P((int));
107 ** DAEMON.C -- routines to use when running as a daemon.
109 ** This entire file is highly dependent on the 4.2 BSD
110 ** interprocess communication primitives. No attempt has
111 ** been made to make this file portable to Version 7,
112 ** Version 6, MPX files, etc. If you should try such a
113 ** thing yourself, I recommend chucking the entire file
114 ** and starting from scratch. Basic semantics are:
117 ** Opens a port and initiates a connection.
118 ** Returns in a child. Must set InChannel and
119 ** OutChannel appropriately.
121 ** Close any open files associated with getting
122 ** the connection; this is used when running the queue,
123 ** etc., to avoid having extra file descriptors during
124 ** the queue run and to avoid confusing the network
125 ** code (if it cares).
126 ** makeconnection(host, port, mci, e, enough)
127 ** Make a connection to the named host on the given
128 ** port. Returns zero on success, else an exit status
129 ** describing the error.
130 ** host_map_lookup(map, hbuf, avp, pstat)
131 ** Convert the entry in hbuf into a canonical form.
134 static DAEMON_T Daemons[MAXDAEMONS];
135 static int NDaemons = 0; /* actual number of daemons */
137 static time_t NextDiskSpaceCheck = 0;
140 ** GETREQUESTS -- open mail IPC port and get requests.
143 ** e -- the current envelope.
149 ** Waits until some interesting activity occurs. When
150 ** it does, a child is created to process it, and the
151 ** parent waits for completion. Return from this
152 ** routine is always in the child. The file pointers
153 ** "InChannel" and "OutChannel" should be set to point
154 ** to the communication channel.
155 ** May restart persistent queue runners if they have ended
164 int idx, curdaemon = -1;
165 int i, olddaemon = 0;
169 char status[MAXLINE];
171 SOCKADDR_LEN_T len = sizeof sa;
172 #if _FFR_QUEUE_RUN_PARANOIA
174 #endif /* _FFR_QUEUE_RUN_PARANOIA */
176 extern int ControlSocket;
177 # endif /* NETUNIX */
178 extern ENVELOPE BlankEnvelope;
179 extern bool refuseconnections __P((char *, ENVELOPE *, int, bool));
182 /* initialize data for function that generates queue ids */
184 for (idx = 0; idx < NDaemons; idx++)
186 Daemons[idx].d_port = setupdaemon(&(Daemons[idx].d_addr));
187 Daemons[idx].d_firsttime = true;
188 Daemons[idx].d_refuse_connections_until = (time_t) 0;
192 ** Try to actually open the connection.
197 for (idx = 0; idx < NDaemons; idx++)
199 sm_dprintf("getrequests: daemon %s: port %d\n",
201 ntohs(Daemons[idx].d_port));
205 /* get a socket for the SMTP connection */
206 for (idx = 0; idx < NDaemons; idx++)
207 Daemons[idx].d_socksize = opendaemonsocket(&Daemons[idx], true);
209 if (opencontrolsocket() < 0)
210 sm_syslog(LOG_WARNING, NOQID,
211 "daemon could not open control socket %s: %s",
212 ControlSocketName, sm_errstring(errno));
214 /* If there are any queue runners released reapchild() co-ord's */
215 (void) sm_signal(SIGCHLD, reapchild);
217 /* write the pid to file, command line args to syslog */
222 char jbuf[MAXHOSTNAMELEN];
224 expand("\201j", jbuf, sizeof jbuf, e);
225 j_has_dot = strchr(jbuf, '.') != NULL;
229 /* Add parent process as first item */
230 proc_list_add(CurrentPid, "Sendmail daemon", PROC_DAEMON, 0, -1, NULL);
234 for (idx = 0; idx < NDaemons; idx++)
235 sm_dprintf("getrequests: daemon %s: %d\n",
237 Daemons[idx].d_socket);
243 auto SOCKADDR_LEN_T lotherend;
244 bool timedout = false;
245 bool control = false;
251 #endif /* STARTTLS */
253 /* see if we are rejecting connections */
254 (void) sm_blocksignal(SIGALRM);
257 for (idx = 0; idx < NDaemons; idx++)
260 ** XXX do this call outside the loop?
261 ** no: refuse_connections may sleep().
265 if (now < Daemons[idx].d_refuse_connections_until)
267 if (bitnset(D_DISABLE, Daemons[idx].d_flags))
269 if (refuseconnections(Daemons[idx].d_name, e, idx,
272 if (Daemons[idx].d_socket >= 0)
274 /* close socket so peer fails quickly */
275 (void) close(Daemons[idx].d_socket);
276 Daemons[idx].d_socket = -1;
279 /* refuse connections for next 15 seconds */
280 Daemons[idx].d_refuse_connections_until = now + 15;
282 else if (Daemons[idx].d_socket < 0 ||
283 Daemons[idx].d_firsttime)
285 if (!Daemons[idx].d_firsttime && LogLevel > 8)
286 sm_syslog(LOG_INFO, NOQID,
287 "accepting connections again for daemon %s",
288 Daemons[idx].d_name);
290 /* arrange to (re)open the socket if needed */
291 (void) opendaemonsocket(&Daemons[idx], false);
292 Daemons[idx].d_firsttime = false;
296 /* May have been sleeping above, check again */
299 getrequests_checkdiskspace(e);
302 /* check for disaster */
304 char jbuf[MAXHOSTNAMELEN];
306 expand("\201j", jbuf, sizeof jbuf, e);
307 if (!wordinclass(jbuf, 'w'))
309 dumpstate("daemon lost $j");
310 sm_syslog(LOG_ALERT, NOQID,
311 "daemon process doesn't have $j in $=w; see syslog");
314 else if (j_has_dot && strchr(jbuf, '.') == NULL)
316 dumpstate("daemon $j lost dot");
317 sm_syslog(LOG_ALERT, NOQID,
318 "daemon process $j lost dot; see syslog");
326 ** Andrew Sun <asun@ieps-sun.ml.com> claims that this will
327 ** fix the SVr4 problem. But it seems to have gone away,
328 ** so is it worth doing this?
331 if (DaemonSocket >= 0 &&
332 SetNonBlocking(DaemonSocket, false) < 0)
335 (void) sm_releasesignal(SIGALRM);
339 bool setproc = false;
342 struct timeval timeout;
346 for (idx = 0; idx < NDaemons; idx++)
348 /* wait for a connection */
349 if (Daemons[idx].d_socket >= 0)
353 Daemons[idx].d_flags))
355 sm_setproctitle(true, e,
356 "accepting connections");
359 if (Daemons[idx].d_socket > highest)
360 highest = Daemons[idx].d_socket;
361 SM_FD_SET(Daemons[idx].d_socket,
367 if (ControlSocket >= 0)
369 if (ControlSocket > highest)
370 highest = ControlSocket;
371 SM_FD_SET(ControlSocket, &readfds);
378 t = select(highest + 1, FDSET_CAST &readfds,
379 NULL, NULL, &timeout);
381 /* Did someone signal while waiting? */
387 (void) runqueue(true, false, false, false);
388 #if _FFR_QUEUE_RUN_PARANOIA
390 #endif /* _FFR_QUEUE_RUN_PARANOIA */
392 #if _FFR_QUEUE_RUN_PARANOIA
393 else if (QueueIntvl > 0 &&
394 lastrun + QueueIntvl + 60 < now)
398 ** set lastrun unconditionally to avoid
399 ** calling checkqueuerunner() all the time.
400 ** That's also why we currently ignore the
401 ** result of the function call.
404 (void) checkqueuerunner();
407 #endif /* _FFR_QUEUE_RUN_PARANOIA */
418 /* look "round-robin" for an active socket */
419 if ((idx = olddaemon + 1) >= NDaemons)
421 for (i = 0; i < NDaemons; i++)
423 if (Daemons[idx].d_socket >= 0 &&
424 SM_FD_ISSET(Daemons[idx].d_socket,
427 lotherend = Daemons[idx].d_socksize;
428 memset(&RealHostAddr, '\0',
429 sizeof RealHostAddr);
430 t = accept(Daemons[idx].d_socket,
431 (struct sockaddr *)&RealHostAddr,
435 ** If remote side closes before
436 ** accept() finishes, sockaddr
437 ** might not be fully filled in.
442 # ifdef BSD4_4_SOCKADDR
443 RealHostAddr.sa.sa_len == 0 ||
444 # endif /* BSD4_4_SOCKADDR */
445 RealHostAddr.sa.sa_family != Daemons[idx].d_addr.sa.sa_family))
451 olddaemon = curdaemon = idx;
454 if (++idx >= NDaemons)
458 if (curdaemon == -1 && ControlSocket >= 0 &&
459 SM_FD_ISSET(ControlSocket, &readfds))
461 struct sockaddr_un sa_un;
463 lotherend = sizeof sa_un;
464 memset(&sa_un, '\0', sizeof sa_un);
465 t = accept(ControlSocket,
466 (struct sockaddr *)&sa_un,
470 ** If remote side closes before
471 ** accept() finishes, sockaddr
472 ** might not be fully filled in.
477 # ifdef BSD4_4_SOCKADDR
478 sa_un.sun_len == 0 ||
479 # endif /* BSD4_4_SOCKADDR */
480 sa_un.sun_family != AF_UNIX))
492 /* No daemon to service */
496 if (t >= 0 || errno != EINTR)
505 (void) sm_blocksignal(SIGALRM);
510 /* let's ignore these temporary errors */
511 if (save_errno == EINTR
513 || save_errno == EAGAIN
516 || save_errno == ECONNABORTED
517 #endif /* ECONNABORTED */
519 || save_errno == EWOULDBLOCK
520 #endif /* EWOULDBLOCK */
524 syserr("getrequests: accept");
526 /* arrange to re-open the socket next time around */
527 (void) close(Daemons[curdaemon].d_socket);
528 Daemons[curdaemon].d_socket = -1;
529 #if SO_REUSEADDR_IS_BROKEN
531 ** Give time for bound socket to be released.
532 ** This creates a denial-of-service if you can
533 ** force accept() to fail on affected systems.
536 Daemons[curdaemon].d_refuse_connections_until = curtime() + 15;
537 #endif /* SO_REUSEADDR_IS_BROKEN */
543 /* set some daemon related macros */
544 switch (Daemons[curdaemon].d_addr.sa.sa_family)
547 macdefine(&BlankEnvelope.e_macro, A_PERM,
548 macid("{daemon_family}"), "unspec");
550 #if _FFR_DAEMON_NETUNIX
553 macdefine(&BlankEnvelope.e_macro, A_PERM,
554 macid("{daemon_family}"), "local");
556 # endif /* NETUNIX */
557 #endif /* _FFR_DAEMON_NETUNIX */
560 macdefine(&BlankEnvelope.e_macro, A_PERM,
561 macid("{daemon_family}"), "inet");
566 macdefine(&BlankEnvelope.e_macro, A_PERM,
567 macid("{daemon_family}"), "inet6");
569 #endif /* NETINET6 */
572 macdefine(&BlankEnvelope.e_macro, A_PERM,
573 macid("{daemon_family}"), "iso");
578 macdefine(&BlankEnvelope.e_macro, A_PERM,
579 macid("{daemon_family}"), "ns");
584 macdefine(&BlankEnvelope.e_macro, A_PERM,
585 macid("{daemon_family}"), "x.25");
589 macdefine(&BlankEnvelope.e_macro, A_PERM,
590 macid("{daemon_name}"),
591 Daemons[curdaemon].d_name);
592 if (Daemons[curdaemon].d_mflags != NULL)
593 macdefine(&BlankEnvelope.e_macro, A_PERM,
594 macid("{daemon_flags}"),
595 Daemons[curdaemon].d_mflags);
597 macdefine(&BlankEnvelope.e_macro, A_PERM,
598 macid("{daemon_flags}"), "");
602 ** If connection rate is exceeded here, connection shall be
603 ** refused later by a new call after fork() by the
604 ** validate_connection() function. Closing the connection
605 ** at this point violates RFC 2821.
606 ** Do NOT remove this call, its side effects are needed.
609 connection_rate_check(&RealHostAddr, NULL);
612 ** Create a subprocess to process the mail.
616 sm_dprintf("getrequests: forking (fd = %d)\n", t);
619 ** Advance state of PRNG.
620 ** This is necessary because otherwise all child processes
621 ** will produce the same PRN sequence and hence the selection
622 ** of a queue directory (and other things, e.g., MX selection)
623 ** are not "really" random.
626 /* XXX get some better "random" data? */
628 RAND_seed((void *) &NextDiskSpaceCheck,
629 sizeof NextDiskSpaceCheck);
630 RAND_seed((void *) &now, sizeof now);
631 RAND_seed((void *) &seed, sizeof seed);
634 #endif /* STARTTLS */
638 ** Update MX records for FallbackMX.
639 ** Let's hope this is fast otherwise we screw up the
643 if (FallbackMX != NULL)
644 (void) getfallbackmxrr(FallbackMX);
645 #endif /* NAMED_BIND */
649 /* don't fork, handle connection in this process */
651 pipefd[0] = pipefd[1] = -1;
656 ** Create a pipe to keep the child from writing to
657 ** the socket until after the parent has closed
658 ** it. Otherwise the parent may hang if the child
659 ** has closed it first.
662 if (pipe(pipefd) < 0)
663 pipefd[0] = pipefd[1] = -1;
665 (void) sm_blocksignal(SIGCHLD);
669 syserr("daemon: cannot fork");
672 (void) close(pipefd[0]);
673 (void) close(pipefd[1]);
675 (void) sm_releasesignal(SIGCHLD);
685 SM_FILE_T *inchannel, *outchannel = NULL;
688 ** CHILD -- return to caller.
689 ** Collect verified idea of sending host.
690 ** Verify calling user id if possible here.
693 /* Reset global flags */
694 RestartRequest = NULL;
695 RestartWorkGroup = false;
696 ShutdownRequest = NULL;
698 CurrentPid = getpid();
699 close_sendmail_pid();
701 (void) sm_releasesignal(SIGALRM);
702 (void) sm_releasesignal(SIGCHLD);
703 (void) sm_signal(SIGCHLD, SIG_DFL);
704 (void) sm_signal(SIGHUP, SIG_DFL);
705 (void) sm_signal(SIGTERM, intsig);
707 /* turn on profiling */
711 ** Initialize exception stack and default exception
712 ** handler for child process.
715 sm_exc_newthread(fatal_error);
719 macdefine(&BlankEnvelope.e_macro, A_TEMP,
720 macid("{daemon_addr}"),
721 anynet_ntoa(&Daemons[curdaemon].d_addr));
722 (void) sm_snprintf(status, sizeof status, "%d",
723 ntohs(Daemons[curdaemon].d_port));
724 macdefine(&BlankEnvelope.e_macro, A_TEMP,
725 macid("{daemon_port}"), status);
728 for (idx = 0; idx < NDaemons; idx++)
730 if (Daemons[idx].d_socket >= 0)
731 (void) close(Daemons[idx].d_socket);
732 Daemons[idx].d_socket = -1;
736 /* Avoid SMTP daemon actions if control command */
739 /* Add control socket process */
740 proc_list_add(CurrentPid,
741 "console socket child",
742 PROC_CONTROL_CHILD, 0, -1, NULL);
748 /* clean up background delivery children */
749 (void) sm_signal(SIGCHLD, reapchild);
751 /* Add parent process as first child item */
752 proc_list_add(CurrentPid, "daemon child",
753 PROC_DAEMON_CHILD, 0, -1, NULL);
755 /* don't schedule queue runs if ETRN */
757 #if _FFR_SS_PER_DAEMON
758 if (Daemons[curdaemon].d_supersafe !=
760 SuperSafe = Daemons[curdaemon].d_supersafe;
761 #endif /* _FFR_SS_PER_DAEMON */
762 #if _FFR_DM_PER_DAEMON
763 if (Daemons[curdaemon].d_dm != DM_NOTSET)
765 Daemons[curdaemon].d_dm, e);
766 #endif /* _FFR_DM_PER_DAEMON */
769 sm_setproctitle(true, e, "startup with %s",
770 anynet_ntoa(&RealHostAddr));
778 ** Wait for the parent to close the write end
779 ** of the pipe, which we will see as an EOF.
780 ** This guarantees that we won't write to the
781 ** socket until after the parent has closed
785 /* close the write end of the pipe */
786 (void) close(pipefd[1]);
788 /* we shouldn't be interrupted, but ... */
789 while (read(pipefd[0], &c, 1) < 0 &&
792 (void) close(pipefd[0]);
795 /* control socket processing */
798 control_command(t, e);
803 /* determine host name */
804 p = hostnamebyanyaddr(&RealHostAddr);
805 if (strlen(p) > MAXNAME) /* XXX - 1 ? */
807 RealHostName = newstr(p);
808 if (RealHostName[0] == '[')
810 macdefine(&BlankEnvelope.e_macro, A_PERM,
811 macid("{client_resolve}"),
812 h_errno == TRY_AGAIN ? "TEMP" : "FAIL");
816 macdefine(&BlankEnvelope.e_macro, A_PERM,
817 macid("{client_resolve}"), "OK");
819 sm_setproctitle(true, e, "startup with %s", p);
820 markstats(e, NULL, STATS_CONNECT);
822 if ((inchannel = sm_io_open(SmFtStdiofd,
828 (outchannel = sm_io_open(SmFtStdiofd,
834 syserr("cannot open SMTP server channel, fd=%d",
836 finis(false, true, EX_OK);
838 sm_io_automode(inchannel, outchannel);
840 InChannel = inchannel;
841 OutChannel = outchannel;
842 DisConnected = false;
845 if (!xla_host_ok(RealHostName))
847 message("421 4.4.5 Too many SMTP sessions for this host");
848 finis(false, true, EX_OK);
851 /* find out name for interface of connection */
852 if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
853 NULL), &sa.sa, &len) == 0)
855 p = hostnamebyanyaddr(&sa);
857 sm_dprintf("getreq: got name %s\n", p);
858 macdefine(&BlankEnvelope.e_macro, A_TEMP,
859 macid("{if_name}"), p);
862 ** Do this only if it is not the loopback
871 addr = anynet_ntoa(&sa);
872 (void) sm_snprintf(family,
874 "%d", sa.sa.sa_family);
875 macdefine(&BlankEnvelope.e_macro,
877 macid("{if_addr}"), addr);
878 macdefine(&BlankEnvelope.e_macro,
880 macid("{if_family}"), family);
882 sm_dprintf("getreq: got addr %s and family %s\n",
887 macdefine(&BlankEnvelope.e_macro,
889 macid("{if_addr}"), NULL);
890 macdefine(&BlankEnvelope.e_macro,
892 macid("{if_family}"), NULL);
898 sm_dprintf("getreq: getsockname failed\n");
899 macdefine(&BlankEnvelope.e_macro, A_PERM,
900 macid("{if_name}"), NULL);
901 macdefine(&BlankEnvelope.e_macro, A_PERM,
902 macid("{if_addr}"), NULL);
903 macdefine(&BlankEnvelope.e_macro, A_PERM,
904 macid("{if_family}"), NULL);
909 /* parent -- keep track of children */
912 (void) sm_snprintf(status, sizeof status,
913 "control socket server child");
914 proc_list_add(pid, status, PROC_CONTROL, 0, -1, NULL);
918 (void) sm_snprintf(status, sizeof status,
919 "SMTP server child for %s",
920 anynet_ntoa(&RealHostAddr));
921 proc_list_add(pid, status, PROC_DAEMON, 0, -1,
924 (void) sm_releasesignal(SIGCHLD);
926 /* close the read end of the synchronization pipe */
929 (void) close(pipefd[0]);
933 /* close the port so that others will hang (for a while) */
936 /* release the child by closing the read end of the sync pipe */
939 (void) close(pipefd[1]);
944 sm_dprintf("getreq: returning\n");
947 /* set the filters for this daemon */
948 if (Daemons[curdaemon].d_inputfilterlist != NULL)
952 Daemons[curdaemon].d_inputfilters[i] != NULL);
955 InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
958 InputFilters[i] = NULL;
961 return &Daemons[curdaemon].d_flags;
965 ** GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
974 ** Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
978 getrequests_checkdiskspace(e)
986 if (now < NextDiskSpaceCheck)
989 /* Check if there is available disk space in all queue groups. */
990 if (!enoughdiskspace(0, NULL))
992 for (idx = 0; idx < NDaemons; ++idx)
994 if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
997 /* log only if not logged before */
1001 sm_syslog(LOG_INFO, NOQID,
1002 "rejecting new messages: min free: %ld",
1004 sm_setproctitle(true, e,
1005 "rejecting new messages: min free: %ld",
1009 setbitn(D_ETRNONLY, Daemons[idx].d_flags);
1014 for (idx = 0; idx < NDaemons; ++idx)
1016 if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
1019 /* log only if not logged before */
1023 sm_syslog(LOG_INFO, NOQID,
1024 "accepting new messages (again)");
1028 /* title will be set later */
1029 clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
1033 /* only check disk space once a minute */
1034 NextDiskSpaceCheck = now + 60;
1038 ** OPENDAEMONSOCKET -- open SMTP socket
1040 ** Deals with setting all appropriate options.
1043 ** d -- the structure for the daemon to open.
1044 ** firsttime -- set if this is the initial open.
1047 ** Size in bytes of the daemon socket addr.
1050 ** Leaves DaemonSocket set to the open socket.
1051 ** Exits if the socket cannot be created.
1054 #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
1057 opendaemonsocket(d, firsttime)
1063 SOCKADDR_LEN_T socksize = 0;
1068 sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
1074 if (firsttime || d->d_socket < 0)
1076 #if _FFR_DAEMON_NETUNIX
1078 if (d->d_addr.sa.sa_family == AF_UNIX)
1081 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
1083 /* if not safe, don't use it */
1084 rval = safefile(d->d_addr.sunix.sun_path,
1087 S_IRUSR|S_IWUSR, NULL);
1091 syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
1093 d->d_addr.sunix.sun_path);
1097 /* Don't try to overtake an existing socket */
1098 (void) unlink(d->d_addr.sunix.sun_path);
1100 # endif /* NETUNIX */
1101 #endif /* _FFR_DOMAIN_NETUNIX */
1102 d->d_socket = socket(d->d_addr.sa.sa_family,
1104 if (d->d_socket < 0)
1107 syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
1110 if (bitnset(D_OPTIONAL, d->d_flags) &&
1111 (!transienterror(save_errno) ||
1112 ntries >= MAXOPENTRIES - 1))
1114 syserr("opendaemonsocket: daemon %s: optional socket disabled",
1116 setbitn(D_DISABLE, d->d_flags);
1122 sm_syslog(LOG_ALERT, NOQID,
1123 "daemon %s: problem creating SMTP socket",
1129 if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
1131 save_errno = EINVAL;
1132 syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
1133 d->d_name, d->d_socket);
1137 /* turn on network debugging? */
1139 (void) setsockopt(d->d_socket, SOL_SOCKET,
1140 SO_DEBUG, (char *)&on,
1143 (void) setsockopt(d->d_socket, SOL_SOCKET,
1144 SO_REUSEADDR, (char *)&on, sizeof on);
1145 (void) setsockopt(d->d_socket, SOL_SOCKET,
1146 SO_KEEPALIVE, (char *)&on, sizeof on);
1149 if (d->d_tcprcvbufsize > 0)
1151 if (setsockopt(d->d_socket, SOL_SOCKET,
1153 (char *) &d->d_tcprcvbufsize,
1154 sizeof(d->d_tcprcvbufsize)) < 0)
1155 syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
1157 #endif /* SO_RCVBUF */
1159 if (d->d_tcpsndbufsize > 0)
1161 if (setsockopt(d->d_socket, SOL_SOCKET,
1163 (char *) &d->d_tcpsndbufsize,
1164 sizeof(d->d_tcpsndbufsize)) < 0)
1165 syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
1167 #endif /* SO_SNDBUF */
1169 if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
1170 fcntl(d->d_socket, F_SETFD,
1171 fdflags | FD_CLOEXEC) == -1)
1174 syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
1176 fdflags == -1 ? "get" : "set",
1177 sm_errstring(save_errno));
1178 (void) close(d->d_socket);
1182 switch (d->d_addr.sa.sa_family)
1184 #if _FFR_DAEMON_NETUNIX
1187 socksize = sizeof d->d_addr.sunix;
1189 # endif /* NETUNIX */
1190 #endif /* _FFR_DAEMON_NETUNIX */
1193 socksize = sizeof d->d_addr.sin;
1195 #endif /* NETINET */
1199 socksize = sizeof d->d_addr.sin6;
1201 #endif /* NETINET6 */
1205 socksize = sizeof d->d_addr.siso;
1210 socksize = sizeof d->d_addr;
1214 if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
1216 /* probably another daemon already */
1218 syserr("opendaemonsocket: daemon %s: cannot bind",
1220 (void) close(d->d_socket);
1225 listen(d->d_socket, d->d_listenqueue) < 0)
1228 syserr("opendaemonsocket: daemon %s: cannot listen",
1230 (void) close(d->d_socket);
1234 } while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
1235 syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
1238 return -1; /* avoid compiler warning on IRIX */
1241 ** SETUPDAEMON -- setup socket for daemon
1244 ** daemonaddr -- socket for daemon
1247 ** port number on which daemon should run
1251 static unsigned short
1252 setupdaemon(daemonaddr)
1253 SOCKADDR *daemonaddr;
1255 unsigned short port;
1258 ** Set up the address for the mailer.
1261 if (daemonaddr->sa.sa_family == AF_UNSPEC)
1263 memset(daemonaddr, '\0', sizeof *daemonaddr);
1265 daemonaddr->sa.sa_family = AF_INET;
1266 #endif /* NETINET */
1269 switch (daemonaddr->sa.sa_family)
1273 if (daemonaddr->sin.sin_addr.s_addr == 0)
1274 daemonaddr->sin.sin_addr.s_addr = INADDR_ANY;
1275 port = daemonaddr->sin.sin_port;
1277 #endif /* NETINET */
1281 if (IN6_IS_ADDR_UNSPECIFIED(&daemonaddr->sin6.sin6_addr))
1282 daemonaddr->sin6.sin6_addr = in6addr_any;
1283 port = daemonaddr->sin6.sin6_port;
1285 #endif /* NETINET6 */
1288 /* unknown protocol */
1294 #ifdef NO_GETSERVBYNAME
1296 #else /* NO_GETSERVBYNAME */
1298 register struct servent *sp;
1300 sp = getservbyname("smtp", "tcp");
1303 syserr("554 5.3.5 service \"smtp\" unknown");
1309 #endif /* NO_GETSERVBYNAME */
1312 switch (daemonaddr->sa.sa_family)
1316 daemonaddr->sin.sin_port = port;
1318 #endif /* NETINET */
1322 daemonaddr->sin6.sin6_port = port;
1324 #endif /* NETINET6 */
1327 /* unknown protocol */
1333 ** CLRDAEMON -- reset the daemon connection
1342 ** releases any resources used by the passive daemon.
1350 for (i = 0; i < NDaemons; i++)
1352 if (Daemons[i].d_socket >= 0)
1353 (void) close(Daemons[i].d_socket);
1354 Daemons[i].d_socket = -1;
1359 ** GETMODIFIERS -- get modifier flags
1362 ** v -- the modifiers (input text line).
1363 ** modifiers -- pointer to flag field to represent modifiers.
1366 ** (xallocat()ed) string representation of modifiers.
1369 ** fills in modifiers.
1373 getmodifiers(v, modifiers)
1375 BITMAP256 modifiers;
1378 char *h, *f, *flags;
1380 /* maximum length of flags: upper case Option -> "OO " */
1381 l = 3 * strlen(v) + 3;
1383 /* is someone joking? */
1384 if (l < 0 || l > 256)
1387 sm_syslog(LOG_ERR, NOQID,
1388 "getmodifiers too long, ignored");
1393 clrbitmap(modifiers);
1394 for (h = v; *h != '\0'; h++)
1396 if (isascii(*h) && !isspace(*h) && isprint(*h))
1398 setbitn(*h, modifiers);
1411 ** CHKDAEMONMODIFIERS -- check whether all daemons have set a flag.
1414 ** flag -- the flag to test.
1417 ** true iff all daemons have set flag.
1421 chkdaemonmodifiers(flag)
1426 for (i = 0; i < NDaemons; i++)
1427 if (!bitnset((char) flag, Daemons[i].d_flags))
1433 ** SETSOCKADDROPTIONS -- set options for SOCKADDR (daemon or client)
1436 ** p -- the options line.
1437 ** d -- the daemon structure to fill in.
1444 setsockaddroptions(p, d)
1455 if (d->d_addr.sa.sa_family == AF_UNSPEC)
1456 d->d_addr.sa.sa_family = AF_INET;
1457 #endif /* NETINET */
1464 while (isascii(*p) && isspace(*p))
1475 while (isascii(*++v) && isspace(*v))
1477 if (isascii(*f) && islower(*f))
1479 #if _FFR_SS_PER_DAEMON
1480 d->d_supersafe = SAFE_NOTSET;
1481 #endif /* _FFR_SS_PER_DAEMON */
1482 #if _FFR_DM_PER_DAEMON
1483 d->d_dm = DM_NOTSET;
1484 #endif /* _FFR_DM_PER_DAEMON */
1488 case 'A': /* address */
1492 #if _FFR_DM_PER_DAEMON
1493 case 'D': /* DeliveryMode */
1503 syserr("554 5.3.5 Unknown delivery mode %c",
1508 #endif /* _FFR_DM_PER_DAEMON */
1510 case 'F': /* address family */
1511 if (isascii(*v) && isdigit(*v))
1512 d->d_addr.sa.sa_family = atoi(v);
1513 #if _FFR_DAEMON_NETUNIX
1515 else if (sm_strcasecmp(v, "unix") == 0 ||
1516 sm_strcasecmp(v, "local") == 0)
1517 d->d_addr.sa.sa_family = AF_UNIX;
1518 # endif /* NETUNIX */
1519 #endif /* _FFR_DAEMON_NETUNIX */
1521 else if (sm_strcasecmp(v, "inet") == 0)
1522 d->d_addr.sa.sa_family = AF_INET;
1523 #endif /* NETINET */
1525 else if (sm_strcasecmp(v, "inet6") == 0)
1526 d->d_addr.sa.sa_family = AF_INET6;
1527 #endif /* NETINET6 */
1529 else if (sm_strcasecmp(v, "iso") == 0)
1530 d->d_addr.sa.sa_family = AF_ISO;
1533 else if (sm_strcasecmp(v, "ns") == 0)
1534 d->d_addr.sa.sa_family = AF_NS;
1537 else if (sm_strcasecmp(v, "x.25") == 0)
1538 d->d_addr.sa.sa_family = AF_CCITT;
1541 syserr("554 5.3.5 Unknown address family %s in Family=option",
1547 d->d_inputfilterlist = v;
1551 case 'L': /* listen queue size */
1552 d->d_listenqueue = atoi(v);
1555 case 'M': /* modifiers (flags) */
1556 d->d_mflags = getmodifiers(v, d->d_flags);
1559 case 'N': /* name */
1563 case 'P': /* port */
1567 case 'R': /* receive buffer size */
1568 d->d_tcprcvbufsize = atoi(v);
1571 case 'S': /* send buffer size */
1572 d->d_tcpsndbufsize = atoi(v);
1575 #if _FFR_SS_PER_DAEMON
1576 case 'T': /* SuperSafe */
1577 if (tolower(*v) == 'i')
1578 d->d_supersafe = SAFE_INTERACTIVE;
1579 else if (tolower(*v) == 'p')
1581 d->d_supersafe = SAFE_REALLY_POSTMILTER;
1583 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1584 "Warning: SuperSafe=PostMilter requires Milter support (-DMILTER)\n");
1585 # endif /* MILTER */
1587 d->d_supersafe = atobool(v) ? SAFE_REALLY
1590 #endif /* _FFR_SS_PER_DAEMON */
1593 syserr("554 5.3.5 PortOptions parameter \"%s\" unknown",
1598 /* Check addr and port after finding family */
1601 switch (d->d_addr.sa.sa_family)
1603 #if _FFR_DAEMON_NETUNIX
1606 if (strlen(addr) >= sizeof(d->d_addr.sunix.sun_path))
1608 errno = ENAMETOOLONG;
1609 syserr("setsockaddroptions: domain socket name too long: %s > %d",
1610 addr, sizeof(d->d_addr.sunix.sun_path));
1614 /* file safety check done in opendaemonsocket() */
1615 (void) memset(&d->d_addr.sunix.sun_path, '\0',
1616 sizeof(d->d_addr.sunix.sun_path));
1617 (void) sm_strlcpy((char *)&d->d_addr.sunix.sun_path,
1619 sizeof(d->d_addr.sunix.sun_path));
1621 # endif /* NETUNIX */
1622 #endif /* _FFR_DAEMON_NETUNIX */
1625 if (!isascii(*addr) || !isdigit(*addr) ||
1626 ((d->d_addr.sin.sin_addr.s_addr = inet_addr(addr))
1629 register struct hostent *hp;
1631 hp = sm_gethostbyname(addr, AF_INET);
1633 syserr("554 5.3.0 host \"%s\" unknown",
1637 while (*(hp->h_addr_list) != NULL &&
1638 hp->h_addrtype != AF_INET)
1640 if (*(hp->h_addr_list) == NULL)
1641 syserr("554 5.3.0 host \"%s\" unknown",
1644 memmove(&d->d_addr.sin.sin_addr,
1650 # endif /* NETINET6 */
1654 #endif /* NETINET */
1658 if (anynet_pton(AF_INET6, addr,
1659 &d->d_addr.sin6.sin6_addr) != 1)
1661 register struct hostent *hp;
1663 hp = sm_gethostbyname(addr, AF_INET6);
1665 syserr("554 5.3.0 host \"%s\" unknown",
1669 while (*(hp->h_addr_list) != NULL &&
1670 hp->h_addrtype != AF_INET6)
1672 if (*(hp->h_addr_list) == NULL)
1673 syserr("554 5.3.0 host \"%s\" unknown",
1676 memmove(&d->d_addr.sin6.sin6_addr,
1684 #endif /* NETINET6 */
1687 syserr("554 5.3.5 address= option unsupported for family %d",
1688 d->d_addr.sa.sa_family);
1695 switch (d->d_addr.sa.sa_family)
1699 if (isascii(*port) && isdigit(*port))
1700 d->d_addr.sin.sin_port = htons((unsigned short)
1701 atoi((const char *) port));
1704 # ifdef NO_GETSERVBYNAME
1705 syserr("554 5.3.5 invalid port number: %s",
1707 # else /* NO_GETSERVBYNAME */
1708 register struct servent *sp;
1710 sp = getservbyname(port, "tcp");
1712 syserr("554 5.3.5 service \"%s\" unknown",
1715 d->d_addr.sin.sin_port = sp->s_port;
1716 # endif /* NO_GETSERVBYNAME */
1719 #endif /* NETINET */
1723 if (isascii(*port) && isdigit(*port))
1724 d->d_addr.sin6.sin6_port = htons((unsigned short)
1728 # ifdef NO_GETSERVBYNAME
1729 syserr("554 5.3.5 invalid port number: %s",
1731 # else /* NO_GETSERVBYNAME */
1732 register struct servent *sp;
1734 sp = getservbyname(port, "tcp");
1736 syserr("554 5.3.5 service \"%s\" unknown",
1739 d->d_addr.sin6.sin6_port = sp->s_port;
1740 # endif /* NO_GETSERVBYNAME */
1743 #endif /* NETINET6 */
1747 /* assume two byte transport selector */
1748 if (isascii(*port) && isdigit(*port))
1749 portno = htons((unsigned short) atoi(port));
1752 # ifdef NO_GETSERVBYNAME
1753 syserr("554 5.3.5 invalid port number: %s",
1755 # else /* NO_GETSERVBYNAME */
1756 register struct servent *sp;
1758 sp = getservbyname(port, "tcp");
1760 syserr("554 5.3.5 service \"%s\" unknown",
1763 portno = sp->s_port;
1764 # endif /* NO_GETSERVBYNAME */
1766 memmove(TSEL(&d->d_addr.siso),
1767 (char *) &portno, 2);
1772 syserr("554 5.3.5 Port= option unsupported for family %d",
1773 d->d_addr.sa.sa_family);
1779 ** SETDAEMONOPTIONS -- set options for running the MTA daemon
1782 ** p -- the options line.
1785 ** true if successful, false otherwise.
1788 ** increments number of daemons.
1791 #define DEF_LISTENQUEUE 10
1799 static struct dflags DaemonFlags[] =
1801 { "AUTHREQ", D_AUTHREQ },
1802 { "BINDIF", D_BINDIF },
1803 { "CANONREQ", D_CANONREQ },
1804 { "IFNHELO", D_IFNHELO },
1805 { "FQMAIL", D_FQMAIL },
1806 { "FQRCPT", D_FQRCPT },
1807 { "SMTPS", D_SMTPS },
1808 { "UNQUALOK", D_UNQUALOK },
1809 { "NOAUTH", D_NOAUTH },
1810 { "NOCANON", D_NOCANON },
1811 { "NOETRN", D_NOETRN },
1812 { "NOTLS", D_NOTLS },
1813 { "ETRNONLY", D_ETRNONLY },
1814 { "OPTIONAL", D_OPTIONAL },
1815 { "DISABLE", D_DISABLE },
1816 { "ISSET", D_ISSET },
1824 register struct dflags *df;
1827 for (df = DaemonFlags; df->d_name != NULL; df++)
1829 if (!bitnset(df->d_flag, d->d_flags))
1832 sm_dprintf("<%s", df->d_name);
1834 sm_dprintf(",%s", df->d_name);
1845 if (NDaemons >= MAXDAEMONS)
1847 Daemons[NDaemons].d_socket = -1;
1848 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1849 clrbitmap(Daemons[NDaemons].d_flags);
1850 setsockaddroptions(p, &Daemons[NDaemons]);
1853 if (Daemons[NDaemons].d_inputfilterlist != NULL)
1854 Daemons[NDaemons].d_inputfilterlist = newstr(Daemons[NDaemons].d_inputfilterlist);
1857 if (Daemons[NDaemons].d_name != NULL)
1858 Daemons[NDaemons].d_name = newstr(Daemons[NDaemons].d_name);
1863 (void) sm_snprintf(num, sizeof num, "Daemon%d", NDaemons);
1864 Daemons[NDaemons].d_name = newstr(num);
1869 sm_dprintf("Daemon %s flags: ", Daemons[NDaemons].d_name);
1870 printdaemonflags(&Daemons[NDaemons]);
1877 ** INITDAEMON -- initialize daemon if not yet done.
1886 ** initializes structure for one daemon.
1894 Daemons[NDaemons].d_socket = -1;
1895 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1896 Daemons[NDaemons].d_name = "Daemon0";
1901 ** SETCLIENTOPTIONS -- set options for running the client
1904 ** p -- the options line.
1910 static DAEMON_T ClientSettings[AF_MAX + 1];
1919 memset(&d, '\0', sizeof d);
1920 setsockaddroptions(p, &d);
1922 /* grab what we need */
1923 family = d.d_addr.sa.sa_family;
1924 STRUCTCOPY(d, ClientSettings[family]);
1925 setbitn(D_ISSET, ClientSettings[family].d_flags); /* mark as set */
1926 if (d.d_name != NULL)
1927 ClientSettings[family].d_name = newstr(d.d_name);
1932 (void) sm_snprintf(num, sizeof num, "Client%d", family);
1933 ClientSettings[family].d_name = newstr(num);
1937 ** ADDR_FAMILY -- determine address family from address
1940 ** addr -- the string representation of the address
1943 ** AF_INET, AF_INET6 or AF_UNSPEC
1955 #endif /* NETINET6 */
1958 if (inet_addr(addr) != INADDR_NONE)
1961 sm_dprintf("addr_family(%s): INET\n", addr);
1964 #endif /* NETINET */
1966 if (anynet_pton(AF_INET6, addr, &clt_addr.sin6.sin6_addr) == 1)
1969 sm_dprintf("addr_family(%s): INET6\n", addr);
1972 #endif /* NETINET6 */
1973 #if _FFR_DAEMON_NETUNIX
1978 sm_dprintf("addr_family(%s): LOCAL\n", addr);
1981 # endif /* NETUNIX */
1982 #endif /* _FFR_DAEMON_NETUNIX */
1984 sm_dprintf("addr_family(%s): UNSPEC\n", addr);
1989 ** CHKCLIENTMODIFIERS -- check whether all clients have set a flag.
1992 ** flag -- the flag to test.
1995 ** true iff all configured clients have set the flag.
1999 chkclientmodifiers(flag)
2006 for (i = 0; i < AF_MAX; i++)
2008 if (bitnset(D_ISSET, ClientSettings[i].d_flags))
2010 if (!bitnset((char) flag, ClientSettings[i].d_flags))
2020 ** SETUP_DAEMON_FILTERS -- Parse per-socket filters
2030 setup_daemon_milters()
2034 if (OpMode == MD_SMTP)
2036 /* no need to configure the daemons */
2040 for (idx = 0; idx < NDaemons; idx++)
2042 if (Daemons[idx].d_inputfilterlist != NULL)
2044 milter_config(Daemons[idx].d_inputfilterlist,
2045 Daemons[idx].d_inputfilters,
2052 ** MAKECONNECTION -- make a connection to an SMTP socket on a machine.
2055 ** host -- the name of the host.
2056 ** port -- the port number to connect to.
2057 ** mci -- a pointer to the mail connection information
2058 ** structure to be filled in.
2059 ** e -- the current envelope.
2060 ** enough -- time at which to stop further connection attempts.
2061 ** (0 means no limit)
2064 ** An exit code telling whether the connection could be
2065 ** made and if not why not.
2071 static jmp_buf CtxConnectTimeout;
2073 SOCKADDR CurHostAddr; /* address of current host */
2076 makeconnection(host, port, mci, e, enough)
2078 volatile unsigned int port;
2083 register volatile int addrno = 0;
2085 register struct hostent *volatile hp = (struct hostent *) NULL;
2089 volatile SOCKADDR_LEN_T addrlen;
2090 volatile bool firstconnect;
2091 SM_EVENT *volatile ev = NULL;
2093 volatile bool v6found = false;
2094 #endif /* NETINET6 */
2095 volatile int family = InetMode;
2097 volatile SOCKADDR_LEN_T socksize = 0;
2098 volatile bool clt_bind;
2101 extern ENVELOPE BlankEnvelope;
2103 /* retranslate {daemon_flags} into bitmap */
2105 if ((p = macvalue(macid("{daemon_flags}"), e)) != NULL)
2107 for (; *p != '\0'; p++)
2109 if (!(isascii(*p) && isspace(*p)))
2110 setbitn(bitidx(*p), d_flags);
2116 #endif /* NETINET6 */
2119 /* Set up the address for outgoing connection. */
2120 if (bitnset(D_BINDIF, d_flags) &&
2121 (p = macvalue(macid("{if_addr}"), e)) != NULL &&
2125 char p6[INET6_ADDRSTRLEN];
2126 #endif /* NETINET6 */
2128 memset(&clt_addr, '\0', sizeof clt_addr);
2130 /* infer the address family from the address itself */
2131 clt_addr.sa.sa_family = addr_family(p);
2132 switch (clt_addr.sa.sa_family)
2136 clt_addr.sin.sin_addr.s_addr = inet_addr(p);
2137 if (clt_addr.sin.sin_addr.s_addr != INADDR_NONE &&
2138 clt_addr.sin.sin_addr.s_addr != INADDR_LOOPBACK)
2141 socksize = sizeof (struct sockaddr_in);
2144 #endif /* NETINET */
2148 if (inet_addr(p) != INADDR_NONE)
2149 (void) sm_snprintf(p6, sizeof p6,
2150 "IPv6:::ffff:%s", p);
2152 (void) sm_strlcpy(p6, p, sizeof p6);
2153 if (anynet_pton(AF_INET6, p6,
2154 &clt_addr.sin6.sin6_addr) == 1 &&
2155 !IN6_IS_ADDR_LOOPBACK(&clt_addr.sin6.sin6_addr))
2158 socksize = sizeof (struct sockaddr_in6);
2161 #endif /* NETINET6 */
2165 syserr("554 5.3.5 Address= option unsupported for family %d",
2166 clt_addr.sa.sa_family);
2171 family = clt_addr.sa.sa_family;
2174 /* D_BINDIF not set or not available, fallback to ClientPortOptions */
2177 STRUCTCOPY(ClientSettings[family].d_addr, clt_addr);
2178 switch (clt_addr.sa.sa_family)
2182 if (clt_addr.sin.sin_addr.s_addr == 0)
2183 clt_addr.sin.sin_addr.s_addr = INADDR_ANY;
2186 if (clt_addr.sin.sin_port != 0)
2188 socksize = sizeof (struct sockaddr_in);
2190 #endif /* NETINET */
2193 if (IN6_IS_ADDR_UNSPECIFIED(&clt_addr.sin6.sin6_addr))
2194 clt_addr.sin6.sin6_addr = in6addr_any;
2197 socksize = sizeof (struct sockaddr_in6);
2198 if (clt_addr.sin6.sin6_port != 0)
2201 #endif /* NETINET6 */
2204 socksize = sizeof clt_addr.siso;
2214 ** Set up the address for the mailer.
2215 ** Accept "[a.b.c.d]" syntax for host name.
2220 memset(&CurHostAddr, '\0', sizeof CurHostAddr);
2221 memset(&addr, '\0', sizeof addr);
2222 SmtpPhase = mci->mci_phase = "initial connection";
2227 p = strchr(host, ']');
2231 unsigned long hid = INADDR_NONE;
2232 #endif /* NETINET */
2234 struct sockaddr_in6 hid6;
2235 #endif /* NETINET6 */
2239 memset(&hid6, '\0', sizeof hid6);
2240 #endif /* NETINET6 */
2242 if (family == AF_INET &&
2243 (hid = inet_addr(&host[1])) != INADDR_NONE)
2245 addr.sin.sin_family = AF_INET;
2246 addr.sin.sin_addr.s_addr = hid;
2249 #endif /* NETINET */
2251 if (family == AF_INET6 &&
2252 anynet_pton(AF_INET6, &host[1],
2253 &hid6.sin6_addr) == 1)
2255 addr.sin6.sin6_family = AF_INET6;
2256 addr.sin6.sin6_addr = hid6.sin6_addr;
2259 #endif /* NETINET6 */
2261 /* try it as a host name (avoid MX lookup) */
2262 hp = sm_gethostbyname(&host[1], family);
2263 if (hp == NULL && p[-1] == '.')
2266 int oldopts = _res.options;
2268 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2269 #endif /* NAMED_BIND */
2271 hp = sm_gethostbyname(&host[1],
2275 _res.options = oldopts;
2276 #endif /* NAMED_BIND */
2285 extern char MsgBuf[];
2288 "553 Invalid numeric domain spec \"%s\"",
2290 mci_setstat(mci, EX_NOHOST, "5.1.2", MsgBuf);
2297 /* contortion to get around SGI cc complaints */
2299 p = &host[strlen(host) - 1];
2300 hp = sm_gethostbyname(host, family);
2301 if (hp == NULL && *p == '.')
2304 int oldopts = _res.options;
2306 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2307 #endif /* NAMED_BIND */
2309 hp = sm_gethostbyname(host, family);
2312 _res.options = oldopts;
2313 #endif /* NAMED_BIND */
2320 /* check for name server timeouts */
2322 if (WorkAroundBrokenAAAA && family == AF_INET6 &&
2326 ** An attempt with family AF_INET may
2327 ** succeed By skipping the next section
2328 ** of code, we will try AF_INET before
2333 sm_dprintf("makeconnection: WorkAroundBrokenAAAA: Trying AF_INET lookup (AF_INET6 failed)\n");
2336 # endif /* NETINET6 */
2338 if (errno == ETIMEDOUT ||
2339 h_errno == TRY_AGAIN ||
2340 (errno == ECONNREFUSED && UseNameServer))
2343 mci_setstat(mci, EX_TEMPFAIL,
2349 #endif /* NAMED_BIND */
2352 ** Try v6 first, then fall back to v4.
2353 ** If we found a v6 address, but no v4
2354 ** addresses, then TEMPFAIL.
2357 if (family == AF_INET6)
2364 #endif /* NETINET6 */
2366 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2370 addr.sa.sa_family = hp->h_addrtype;
2371 switch (hp->h_addrtype)
2375 memmove(&addr.sin.sin_addr,
2379 #endif /* NETINET */
2383 memmove(&addr.sin6.sin6_addr,
2387 #endif /* NETINET6 */
2390 if (hp->h_length > sizeof addr.sa.sa_data)
2392 syserr("makeconnection: long sa_data: family %d len %d",
2393 hp->h_addrtype, hp->h_length);
2394 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2398 memmove(addr.sa.sa_data, hp->h_addr, hp->h_length);
2405 ** Determine the port number.
2410 #ifdef NO_GETSERVBYNAME
2412 #else /* NO_GETSERVBYNAME */
2413 register struct servent *sp = getservbyname("smtp", "tcp");
2418 sm_syslog(LOG_ERR, NOQID,
2419 "makeconnection: service \"smtp\" unknown");
2424 #endif /* NO_GETSERVBYNAME */
2428 if (addr.sa.sa_family == AF_INET6 &&
2429 IN6_IS_ADDR_V4MAPPED(&addr.sin6.sin6_addr) &&
2430 ClientSettings[AF_INET].d_addr.sa.sa_family != 0)
2433 ** Ignore mapped IPv4 address since
2434 ** there is a ClientPortOptions setting
2440 #endif /* NETINET6 */
2442 switch (addr.sa.sa_family)
2446 addr.sin.sin_port = port;
2447 addrlen = sizeof (struct sockaddr_in);
2449 #endif /* NETINET */
2453 addr.sin6.sin6_port = port;
2454 addrlen = sizeof (struct sockaddr_in6);
2456 #endif /* NETINET6 */
2460 /* assume two byte transport selector */
2461 memmove(TSEL((struct sockaddr_iso *) &addr), (char *) &port, 2);
2462 addrlen = sizeof (struct sockaddr_iso);
2467 syserr("Can't connect to address family %d", addr.sa.sa_family);
2468 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2473 #endif /* NETINET6 */
2478 ** Try to actually open the connection.
2482 /* if too many connections, don't bother trying */
2483 if (!xla_noqueue_ok(host))
2488 # endif /* NETINET6 */
2493 firstconnect = true;
2497 sm_dprintf("makeconnection (%s [%s].%d (%d))\n",
2498 host, anynet_ntoa(&addr), ntohs(port),
2499 (int) addr.sa.sa_family);
2501 /* save for logging */
2505 if (bitnset(M_SECURE_PORT, mci->mci_mailer->m_flags))
2507 int rport = IPPORT_RESERVED - 1;
2509 s = rresvport(&rport);
2512 #endif /* HASRRESVPORT */
2514 s = socket(addr.sa.sa_family, SOCK_STREAM, 0);
2519 syserr("makeconnection: cannot create socket");
2523 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2527 #endif /* NETINET6 */
2533 if (ClientSettings[family].d_tcpsndbufsize > 0)
2535 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
2536 (char *) &ClientSettings[family].d_tcpsndbufsize,
2537 sizeof(ClientSettings[family].d_tcpsndbufsize)) < 0)
2538 syserr("makeconnection: setsockopt(SO_SNDBUF)");
2540 #endif /* SO_SNDBUF */
2542 if (ClientSettings[family].d_tcprcvbufsize > 0)
2544 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
2545 (char *) &ClientSettings[family].d_tcprcvbufsize,
2546 sizeof(ClientSettings[family].d_tcprcvbufsize)) < 0)
2547 syserr("makeconnection: setsockopt(SO_RCVBUF)");
2549 #endif /* SO_RCVBUF */
2552 sm_dprintf("makeconnection: fd=%d\n", s);
2554 /* turn on network debugging? */
2559 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
2560 (char *)&on, sizeof on);
2562 if (e->e_xfp != NULL) /* for debugging */
2563 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
2564 errno = 0; /* for debugging */
2570 switch (clt_addr.sa.sa_family)
2574 if (clt_addr.sin.sin_port != 0)
2575 (void) setsockopt(s, SOL_SOCKET,
2580 #endif /* NETINET */
2584 if (clt_addr.sin6.sin6_port != 0)
2585 (void) setsockopt(s, SOL_SOCKET,
2590 #endif /* NETINET6 */
2593 if (bind(s, &clt_addr.sa, socksize) < 0)
2598 syserr("makeconnection: cannot bind socket [%s]",
2599 anynet_ntoa(&clt_addr));
2603 #endif /* NETINET6 */
2610 ** Linux seems to hang in connect for 90 minutes (!!!).
2611 ** Time out the connect to avoid this problem.
2614 if (setjmp(CtxConnectTimeout) == 0)
2618 if (e->e_ntries <= 0 && TimeOuts.to_iconnect != 0)
2619 ev = sm_setevent(TimeOuts.to_iconnect,
2621 else if (TimeOuts.to_connect != 0)
2622 ev = sm_setevent(TimeOuts.to_connect,
2627 switch (ConnectOnlyTo.sa.sa_family)
2631 addr.sin.sin_addr.s_addr = ConnectOnlyTo.sin.sin_addr.s_addr;
2633 #endif /* NETINET */
2637 memmove(&addr.sin6.sin6_addr,
2638 &ConnectOnlyTo.sin6.sin6_addr,
2641 #endif /* NETINET6 */
2644 sm_dprintf("Connecting to [%s]...\n", anynet_ntoa(&addr));
2645 i = connect(s, (struct sockaddr *) &addr, addrlen);
2655 /* couldn't connect.... figure out why */
2658 /* if running demand-dialed connection, try again */
2659 if (DialDelay > 0 && firstconnect &&
2660 bitnset(M_DIALDELAY, mci->mci_mailer->m_flags))
2663 sm_dprintf("Connect failed (%s); trying again...\n",
2664 sm_errstring(save_errno));
2665 firstconnect = false;
2666 (void) sleep(DialDelay);
2671 sm_syslog(LOG_INFO, e->e_id,
2672 "makeconnection (%s [%s]) failed: %s",
2673 host, anynet_ntoa(&addr),
2674 sm_errstring(save_errno));
2678 #endif /* NETINET6 */
2679 if (hp != NULL && hp->h_addr_list[addrno] != NULL &&
2680 (enough == 0 || curtime() < enough))
2683 sm_dprintf("Connect failed (%s); trying new address....\n",
2684 sm_errstring(save_errno));
2685 switch (addr.sa.sa_family)
2689 memmove(&addr.sin.sin_addr,
2690 hp->h_addr_list[addrno++],
2693 #endif /* NETINET */
2697 memmove(&addr.sin6.sin6_addr,
2698 hp->h_addr_list[addrno++],
2701 #endif /* NETINET6 */
2704 memmove(addr.sa.sa_data,
2705 hp->h_addr_list[addrno++],
2714 if (family == AF_INET6)
2717 sm_dprintf("Connect failed (%s); retrying with AF_INET....\n",
2718 sm_errstring(save_errno));
2729 #endif /* NETINET6 */
2730 /* couldn't open connection */
2732 /* Don't clobber an already saved errno from v4retry */
2734 #endif /* NETINET6 */
2737 sm_dprintf("Connect failed (%s)\n",
2738 sm_errstring(save_errno));
2742 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2746 #endif /* NETINET6 */
2757 #endif /* NETINET6 */
2759 /* connection ok, put it into canonical form */
2760 mci->mci_out = NULL;
2761 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2763 SM_IO_WRONLY_B, NULL)) == NULL ||
2765 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2767 SM_IO_RDONLY_B, NULL)) == NULL)
2770 syserr("cannot open SMTP client channel, fd=%d", s);
2771 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2772 if (mci->mci_out != NULL)
2773 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2778 sm_io_automode(mci->mci_out, mci->mci_in);
2780 /* set {client_flags} */
2781 if (ClientSettings[addr.sa.sa_family].d_mflags != NULL)
2783 macdefine(&mci->mci_macro, A_PERM,
2784 macid("{client_flags}"),
2785 ClientSettings[addr.sa.sa_family].d_mflags);
2788 macdefine(&mci->mci_macro, A_PERM,
2789 macid("{client_flags}"), "");
2791 /* "add" {client_flags} to bitmap */
2792 if (bitnset(D_IFNHELO, ClientSettings[addr.sa.sa_family].d_flags))
2794 /* look for just this one flag */
2795 setbitn(D_IFNHELO, d_flags);
2798 /* find out name for Interface through which we connect */
2800 if (getsockname(s, &addr.sa, &len) == 0)
2805 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2806 macid("{if_addr_out}"), anynet_ntoa(&addr));
2807 (void) sm_snprintf(family, sizeof(family), "%d",
2809 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2810 macid("{if_family_out}"), family);
2812 name = hostnamebyanyaddr(&addr);
2813 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2814 macid("{if_name_out}"), name);
2817 /* log connection information */
2818 sm_syslog(LOG_INFO, e->e_id,
2819 "SMTP outgoing connect on %.40s", name);
2821 if (bitnset(D_IFNHELO, d_flags))
2823 if (name[0] != '[' && strchr(name, '.') != NULL)
2824 mci->mci_heloname = newstr(name);
2829 macdefine(&BlankEnvelope.e_macro, A_PERM,
2830 macid("{if_name_out}"), NULL);
2831 macdefine(&BlankEnvelope.e_macro, A_PERM,
2832 macid("{if_addr_out}"), NULL);
2833 macdefine(&BlankEnvelope.e_macro, A_PERM,
2834 macid("{if_family_out}"), NULL);
2838 /* Use the configured HeloName as appropriate */
2839 if (HeloName != NULL && HeloName[0] != '\0')
2840 mci->mci_heloname = newstr(HeloName);
2841 #endif /* _FFR_HELONAME */
2843 mci_setstat(mci, EX_OK, NULL, NULL);
2848 connecttimeout(ignore)
2852 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2853 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2858 longjmp(CtxConnectTimeout, 1);
2861 ** MAKECONNECTION_DS -- make a connection to a domain socket.
2864 ** mux_path -- the path of the socket to connect to.
2865 ** mci -- a pointer to the mail connection information
2866 ** structure to be filled in.
2869 ** An exit code telling whether the connection could be
2870 ** made and if not why not.
2878 makeconnection_ds(mux_path, mci)
2883 int rval, save_errno;
2884 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK;
2885 struct sockaddr_un unix_addr;
2887 /* if not safe, don't connect */
2888 rval = safefile(mux_path, RunAsUid, RunAsGid, RunAsUserName,
2889 sff, S_IRUSR|S_IWUSR, NULL);
2893 syserr("makeconnection_ds: unsafe domain socket %s",
2895 mci_setstat(mci, EX_TEMPFAIL, "4.3.5", NULL);
2900 /* prepare address structure */
2901 memset(&unix_addr, '\0', sizeof unix_addr);
2902 unix_addr.sun_family = AF_UNIX;
2904 if (strlen(mux_path) >= sizeof unix_addr.sun_path)
2906 syserr("makeconnection_ds: domain socket name %s too long",
2909 /* XXX why TEMPFAIL but 5.x.y ? */
2910 mci_setstat(mci, EX_TEMPFAIL, "5.3.5", NULL);
2911 errno = ENAMETOOLONG;
2912 return EX_UNAVAILABLE;
2914 (void) sm_strlcpy(unix_addr.sun_path, mux_path,
2915 sizeof unix_addr.sun_path);
2917 /* initialize domain socket */
2918 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2922 syserr("makeconnection_ds: could not create domain socket %s",
2924 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2929 /* connect to server */
2930 if (connect(sock, (struct sockaddr *) &unix_addr,
2931 sizeof(unix_addr)) == -1)
2934 syserr("Could not connect to socket %s", mux_path);
2935 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2941 /* connection ok, put it into canonical form */
2942 mci->mci_out = NULL;
2943 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2944 (void *) &sock, SM_IO_WRONLY_B, NULL))
2946 || (sock = dup(sock)) < 0 ||
2947 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2948 (void *) &sock, SM_IO_RDONLY_B, NULL))
2952 syserr("cannot open SMTP client channel, fd=%d", sock);
2953 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2954 if (mci->mci_out != NULL)
2955 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2960 sm_io_automode(mci->mci_out, mci->mci_in);
2962 mci_setstat(mci, EX_OK, NULL, NULL);
2966 #endif /* NETUNIX */
2968 ** SHUTDOWN_DAEMON -- Performs a clean shutdown of the daemon
2977 ** closes control socket, exits.
2986 sm_allsignals(true);
2988 reason = ShutdownRequest;
2989 ShutdownRequest = NULL;
2993 sm_syslog(LOG_INFO, CurEnv->e_id, "stopping daemon, reason=%s",
2994 reason == NULL ? "implicit call" : reason);
2997 closecontrolsocket(true);
3002 for (i = 0; i < NDaemons; i++)
3004 if (Daemons[i].d_socket >= 0)
3006 (void) close(Daemons[i].d_socket);
3007 Daemons[i].d_socket = -1;
3009 #if _FFR_DAEMON_NETUNIX
3011 /* Remove named sockets */
3012 if (Daemons[i].d_addr.sa.sa_family == AF_UNIX)
3015 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_MUSTOWN|SFF_EXECOK|SFF_CREAT;
3017 /* if not safe, don't use it */
3018 rval = safefile(Daemons[i].d_addr.sunix.sun_path,
3021 S_IRUSR|S_IWUSR, NULL);
3023 unlink(Daemons[i].d_addr.sunix.sun_path) < 0)
3025 sm_syslog(LOG_WARNING, NOQID,
3026 "Could not remove daemon %s socket: %s: %s",
3028 Daemons[i].d_addr.sunix.sun_path,
3029 sm_errstring(errno));
3032 # endif /* NETUNIX */
3033 #endif /* _FFR_DAEMON_NETUNIX */
3037 finis(false, true, EX_OK);
3040 ** RESTART_DAEMON -- Performs a clean restart of the daemon
3049 ** restarts the daemon or exits if restart fails.
3052 /* Make a non-DFL/IGN signal a noop */
3053 #define SM_NOOP_SIGNAL(sig, old) \
3056 (old) = sm_signal((sig), sm_signal_noop); \
3057 if ((old) == SIG_IGN || (old) == SIG_DFL) \
3058 (void) sm_signal((sig), (old)); \
3067 sigfunc_t ignore, oalrm, ousr1;
3068 extern int DtableSize;
3070 /* clear the events to turn off SIGALRMs */
3072 sm_allsignals(true);
3074 reason = RestartRequest;
3075 RestartRequest = NULL;
3078 if (SaveArgv[0][0] != '/')
3081 sm_syslog(LOG_INFO, NOQID,
3082 "could not restart: need full path");
3083 finis(false, true, EX_OSFILE);
3087 sm_syslog(LOG_INFO, NOQID, "restarting %s due to %s",
3089 reason == NULL ? "implicit call" : reason);
3091 closecontrolsocket(true);
3093 cleanup_shm(DaemonPid == getpid());
3094 #endif /* SM_CONF_SHM */
3096 /* close locked pid file */
3097 close_sendmail_pid();
3100 ** Want to drop to the user who started the process in all cases
3101 ** *but* when running as "smmsp" for the clientmqueue queue run
3102 ** daemon. In that case, UseMSP will be true, RunAsUid should not
3103 ** be root, and RealUid should be either 0 or RunAsUid.
3106 drop = !(UseMSP && RunAsUid != 0 &&
3107 (RealUid == 0 || RealUid == RunAsUid));
3109 if (drop_privileges(drop) != EX_OK)
3112 sm_syslog(LOG_ALERT, NOQID,
3113 "could not drop privileges: %s",
3114 sm_errstring(errno));
3115 finis(false, true, EX_OSERR);
3119 sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
3122 ** Need to allow signals before execve() to make them "harmless".
3123 ** However, the default action can be "terminate", so it isn't
3124 ** really harmless. Setting signals to IGN will cause them to be
3125 ** ignored in the new process to, so that isn't a good alternative.
3128 SM_NOOP_SIGNAL(SIGALRM, oalrm);
3129 SM_NOOP_SIGNAL(SIGCHLD, ignore);
3130 SM_NOOP_SIGNAL(SIGHUP, ignore);
3131 SM_NOOP_SIGNAL(SIGINT, ignore);
3132 SM_NOOP_SIGNAL(SIGPIPE, ignore);
3133 SM_NOOP_SIGNAL(SIGTERM, ignore);
3135 SM_NOOP_SIGNAL(SIGUSR1, ousr1);
3136 #endif /* SIGUSR1 */
3138 /* Turn back on signals */
3139 sm_allsignals(false);
3141 (void) execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
3144 /* block signals again and restore needed signals */
3145 sm_allsignals(true);
3147 /* For finis() events */
3148 (void) sm_signal(SIGALRM, oalrm);
3151 /* For debugging finis() */
3152 (void) sm_signal(SIGUSR1, ousr1);
3153 #endif /* SIGUSR1 */
3157 sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %s",
3158 SaveArgv[0], sm_errstring(errno));
3159 finis(false, true, EX_OSFILE);
3163 ** MYHOSTNAME -- return the name of this host.
3166 ** hostbuf -- a place to return the name of this host.
3167 ** size -- the size of hostbuf.
3170 ** A list of aliases for this host.
3173 ** Adds numeric codes to $=w.
3177 myhostname(hostbuf, size)
3181 register struct hostent *hp;
3183 if (gethostname(hostbuf, size) < 0 || hostbuf[0] == '\0')
3184 (void) sm_strlcpy(hostbuf, "localhost", size);
3185 hp = sm_gethostbyname(hostbuf, InetMode);
3186 #if NETINET && NETINET6
3187 if (hp == NULL && InetMode == AF_INET6)
3190 ** It's possible that this IPv6 enabled machine doesn't
3191 ** actually have any IPv6 interfaces and, therefore, no
3192 ** IPv6 addresses. Fall back to AF_INET.
3195 hp = sm_gethostbyname(hostbuf, AF_INET);
3197 #endif /* NETINET && NETINET6 */
3200 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
3201 (void) cleanstrcpy(hostbuf, hp->h_name, size);
3204 if (strchr(hostbuf, '.') == NULL)
3208 domainname = ni_propval("/locations", NULL, "resolver",
3210 if (domainname != NULL &&
3211 strlen(domainname) + strlen(hostbuf) + 1 < size)
3212 (void) sm_strlcat2(hostbuf, ".", domainname, size);
3214 #endif /* NETINFO */
3217 ** If there is still no dot in the name, try looking for a
3221 if (strchr(hostbuf, '.') == NULL)
3225 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
3227 if (strchr(*ha, '.') != NULL)
3229 (void) cleanstrcpy(hostbuf, *ha, size - 1);
3230 hostbuf[size - 1] = '\0';
3237 ** If _still_ no dot, wait for a while and try again -- it is
3238 ** possible that some service is starting up. This can result
3239 ** in excessive delays if the system is badly configured, but
3240 ** there really isn't a way around that, particularly given that
3241 ** the config file hasn't been read at this point.
3242 ** All in all, a bit of a mess.
3245 if (strchr(hostbuf, '.') == NULL &&
3246 !getcanonname(hostbuf, size, true, NULL))
3248 sm_syslog(LOG_CRIT, NOQID,
3249 "My unqualified host name (%s) unknown; sleeping for retry",
3251 message("My unqualified host name (%s) unknown; sleeping for retry",
3254 if (!getcanonname(hostbuf, size, true, NULL))
3256 sm_syslog(LOG_ALERT, NOQID,
3257 "unable to qualify my own domain name (%s) -- using short name",
3259 message("WARNING: unable to qualify my own domain name (%s) -- using short name",
3266 ** ADDRCMP -- compare two host addresses
3269 ** hp -- hostent structure for the first address
3270 ** ha -- actual first address
3271 ** sa -- second address
3274 ** 0 -- if ha and sa match
3275 ** else -- they don't match
3286 #endif /* NETINET6 */
3288 switch (sa->sa.sa_family)
3292 if (hp->h_addrtype == AF_INET)
3293 return memcmp(ha, (char *) &sa->sin.sin_addr, INADDRSZ);
3295 #endif /* NETINET */
3299 a = (unsigned char *) &sa->sin6.sin6_addr;
3301 /* Straight binary comparison */
3302 if (hp->h_addrtype == AF_INET6)
3303 return memcmp(ha, a, IN6ADDRSZ);
3305 /* If IPv4-mapped IPv6 address, compare the IPv4 section */
3306 if (hp->h_addrtype == AF_INET &&
3307 IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr))
3308 return memcmp(a + IN6ADDRSZ - INADDRSZ, ha, INADDRSZ);
3310 #endif /* NETINET6 */
3315 ** GETAUTHINFO -- get the real host name associated with a file descriptor
3317 ** Uses RFC1413 protocol to try to get info from the other end.
3320 ** fd -- the descriptor
3321 ** may_be_forged -- an outage that is set to true if the
3322 ** forward lookup of RealHostName does not match
3323 ** RealHostAddr; set to false if they do match.
3326 ** The user@host information associated with this descriptor.
3329 static jmp_buf CtxAuthTimeout;
3336 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
3337 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
3342 longjmp(CtxAuthTimeout, 1);
3346 getauthinfo(fd, may_be_forged)
3348 bool *may_be_forged;
3350 unsigned short SM_NONVOLATILE port = 0;
3351 SOCKADDR_LEN_T falen;
3352 register char *volatile p = NULL;
3354 SOCKADDR_LEN_T lalen;
3355 #ifndef NO_GETSERVBYNAME
3356 register struct servent *sp;
3358 static unsigned short port4 = 0;
3359 # endif /* NETINET */
3361 static unsigned short port6 = 0;
3362 # endif /* NETINET6 */
3363 #endif /* ! NO_GETSERVBYNAME */
3370 char *ostype = NULL;
3372 char ibuf[MAXNAME + 1];
3373 static char hbuf[MAXNAME + MAXAUTHINFO + 11];
3375 *may_be_forged = false;
3376 falen = sizeof RealHostAddr;
3377 if (isatty(fd) || (i = getpeername(fd, &RealHostAddr.sa, &falen)) < 0 ||
3378 falen <= 0 || RealHostAddr.sa.sa_family == 0)
3383 ** ENOTSOCK is OK: bail on anything else, but reset
3384 ** errno in this case, so a mis-report doesn't
3388 if (errno != ENOTSOCK)
3392 (void) sm_strlcpyn(hbuf, sizeof hbuf, 2, RealUserName,
3395 sm_dprintf("getauthinfo: %s\n", hbuf);
3399 if (RealHostName == NULL)
3401 /* translate that to a host name */
3402 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
3403 if (strlen(RealHostName) > MAXNAME)
3404 RealHostName[MAXNAME] = '\0'; /* XXX - 1 ? */
3407 /* cross check RealHostName with forward DNS lookup */
3408 if (anynet_ntoa(&RealHostAddr)[0] != '[' &&
3409 RealHostName[0] != '[')
3413 family = RealHostAddr.sa.sa_family;
3414 #if NETINET6 && NEEDSGETIPNODE
3416 ** If RealHostAddr is an IPv6 connection with an
3417 ** IPv4-mapped address, we need RealHostName's IPv4
3418 ** address(es) for addrcmp() to compare against
3421 ** Actually, we only need to do this for systems
3422 ** which NEEDSGETIPNODE since the real getipnodebyname()
3423 ** already does V4MAPPED address via the AI_V4MAPPEDCFG
3424 ** flag. A better fix to this problem is to add this
3425 ** functionality to our stub getipnodebyname().
3428 if (family == AF_INET6 &&
3429 IN6_IS_ADDR_V4MAPPED(&RealHostAddr.sin6.sin6_addr))
3431 #endif /* NETINET6 && NEEDSGETIPNODE */
3433 /* try to match the reverse against the forward lookup */
3434 hp = sm_gethostbyname(RealHostName, family);
3437 /* XXX: Could be a temporary error on forward lookup */
3438 *may_be_forged = true;
3442 for (ha = hp->h_addr_list; *ha != NULL; ha++)
3444 if (addrcmp(hp, *ha, &RealHostAddr) == 0)
3447 *may_be_forged = *ha == NULL;
3451 #endif /* NETINET6 */
3455 if (TimeOuts.to_ident == 0)
3459 switch (RealHostAddr.sa.sa_family)
3463 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3465 la.sa.sa_family != AF_INET)
3470 port = RealHostAddr.sin.sin_port;
3472 /* create ident query */
3473 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3474 ntohs(RealHostAddr.sin.sin_port),
3475 ntohs(la.sin.sin_port));
3477 /* create local address */
3478 la.sin.sin_port = 0;
3480 /* create foreign address */
3481 # ifdef NO_GETSERVBYNAME
3482 RealHostAddr.sin.sin_port = htons(113);
3483 # else /* NO_GETSERVBYNAME */
3486 ** getservbyname() consumes about 5% of the time
3487 ** when receiving a small message (almost all of the time
3488 ** spent in this routine).
3489 ** Hence we store the port in a static variable
3490 ** to save this time.
3491 ** The portnumber shouldn't change very often...
3492 ** This code makes the assumption that the port number
3498 sp = getservbyname("auth", "tcp");
3504 RealHostAddr.sin.sin_port = port4;
3506 # endif /* NO_GETSERVBYNAME */
3507 #endif /* NETINET */
3511 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3513 la.sa.sa_family != AF_INET6)
3518 port = RealHostAddr.sin6.sin6_port;
3520 /* create ident query */
3521 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3522 ntohs(RealHostAddr.sin6.sin6_port),
3523 ntohs(la.sin6.sin6_port));
3525 /* create local address */
3526 la.sin6.sin6_port = 0;
3528 /* create foreign address */
3529 # ifdef NO_GETSERVBYNAME
3530 RealHostAddr.sin6.sin6_port = htons(113);
3531 # else /* NO_GETSERVBYNAME */
3534 sp = getservbyname("auth", "tcp");
3540 RealHostAddr.sin6.sin6_port = port6;
3542 # endif /* NO_GETSERVBYNAME */
3543 #endif /* NETINET6 */
3550 if (setjmp(CtxAuthTimeout) != 0)
3557 /* put a timeout around the whole thing */
3558 ev = sm_setevent(TimeOuts.to_ident, authtimeout, 0);
3560 /* connect to foreign IDENT server using same address as SMTP socket */
3561 s = socket(la.sa.sa_family, SOCK_STREAM, 0);
3567 if (bind(s, &la.sa, lalen) < 0 ||
3568 connect(s, &RealHostAddr.sa, lalen) < 0)
3572 sm_dprintf("getauthinfo: sent %s", ibuf);
3575 if (write(s, ibuf, strlen(ibuf)) < 0)
3580 nleft = sizeof ibuf - 1;
3581 while ((i = read(s, p, nleft)) > 0)
3588 if ((s = strchr(ibuf, '\n')) != NULL)
3602 if (i < 0 || p == &ibuf[0])
3605 if (p >= &ibuf[2] && *--p == '\n' && *--p == '\r')
3610 sm_dprintf("getauthinfo: got %s\n", ibuf);
3613 p = strchr(ibuf, ':');
3616 /* malformed response */
3619 while (isascii(*++p) && isspace(*p))
3621 if (sm_strncasecmp(p, "userid", 6) != 0)
3623 /* presumably an error string */
3627 while (isascii(*p) && isspace(*p))
3631 /* either useridxx or malformed response */
3635 /* p now points to the OSTYPE field */
3636 while (isascii(*p) && isspace(*p))
3642 /* malformed response */
3650 charset = strchr(ostype, ',');
3651 if (charset != NULL)
3655 /* 1413 says don't do this -- but it's broken otherwise */
3656 while (isascii(*++p) && isspace(*p))
3659 /* p now points to the authenticated name -- copy carefully */
3660 if (sm_strncasecmp(ostype, "other", 5) == 0 &&
3661 (ostype[5] == ' ' || ostype[5] == '\0'))
3663 (void) sm_strlcpy(hbuf, "IDENT:", sizeof hbuf);
3664 cleanstrcpy(&hbuf[6], p, MAXAUTHINFO);
3667 cleanstrcpy(hbuf, p, MAXAUTHINFO);
3669 (void) sm_strlcpyn(&hbuf[len], sizeof hbuf - len, 2, "@",
3670 RealHostName == NULL ? "localhost" : RealHostName);
3678 /* put back the original incoming port */
3679 switch (RealHostAddr.sa.sa_family)
3684 RealHostAddr.sin.sin_port = port;
3686 #endif /* NETINET */
3691 RealHostAddr.sin6.sin6_port = port;
3693 #endif /* NETINET6 */
3696 if (RealHostName == NULL)
3699 sm_dprintf("getauthinfo: NULL\n");
3702 (void) sm_strlcpy(hbuf, RealHostName, sizeof hbuf);
3706 # ifndef GET_IPOPT_DST
3707 # define GET_IPOPT_DST(dst) (dst)
3708 # endif /* ! GET_IPOPT_DST */
3710 ** Extract IP source routing information.
3712 ** Format of output for a connection from site a through b
3714 ** loose: @site-c@site-b:site-a
3715 ** strict: !@site-c@site-b:site-a
3717 ** o - pointer within ipopt_list structure.
3718 ** q - pointer within ls/ss rr route data
3719 ** p - pointer to hbuf
3722 if (RealHostAddr.sa.sa_family == AF_INET)
3724 SOCKOPT_LEN_T ipoptlen;
3729 struct IPOPTION ipopt;
3731 ipoptlen = sizeof ipopt;
3732 if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
3733 (char *) &ipopt, &ipoptlen) < 0)
3737 o = (unsigned char *) ipopt.IP_LIST;
3738 while (o != NULL && o < (unsigned char *) &ipopt + ipoptlen)
3754 ** o[0] is the option type (loose/strict).
3755 ** o[1] is the length of this option,
3756 ** including option type and
3758 ** o[2] is the pointer into the route
3760 ** o[3] begins the route data.
3763 p = &hbuf[strlen(hbuf)];
3764 l = sizeof hbuf - (hbuf - p) - 6;
3765 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
3767 *o == IPOPT_SSRR ? "!" : "",
3768 l > 240 ? 120 : l / 2,
3769 inet_ntoa(GET_IPOPT_DST(ipopt.IP_DST)));
3774 j = o[1] / sizeof(struct in_addr) - 1;
3776 /* q skips length and router pointer to data */
3778 for ( ; j >= 0; j--)
3780 struct in_addr addr;
3782 memcpy(&addr, q, sizeof(addr));
3783 (void) sm_snprintf(p,
3793 q += sizeof(struct in_addr);
3799 /* Skip over option */
3804 (void) sm_snprintf(p, SPACELEFT(hbuf, p), "]");
3809 #endif /* IP_SRCROUTE */
3810 if (RealHostName != NULL && RealHostName[0] != '[')
3812 p = &hbuf[strlen(hbuf)];
3813 (void) sm_snprintf(p, SPACELEFT(hbuf, p), " [%.100s]",
3814 anynet_ntoa(&RealHostAddr));
3818 p = &hbuf[strlen(hbuf)];
3819 (void) sm_strlcpy(p, " (may be forged)", SPACELEFT(hbuf, p));
3820 macdefine(&BlankEnvelope.e_macro, A_PERM,
3821 macid("{client_resolve}"), "FORGED");
3826 #endif /* IP_SRCROUTE */
3828 /* put back the original incoming port */
3829 switch (RealHostAddr.sa.sa_family)
3834 RealHostAddr.sin.sin_port = port;
3836 #endif /* NETINET */
3841 RealHostAddr.sin6.sin6_port = port;
3843 #endif /* NETINET6 */
3847 sm_dprintf("getauthinfo: %s\n", hbuf);
3851 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
3854 ** map -- a pointer to this map.
3855 ** name -- the (presumably unqualified) hostname.
3856 ** av -- unused -- for compatibility with other mapping
3858 ** statp -- an exit status (out parameter) -- set to
3859 ** EX_TEMPFAIL if the name server is unavailable.
3862 ** The mapping, if found.
3863 ** NULL if no mapping found.
3866 ** Looks up the host specified in hbuf. If it is not
3867 ** the canonical name for that host, return the canonical
3868 ** name (unless MF_MATCHONLY is set, which will cause the
3869 ** status only to be returned).
3873 host_map_lookup(map, name, av, statp)
3879 register struct hostent *hp;
3881 struct in_addr in_addr;
3882 #endif /* NETINET */
3884 struct in6_addr in6_addr;
3885 #endif /* NETINET6 */
3886 char *cp, *ans = NULL;
3890 time_t SM_NONVOLATILE retrans = 0;
3891 int SM_NONVOLATILE retry = 0;
3892 #endif /* NAMED_BIND */
3893 char hbuf[MAXNAME + 1];
3896 ** See if we have already looked up this name. If so, just
3897 ** return it (unless expired).
3901 s = stab(name, ST_NAMECANON, ST_ENTER);
3902 if (bitset(NCF_VALID, s->s_namecanon.nc_flags) &&
3903 s->s_namecanon.nc_exp >= now)
3906 sm_dprintf("host_map_lookup(%s) => CACHE %s\n",
3908 s->s_namecanon.nc_cname == NULL
3910 : s->s_namecanon.nc_cname);
3911 errno = s->s_namecanon.nc_errno;
3912 SM_SET_H_ERRNO(s->s_namecanon.nc_herrno);
3913 *statp = s->s_namecanon.nc_stat;
3914 if (*statp == EX_TEMPFAIL)
3916 CurEnv->e_status = "4.4.3";
3917 message("851 %s: Name server timeout",
3918 shortenstring(name, 33));
3920 if (*statp != EX_OK)
3922 if (s->s_namecanon.nc_cname == NULL)
3924 syserr("host_map_lookup(%s): bogus NULL cache entry, errno=%d, h_errno=%d",
3926 s->s_namecanon.nc_errno,
3927 s->s_namecanon.nc_herrno);
3930 if (bitset(MF_MATCHONLY, map->map_mflags))
3931 cp = map_rewrite(map, name, strlen(name), NULL);
3933 cp = map_rewrite(map,
3934 s->s_namecanon.nc_cname,
3935 strlen(s->s_namecanon.nc_cname),
3941 ** If we are running without a regular network connection (usually
3942 ** dial-on-demand) and we are just queueing, we want to avoid DNS
3943 ** lookups because those could try to connect to a server.
3946 if (CurEnv->e_sendmode == SM_DEFER &&
3947 bitset(MF_DEFER, map->map_mflags))
3950 sm_dprintf("host_map_lookup(%s) => DEFERRED\n", name);
3951 *statp = EX_TEMPFAIL;
3956 ** If first character is a bracket, then it is an address
3957 ** lookup. Address is copied into a temporary buffer to
3958 ** strip the brackets and to preserve name if address is
3963 sm_dprintf("host_map_lookup(%s) => ", name);
3965 if (map->map_timeout > 0)
3967 retrans = _res.retrans;
3968 _res.retrans = map->map_timeout;
3970 if (map->map_retry > 0)
3973 _res.retry = map->map_retry;
3975 #endif /* NAMED_BIND */
3977 /* set default TTL */
3978 s->s_namecanon.nc_exp = now + SM_DEFAULT_TTL;
3983 (void) sm_strlcpy(hbuf, name, sizeof hbuf);
3984 if (getcanonname(hbuf, sizeof hbuf - 1, !HasWildcardMX, &ttl))
3988 s->s_namecanon.nc_exp = now + SM_MIN(ttl,
3994 if ((cp = strchr(name, ']')) == NULL)
3997 sm_dprintf("FAILED\n");
4004 if ((in_addr.s_addr = inet_addr(&name[1])) != INADDR_NONE)
4005 hp = sm_gethostbyaddr((char *)&in_addr,
4007 #endif /* NETINET */
4010 anynet_pton(AF_INET6, &name[1], &in6_addr) == 1)
4011 hp = sm_gethostbyaddr((char *)&in6_addr,
4012 IN6ADDRSZ, AF_INET6);
4013 #endif /* NETINET6 */
4018 /* found a match -- copy out */
4019 ans = denlstring((char *) hp->h_name, true, true);
4021 if (ans == hp->h_name)
4023 static char n[MAXNAME + 1];
4025 /* hp->h_name is about to disappear */
4026 (void) sm_strlcpy(n, ans, sizeof n);
4031 #endif /* NETINET6 */
4035 if (map->map_timeout > 0)
4036 _res.retrans = retrans;
4037 if (map->map_retry > 0)
4039 #endif /* NAMED_BIND */
4041 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
4043 /* Found an answer */
4046 s->s_namecanon.nc_stat = *statp = EX_OK;
4047 if (s->s_namecanon.nc_cname != NULL)
4048 sm_free(s->s_namecanon.nc_cname);
4049 s->s_namecanon.nc_cname = sm_strdup_x(ans);
4050 if (bitset(MF_MATCHONLY, map->map_mflags))
4051 cp = map_rewrite(map, name, strlen(name), NULL);
4053 cp = map_rewrite(map, ans, strlen(ans), av);
4055 sm_dprintf("FOUND %s\n", ans);
4060 /* No match found */
4061 s->s_namecanon.nc_errno = errno;
4063 s->s_namecanon.nc_herrno = h_errno;
4065 sm_dprintf("FAIL (%d)\n", h_errno);
4071 CurEnv->e_status = "4.4.3";
4072 message("851 %s: Name server timeout",
4073 shortenstring(name, 33));
4075 *statp = EX_TEMPFAIL;
4078 case HOST_NOT_FOUND:
4084 *statp = EX_SOFTWARE;
4088 *statp = EX_UNAVAILABLE;
4091 #else /* NAMED_BIND */
4093 sm_dprintf("FAIL\n");
4095 #endif /* NAMED_BIND */
4096 s->s_namecanon.nc_stat = *statp;
4100 ** HOST_MAP_INIT -- initialize host class structures
4103 ** map -- a pointer to this map.
4104 ** args -- argument string.
4111 host_map_init(map, args)
4115 register char *p = args;
4119 while (isascii(*p) && isspace(*p))
4130 map->map_tapp = ++p;
4134 map->map_mflags |= MF_MATCHONLY;
4138 map->map_mflags |= MF_NODEFER;
4141 case 'S': /* only for consistency */
4142 map->map_spacesub = *++p;
4146 map->map_mflags |= MF_DEFER;
4153 while (isascii(*++p) && isspace(*p))
4158 map->map_timeout = convtime(p, 's');
4165 while (isascii(*++p) && isspace(*p))
4167 map->map_retry = atoi(p);
4170 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
4175 if (map->map_app != NULL)
4176 map->map_app = newstr(map->map_app);
4177 if (map->map_tapp != NULL)
4178 map->map_tapp = newstr(map->map_tapp);
4184 ** ANYNET_NTOP -- convert an IPv6 network address to printable form.
4187 ** s6a -- a pointer to an in6_addr structure.
4188 ** dst -- buffer to store result in
4189 ** dst_len -- size of dst buffer
4192 ** A printable version of that structure.
4196 anynet_ntop(s6a, dst, dst_len)
4197 struct in6_addr *s6a;
4203 if (IN6_IS_ADDR_V4MAPPED(s6a))
4204 ap = (char *) inet_ntop(AF_INET,
4205 &s6a->s6_addr[IN6ADDRSZ - INADDRSZ],
4212 /* Save pointer to beginning of string */
4215 /* Add IPv6: protocol tag */
4216 sz = sm_strlcpy(dst, "IPv6:", dst_len);
4221 ap = (char *) inet_ntop(AF_INET6, s6a, dst, dst_len);
4223 /* Restore pointer to beginning of string */
4231 ** ANYNET_PTON -- convert printed form to network address.
4233 ** Wrapper for inet_pton() which handles IPv6: labels.
4236 ** family -- address family
4238 ** dst -- destination address structure
4241 ** 1 if the address was valid
4242 ** 0 if the address wasn't parseable
4247 anynet_pton(family, src, dst)
4252 if (family == AF_INET6 && sm_strncasecmp(src, "IPv6:", 5) == 0)
4254 return inet_pton(family, src, dst);
4256 #endif /* NETINET6 */
4258 ** ANYNET_NTOA -- convert a network address to printable form.
4261 ** sap -- a pointer to a sockaddr structure.
4264 ** A printable version of that sockaddr.
4267 #ifdef USE_SOCK_STREAM
4270 # include <net/if_dl.h>
4271 # endif /* NETLINK */
4275 register SOCKADDR *sap;
4280 static char buf[100];
4282 /* check for null/zero family */
4285 if (sap->sa.sa_family == 0)
4288 switch (sap->sa.sa_family)
4292 if (sap->sunix.sun_path[0] != '\0')
4293 (void) sm_snprintf(buf, sizeof buf, "[UNIX: %.64s]",
4294 sap->sunix.sun_path);
4296 (void) sm_strlcpy(buf, "[UNIX: localhost]", sizeof buf);
4298 # endif /* NETUNIX */
4302 return (char *) inet_ntoa(sap->sin.sin_addr);
4303 # endif /* NETINET */
4307 ap = anynet_ntop(&sap->sin6.sin6_addr, buf, sizeof buf);
4311 # endif /* NETINET6 */
4315 (void) sm_snprintf(buf, sizeof buf, "[LINK: %s]",
4316 link_ntoa((struct sockaddr_dl *) &sap->sa));
4318 # endif /* NETLINK */
4320 /* this case is needed when nothing is #defined */
4321 /* in order to keep the switch syntactically correct */
4325 /* unknown family -- just dump bytes */
4326 (void) sm_snprintf(buf, sizeof buf, "Family %d: ", sap->sa.sa_family);
4327 bp = &buf[strlen(buf)];
4328 ap = sap->sa.sa_data;
4329 for (l = sizeof sap->sa.sa_data; --l >= 0; )
4331 (void) sm_snprintf(bp, SPACELEFT(buf, bp), "%02x:",
4339 ** HOSTNAMEBYANYADDR -- return name of host based on address
4342 ** sap -- SOCKADDR pointer
4345 ** text representation of host name.
4352 hostnamebyanyaddr(sap)
4353 register SOCKADDR *sap;
4355 register struct hostent *hp;
4358 # endif /* NAMED_BIND */
4360 struct in6_addr in6_addr;
4361 # endif /* NETINET6 */
4364 /* shorten name server timeout to avoid higher level timeouts */
4365 saveretry = _res.retry;
4366 if (_res.retry * _res.retrans > 20)
4367 _res.retry = 20 / _res.retrans;
4368 # endif /* NAMED_BIND */
4370 switch (sap->sa.sa_family)
4374 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
4377 # endif /* NETINET */
4381 hp = sm_gethostbyaddr((char *) &sap->sin6.sin6_addr,
4382 IN6ADDRSZ, AF_INET6);
4384 # endif /* NETINET6 */
4388 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
4389 sizeof sap->siso.siso_addr, AF_ISO);
4391 # endif /* NETISO */
4397 # endif /* NETUNIX */
4400 hp = sm_gethostbyaddr(sap->sa.sa_data, sizeof sap->sa.sa_data,
4406 _res.retry = saveretry;
4407 # endif /* NAMED_BIND */
4409 # if NETINET || NETINET6
4410 if (hp != NULL && hp->h_name[0] != '['
4412 && inet_pton(AF_INET6, hp->h_name, &in6_addr) != 1
4413 # endif /* NETINET6 */
4415 && inet_addr(hp->h_name) == INADDR_NONE
4416 # endif /* NETINET */
4421 name = denlstring((char *) hp->h_name, true, true);
4423 if (name == hp->h_name)
4425 static char n[MAXNAME + 1];
4427 /* Copy the string, hp->h_name is about to disappear */
4428 (void) sm_strlcpy(n, name, sizeof n);
4432 # endif /* NETINET6 */
4435 # endif /* NETINET || NETINET6 */
4443 # endif /* NETINET6 */
4446 if (sap->sa.sa_family == AF_UNIX && sap->sunix.sun_path[0] == '\0')
4448 # endif /* NETUNIX */
4450 static char buf[203];
4452 (void) sm_snprintf(buf, sizeof buf, "[%.200s]",
4457 #endif /* USE_SOCK_STREAM */