2 * Copyright (c) 1998-2007 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.
17 SM_RCSID("@(#)$Id: daemon.c,v 8.680 2008/02/14 00:20:26 ca Exp $")
19 #if defined(SOCK_STREAM) || defined(__GNU_LIBRARY__)
20 # define USE_SOCK_STREAM 1
21 #endif /* defined(SOCK_STREAM) || defined(__GNU_LIBRARY__) */
23 #if defined(USE_SOCK_STREAM)
24 # if NETINET || NETINET6
25 # include <arpa/inet.h>
26 # endif /* NETINET || NETINET6 */
29 # define NO_DATA NO_ADDRESS
30 # endif /* ! NO_DATA */
31 # endif /* NAMED_BIND */
32 #endif /* defined(USE_SOCK_STREAM) */
35 # include <openssl/rand.h>
40 #if IP_SRCROUTE && NETINET
41 # include <netinet/in_systm.h>
42 # include <netinet/ip.h>
44 # include <netinet/in.h>
46 # define IPOPTION ip_opts
47 # define IP_LIST ip_opts
48 # define IP_DST ip_dst
49 # endif /* ! IPOPTION */
51 # include <netinet/ip_var.h>
53 # define IPOPTION ipoption
54 # define IP_LIST ipopt_list
55 # define IP_DST ipopt_dst
56 # endif /* ! IPOPTION */
57 # endif /* HAS_IN_H */
58 #endif /* IP_SRCROUTE && NETINET */
65 static void connecttimeout __P((int));
66 static int opendaemonsocket __P((DAEMON_T *, bool));
67 static unsigned short setupdaemon __P((SOCKADDR *));
68 static void getrequests_checkdiskspace __P((ENVELOPE *e));
69 static void setsockaddroptions __P((char *, DAEMON_T *));
70 static void printdaemonflags __P((DAEMON_T *));
71 static int addr_family __P((char *));
72 static int addrcmp __P((struct hostent *, char *, SOCKADDR *));
73 static void authtimeout __P((int));
76 ** DAEMON.C -- routines to use when running as a daemon.
78 ** This entire file is highly dependent on the 4.2 BSD
79 ** interprocess communication primitives. No attempt has
80 ** been made to make this file portable to Version 7,
81 ** Version 6, MPX files, etc. If you should try such a
82 ** thing yourself, I recommend chucking the entire file
83 ** and starting from scratch. Basic semantics are:
86 ** Opens a port and initiates a connection.
87 ** Returns in a child. Must set InChannel and
88 ** OutChannel appropriately.
90 ** Close any open files associated with getting
91 ** the connection; this is used when running the queue,
92 ** etc., to avoid having extra file descriptors during
93 ** the queue run and to avoid confusing the network
94 ** code (if it cares).
95 ** makeconnection(host, port, mci, e, enough)
96 ** Make a connection to the named host on the given
97 ** port. Returns zero on success, else an exit status
98 ** describing the error.
99 ** host_map_lookup(map, hbuf, avp, pstat)
100 ** Convert the entry in hbuf into a canonical form.
103 static int NDaemons = 0; /* actual number of daemons */
105 static time_t NextDiskSpaceCheck = 0;
108 ** GETREQUESTS -- open mail IPC port and get requests.
111 ** e -- the current envelope.
117 ** Waits until some interesting activity occurs. When
118 ** it does, a child is created to process it, and the
119 ** parent waits for completion. Return from this
120 ** routine is always in the child. The file pointers
121 ** "InChannel" and "OutChannel" should be set to point
122 ** to the communication channel.
123 ** May restart persistent queue runners if they have ended
132 int idx, curdaemon = -1;
133 int i, olddaemon = 0;
137 char status[MAXLINE];
139 SOCKADDR_LEN_T len = sizeof(sa);
140 #if _FFR_QUEUE_RUN_PARANOIA
142 #endif /* _FFR_QUEUE_RUN_PARANOIA */
144 extern int ControlSocket;
145 # endif /* NETUNIX */
146 extern ENVELOPE BlankEnvelope;
149 /* initialize data for function that generates queue ids */
151 for (idx = 0; idx < NDaemons; idx++)
153 Daemons[idx].d_port = setupdaemon(&(Daemons[idx].d_addr));
154 Daemons[idx].d_firsttime = true;
155 Daemons[idx].d_refuse_connections_until = (time_t) 0;
159 ** Try to actually open the connection.
164 for (idx = 0; idx < NDaemons; idx++)
166 sm_dprintf("getrequests: daemon %s: port %d\n",
168 ntohs(Daemons[idx].d_port));
172 /* get a socket for the SMTP connection */
173 for (idx = 0; idx < NDaemons; idx++)
174 Daemons[idx].d_socksize = opendaemonsocket(&Daemons[idx], true);
176 if (opencontrolsocket() < 0)
177 sm_syslog(LOG_WARNING, NOQID,
178 "daemon could not open control socket %s: %s",
179 ControlSocketName, sm_errstring(errno));
181 /* If there are any queue runners released reapchild() co-ord's */
182 (void) sm_signal(SIGCHLD, reapchild);
184 /* write the pid to file, command line args to syslog */
189 char jbuf[MAXHOSTNAMELEN];
191 expand("\201j", jbuf, sizeof(jbuf), e);
192 j_has_dot = strchr(jbuf, '.') != NULL;
196 /* Add parent process as first item */
197 proc_list_add(CurrentPid, "Sendmail daemon", PROC_DAEMON, 0, -1, NULL);
201 for (idx = 0; idx < NDaemons; idx++)
202 sm_dprintf("getrequests: daemon %s: %d\n",
204 Daemons[idx].d_socket);
210 auto SOCKADDR_LEN_T lotherend;
211 bool timedout = false;
212 bool control = false;
218 #endif /* STARTTLS */
220 /* see if we are rejecting connections */
221 (void) sm_blocksignal(SIGALRM);
224 for (idx = 0; idx < NDaemons; idx++)
227 ** XXX do this call outside the loop?
228 ** no: refuse_connections may sleep().
232 if (now < Daemons[idx].d_refuse_connections_until)
234 if (bitnset(D_DISABLE, Daemons[idx].d_flags))
236 if (refuseconnections(e, idx, curdaemon == idx))
238 if (Daemons[idx].d_socket >= 0)
240 /* close socket so peer fails quickly */
241 (void) close(Daemons[idx].d_socket);
242 Daemons[idx].d_socket = -1;
245 /* refuse connections for next 15 seconds */
246 Daemons[idx].d_refuse_connections_until = now + 15;
248 else if (Daemons[idx].d_socket < 0 ||
249 Daemons[idx].d_firsttime)
251 if (!Daemons[idx].d_firsttime && LogLevel > 8)
252 sm_syslog(LOG_INFO, NOQID,
253 "accepting connections again for daemon %s",
254 Daemons[idx].d_name);
256 /* arrange to (re)open the socket if needed */
257 (void) opendaemonsocket(&Daemons[idx], false);
258 Daemons[idx].d_firsttime = false;
262 /* May have been sleeping above, check again */
265 getrequests_checkdiskspace(e);
268 /* check for disaster */
270 char jbuf[MAXHOSTNAMELEN];
272 expand("\201j", jbuf, sizeof(jbuf), e);
273 if (!wordinclass(jbuf, 'w'))
275 dumpstate("daemon lost $j");
276 sm_syslog(LOG_ALERT, NOQID,
277 "daemon process doesn't have $j in $=w; see syslog");
280 else if (j_has_dot && strchr(jbuf, '.') == NULL)
282 dumpstate("daemon $j lost dot");
283 sm_syslog(LOG_ALERT, NOQID,
284 "daemon process $j lost dot; see syslog");
292 ** Andrew Sun <asun@ieps-sun.ml.com> claims that this will
293 ** fix the SVr4 problem. But it seems to have gone away,
294 ** so is it worth doing this?
297 if (DaemonSocket >= 0 &&
298 SetNonBlocking(DaemonSocket, false) < 0)
301 (void) sm_releasesignal(SIGALRM);
305 bool setproc = false;
308 struct timeval timeout;
312 for (idx = 0; idx < NDaemons; idx++)
314 /* wait for a connection */
315 if (Daemons[idx].d_socket >= 0)
319 Daemons[idx].d_flags))
321 sm_setproctitle(true, e,
322 "accepting connections");
325 if (Daemons[idx].d_socket > highest)
326 highest = Daemons[idx].d_socket;
327 SM_FD_SET(Daemons[idx].d_socket,
333 if (ControlSocket >= 0)
335 if (ControlSocket > highest)
336 highest = ControlSocket;
337 SM_FD_SET(ControlSocket, &readfds);
344 t = select(highest + 1, FDSET_CAST &readfds,
345 NULL, NULL, &timeout);
347 /* Did someone signal while waiting? */
353 (void) runqueue(true, false, false, false);
354 #if _FFR_QUEUE_RUN_PARANOIA
356 #endif /* _FFR_QUEUE_RUN_PARANOIA */
358 #if _FFR_QUEUE_RUN_PARANOIA
359 else if (CheckQueueRunners > 0 && QueueIntvl > 0 &&
360 lastrun + QueueIntvl + CheckQueueRunners < now)
364 ** set lastrun unconditionally to avoid
365 ** calling checkqueuerunner() all the time.
366 ** That's also why we currently ignore the
367 ** result of the function call.
370 (void) checkqueuerunner();
373 #endif /* _FFR_QUEUE_RUN_PARANOIA */
384 /* look "round-robin" for an active socket */
385 if ((idx = olddaemon + 1) >= NDaemons)
387 for (i = 0; i < NDaemons; i++)
389 if (Daemons[idx].d_socket >= 0 &&
390 SM_FD_ISSET(Daemons[idx].d_socket,
393 lotherend = Daemons[idx].d_socksize;
394 memset(&RealHostAddr, '\0',
395 sizeof(RealHostAddr));
396 t = accept(Daemons[idx].d_socket,
397 (struct sockaddr *)&RealHostAddr,
401 ** If remote side closes before
402 ** accept() finishes, sockaddr
403 ** might not be fully filled in.
408 # ifdef BSD4_4_SOCKADDR
409 RealHostAddr.sa.sa_len == 0 ||
410 # endif /* BSD4_4_SOCKADDR */
411 RealHostAddr.sa.sa_family != Daemons[idx].d_addr.sa.sa_family))
417 olddaemon = curdaemon = idx;
420 if (++idx >= NDaemons)
424 if (curdaemon == -1 && ControlSocket >= 0 &&
425 SM_FD_ISSET(ControlSocket, &readfds))
427 struct sockaddr_un sa_un;
429 lotherend = sizeof(sa_un);
430 memset(&sa_un, '\0', sizeof(sa_un));
431 t = accept(ControlSocket,
432 (struct sockaddr *)&sa_un,
436 ** If remote side closes before
437 ** accept() finishes, sockaddr
438 ** might not be fully filled in.
443 # ifdef BSD4_4_SOCKADDR
444 sa_un.sun_len == 0 ||
445 # endif /* BSD4_4_SOCKADDR */
446 sa_un.sun_family != AF_UNIX))
458 /* No daemon to service */
462 if (t >= 0 || errno != EINTR)
471 (void) sm_blocksignal(SIGALRM);
476 /* let's ignore these temporary errors */
477 if (save_errno == EINTR
479 || save_errno == EAGAIN
482 || save_errno == ECONNABORTED
483 #endif /* ECONNABORTED */
485 || save_errno == EWOULDBLOCK
486 #endif /* EWOULDBLOCK */
490 syserr("getrequests: accept");
494 /* arrange to re-open socket next time around */
495 (void) close(Daemons[curdaemon].d_socket);
496 Daemons[curdaemon].d_socket = -1;
497 #if SO_REUSEADDR_IS_BROKEN
499 ** Give time for bound socket to be released.
500 ** This creates a denial-of-service if you can
501 ** force accept() to fail on affected systems.
504 Daemons[curdaemon].d_refuse_connections_until =
506 #endif /* SO_REUSEADDR_IS_BROKEN */
513 /* set some daemon related macros */
514 switch (Daemons[curdaemon].d_addr.sa.sa_family)
517 macdefine(&BlankEnvelope.e_macro, A_PERM,
518 macid("{daemon_family}"), "unspec");
520 #if _FFR_DAEMON_NETUNIX
523 macdefine(&BlankEnvelope.e_macro, A_PERM,
524 macid("{daemon_family}"), "local");
526 # endif /* NETUNIX */
527 #endif /* _FFR_DAEMON_NETUNIX */
530 macdefine(&BlankEnvelope.e_macro, A_PERM,
531 macid("{daemon_family}"), "inet");
536 macdefine(&BlankEnvelope.e_macro, A_PERM,
537 macid("{daemon_family}"), "inet6");
539 #endif /* NETINET6 */
542 macdefine(&BlankEnvelope.e_macro, A_PERM,
543 macid("{daemon_family}"), "iso");
548 macdefine(&BlankEnvelope.e_macro, A_PERM,
549 macid("{daemon_family}"), "ns");
554 macdefine(&BlankEnvelope.e_macro, A_PERM,
555 macid("{daemon_family}"), "x.25");
559 macdefine(&BlankEnvelope.e_macro, A_PERM,
560 macid("{daemon_name}"),
561 Daemons[curdaemon].d_name);
562 if (Daemons[curdaemon].d_mflags != NULL)
563 macdefine(&BlankEnvelope.e_macro, A_PERM,
564 macid("{daemon_flags}"),
565 Daemons[curdaemon].d_mflags);
567 macdefine(&BlankEnvelope.e_macro, A_PERM,
568 macid("{daemon_flags}"), "");
572 ** If connection rate is exceeded here, connection shall be
573 ** refused later by a new call after fork() by the
574 ** validate_connection() function. Closing the connection
575 ** at this point violates RFC 2821.
576 ** Do NOT remove this call, its side effects are needed.
579 connection_rate_check(&RealHostAddr, NULL);
582 ** Create a subprocess to process the mail.
586 sm_dprintf("getrequests: forking (fd = %d)\n", t);
589 ** Advance state of PRNG.
590 ** This is necessary because otherwise all child processes
591 ** will produce the same PRN sequence and hence the selection
592 ** of a queue directory (and other things, e.g., MX selection)
593 ** are not "really" random.
596 /* XXX get some better "random" data? */
598 RAND_seed((void *) &NextDiskSpaceCheck,
599 sizeof(NextDiskSpaceCheck));
600 RAND_seed((void *) &now, sizeof(now));
601 RAND_seed((void *) &seed, sizeof(seed));
604 #endif /* STARTTLS */
608 ** Update MX records for FallbackMX.
609 ** Let's hope this is fast otherwise we screw up the
613 if (FallbackMX != NULL)
614 (void) getfallbackmxrr(FallbackMX);
615 #endif /* NAMED_BIND */
619 /* don't fork, handle connection in this process */
621 pipefd[0] = pipefd[1] = -1;
626 ** Create a pipe to keep the child from writing to
627 ** the socket until after the parent has closed
628 ** it. Otherwise the parent may hang if the child
629 ** has closed it first.
632 if (pipe(pipefd) < 0)
633 pipefd[0] = pipefd[1] = -1;
635 (void) sm_blocksignal(SIGCHLD);
639 syserr("daemon: cannot fork");
642 (void) close(pipefd[0]);
643 (void) close(pipefd[1]);
645 (void) sm_releasesignal(SIGCHLD);
655 SM_FILE_T *inchannel, *outchannel = NULL;
658 ** CHILD -- return to caller.
659 ** Collect verified idea of sending host.
660 ** Verify calling user id if possible here.
663 /* Reset global flags */
664 RestartRequest = NULL;
665 RestartWorkGroup = false;
666 ShutdownRequest = NULL;
668 CurrentPid = getpid();
669 close_sendmail_pid();
671 (void) sm_releasesignal(SIGALRM);
672 (void) sm_releasesignal(SIGCHLD);
673 (void) sm_signal(SIGCHLD, SIG_DFL);
674 (void) sm_signal(SIGHUP, SIG_DFL);
675 (void) sm_signal(SIGTERM, intsig);
677 /* turn on profiling */
681 ** Initialize exception stack and default exception
682 ** handler for child process.
685 sm_exc_newthread(fatal_error);
689 macdefine(&BlankEnvelope.e_macro, A_TEMP,
690 macid("{daemon_addr}"),
691 anynet_ntoa(&Daemons[curdaemon].d_addr));
692 (void) sm_snprintf(status, sizeof(status), "%d",
693 ntohs(Daemons[curdaemon].d_port));
694 macdefine(&BlankEnvelope.e_macro, A_TEMP,
695 macid("{daemon_port}"), status);
698 for (idx = 0; idx < NDaemons; idx++)
700 if (Daemons[idx].d_socket >= 0)
701 (void) close(Daemons[idx].d_socket);
702 Daemons[idx].d_socket = -1;
706 /* Avoid SMTP daemon actions if control command */
709 /* Add control socket process */
710 proc_list_add(CurrentPid,
711 "console socket child",
712 PROC_CONTROL_CHILD, 0, -1, NULL);
718 /* clean up background delivery children */
719 (void) sm_signal(SIGCHLD, reapchild);
721 /* Add parent process as first child item */
722 proc_list_add(CurrentPid, "daemon child",
723 PROC_DAEMON_CHILD, 0, -1, NULL);
724 /* don't schedule queue runs if ETRN */
728 ** Hack: override global variables if
729 ** the corresponding DaemonPortOption
732 #if _FFR_SS_PER_DAEMON
733 if (Daemons[curdaemon].d_supersafe !=
735 SuperSafe = Daemons[curdaemon].
737 #endif /* _FFR_SS_PER_DAEMON */
738 if (Daemons[curdaemon].d_dm != DM_NOTSET)
740 Daemons[curdaemon].d_dm, e);
742 if (Daemons[curdaemon].d_refuseLA !=
744 RefuseLA = Daemons[curdaemon].
746 if (Daemons[curdaemon].d_queueLA != DPO_NOTSET)
747 QueueLA = Daemons[curdaemon].d_queueLA;
748 if (Daemons[curdaemon].d_delayLA != DPO_NOTSET)
749 DelayLA = Daemons[curdaemon].d_delayLA;
750 if (Daemons[curdaemon].d_maxchildren !=
752 MaxChildren = Daemons[curdaemon].
755 sm_setproctitle(true, e, "startup with %s",
756 anynet_ntoa(&RealHostAddr));
764 ** Wait for the parent to close the write end
765 ** of the pipe, which we will see as an EOF.
766 ** This guarantees that we won't write to the
767 ** socket until after the parent has closed
771 /* close the write end of the pipe */
772 (void) close(pipefd[1]);
774 /* we shouldn't be interrupted, but ... */
775 while (read(pipefd[0], &c, 1) < 0 &&
778 (void) close(pipefd[0]);
781 /* control socket processing */
784 control_command(t, e);
789 /* determine host name */
790 p = hostnamebyanyaddr(&RealHostAddr);
791 if (strlen(p) > MAXNAME) /* XXX - 1 ? */
793 RealHostName = newstr(p);
794 if (RealHostName[0] == '[')
796 macdefine(&BlankEnvelope.e_macro, A_PERM,
797 macid("{client_resolve}"),
798 h_errno == TRY_AGAIN ? "TEMP" : "FAIL");
802 macdefine(&BlankEnvelope.e_macro, A_PERM,
803 macid("{client_resolve}"), "OK");
805 sm_setproctitle(true, e, "startup with %s", p);
806 markstats(e, NULL, STATS_CONNECT);
808 if ((inchannel = sm_io_open(SmFtStdiofd,
814 (outchannel = sm_io_open(SmFtStdiofd,
820 syserr("cannot open SMTP server channel, fd=%d",
822 finis(false, true, EX_OK);
824 sm_io_automode(inchannel, outchannel);
826 InChannel = inchannel;
827 OutChannel = outchannel;
828 DisConnected = false;
831 if (!xla_host_ok(RealHostName))
833 message("421 4.4.5 Too many SMTP sessions for this host");
834 finis(false, true, EX_OK);
837 /* find out name for interface of connection */
838 if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
839 NULL), &sa.sa, &len) == 0)
841 p = hostnamebyanyaddr(&sa);
843 sm_dprintf("getreq: got name %s\n", p);
844 macdefine(&BlankEnvelope.e_macro, A_TEMP,
845 macid("{if_name}"), p);
848 ** Do this only if it is not the loopback
857 addr = anynet_ntoa(&sa);
858 (void) sm_snprintf(family,
860 "%d", sa.sa.sa_family);
861 macdefine(&BlankEnvelope.e_macro,
863 macid("{if_addr}"), addr);
864 macdefine(&BlankEnvelope.e_macro,
866 macid("{if_family}"), family);
868 sm_dprintf("getreq: got addr %s and family %s\n",
873 macdefine(&BlankEnvelope.e_macro,
875 macid("{if_addr}"), NULL);
876 macdefine(&BlankEnvelope.e_macro,
878 macid("{if_family}"), NULL);
884 sm_dprintf("getreq: getsockname failed\n");
885 macdefine(&BlankEnvelope.e_macro, A_PERM,
886 macid("{if_name}"), NULL);
887 macdefine(&BlankEnvelope.e_macro, A_PERM,
888 macid("{if_addr}"), NULL);
889 macdefine(&BlankEnvelope.e_macro, A_PERM,
890 macid("{if_family}"), NULL);
895 /* parent -- keep track of children */
898 (void) sm_snprintf(status, sizeof(status),
899 "control socket server child");
900 proc_list_add(pid, status, PROC_CONTROL, 0, -1, NULL);
904 (void) sm_snprintf(status, sizeof(status),
905 "SMTP server child for %s",
906 anynet_ntoa(&RealHostAddr));
907 proc_list_add(pid, status, PROC_DAEMON, 0, -1,
910 (void) sm_releasesignal(SIGCHLD);
912 /* close the read end of the synchronization pipe */
915 (void) close(pipefd[0]);
919 /* close the port so that others will hang (for a while) */
922 /* release the child by closing the read end of the sync pipe */
925 (void) close(pipefd[1]);
930 sm_dprintf("getreq: returning\n");
933 /* set the filters for this daemon */
934 if (Daemons[curdaemon].d_inputfilterlist != NULL)
938 Daemons[curdaemon].d_inputfilters[i] != NULL);
941 InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
944 InputFilters[i] = NULL;
947 return &Daemons[curdaemon].d_flags;
951 ** GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
960 ** Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
964 getrequests_checkdiskspace(e)
972 if (now < NextDiskSpaceCheck)
975 /* Check if there is available disk space in all queue groups. */
976 if (!enoughdiskspace(0, NULL))
978 for (idx = 0; idx < NDaemons; ++idx)
980 if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
983 /* log only if not logged before */
987 sm_syslog(LOG_INFO, NOQID,
988 "rejecting new messages: min free: %ld",
990 sm_setproctitle(true, e,
991 "rejecting new messages: min free: %ld",
995 setbitn(D_ETRNONLY, Daemons[idx].d_flags);
1000 for (idx = 0; idx < NDaemons; ++idx)
1002 if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
1005 /* log only if not logged before */
1009 sm_syslog(LOG_INFO, NOQID,
1010 "accepting new messages (again)");
1014 /* title will be set later */
1015 clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
1019 /* only check disk space once a minute */
1020 NextDiskSpaceCheck = now + 60;
1024 ** OPENDAEMONSOCKET -- open SMTP socket
1026 ** Deals with setting all appropriate options.
1029 ** d -- the structure for the daemon to open.
1030 ** firsttime -- set if this is the initial open.
1033 ** Size in bytes of the daemon socket addr.
1036 ** Leaves DaemonSocket set to the open socket.
1037 ** Exits if the socket cannot be created.
1040 #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
1043 opendaemonsocket(d, firsttime)
1049 SOCKADDR_LEN_T socksize = 0;
1054 sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
1060 if (firsttime || d->d_socket < 0)
1062 #if _FFR_DAEMON_NETUNIX
1064 if (d->d_addr.sa.sa_family == AF_UNIX)
1067 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
1069 /* if not safe, don't use it */
1070 rval = safefile(d->d_addr.sunix.sun_path,
1073 S_IRUSR|S_IWUSR, NULL);
1077 syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
1079 d->d_addr.sunix.sun_path);
1083 /* Don't try to overtake an existing socket */
1084 (void) unlink(d->d_addr.sunix.sun_path);
1086 # endif /* NETUNIX */
1087 #endif /* _FFR_DOMAIN_NETUNIX */
1088 d->d_socket = socket(d->d_addr.sa.sa_family,
1090 if (d->d_socket < 0)
1093 syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
1096 if (bitnset(D_OPTIONAL, d->d_flags) &&
1097 (!transienterror(save_errno) ||
1098 ntries >= MAXOPENTRIES - 1))
1100 syserr("opendaemonsocket: daemon %s: optional socket disabled",
1102 setbitn(D_DISABLE, d->d_flags);
1108 sm_syslog(LOG_ALERT, NOQID,
1109 "daemon %s: problem creating SMTP socket",
1115 if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
1117 save_errno = EINVAL;
1118 syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
1119 d->d_name, d->d_socket);
1123 /* turn on network debugging? */
1125 (void) setsockopt(d->d_socket, SOL_SOCKET,
1126 SO_DEBUG, (char *)&on,
1129 (void) setsockopt(d->d_socket, SOL_SOCKET,
1130 SO_REUSEADDR, (char *)&on, sizeof(on));
1131 (void) setsockopt(d->d_socket, SOL_SOCKET,
1132 SO_KEEPALIVE, (char *)&on, sizeof(on));
1135 if (d->d_tcprcvbufsize > 0)
1137 if (setsockopt(d->d_socket, SOL_SOCKET,
1139 (char *) &d->d_tcprcvbufsize,
1140 sizeof(d->d_tcprcvbufsize)) < 0)
1141 syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
1143 #endif /* SO_RCVBUF */
1145 if (d->d_tcpsndbufsize > 0)
1147 if (setsockopt(d->d_socket, SOL_SOCKET,
1149 (char *) &d->d_tcpsndbufsize,
1150 sizeof(d->d_tcpsndbufsize)) < 0)
1151 syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
1153 #endif /* SO_SNDBUF */
1155 if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
1156 fcntl(d->d_socket, F_SETFD,
1157 fdflags | FD_CLOEXEC) == -1)
1160 syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
1162 fdflags == -1 ? "get" : "set",
1163 sm_errstring(save_errno));
1164 (void) close(d->d_socket);
1168 switch (d->d_addr.sa.sa_family)
1170 #if _FFR_DAEMON_NETUNIX
1173 socksize = sizeof(d->d_addr.sunix);
1175 # endif /* NETUNIX */
1176 #endif /* _FFR_DAEMON_NETUNIX */
1179 socksize = sizeof(d->d_addr.sin);
1181 #endif /* NETINET */
1185 socksize = sizeof(d->d_addr.sin6);
1187 #endif /* NETINET6 */
1191 socksize = sizeof(d->d_addr.siso);
1196 socksize = sizeof(d->d_addr);
1200 if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
1202 /* probably another daemon already */
1204 syserr("opendaemonsocket: daemon %s: cannot bind",
1206 (void) close(d->d_socket);
1211 listen(d->d_socket, d->d_listenqueue) < 0)
1214 syserr("opendaemonsocket: daemon %s: cannot listen",
1216 (void) close(d->d_socket);
1220 } while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
1221 syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
1224 return -1; /* avoid compiler warning on IRIX */
1227 ** SETUPDAEMON -- setup socket for daemon
1230 ** daemonaddr -- socket for daemon
1233 ** port number on which daemon should run
1237 static unsigned short
1238 setupdaemon(daemonaddr)
1239 SOCKADDR *daemonaddr;
1241 unsigned short port;
1244 ** Set up the address for the mailer.
1247 if (daemonaddr->sa.sa_family == AF_UNSPEC)
1249 memset(daemonaddr, '\0', sizeof(*daemonaddr));
1251 daemonaddr->sa.sa_family = AF_INET;
1252 #endif /* NETINET */
1255 switch (daemonaddr->sa.sa_family)
1259 if (daemonaddr->sin.sin_addr.s_addr == 0)
1260 daemonaddr->sin.sin_addr.s_addr =
1261 LocalDaemon ? htonl(INADDR_LOOPBACK) : INADDR_ANY;
1262 port = daemonaddr->sin.sin_port;
1264 #endif /* NETINET */
1268 if (IN6_IS_ADDR_UNSPECIFIED(&daemonaddr->sin6.sin6_addr))
1269 daemonaddr->sin6.sin6_addr =
1270 LocalDaemon ? in6addr_loopback : in6addr_any;
1271 port = daemonaddr->sin6.sin6_port;
1273 #endif /* NETINET6 */
1276 /* unknown protocol */
1282 #ifdef NO_GETSERVBYNAME
1284 #else /* NO_GETSERVBYNAME */
1286 register struct servent *sp;
1288 sp = getservbyname("smtp", "tcp");
1291 syserr("554 5.3.5 service \"smtp\" unknown");
1297 #endif /* NO_GETSERVBYNAME */
1300 switch (daemonaddr->sa.sa_family)
1304 daemonaddr->sin.sin_port = port;
1306 #endif /* NETINET */
1310 daemonaddr->sin6.sin6_port = port;
1312 #endif /* NETINET6 */
1315 /* unknown protocol */
1321 ** CLRDAEMON -- reset the daemon connection
1330 ** releases any resources used by the passive daemon.
1338 for (i = 0; i < NDaemons; i++)
1340 if (Daemons[i].d_socket >= 0)
1341 (void) close(Daemons[i].d_socket);
1342 Daemons[i].d_socket = -1;
1347 ** GETMODIFIERS -- get modifier flags
1350 ** v -- the modifiers (input text line).
1351 ** modifiers -- pointer to flag field to represent modifiers.
1354 ** (xallocat()ed) string representation of modifiers.
1357 ** fills in modifiers.
1361 getmodifiers(v, modifiers)
1363 BITMAP256 modifiers;
1366 char *h, *f, *flags;
1368 /* maximum length of flags: upper case Option -> "OO " */
1369 l = 3 * strlen(v) + 3;
1371 /* is someone joking? */
1372 if (l < 0 || l > 256)
1375 sm_syslog(LOG_ERR, NOQID,
1376 "getmodifiers too long, ignored");
1381 clrbitmap(modifiers);
1382 for (h = v; *h != '\0'; h++)
1384 if (isascii(*h) && !isspace(*h) && isprint(*h))
1386 setbitn(*h, modifiers);
1399 ** CHKDAEMONMODIFIERS -- check whether all daemons have set a flag.
1402 ** flag -- the flag to test.
1405 ** true iff all daemons have set flag.
1409 chkdaemonmodifiers(flag)
1414 for (i = 0; i < NDaemons; i++)
1415 if (!bitnset((char) flag, Daemons[i].d_flags))
1421 ** SETSOCKADDROPTIONS -- set options for SOCKADDR (daemon or client)
1424 ** p -- the options line.
1425 ** d -- the daemon structure to fill in.
1432 setsockaddroptions(p, d)
1443 if (d->d_addr.sa.sa_family == AF_UNSPEC)
1444 d->d_addr.sa.sa_family = AF_INET;
1445 #endif /* NETINET */
1446 #if _FFR_SS_PER_DAEMON
1447 d->d_supersafe = DPO_NOTSET;
1448 #endif /* _FFR_SS_PER_DAEMON */
1449 d->d_dm = DM_NOTSET;
1450 d->d_refuseLA = DPO_NOTSET;
1451 d->d_queueLA = DPO_NOTSET;
1452 d->d_delayLA = DPO_NOTSET;
1453 d->d_maxchildren = DPO_NOTSET;
1460 while (isascii(*p) && isspace(*p))
1471 while (isascii(*++v) && isspace(*v))
1476 case 'A': /* address */
1479 #endif /* !_FFR_DPO_CS */
1484 d->d_maxchildren = atoi(v);
1487 case 'D': /* DeliveryMode */
1497 syserr("554 5.3.5 Unknown delivery mode %c",
1503 case 'd': /* delayLA */
1504 d->d_delayLA = atoi(v);
1507 case 'F': /* address family */
1510 #endif /* !_FFR_DPO_CS */
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",
1549 # endif /* !_FFR_DPO_CS */
1550 d->d_inputfilterlist = v;
1554 case 'L': /* listen queue size */
1557 #endif /* !_FFR_DPO_CS */
1558 d->d_listenqueue = atoi(v);
1561 case 'M': /* modifiers (flags) */
1564 #endif /* !_FFR_DPO_CS */
1565 d->d_mflags = getmodifiers(v, d->d_flags);
1568 case 'N': /* name */
1571 #endif /* !_FFR_DPO_CS */
1575 case 'P': /* port */
1578 #endif /* !_FFR_DPO_CS */
1583 d->d_queueLA = atoi(v);
1586 case 'R': /* receive buffer size */
1587 d->d_tcprcvbufsize = atoi(v);
1591 d->d_refuseLA = atoi(v);
1594 case 'S': /* send buffer size */
1597 #endif /* !_FFR_DPO_CS */
1598 d->d_tcpsndbufsize = atoi(v);
1601 #if _FFR_SS_PER_DAEMON
1602 case 'T': /* SuperSafe */
1603 if (tolower(*v) == 'i')
1604 d->d_supersafe = SAFE_INTERACTIVE;
1605 else if (tolower(*v) == 'p')
1607 d->d_supersafe = SAFE_REALLY_POSTMILTER;
1609 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1610 "Warning: SuperSafe=PostMilter requires Milter support (-DMILTER)\n");
1611 # endif /* MILTER */
1613 d->d_supersafe = atobool(v) ? SAFE_REALLY
1616 #endif /* _FFR_SS_PER_DAEMON */
1619 syserr("554 5.3.5 PortOptions parameter \"%s\" unknown",
1624 /* Check addr and port after finding family */
1627 switch (d->d_addr.sa.sa_family)
1629 #if _FFR_DAEMON_NETUNIX
1632 if (strlen(addr) >= sizeof(d->d_addr.sunix.sun_path))
1634 errno = ENAMETOOLONG;
1635 syserr("setsockaddroptions: domain socket name too long: %s > %d",
1636 addr, sizeof(d->d_addr.sunix.sun_path));
1640 /* file safety check done in opendaemonsocket() */
1641 (void) memset(&d->d_addr.sunix.sun_path, '\0',
1642 sizeof(d->d_addr.sunix.sun_path));
1643 (void) sm_strlcpy((char *)&d->d_addr.sunix.sun_path,
1645 sizeof(d->d_addr.sunix.sun_path));
1647 # endif /* NETUNIX */
1648 #endif /* _FFR_DAEMON_NETUNIX */
1651 if (!isascii(*addr) || !isdigit(*addr) ||
1652 ((d->d_addr.sin.sin_addr.s_addr = inet_addr(addr))
1655 register struct hostent *hp;
1657 hp = sm_gethostbyname(addr, AF_INET);
1659 syserr("554 5.3.0 host \"%s\" unknown",
1663 while (*(hp->h_addr_list) != NULL &&
1664 hp->h_addrtype != AF_INET)
1666 if (*(hp->h_addr_list) == NULL)
1667 syserr("554 5.3.0 host \"%s\" unknown",
1670 memmove(&d->d_addr.sin.sin_addr,
1676 # endif /* NETINET6 */
1680 #endif /* NETINET */
1684 if (anynet_pton(AF_INET6, addr,
1685 &d->d_addr.sin6.sin6_addr) != 1)
1687 register struct hostent *hp;
1689 hp = sm_gethostbyname(addr, AF_INET6);
1691 syserr("554 5.3.0 host \"%s\" unknown",
1695 while (*(hp->h_addr_list) != NULL &&
1696 hp->h_addrtype != AF_INET6)
1698 if (*(hp->h_addr_list) == NULL)
1699 syserr("554 5.3.0 host \"%s\" unknown",
1702 memmove(&d->d_addr.sin6.sin6_addr,
1710 #endif /* NETINET6 */
1713 syserr("554 5.3.5 address= option unsupported for family %d",
1714 d->d_addr.sa.sa_family);
1721 switch (d->d_addr.sa.sa_family)
1725 if (isascii(*port) && isdigit(*port))
1726 d->d_addr.sin.sin_port = htons((unsigned short)
1727 atoi((const char *) port));
1730 # ifdef NO_GETSERVBYNAME
1731 syserr("554 5.3.5 invalid port number: %s",
1733 # else /* NO_GETSERVBYNAME */
1734 register struct servent *sp;
1736 sp = getservbyname(port, "tcp");
1738 syserr("554 5.3.5 service \"%s\" unknown",
1741 d->d_addr.sin.sin_port = sp->s_port;
1742 # endif /* NO_GETSERVBYNAME */
1745 #endif /* NETINET */
1749 if (isascii(*port) && isdigit(*port))
1750 d->d_addr.sin6.sin6_port = htons((unsigned short)
1754 # ifdef NO_GETSERVBYNAME
1755 syserr("554 5.3.5 invalid port number: %s",
1757 # else /* NO_GETSERVBYNAME */
1758 register struct servent *sp;
1760 sp = getservbyname(port, "tcp");
1762 syserr("554 5.3.5 service \"%s\" unknown",
1765 d->d_addr.sin6.sin6_port = sp->s_port;
1766 # endif /* NO_GETSERVBYNAME */
1769 #endif /* NETINET6 */
1773 /* assume two byte transport selector */
1774 if (isascii(*port) && isdigit(*port))
1775 portno = htons((unsigned short) atoi(port));
1778 # ifdef NO_GETSERVBYNAME
1779 syserr("554 5.3.5 invalid port number: %s",
1781 # else /* NO_GETSERVBYNAME */
1782 register struct servent *sp;
1784 sp = getservbyname(port, "tcp");
1786 syserr("554 5.3.5 service \"%s\" unknown",
1789 portno = sp->s_port;
1790 # endif /* NO_GETSERVBYNAME */
1792 memmove(TSEL(&d->d_addr.siso),
1793 (char *) &portno, 2);
1798 syserr("554 5.3.5 Port= option unsupported for family %d",
1799 d->d_addr.sa.sa_family);
1805 ** SETDAEMONOPTIONS -- set options for running the MTA daemon
1808 ** p -- the options line.
1811 ** true if successful, false otherwise.
1814 ** increments number of daemons.
1817 #define DEF_LISTENQUEUE 10
1825 static struct dflags DaemonFlags[] =
1827 { "AUTHREQ", D_AUTHREQ },
1828 { "BINDIF", D_BINDIF },
1829 { "CANONREQ", D_CANONREQ },
1830 { "IFNHELO", D_IFNHELO },
1831 { "FQMAIL", D_FQMAIL },
1832 { "FQRCPT", D_FQRCPT },
1833 { "SMTPS", D_SMTPS },
1834 { "UNQUALOK", D_UNQUALOK },
1835 { "NOAUTH", D_NOAUTH },
1836 { "NOCANON", D_NOCANON },
1837 { "NOETRN", D_NOETRN },
1838 { "NOTLS", D_NOTLS },
1839 { "ETRNONLY", D_ETRNONLY },
1840 { "OPTIONAL", D_OPTIONAL },
1841 { "DISABLE", D_DISABLE },
1842 { "ISSET", D_ISSET },
1850 register struct dflags *df;
1853 for (df = DaemonFlags; df->d_name != NULL; df++)
1855 if (!bitnset(df->d_flag, d->d_flags))
1858 sm_dprintf("<%s", df->d_name);
1860 sm_dprintf(",%s", df->d_name);
1871 if (NDaemons >= MAXDAEMONS)
1873 Daemons[NDaemons].d_socket = -1;
1874 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1875 clrbitmap(Daemons[NDaemons].d_flags);
1876 setsockaddroptions(p, &Daemons[NDaemons]);
1879 if (Daemons[NDaemons].d_inputfilterlist != NULL)
1880 Daemons[NDaemons].d_inputfilterlist = newstr(Daemons[NDaemons].d_inputfilterlist);
1883 if (Daemons[NDaemons].d_name != NULL)
1884 Daemons[NDaemons].d_name = newstr(Daemons[NDaemons].d_name);
1889 (void) sm_snprintf(num, sizeof(num), "Daemon%d", NDaemons);
1890 Daemons[NDaemons].d_name = newstr(num);
1895 sm_dprintf("Daemon %s flags: ", Daemons[NDaemons].d_name);
1896 printdaemonflags(&Daemons[NDaemons]);
1903 ** INITDAEMON -- initialize daemon if not yet done.
1912 ** initializes structure for one daemon.
1920 Daemons[NDaemons].d_socket = -1;
1921 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1922 Daemons[NDaemons].d_name = "Daemon0";
1927 ** SETCLIENTOPTIONS -- set options for running the client
1930 ** p -- the options line.
1936 static DAEMON_T ClientSettings[AF_MAX + 1];
1945 memset(&d, '\0', sizeof(d));
1946 setsockaddroptions(p, &d);
1948 /* grab what we need */
1949 family = d.d_addr.sa.sa_family;
1950 STRUCTCOPY(d, ClientSettings[family]);
1951 setbitn(D_ISSET, ClientSettings[family].d_flags); /* mark as set */
1952 if (d.d_name != NULL)
1953 ClientSettings[family].d_name = newstr(d.d_name);
1958 (void) sm_snprintf(num, sizeof(num), "Client%d", family);
1959 ClientSettings[family].d_name = newstr(num);
1963 ** ADDR_FAMILY -- determine address family from address
1966 ** addr -- the string representation of the address
1969 ** AF_INET, AF_INET6 or AF_UNSPEC
1981 #endif /* NETINET6 */
1984 if (inet_addr(addr) != INADDR_NONE)
1987 sm_dprintf("addr_family(%s): INET\n", addr);
1990 #endif /* NETINET */
1992 if (anynet_pton(AF_INET6, addr, &clt_addr.sin6.sin6_addr) == 1)
1995 sm_dprintf("addr_family(%s): INET6\n", addr);
1998 #endif /* NETINET6 */
1999 #if _FFR_DAEMON_NETUNIX
2004 sm_dprintf("addr_family(%s): LOCAL\n", addr);
2007 # endif /* NETUNIX */
2008 #endif /* _FFR_DAEMON_NETUNIX */
2010 sm_dprintf("addr_family(%s): UNSPEC\n", addr);
2015 ** CHKCLIENTMODIFIERS -- check whether all clients have set a flag.
2018 ** flag -- the flag to test.
2021 ** true iff all configured clients have set the flag.
2025 chkclientmodifiers(flag)
2032 for (i = 0; i < AF_MAX; i++)
2034 if (bitnset(D_ISSET, ClientSettings[i].d_flags))
2036 if (!bitnset((char) flag, ClientSettings[i].d_flags))
2046 ** SETUP_DAEMON_FILTERS -- Parse per-socket filters
2056 setup_daemon_milters()
2060 if (OpMode == MD_SMTP)
2062 /* no need to configure the daemons */
2066 for (idx = 0; idx < NDaemons; idx++)
2068 if (Daemons[idx].d_inputfilterlist != NULL)
2070 milter_config(Daemons[idx].d_inputfilterlist,
2071 Daemons[idx].d_inputfilters,
2078 ** MAKECONNECTION -- make a connection to an SMTP socket on a machine.
2081 ** host -- the name of the host.
2082 ** port -- the port number to connect to.
2083 ** mci -- a pointer to the mail connection information
2084 ** structure to be filled in.
2085 ** e -- the current envelope.
2086 ** enough -- time at which to stop further connection attempts.
2087 ** (0 means no limit)
2090 ** An exit code telling whether the connection could be
2091 ** made and if not why not.
2097 static jmp_buf CtxConnectTimeout;
2099 SOCKADDR CurHostAddr; /* address of current host */
2102 makeconnection(host, port, mci, e, enough)
2104 volatile unsigned int port;
2109 register volatile int addrno = 0;
2111 register struct hostent *volatile hp = (struct hostent *) NULL;
2115 volatile SOCKADDR_LEN_T addrlen;
2116 volatile bool firstconnect = true;
2117 SM_EVENT *volatile ev = NULL;
2119 volatile bool v6found = false;
2120 #endif /* NETINET6 */
2121 volatile int family = InetMode;
2123 volatile SOCKADDR_LEN_T socksize = 0;
2124 volatile bool clt_bind;
2127 extern ENVELOPE BlankEnvelope;
2129 /* retranslate {daemon_flags} into bitmap */
2131 if ((p = macvalue(macid("{daemon_flags}"), e)) != NULL)
2133 for (; *p != '\0'; p++)
2135 if (!(isascii(*p) && isspace(*p)))
2136 setbitn(bitidx(*p), d_flags);
2142 #endif /* NETINET6 */
2145 /* Set up the address for outgoing connection. */
2146 if (bitnset(D_BINDIF, d_flags) &&
2147 (p = macvalue(macid("{if_addr}"), e)) != NULL &&
2151 char p6[INET6_ADDRSTRLEN];
2152 #endif /* NETINET6 */
2154 memset(&clt_addr, '\0', sizeof(clt_addr));
2156 /* infer the address family from the address itself */
2157 clt_addr.sa.sa_family = addr_family(p);
2158 switch (clt_addr.sa.sa_family)
2162 clt_addr.sin.sin_addr.s_addr = inet_addr(p);
2163 if (clt_addr.sin.sin_addr.s_addr != INADDR_NONE &&
2164 clt_addr.sin.sin_addr.s_addr != INADDR_LOOPBACK)
2167 socksize = sizeof(struct sockaddr_in);
2170 #endif /* NETINET */
2174 if (inet_addr(p) != INADDR_NONE)
2175 (void) sm_snprintf(p6, sizeof(p6),
2176 "IPv6:::ffff:%s", p);
2178 (void) sm_strlcpy(p6, p, sizeof(p6));
2179 if (anynet_pton(AF_INET6, p6,
2180 &clt_addr.sin6.sin6_addr) == 1 &&
2181 !IN6_IS_ADDR_LOOPBACK(&clt_addr.sin6.sin6_addr))
2184 socksize = sizeof(struct sockaddr_in6);
2187 #endif /* NETINET6 */
2191 syserr("554 5.3.5 Address= option unsupported for family %d",
2192 clt_addr.sa.sa_family);
2197 family = clt_addr.sa.sa_family;
2200 /* D_BINDIF not set or not available, fallback to ClientPortOptions */
2203 STRUCTCOPY(ClientSettings[family].d_addr, clt_addr);
2204 switch (clt_addr.sa.sa_family)
2208 if (clt_addr.sin.sin_addr.s_addr == 0)
2209 clt_addr.sin.sin_addr.s_addr = LocalDaemon ?
2210 htonl(INADDR_LOOPBACK) : INADDR_ANY;
2213 if (clt_addr.sin.sin_port != 0)
2215 socksize = sizeof(struct sockaddr_in);
2217 #endif /* NETINET */
2220 if (IN6_IS_ADDR_UNSPECIFIED(&clt_addr.sin6.sin6_addr))
2221 clt_addr.sin6.sin6_addr = LocalDaemon ?
2222 in6addr_loopback : in6addr_any;
2225 socksize = sizeof(struct sockaddr_in6);
2226 if (clt_addr.sin6.sin6_port != 0)
2229 #endif /* NETINET6 */
2232 socksize = sizeof(clt_addr.siso);
2242 ** Set up the address for the mailer.
2243 ** Accept "[a.b.c.d]" syntax for host name.
2248 memset(&CurHostAddr, '\0', sizeof(CurHostAddr));
2249 memset(&addr, '\0', sizeof(addr));
2250 SmtpPhase = mci->mci_phase = "initial connection";
2255 p = strchr(host, ']');
2259 unsigned long hid = INADDR_NONE;
2260 #endif /* NETINET */
2262 struct sockaddr_in6 hid6;
2263 #endif /* NETINET6 */
2267 memset(&hid6, '\0', sizeof(hid6));
2268 #endif /* NETINET6 */
2270 if (family == AF_INET &&
2271 (hid = inet_addr(&host[1])) != INADDR_NONE)
2273 addr.sin.sin_family = AF_INET;
2274 addr.sin.sin_addr.s_addr = hid;
2277 #endif /* NETINET */
2279 if (family == AF_INET6 &&
2280 anynet_pton(AF_INET6, &host[1],
2281 &hid6.sin6_addr) == 1)
2283 addr.sin6.sin6_family = AF_INET6;
2284 addr.sin6.sin6_addr = hid6.sin6_addr;
2287 #endif /* NETINET6 */
2289 /* try it as a host name (avoid MX lookup) */
2290 hp = sm_gethostbyname(&host[1], family);
2291 if (hp == NULL && p[-1] == '.')
2294 int oldopts = _res.options;
2296 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2297 #endif /* NAMED_BIND */
2299 hp = sm_gethostbyname(&host[1],
2303 _res.options = oldopts;
2304 #endif /* NAMED_BIND */
2313 extern char MsgBuf[];
2316 "553 Invalid numeric domain spec \"%s\"",
2318 mci_setstat(mci, EX_NOHOST, "5.1.2", MsgBuf);
2325 /* contortion to get around SGI cc complaints */
2327 p = &host[strlen(host) - 1];
2328 hp = sm_gethostbyname(host, family);
2329 if (hp == NULL && *p == '.')
2332 int oldopts = _res.options;
2334 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2335 #endif /* NAMED_BIND */
2337 hp = sm_gethostbyname(host, family);
2340 _res.options = oldopts;
2341 #endif /* NAMED_BIND */
2348 /* check for name server timeouts */
2350 if (WorkAroundBrokenAAAA && family == AF_INET6 &&
2354 ** An attempt with family AF_INET may
2355 ** succeed By skipping the next section
2356 ** of code, we will try AF_INET before
2361 sm_dprintf("makeconnection: WorkAroundBrokenAAAA: Trying AF_INET lookup (AF_INET6 failed)\n");
2364 # endif /* NETINET6 */
2366 if (errno == ETIMEDOUT ||
2367 # if _FFR_GETHBN_ExFILE
2370 # endif /* EMFILE */
2373 # endif /* ENFILE */
2374 # endif /* _FFR_GETHBN_ExFILE */
2375 h_errno == TRY_AGAIN ||
2376 (errno == ECONNREFUSED && UseNameServer))
2379 mci_setstat(mci, EX_TEMPFAIL,
2385 #endif /* NAMED_BIND */
2388 ** Try v6 first, then fall back to v4.
2389 ** If we found a v6 address, but no v4
2390 ** addresses, then TEMPFAIL.
2393 if (family == AF_INET6)
2400 #endif /* NETINET6 */
2402 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2406 addr.sa.sa_family = hp->h_addrtype;
2407 switch (hp->h_addrtype)
2411 memmove(&addr.sin.sin_addr,
2415 #endif /* NETINET */
2419 memmove(&addr.sin6.sin6_addr,
2423 #endif /* NETINET6 */
2426 if (hp->h_length > sizeof(addr.sa.sa_data))
2428 syserr("makeconnection: long sa_data: family %d len %d",
2429 hp->h_addrtype, hp->h_length);
2430 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2434 memmove(addr.sa.sa_data, hp->h_addr, hp->h_length);
2441 ** Determine the port number.
2446 #ifdef NO_GETSERVBYNAME
2448 #else /* NO_GETSERVBYNAME */
2449 register struct servent *sp = getservbyname("smtp", "tcp");
2454 sm_syslog(LOG_ERR, NOQID,
2455 "makeconnection: service \"smtp\" unknown");
2460 #endif /* NO_GETSERVBYNAME */
2464 if (addr.sa.sa_family == AF_INET6 &&
2465 IN6_IS_ADDR_V4MAPPED(&addr.sin6.sin6_addr) &&
2466 ClientSettings[AF_INET].d_addr.sa.sa_family != 0)
2469 ** Ignore mapped IPv4 address since
2470 ** there is a ClientPortOptions setting
2476 #endif /* NETINET6 */
2478 switch (addr.sa.sa_family)
2482 addr.sin.sin_port = port;
2483 addrlen = sizeof(struct sockaddr_in);
2485 #endif /* NETINET */
2489 addr.sin6.sin6_port = port;
2490 addrlen = sizeof(struct sockaddr_in6);
2492 #endif /* NETINET6 */
2496 /* assume two byte transport selector */
2497 memmove(TSEL((struct sockaddr_iso *) &addr), (char *) &port, 2);
2498 addrlen = sizeof(struct sockaddr_iso);
2503 syserr("Can't connect to address family %d", addr.sa.sa_family);
2504 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2509 #endif /* NETINET6 */
2514 ** Try to actually open the connection.
2518 /* if too many connections, don't bother trying */
2519 if (!xla_noqueue_ok(host))
2524 # endif /* NETINET6 */
2532 sm_dprintf("makeconnection (%s [%s].%d (%d))\n",
2533 host, anynet_ntoa(&addr), ntohs(port),
2534 (int) addr.sa.sa_family);
2536 /* save for logging */
2540 if (bitnset(M_SECURE_PORT, mci->mci_mailer->m_flags))
2542 int rport = IPPORT_RESERVED - 1;
2544 s = rresvport(&rport);
2547 #endif /* HASRRESVPORT */
2549 s = socket(addr.sa.sa_family, SOCK_STREAM, 0);
2554 syserr("makeconnection: cannot create socket");
2558 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2562 #endif /* NETINET6 */
2568 if (ClientSettings[family].d_tcpsndbufsize > 0)
2570 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
2571 (char *) &ClientSettings[family].d_tcpsndbufsize,
2572 sizeof(ClientSettings[family].d_tcpsndbufsize)) < 0)
2573 syserr("makeconnection: setsockopt(SO_SNDBUF)");
2575 #endif /* SO_SNDBUF */
2577 if (ClientSettings[family].d_tcprcvbufsize > 0)
2579 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
2580 (char *) &ClientSettings[family].d_tcprcvbufsize,
2581 sizeof(ClientSettings[family].d_tcprcvbufsize)) < 0)
2582 syserr("makeconnection: setsockopt(SO_RCVBUF)");
2584 #endif /* SO_RCVBUF */
2587 sm_dprintf("makeconnection: fd=%d\n", s);
2589 /* turn on network debugging? */
2594 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
2595 (char *)&on, sizeof(on));
2597 if (e->e_xfp != NULL) /* for debugging */
2598 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
2599 errno = 0; /* for debugging */
2605 switch (clt_addr.sa.sa_family)
2609 if (clt_addr.sin.sin_port != 0)
2610 (void) setsockopt(s, SOL_SOCKET,
2615 #endif /* NETINET */
2619 if (clt_addr.sin6.sin6_port != 0)
2620 (void) setsockopt(s, SOL_SOCKET,
2625 #endif /* NETINET6 */
2628 if (bind(s, &clt_addr.sa, socksize) < 0)
2633 syserr("makeconnection: cannot bind socket [%s]",
2634 anynet_ntoa(&clt_addr));
2638 #endif /* NETINET6 */
2645 ** Linux seems to hang in connect for 90 minutes (!!!).
2646 ** Time out the connect to avoid this problem.
2649 if (setjmp(CtxConnectTimeout) == 0)
2653 if (e->e_ntries <= 0 && TimeOuts.to_iconnect != 0)
2654 ev = sm_setevent(TimeOuts.to_iconnect,
2656 else if (TimeOuts.to_connect != 0)
2657 ev = sm_setevent(TimeOuts.to_connect,
2662 switch (ConnectOnlyTo.sa.sa_family)
2666 addr.sin.sin_addr.s_addr = ConnectOnlyTo.sin.sin_addr.s_addr;
2668 #endif /* NETINET */
2672 memmove(&addr.sin6.sin6_addr,
2673 &ConnectOnlyTo.sin6.sin6_addr,
2676 #endif /* NETINET6 */
2679 sm_dprintf("Connecting to [%s]...\n", anynet_ntoa(&addr));
2680 i = connect(s, (struct sockaddr *) &addr, addrlen);
2690 /* couldn't connect.... figure out why */
2693 /* if running demand-dialed connection, try again */
2694 if (DialDelay > 0 && firstconnect &&
2695 bitnset(M_DIALDELAY, mci->mci_mailer->m_flags))
2698 sm_dprintf("Connect failed (%s); trying again...\n",
2699 sm_errstring(save_errno));
2700 firstconnect = false;
2701 (void) sleep(DialDelay);
2706 sm_syslog(LOG_INFO, e->e_id,
2707 "makeconnection (%s [%s]) failed: %s",
2708 host, anynet_ntoa(&addr),
2709 sm_errstring(save_errno));
2713 #endif /* NETINET6 */
2714 if (hp != NULL && hp->h_addr_list[addrno] != NULL &&
2715 (enough == 0 || curtime() < enough))
2718 sm_dprintf("Connect failed (%s); trying new address....\n",
2719 sm_errstring(save_errno));
2720 switch (addr.sa.sa_family)
2724 memmove(&addr.sin.sin_addr,
2725 hp->h_addr_list[addrno++],
2728 #endif /* NETINET */
2732 memmove(&addr.sin6.sin6_addr,
2733 hp->h_addr_list[addrno++],
2736 #endif /* NETINET6 */
2739 memmove(addr.sa.sa_data,
2740 hp->h_addr_list[addrno++],
2749 if (family == AF_INET6)
2752 sm_dprintf("Connect failed (%s); retrying with AF_INET....\n",
2753 sm_errstring(save_errno));
2764 #endif /* NETINET6 */
2765 /* couldn't open connection */
2767 /* Don't clobber an already saved errno from v4retry */
2769 #endif /* NETINET6 */
2772 sm_dprintf("Connect failed (%s)\n",
2773 sm_errstring(save_errno));
2777 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2781 #endif /* NETINET6 */
2792 #endif /* NETINET6 */
2794 /* connection ok, put it into canonical form */
2795 mci->mci_out = NULL;
2796 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2798 SM_IO_WRONLY_B, NULL)) == NULL ||
2800 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2802 SM_IO_RDONLY_B, NULL)) == NULL)
2805 syserr("cannot open SMTP client channel, fd=%d", s);
2806 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2807 if (mci->mci_out != NULL)
2808 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2813 sm_io_automode(mci->mci_out, mci->mci_in);
2815 /* set {client_flags} */
2816 if (ClientSettings[addr.sa.sa_family].d_mflags != NULL)
2818 macdefine(&mci->mci_macro, A_PERM,
2819 macid("{client_flags}"),
2820 ClientSettings[addr.sa.sa_family].d_mflags);
2823 macdefine(&mci->mci_macro, A_PERM,
2824 macid("{client_flags}"), "");
2826 /* "add" {client_flags} to bitmap */
2827 if (bitnset(D_IFNHELO, ClientSettings[addr.sa.sa_family].d_flags))
2829 /* look for just this one flag */
2830 setbitn(D_IFNHELO, d_flags);
2833 /* find out name for Interface through which we connect */
2835 if (getsockname(s, &addr.sa, &len) == 0)
2840 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2841 macid("{if_addr_out}"), anynet_ntoa(&addr));
2842 (void) sm_snprintf(family, sizeof(family), "%d",
2844 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2845 macid("{if_family_out}"), family);
2847 name = hostnamebyanyaddr(&addr);
2848 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2849 macid("{if_name_out}"), name);
2852 /* log connection information */
2853 sm_syslog(LOG_INFO, e->e_id,
2854 "SMTP outgoing connect on %.40s", name);
2856 if (bitnset(D_IFNHELO, d_flags))
2858 if (name[0] != '[' && strchr(name, '.') != NULL)
2859 mci->mci_heloname = newstr(name);
2864 macdefine(&BlankEnvelope.e_macro, A_PERM,
2865 macid("{if_name_out}"), NULL);
2866 macdefine(&BlankEnvelope.e_macro, A_PERM,
2867 macid("{if_addr_out}"), NULL);
2868 macdefine(&BlankEnvelope.e_macro, A_PERM,
2869 macid("{if_family_out}"), NULL);
2872 /* Use the configured HeloName as appropriate */
2873 if (HeloName != NULL && HeloName[0] != '\0')
2874 mci->mci_heloname = newstr(HeloName);
2876 mci_setstat(mci, EX_OK, NULL, NULL);
2881 connecttimeout(ignore)
2885 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2886 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2891 longjmp(CtxConnectTimeout, 1);
2894 ** MAKECONNECTION_DS -- make a connection to a domain socket.
2897 ** mux_path -- the path of the socket to connect to.
2898 ** mci -- a pointer to the mail connection information
2899 ** structure to be filled in.
2902 ** An exit code telling whether the connection could be
2903 ** made and if not why not.
2911 makeconnection_ds(mux_path, mci)
2916 int rval, save_errno;
2917 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK;
2918 struct sockaddr_un unix_addr;
2920 /* if not safe, don't connect */
2921 rval = safefile(mux_path, RunAsUid, RunAsGid, RunAsUserName,
2922 sff, S_IRUSR|S_IWUSR, NULL);
2926 syserr("makeconnection_ds: unsafe domain socket %s",
2928 mci_setstat(mci, EX_TEMPFAIL, "4.3.5", NULL);
2933 /* prepare address structure */
2934 memset(&unix_addr, '\0', sizeof(unix_addr));
2935 unix_addr.sun_family = AF_UNIX;
2937 if (strlen(mux_path) >= sizeof(unix_addr.sun_path))
2939 syserr("makeconnection_ds: domain socket name %s too long",
2942 /* XXX why TEMPFAIL but 5.x.y ? */
2943 mci_setstat(mci, EX_TEMPFAIL, "5.3.5", NULL);
2944 errno = ENAMETOOLONG;
2945 return EX_UNAVAILABLE;
2947 (void) sm_strlcpy(unix_addr.sun_path, mux_path,
2948 sizeof(unix_addr.sun_path));
2950 /* initialize domain socket */
2951 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2955 syserr("makeconnection_ds: could not create domain socket %s",
2957 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2962 /* connect to server */
2963 if (connect(sock, (struct sockaddr *) &unix_addr,
2964 sizeof(unix_addr)) == -1)
2967 syserr("Could not connect to socket %s", mux_path);
2968 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2974 /* connection ok, put it into canonical form */
2975 mci->mci_out = NULL;
2976 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2977 (void *) &sock, SM_IO_WRONLY_B, NULL))
2979 || (sock = dup(sock)) < 0 ||
2980 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2981 (void *) &sock, SM_IO_RDONLY_B, NULL))
2985 syserr("cannot open SMTP client channel, fd=%d", sock);
2986 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2987 if (mci->mci_out != NULL)
2988 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2993 sm_io_automode(mci->mci_out, mci->mci_in);
2995 mci_setstat(mci, EX_OK, NULL, NULL);
2999 #endif /* NETUNIX */
3001 ** SHUTDOWN_DAEMON -- Performs a clean shutdown of the daemon
3010 ** closes control socket, exits.
3019 sm_allsignals(true);
3021 reason = ShutdownRequest;
3022 ShutdownRequest = NULL;
3026 sm_syslog(LOG_INFO, CurEnv->e_id, "stopping daemon, reason=%s",
3027 reason == NULL ? "implicit call" : reason);
3030 closecontrolsocket(true);
3035 for (i = 0; i < NDaemons; i++)
3037 if (Daemons[i].d_socket >= 0)
3039 (void) close(Daemons[i].d_socket);
3040 Daemons[i].d_socket = -1;
3042 #if _FFR_DAEMON_NETUNIX
3044 /* Remove named sockets */
3045 if (Daemons[i].d_addr.sa.sa_family == AF_UNIX)
3048 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_MUSTOWN|SFF_EXECOK|SFF_CREAT;
3050 /* if not safe, don't use it */
3051 rval = safefile(Daemons[i].d_addr.sunix.sun_path,
3054 S_IRUSR|S_IWUSR, NULL);
3056 unlink(Daemons[i].d_addr.sunix.sun_path) < 0)
3058 sm_syslog(LOG_WARNING, NOQID,
3059 "Could not remove daemon %s socket: %s: %s",
3061 Daemons[i].d_addr.sunix.sun_path,
3062 sm_errstring(errno));
3065 # endif /* NETUNIX */
3066 #endif /* _FFR_DAEMON_NETUNIX */
3070 finis(false, true, EX_OK);
3073 ** RESTART_DAEMON -- Performs a clean restart of the daemon
3082 ** restarts the daemon or exits if restart fails.
3085 /* Make a non-DFL/IGN signal a noop */
3086 #define SM_NOOP_SIGNAL(sig, old) \
3089 (old) = sm_signal((sig), sm_signal_noop); \
3090 if ((old) == SIG_IGN || (old) == SIG_DFL) \
3091 (void) sm_signal((sig), (old)); \
3100 sigfunc_t ignore, oalrm, ousr1;
3101 extern int DtableSize;
3103 /* clear the events to turn off SIGALRMs */
3105 sm_allsignals(true);
3107 reason = RestartRequest;
3108 RestartRequest = NULL;
3111 if (SaveArgv[0][0] != '/')
3114 sm_syslog(LOG_INFO, NOQID,
3115 "could not restart: need full path");
3116 finis(false, true, EX_OSFILE);
3120 sm_syslog(LOG_INFO, NOQID, "restarting %s due to %s",
3122 reason == NULL ? "implicit call" : reason);
3124 closecontrolsocket(true);
3126 cleanup_shm(DaemonPid == getpid());
3127 #endif /* SM_CONF_SHM */
3129 /* close locked pid file */
3130 close_sendmail_pid();
3133 ** Want to drop to the user who started the process in all cases
3134 ** *but* when running as "smmsp" for the clientmqueue queue run
3135 ** daemon. In that case, UseMSP will be true, RunAsUid should not
3136 ** be root, and RealUid should be either 0 or RunAsUid.
3139 drop = !(UseMSP && RunAsUid != 0 &&
3140 (RealUid == 0 || RealUid == RunAsUid));
3142 if (drop_privileges(drop) != EX_OK)
3145 sm_syslog(LOG_ALERT, NOQID,
3146 "could not drop privileges: %s",
3147 sm_errstring(errno));
3148 finis(false, true, EX_OSERR);
3152 sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
3155 ** Need to allow signals before execve() to make them "harmless".
3156 ** However, the default action can be "terminate", so it isn't
3157 ** really harmless. Setting signals to IGN will cause them to be
3158 ** ignored in the new process to, so that isn't a good alternative.
3161 SM_NOOP_SIGNAL(SIGALRM, oalrm);
3162 SM_NOOP_SIGNAL(SIGCHLD, ignore);
3163 SM_NOOP_SIGNAL(SIGHUP, ignore);
3164 SM_NOOP_SIGNAL(SIGINT, ignore);
3165 SM_NOOP_SIGNAL(SIGPIPE, ignore);
3166 SM_NOOP_SIGNAL(SIGTERM, ignore);
3168 SM_NOOP_SIGNAL(SIGUSR1, ousr1);
3169 #endif /* SIGUSR1 */
3171 /* Turn back on signals */
3172 sm_allsignals(false);
3174 (void) execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
3177 /* block signals again and restore needed signals */
3178 sm_allsignals(true);
3180 /* For finis() events */
3181 (void) sm_signal(SIGALRM, oalrm);
3184 /* For debugging finis() */
3185 (void) sm_signal(SIGUSR1, ousr1);
3186 #endif /* SIGUSR1 */
3190 sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %s",
3191 SaveArgv[0], sm_errstring(errno));
3192 finis(false, true, EX_OSFILE);
3196 ** MYHOSTNAME -- return the name of this host.
3199 ** hostbuf -- a place to return the name of this host.
3200 ** size -- the size of hostbuf.
3203 ** A list of aliases for this host.
3206 ** Adds numeric codes to $=w.
3210 myhostname(hostbuf, size)
3214 register struct hostent *hp;
3216 if (gethostname(hostbuf, size) < 0 || hostbuf[0] == '\0')
3217 (void) sm_strlcpy(hostbuf, "localhost", size);
3218 hp = sm_gethostbyname(hostbuf, InetMode);
3219 #if NETINET && NETINET6
3220 if (hp == NULL && InetMode == AF_INET6)
3223 ** It's possible that this IPv6 enabled machine doesn't
3224 ** actually have any IPv6 interfaces and, therefore, no
3225 ** IPv6 addresses. Fall back to AF_INET.
3228 hp = sm_gethostbyname(hostbuf, AF_INET);
3230 #endif /* NETINET && NETINET6 */
3233 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
3234 (void) cleanstrcpy(hostbuf, hp->h_name, size);
3237 if (strchr(hostbuf, '.') == NULL)
3241 domainname = ni_propval("/locations", NULL, "resolver",
3243 if (domainname != NULL &&
3244 strlen(domainname) + strlen(hostbuf) + 1 < size)
3245 (void) sm_strlcat2(hostbuf, ".", domainname, size);
3247 #endif /* NETINFO */
3250 ** If there is still no dot in the name, try looking for a
3254 if (strchr(hostbuf, '.') == NULL)
3258 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
3260 if (strchr(*ha, '.') != NULL)
3262 (void) cleanstrcpy(hostbuf, *ha, size - 1);
3263 hostbuf[size - 1] = '\0';
3270 ** If _still_ no dot, wait for a while and try again -- it is
3271 ** possible that some service is starting up. This can result
3272 ** in excessive delays if the system is badly configured, but
3273 ** there really isn't a way around that, particularly given that
3274 ** the config file hasn't been read at this point.
3275 ** All in all, a bit of a mess.
3278 if (strchr(hostbuf, '.') == NULL &&
3279 !getcanonname(hostbuf, size, true, NULL))
3281 sm_syslog(LocalDaemon ? LOG_WARNING : LOG_CRIT, NOQID,
3282 "My unqualified host name (%s) unknown; sleeping for retry",
3284 message("My unqualified host name (%s) unknown; sleeping for retry",
3287 if (!getcanonname(hostbuf, size, true, NULL))
3289 sm_syslog(LocalDaemon ? LOG_WARNING : LOG_ALERT, NOQID,
3290 "unable to qualify my own domain name (%s) -- using short name",
3292 message("WARNING: unable to qualify my own domain name (%s) -- using short name",
3299 ** ADDRCMP -- compare two host addresses
3302 ** hp -- hostent structure for the first address
3303 ** ha -- actual first address
3304 ** sa -- second address
3307 ** 0 -- if ha and sa match
3308 ** else -- they don't match
3319 #endif /* NETINET6 */
3321 switch (sa->sa.sa_family)
3325 if (hp->h_addrtype == AF_INET)
3326 return memcmp(ha, (char *) &sa->sin.sin_addr, INADDRSZ);
3328 #endif /* NETINET */
3332 a = (unsigned char *) &sa->sin6.sin6_addr;
3334 /* Straight binary comparison */
3335 if (hp->h_addrtype == AF_INET6)
3336 return memcmp(ha, a, IN6ADDRSZ);
3338 /* If IPv4-mapped IPv6 address, compare the IPv4 section */
3339 if (hp->h_addrtype == AF_INET &&
3340 IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr))
3341 return memcmp(a + IN6ADDRSZ - INADDRSZ, ha, INADDRSZ);
3343 #endif /* NETINET6 */
3348 ** GETAUTHINFO -- get the real host name associated with a file descriptor
3350 ** Uses RFC1413 protocol to try to get info from the other end.
3353 ** fd -- the descriptor
3354 ** may_be_forged -- an outage that is set to true if the
3355 ** forward lookup of RealHostName does not match
3356 ** RealHostAddr; set to false if they do match.
3359 ** The user@host information associated with this descriptor.
3362 static jmp_buf CtxAuthTimeout;
3369 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
3370 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
3375 longjmp(CtxAuthTimeout, 1);
3379 getauthinfo(fd, may_be_forged)
3381 bool *may_be_forged;
3383 unsigned short SM_NONVOLATILE port = 0;
3384 SOCKADDR_LEN_T falen;
3385 register char *volatile p = NULL;
3387 SOCKADDR_LEN_T lalen;
3388 #ifndef NO_GETSERVBYNAME
3389 register struct servent *sp;
3391 static unsigned short port4 = 0;
3392 # endif /* NETINET */
3394 static unsigned short port6 = 0;
3395 # endif /* NETINET6 */
3396 #endif /* ! NO_GETSERVBYNAME */
3403 char *ostype = NULL;
3405 char ibuf[MAXNAME + 1];
3406 static char hbuf[MAXNAME + MAXAUTHINFO + 11];
3408 *may_be_forged = false;
3409 falen = sizeof(RealHostAddr);
3410 if (isatty(fd) || (i = getpeername(fd, &RealHostAddr.sa, &falen)) < 0 ||
3411 falen <= 0 || RealHostAddr.sa.sa_family == 0)
3416 ** ENOTSOCK is OK: bail on anything else, but reset
3417 ** errno in this case, so a mis-report doesn't
3421 if (errno != ENOTSOCK)
3425 (void) sm_strlcpyn(hbuf, sizeof(hbuf), 2, RealUserName,
3428 sm_dprintf("getauthinfo: %s\n", hbuf);
3432 if (RealHostName == NULL)
3434 /* translate that to a host name */
3435 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
3436 if (strlen(RealHostName) > MAXNAME)
3437 RealHostName[MAXNAME] = '\0'; /* XXX - 1 ? */
3440 /* cross check RealHostName with forward DNS lookup */
3441 if (anynet_ntoa(&RealHostAddr)[0] != '[' &&
3442 RealHostName[0] != '[')
3446 family = RealHostAddr.sa.sa_family;
3447 #if NETINET6 && NEEDSGETIPNODE
3449 ** If RealHostAddr is an IPv6 connection with an
3450 ** IPv4-mapped address, we need RealHostName's IPv4
3451 ** address(es) for addrcmp() to compare against
3454 ** Actually, we only need to do this for systems
3455 ** which NEEDSGETIPNODE since the real getipnodebyname()
3456 ** already does V4MAPPED address via the AI_V4MAPPEDCFG
3457 ** flag. A better fix to this problem is to add this
3458 ** functionality to our stub getipnodebyname().
3461 if (family == AF_INET6 &&
3462 IN6_IS_ADDR_V4MAPPED(&RealHostAddr.sin6.sin6_addr))
3464 #endif /* NETINET6 && NEEDSGETIPNODE */
3466 /* try to match the reverse against the forward lookup */
3467 hp = sm_gethostbyname(RealHostName, family);
3470 /* XXX: Could be a temporary error on forward lookup */
3471 *may_be_forged = true;
3475 for (ha = hp->h_addr_list; *ha != NULL; ha++)
3477 if (addrcmp(hp, *ha, &RealHostAddr) == 0)
3480 *may_be_forged = *ha == NULL;
3484 #endif /* NETINET6 */
3488 if (TimeOuts.to_ident == 0)
3492 switch (RealHostAddr.sa.sa_family)
3496 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3498 la.sa.sa_family != AF_INET)
3503 port = RealHostAddr.sin.sin_port;
3505 /* create ident query */
3506 (void) sm_snprintf(ibuf, sizeof(ibuf), "%d,%d\r\n",
3507 ntohs(RealHostAddr.sin.sin_port),
3508 ntohs(la.sin.sin_port));
3510 /* create local address */
3511 la.sin.sin_port = 0;
3513 /* create foreign address */
3514 # ifdef NO_GETSERVBYNAME
3515 RealHostAddr.sin.sin_port = htons(113);
3516 # else /* NO_GETSERVBYNAME */
3519 ** getservbyname() consumes about 5% of the time
3520 ** when receiving a small message (almost all of the time
3521 ** spent in this routine).
3522 ** Hence we store the port in a static variable
3523 ** to save this time.
3524 ** The portnumber shouldn't change very often...
3525 ** This code makes the assumption that the port number
3531 sp = getservbyname("auth", "tcp");
3537 RealHostAddr.sin.sin_port = port4;
3539 # endif /* NO_GETSERVBYNAME */
3540 #endif /* NETINET */
3544 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3546 la.sa.sa_family != AF_INET6)
3551 port = RealHostAddr.sin6.sin6_port;
3553 /* create ident query */
3554 (void) sm_snprintf(ibuf, sizeof(ibuf), "%d,%d\r\n",
3555 ntohs(RealHostAddr.sin6.sin6_port),
3556 ntohs(la.sin6.sin6_port));
3558 /* create local address */
3559 la.sin6.sin6_port = 0;
3561 /* create foreign address */
3562 # ifdef NO_GETSERVBYNAME
3563 RealHostAddr.sin6.sin6_port = htons(113);
3564 # else /* NO_GETSERVBYNAME */
3567 sp = getservbyname("auth", "tcp");
3573 RealHostAddr.sin6.sin6_port = port6;
3575 # endif /* NO_GETSERVBYNAME */
3576 #endif /* NETINET6 */
3583 if (setjmp(CtxAuthTimeout) != 0)
3590 /* put a timeout around the whole thing */
3591 ev = sm_setevent(TimeOuts.to_ident, authtimeout, 0);
3593 /* connect to foreign IDENT server using same address as SMTP socket */
3594 s = socket(la.sa.sa_family, SOCK_STREAM, 0);
3600 if (bind(s, &la.sa, lalen) < 0 ||
3601 connect(s, &RealHostAddr.sa, lalen) < 0)
3605 sm_dprintf("getauthinfo: sent %s", ibuf);
3608 if (write(s, ibuf, strlen(ibuf)) < 0)
3613 nleft = sizeof(ibuf) - 1;
3614 while ((i = read(s, p, nleft)) > 0)
3621 if ((s = strchr(ibuf, '\n')) != NULL)
3635 if (i < 0 || p == &ibuf[0])
3638 if (p >= &ibuf[2] && *--p == '\n' && *--p == '\r')
3643 sm_dprintf("getauthinfo: got %s\n", ibuf);
3646 p = strchr(ibuf, ':');
3649 /* malformed response */
3652 while (isascii(*++p) && isspace(*p))
3654 if (sm_strncasecmp(p, "userid", 6) != 0)
3656 /* presumably an error string */
3660 while (isascii(*p) && isspace(*p))
3664 /* either useridxx or malformed response */
3668 /* p now points to the OSTYPE field */
3669 while (isascii(*p) && isspace(*p))
3675 /* malformed response */
3683 charset = strchr(ostype, ',');
3684 if (charset != NULL)
3688 /* 1413 says don't do this -- but it's broken otherwise */
3689 while (isascii(*++p) && isspace(*p))
3692 /* p now points to the authenticated name -- copy carefully */
3693 if (sm_strncasecmp(ostype, "other", 5) == 0 &&
3694 (ostype[5] == ' ' || ostype[5] == '\0'))
3696 (void) sm_strlcpy(hbuf, "IDENT:", sizeof(hbuf));
3697 cleanstrcpy(&hbuf[6], p, MAXAUTHINFO);
3700 cleanstrcpy(hbuf, p, MAXAUTHINFO);
3702 (void) sm_strlcpyn(&hbuf[len], sizeof(hbuf) - len, 2, "@",
3703 RealHostName == NULL ? "localhost" : RealHostName);
3711 /* put back the original incoming port */
3712 switch (RealHostAddr.sa.sa_family)
3717 RealHostAddr.sin.sin_port = port;
3719 #endif /* NETINET */
3724 RealHostAddr.sin6.sin6_port = port;
3726 #endif /* NETINET6 */
3729 if (RealHostName == NULL)
3732 sm_dprintf("getauthinfo: NULL\n");
3735 (void) sm_strlcpy(hbuf, RealHostName, sizeof(hbuf));
3739 # ifndef GET_IPOPT_DST
3740 # define GET_IPOPT_DST(dst) (dst)
3741 # endif /* ! GET_IPOPT_DST */
3743 ** Extract IP source routing information.
3745 ** Format of output for a connection from site a through b
3747 ** loose: @site-c@site-b:site-a
3748 ** strict: !@site-c@site-b:site-a
3750 ** o - pointer within ipopt_list structure.
3751 ** q - pointer within ls/ss rr route data
3752 ** p - pointer to hbuf
3755 if (RealHostAddr.sa.sa_family == AF_INET)
3757 SOCKOPT_LEN_T ipoptlen;
3762 struct IPOPTION ipopt;
3764 ipoptlen = sizeof(ipopt);
3765 if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
3766 (char *) &ipopt, &ipoptlen) < 0)
3770 o = (unsigned char *) ipopt.IP_LIST;
3771 while (o != NULL && o < (unsigned char *) &ipopt + ipoptlen)
3787 ** o[0] is the option type (loose/strict).
3788 ** o[1] is the length of this option,
3789 ** including option type and
3791 ** o[2] is the pointer into the route
3793 ** o[3] begins the route data.
3796 p = &hbuf[strlen(hbuf)];
3797 l = sizeof(hbuf) - (hbuf - p) - 6;
3798 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
3800 *o == IPOPT_SSRR ? "!" : "",
3801 l > 240 ? 120 : l / 2,
3802 inet_ntoa(GET_IPOPT_DST(ipopt.IP_DST)));
3807 j = o[1] / sizeof(struct in_addr) - 1;
3809 /* q skips length and router pointer to data */
3811 for ( ; j >= 0; j--)
3813 struct in_addr addr;
3815 memcpy(&addr, q, sizeof(addr));
3816 (void) sm_snprintf(p,
3826 q += sizeof(struct in_addr);
3832 /* Skip over option */
3837 (void) sm_snprintf(p, SPACELEFT(hbuf, p), "]");
3842 #endif /* IP_SRCROUTE */
3843 if (RealHostName != NULL && RealHostName[0] != '[')
3845 p = &hbuf[strlen(hbuf)];
3846 (void) sm_snprintf(p, SPACELEFT(hbuf, p), " [%.100s]",
3847 anynet_ntoa(&RealHostAddr));
3851 p = &hbuf[strlen(hbuf)];
3852 (void) sm_strlcpy(p, " (may be forged)", SPACELEFT(hbuf, p));
3853 macdefine(&BlankEnvelope.e_macro, A_PERM,
3854 macid("{client_resolve}"), "FORGED");
3859 #endif /* IP_SRCROUTE */
3861 /* put back the original incoming port */
3862 switch (RealHostAddr.sa.sa_family)
3867 RealHostAddr.sin.sin_port = port;
3869 #endif /* NETINET */
3874 RealHostAddr.sin6.sin6_port = port;
3876 #endif /* NETINET6 */
3880 sm_dprintf("getauthinfo: %s\n", hbuf);
3884 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
3887 ** map -- a pointer to this map.
3888 ** name -- the (presumably unqualified) hostname.
3889 ** av -- unused -- for compatibility with other mapping
3891 ** statp -- an exit status (out parameter) -- set to
3892 ** EX_TEMPFAIL if the name server is unavailable.
3895 ** The mapping, if found.
3896 ** NULL if no mapping found.
3899 ** Looks up the host specified in hbuf. If it is not
3900 ** the canonical name for that host, return the canonical
3901 ** name (unless MF_MATCHONLY is set, which will cause the
3902 ** status only to be returned).
3906 host_map_lookup(map, name, av, statp)
3912 register struct hostent *hp;
3914 struct in_addr in_addr;
3915 #endif /* NETINET */
3917 struct in6_addr in6_addr;
3918 #endif /* NETINET6 */
3919 char *cp, *ans = NULL;
3923 time_t SM_NONVOLATILE retrans = 0;
3924 int SM_NONVOLATILE retry = 0;
3925 #endif /* NAMED_BIND */
3926 char hbuf[MAXNAME + 1];
3929 ** See if we have already looked up this name. If so, just
3930 ** return it (unless expired).
3934 s = stab(name, ST_NAMECANON, ST_ENTER);
3935 if (bitset(NCF_VALID, s->s_namecanon.nc_flags) &&
3936 s->s_namecanon.nc_exp >= now)
3939 sm_dprintf("host_map_lookup(%s) => CACHE %s\n",
3941 s->s_namecanon.nc_cname == NULL
3943 : s->s_namecanon.nc_cname);
3944 errno = s->s_namecanon.nc_errno;
3945 SM_SET_H_ERRNO(s->s_namecanon.nc_herrno);
3946 *statp = s->s_namecanon.nc_stat;
3947 if (*statp == EX_TEMPFAIL)
3949 CurEnv->e_status = "4.4.3";
3950 message("851 %s: Name server timeout",
3951 shortenstring(name, 33));
3953 if (*statp != EX_OK)
3955 if (s->s_namecanon.nc_cname == NULL)
3957 syserr("host_map_lookup(%s): bogus NULL cache entry, errno=%d, h_errno=%d",
3959 s->s_namecanon.nc_errno,
3960 s->s_namecanon.nc_herrno);
3963 if (bitset(MF_MATCHONLY, map->map_mflags))
3964 cp = map_rewrite(map, name, strlen(name), NULL);
3966 cp = map_rewrite(map,
3967 s->s_namecanon.nc_cname,
3968 strlen(s->s_namecanon.nc_cname),
3974 ** If we are running without a regular network connection (usually
3975 ** dial-on-demand) and we are just queueing, we want to avoid DNS
3976 ** lookups because those could try to connect to a server.
3979 if (CurEnv->e_sendmode == SM_DEFER &&
3980 bitset(MF_DEFER, map->map_mflags))
3983 sm_dprintf("host_map_lookup(%s) => DEFERRED\n", name);
3984 *statp = EX_TEMPFAIL;
3989 ** If first character is a bracket, then it is an address
3990 ** lookup. Address is copied into a temporary buffer to
3991 ** strip the brackets and to preserve name if address is
3996 sm_dprintf("host_map_lookup(%s) => ", name);
3998 if (map->map_timeout > 0)
4000 retrans = _res.retrans;
4001 _res.retrans = map->map_timeout;
4003 if (map->map_retry > 0)
4006 _res.retry = map->map_retry;
4008 #endif /* NAMED_BIND */
4010 /* set default TTL */
4011 s->s_namecanon.nc_exp = now + SM_DEFAULT_TTL;
4016 (void) sm_strlcpy(hbuf, name, sizeof(hbuf));
4017 if (getcanonname(hbuf, sizeof(hbuf) - 1, !HasWildcardMX, &ttl))
4021 s->s_namecanon.nc_exp = now + SM_MIN(ttl,
4027 if ((cp = strchr(name, ']')) == NULL)
4030 sm_dprintf("FAILED\n");
4037 if ((in_addr.s_addr = inet_addr(&name[1])) != INADDR_NONE)
4038 hp = sm_gethostbyaddr((char *)&in_addr,
4040 #endif /* NETINET */
4043 anynet_pton(AF_INET6, &name[1], &in6_addr) == 1)
4044 hp = sm_gethostbyaddr((char *)&in6_addr,
4045 IN6ADDRSZ, AF_INET6);
4046 #endif /* NETINET6 */
4051 /* found a match -- copy out */
4052 ans = denlstring((char *) hp->h_name, true, true);
4054 if (ans == hp->h_name)
4056 static char n[MAXNAME + 1];
4058 /* hp->h_name is about to disappear */
4059 (void) sm_strlcpy(n, ans, sizeof(n));
4064 #endif /* NETINET6 */
4068 if (map->map_timeout > 0)
4069 _res.retrans = retrans;
4070 if (map->map_retry > 0)
4072 #endif /* NAMED_BIND */
4074 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
4076 /* Found an answer */
4079 s->s_namecanon.nc_stat = *statp = EX_OK;
4080 if (s->s_namecanon.nc_cname != NULL)
4081 sm_free(s->s_namecanon.nc_cname);
4082 s->s_namecanon.nc_cname = sm_strdup_x(ans);
4083 if (bitset(MF_MATCHONLY, map->map_mflags))
4084 cp = map_rewrite(map, name, strlen(name), NULL);
4086 cp = map_rewrite(map, ans, strlen(ans), av);
4088 sm_dprintf("FOUND %s\n", ans);
4093 /* No match found */
4094 s->s_namecanon.nc_errno = errno;
4096 s->s_namecanon.nc_herrno = h_errno;
4098 sm_dprintf("FAIL (%d)\n", h_errno);
4104 CurEnv->e_status = "4.4.3";
4105 message("851 %s: Name server timeout",
4106 shortenstring(name, 33));
4108 *statp = EX_TEMPFAIL;
4111 case HOST_NOT_FOUND:
4117 *statp = EX_SOFTWARE;
4121 *statp = EX_UNAVAILABLE;
4124 #else /* NAMED_BIND */
4126 sm_dprintf("FAIL\n");
4128 #endif /* NAMED_BIND */
4129 s->s_namecanon.nc_stat = *statp;
4133 ** HOST_MAP_INIT -- initialize host class structures
4136 ** map -- a pointer to this map.
4137 ** args -- argument string.
4144 host_map_init(map, args)
4148 register char *p = args;
4152 while (isascii(*p) && isspace(*p))
4163 map->map_tapp = ++p;
4167 map->map_mflags |= MF_MATCHONLY;
4171 map->map_mflags |= MF_NODEFER;
4174 case 'S': /* only for consistency */
4175 map->map_spacesub = *++p;
4179 map->map_mflags |= MF_DEFER;
4186 while (isascii(*++p) && isspace(*p))
4191 map->map_timeout = convtime(p, 's');
4198 while (isascii(*++p) && isspace(*p))
4200 map->map_retry = atoi(p);
4203 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
4208 if (map->map_app != NULL)
4209 map->map_app = newstr(map->map_app);
4210 if (map->map_tapp != NULL)
4211 map->map_tapp = newstr(map->map_tapp);
4217 ** ANYNET_NTOP -- convert an IPv6 network address to printable form.
4220 ** s6a -- a pointer to an in6_addr structure.
4221 ** dst -- buffer to store result in
4222 ** dst_len -- size of dst buffer
4225 ** A printable version of that structure.
4229 anynet_ntop(s6a, dst, dst_len)
4230 struct in6_addr *s6a;
4236 if (IN6_IS_ADDR_V4MAPPED(s6a))
4237 ap = (char *) inet_ntop(AF_INET,
4238 &s6a->s6_addr[IN6ADDRSZ - INADDRSZ],
4245 /* Save pointer to beginning of string */
4248 /* Add IPv6: protocol tag */
4249 sz = sm_strlcpy(dst, "IPv6:", dst_len);
4254 ap = (char *) inet_ntop(AF_INET6, s6a, dst, dst_len);
4256 /* Restore pointer to beginning of string */
4264 ** ANYNET_PTON -- convert printed form to network address.
4266 ** Wrapper for inet_pton() which handles IPv6: labels.
4269 ** family -- address family
4271 ** dst -- destination address structure
4274 ** 1 if the address was valid
4275 ** 0 if the address wasn't parseable
4280 anynet_pton(family, src, dst)
4285 if (family == AF_INET6 && sm_strncasecmp(src, "IPv6:", 5) == 0)
4287 return inet_pton(family, src, dst);
4289 #endif /* NETINET6 */
4291 ** ANYNET_NTOA -- convert a network address to printable form.
4294 ** sap -- a pointer to a sockaddr structure.
4297 ** A printable version of that sockaddr.
4300 #ifdef USE_SOCK_STREAM
4303 # include <net/if_dl.h>
4304 # endif /* NETLINK */
4308 register SOCKADDR *sap;
4313 static char buf[100];
4315 /* check for null/zero family */
4318 if (sap->sa.sa_family == 0)
4321 switch (sap->sa.sa_family)
4325 if (sap->sunix.sun_path[0] != '\0')
4326 (void) sm_snprintf(buf, sizeof(buf), "[UNIX: %.64s]",
4327 sap->sunix.sun_path);
4329 (void) sm_strlcpy(buf, "[UNIX: localhost]", sizeof(buf));
4331 # endif /* NETUNIX */
4335 return (char *) inet_ntoa(sap->sin.sin_addr);
4336 # endif /* NETINET */
4340 ap = anynet_ntop(&sap->sin6.sin6_addr, buf, sizeof(buf));
4344 # endif /* NETINET6 */
4348 (void) sm_snprintf(buf, sizeof(buf), "[LINK: %s]",
4349 link_ntoa((struct sockaddr_dl *) &sap->sa));
4351 # endif /* NETLINK */
4353 /* this case is needed when nothing is #defined */
4354 /* in order to keep the switch syntactically correct */
4358 /* unknown family -- just dump bytes */
4359 (void) sm_snprintf(buf, sizeof(buf), "Family %d: ", sap->sa.sa_family);
4360 bp = &buf[strlen(buf)];
4361 ap = sap->sa.sa_data;
4362 for (l = sizeof(sap->sa.sa_data); --l >= 0; )
4364 (void) sm_snprintf(bp, SPACELEFT(buf, bp), "%02x:",
4372 ** HOSTNAMEBYANYADDR -- return name of host based on address
4375 ** sap -- SOCKADDR pointer
4378 ** text representation of host name.
4385 hostnamebyanyaddr(sap)
4386 register SOCKADDR *sap;
4388 register struct hostent *hp;
4391 # endif /* NAMED_BIND */
4393 struct in6_addr in6_addr;
4394 # endif /* NETINET6 */
4397 /* shorten name server timeout to avoid higher level timeouts */
4398 saveretry = _res.retry;
4399 if (_res.retry * _res.retrans > 20)
4400 _res.retry = 20 / _res.retrans;
4401 # endif /* NAMED_BIND */
4403 switch (sap->sa.sa_family)
4407 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
4410 # endif /* NETINET */
4414 hp = sm_gethostbyaddr((char *) &sap->sin6.sin6_addr,
4415 IN6ADDRSZ, AF_INET6);
4417 # endif /* NETINET6 */
4421 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
4422 sizeof(sap->siso.siso_addr), AF_ISO);
4424 # endif /* NETISO */
4430 # endif /* NETUNIX */
4433 hp = sm_gethostbyaddr(sap->sa.sa_data, sizeof(sap->sa.sa_data),
4439 _res.retry = saveretry;
4440 # endif /* NAMED_BIND */
4442 # if NETINET || NETINET6
4443 if (hp != NULL && hp->h_name[0] != '['
4445 && inet_pton(AF_INET6, hp->h_name, &in6_addr) != 1
4446 # endif /* NETINET6 */
4448 && inet_addr(hp->h_name) == INADDR_NONE
4449 # endif /* NETINET */
4454 name = denlstring((char *) hp->h_name, true, true);
4456 if (name == hp->h_name)
4458 static char n[MAXNAME + 1];
4460 /* Copy the string, hp->h_name is about to disappear */
4461 (void) sm_strlcpy(n, name, sizeof(n));
4465 # endif /* NETINET6 */
4468 # endif /* NETINET || NETINET6 */
4476 # endif /* NETINET6 */
4479 if (sap->sa.sa_family == AF_UNIX && sap->sunix.sun_path[0] == '\0')
4481 # endif /* NETUNIX */
4483 static char buf[203];
4485 (void) sm_snprintf(buf, sizeof(buf), "[%.200s]",
4490 #endif /* USE_SOCK_STREAM */