2 * Copyright (c) 1998-2007, 2009, 2010 Proofpoint, 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.698 2013-11-22 20:51:55 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: socket %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;
832 if (!xla_host_ok(RealHostName))
834 message("421 4.4.5 Too many SMTP sessions for this host");
835 finis(false, true, EX_OK);
838 /* find out name for interface of connection */
839 if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
840 NULL), &sa.sa, &len) == 0)
842 p = hostnamebyanyaddr(&sa);
844 sm_dprintf("getreq: got name %s\n", p);
845 macdefine(&BlankEnvelope.e_macro, A_TEMP,
846 macid("{if_name}"), p);
849 ** Do this only if it is not the loopback
858 addr = anynet_ntoa(&sa);
859 (void) sm_snprintf(family,
861 "%d", sa.sa.sa_family);
862 macdefine(&BlankEnvelope.e_macro,
864 macid("{if_addr}"), addr);
865 macdefine(&BlankEnvelope.e_macro,
867 macid("{if_family}"), family);
869 sm_dprintf("getreq: got addr %s and family %s\n",
874 macdefine(&BlankEnvelope.e_macro,
876 macid("{if_addr}"), NULL);
877 macdefine(&BlankEnvelope.e_macro,
879 macid("{if_family}"), NULL);
885 sm_dprintf("getreq: getsockname failed\n");
886 macdefine(&BlankEnvelope.e_macro, A_PERM,
887 macid("{if_name}"), NULL);
888 macdefine(&BlankEnvelope.e_macro, A_PERM,
889 macid("{if_addr}"), NULL);
890 macdefine(&BlankEnvelope.e_macro, A_PERM,
891 macid("{if_family}"), NULL);
896 /* parent -- keep track of children */
899 (void) sm_snprintf(status, sizeof(status),
900 "control socket server child");
901 proc_list_add(pid, status, PROC_CONTROL, 0, -1, NULL);
905 (void) sm_snprintf(status, sizeof(status),
906 "SMTP server child for %s",
907 anynet_ntoa(&RealHostAddr));
908 proc_list_add(pid, status, PROC_DAEMON, 0, -1,
911 (void) sm_releasesignal(SIGCHLD);
913 /* close the read end of the synchronization pipe */
916 (void) close(pipefd[0]);
920 /* close the port so that others will hang (for a while) */
923 /* release the child by closing the read end of the sync pipe */
926 (void) close(pipefd[1]);
931 sm_dprintf("getreq: returning\n");
934 /* set the filters for this daemon */
935 if (Daemons[curdaemon].d_inputfilterlist != NULL)
939 Daemons[curdaemon].d_inputfilters[i] != NULL);
942 InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
945 InputFilters[i] = NULL;
948 return &Daemons[curdaemon].d_flags;
952 ** GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
961 ** Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
965 getrequests_checkdiskspace(e)
973 if (now < NextDiskSpaceCheck)
976 /* Check if there is available disk space in all queue groups. */
977 if (!enoughdiskspace(0, NULL))
979 for (idx = 0; idx < NDaemons; ++idx)
981 if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
984 /* log only if not logged before */
988 sm_syslog(LOG_INFO, NOQID,
989 "rejecting new messages: min free: %ld",
991 sm_setproctitle(true, e,
992 "rejecting new messages: min free: %ld",
996 setbitn(D_ETRNONLY, Daemons[idx].d_flags);
1001 for (idx = 0; idx < NDaemons; ++idx)
1003 if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
1006 /* log only if not logged before */
1010 sm_syslog(LOG_INFO, NOQID,
1011 "accepting new messages (again)");
1015 /* title will be set later */
1016 clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
1020 /* only check disk space once a minute */
1021 NextDiskSpaceCheck = now + 60;
1025 ** OPENDAEMONSOCKET -- open SMTP socket
1027 ** Deals with setting all appropriate options.
1030 ** d -- the structure for the daemon to open.
1031 ** firsttime -- set if this is the initial open.
1034 ** Size in bytes of the daemon socket addr.
1037 ** Leaves DaemonSocket set to the open socket.
1038 ** Exits if the socket cannot be created.
1041 #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
1044 opendaemonsocket(d, firsttime)
1050 SOCKADDR_LEN_T socksize = 0;
1055 sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
1061 if (firsttime || d->d_socket < 0)
1063 #if _FFR_DAEMON_NETUNIX
1065 if (d->d_addr.sa.sa_family == AF_UNIX)
1068 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
1070 /* if not safe, don't use it */
1071 rval = safefile(d->d_addr.sunix.sun_path,
1074 S_IRUSR|S_IWUSR, NULL);
1078 syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
1080 d->d_addr.sunix.sun_path);
1084 /* Don't try to overtake an existing socket */
1085 (void) unlink(d->d_addr.sunix.sun_path);
1087 # endif /* NETUNIX */
1088 #endif /* _FFR_DOMAIN_NETUNIX */
1089 d->d_socket = socket(d->d_addr.sa.sa_family,
1091 if (d->d_socket < 0)
1094 syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
1097 if (bitnset(D_OPTIONAL, d->d_flags) &&
1098 (!transienterror(save_errno) ||
1099 ntries >= MAXOPENTRIES - 1))
1101 syserr("opendaemonsocket: daemon %s: optional socket disabled",
1103 setbitn(D_DISABLE, d->d_flags);
1109 sm_syslog(LOG_ALERT, NOQID,
1110 "daemon %s: problem creating SMTP socket",
1116 if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
1118 save_errno = EINVAL;
1119 syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
1120 d->d_name, d->d_socket);
1124 /* turn on network debugging? */
1126 (void) setsockopt(d->d_socket, SOL_SOCKET,
1127 SO_DEBUG, (char *)&on,
1130 (void) setsockopt(d->d_socket, SOL_SOCKET,
1131 SO_REUSEADDR, (char *)&on, sizeof(on));
1132 (void) setsockopt(d->d_socket, SOL_SOCKET,
1133 SO_KEEPALIVE, (char *)&on, sizeof(on));
1136 if (d->d_tcprcvbufsize > 0)
1138 if (setsockopt(d->d_socket, SOL_SOCKET,
1140 (char *) &d->d_tcprcvbufsize,
1141 sizeof(d->d_tcprcvbufsize)) < 0)
1142 syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
1144 #endif /* SO_RCVBUF */
1146 if (d->d_tcpsndbufsize > 0)
1148 if (setsockopt(d->d_socket, SOL_SOCKET,
1150 (char *) &d->d_tcpsndbufsize,
1151 sizeof(d->d_tcpsndbufsize)) < 0)
1152 syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
1154 #endif /* SO_SNDBUF */
1156 if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
1157 fcntl(d->d_socket, F_SETFD,
1158 fdflags | FD_CLOEXEC) == -1)
1161 syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
1163 fdflags == -1 ? "get" : "set",
1164 sm_errstring(save_errno));
1165 (void) close(d->d_socket);
1169 switch (d->d_addr.sa.sa_family)
1171 #if _FFR_DAEMON_NETUNIX
1174 socksize = sizeof(d->d_addr.sunix);
1176 # endif /* NETUNIX */
1177 #endif /* _FFR_DAEMON_NETUNIX */
1180 socksize = sizeof(d->d_addr.sin);
1182 #endif /* NETINET */
1186 socksize = sizeof(d->d_addr.sin6);
1188 #endif /* NETINET6 */
1192 socksize = sizeof(d->d_addr.siso);
1197 socksize = sizeof(d->d_addr);
1201 if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
1203 /* probably another daemon already */
1205 syserr("opendaemonsocket: daemon %s: cannot bind",
1207 (void) close(d->d_socket);
1212 listen(d->d_socket, d->d_listenqueue) < 0)
1215 syserr("opendaemonsocket: daemon %s: cannot listen",
1217 (void) close(d->d_socket);
1221 } while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
1222 syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
1225 return -1; /* avoid compiler warning on IRIX */
1228 ** SETUPDAEMON -- setup socket for daemon
1231 ** daemonaddr -- socket for daemon
1234 ** port number on which daemon should run
1238 static unsigned short
1239 setupdaemon(daemonaddr)
1240 SOCKADDR *daemonaddr;
1242 unsigned short port;
1245 ** Set up the address for the mailer.
1248 if (daemonaddr->sa.sa_family == AF_UNSPEC)
1250 memset(daemonaddr, '\0', sizeof(*daemonaddr));
1252 daemonaddr->sa.sa_family = AF_INET;
1253 #endif /* NETINET */
1256 switch (daemonaddr->sa.sa_family)
1260 if (daemonaddr->sin.sin_addr.s_addr == 0)
1261 daemonaddr->sin.sin_addr.s_addr =
1262 LocalDaemon ? htonl(INADDR_LOOPBACK) : INADDR_ANY;
1263 port = daemonaddr->sin.sin_port;
1265 #endif /* NETINET */
1269 if (IN6_IS_ADDR_UNSPECIFIED(&daemonaddr->sin6.sin6_addr))
1270 daemonaddr->sin6.sin6_addr =
1271 (LocalDaemon && V6LoopbackAddrFound) ?
1272 in6addr_loopback : in6addr_any;
1273 port = daemonaddr->sin6.sin6_port;
1275 #endif /* NETINET6 */
1278 /* unknown protocol */
1284 #ifdef NO_GETSERVBYNAME
1286 #else /* NO_GETSERVBYNAME */
1288 register struct servent *sp;
1290 sp = getservbyname("smtp", "tcp");
1293 syserr("554 5.3.5 service \"smtp\" unknown");
1299 #endif /* NO_GETSERVBYNAME */
1302 switch (daemonaddr->sa.sa_family)
1306 daemonaddr->sin.sin_port = port;
1308 #endif /* NETINET */
1312 daemonaddr->sin6.sin6_port = port;
1314 #endif /* NETINET6 */
1317 /* unknown protocol */
1323 ** CLRDAEMON -- reset the daemon connection
1332 ** releases any resources used by the passive daemon.
1340 for (i = 0; i < NDaemons; i++)
1342 if (Daemons[i].d_socket >= 0)
1343 (void) close(Daemons[i].d_socket);
1344 Daemons[i].d_socket = -1;
1349 ** GETMODIFIERS -- get modifier flags
1352 ** v -- the modifiers (input text line).
1353 ** modifiers -- pointer to flag field to represent modifiers.
1356 ** (xallocat()ed) string representation of modifiers.
1359 ** fills in modifiers.
1363 getmodifiers(v, modifiers)
1365 BITMAP256 modifiers;
1368 char *h, *f, *flags;
1370 /* maximum length of flags: upper case Option -> "OO " */
1371 l = 3 * strlen(v) + 3;
1373 /* is someone joking? */
1374 if (l < 0 || l > 256)
1377 sm_syslog(LOG_ERR, NOQID,
1378 "getmodifiers too long, ignored");
1383 clrbitmap(modifiers);
1384 for (h = v; *h != '\0'; h++)
1386 if (isascii(*h) && !isspace(*h) && isprint(*h))
1388 setbitn(*h, modifiers);
1401 ** CHKDAEMONMODIFIERS -- check whether all daemons have set a flag.
1404 ** flag -- the flag to test.
1407 ** true iff all daemons have set flag.
1411 chkdaemonmodifiers(flag)
1416 for (i = 0; i < NDaemons; i++)
1417 if (!bitnset((char) flag, Daemons[i].d_flags))
1423 ** SETSOCKADDROPTIONS -- set options for SOCKADDR (daemon or client)
1426 ** p -- the options line.
1427 ** d -- the daemon structure to fill in.
1434 setsockaddroptions(p, d)
1445 if (d->d_addr.sa.sa_family == AF_UNSPEC)
1446 d->d_addr.sa.sa_family = AF_INET;
1447 #endif /* NETINET */
1448 #if _FFR_SS_PER_DAEMON
1449 d->d_supersafe = DPO_NOTSET;
1450 #endif /* _FFR_SS_PER_DAEMON */
1451 d->d_dm = DM_NOTSET;
1452 d->d_refuseLA = DPO_NOTSET;
1453 d->d_queueLA = DPO_NOTSET;
1454 d->d_delayLA = DPO_NOTSET;
1455 d->d_maxchildren = DPO_NOTSET;
1462 while (isascii(*p) && isspace(*p))
1473 while (isascii(*++v) && isspace(*v))
1478 case 'A': /* address */
1481 #endif /* !_FFR_DPO_CS */
1486 d->d_maxchildren = atoi(v);
1489 case 'D': /* DeliveryMode */
1499 syserr("554 5.3.5 Unknown delivery mode %c",
1505 case 'd': /* delayLA */
1506 d->d_delayLA = atoi(v);
1509 case 'F': /* address family */
1512 #endif /* !_FFR_DPO_CS */
1513 if (isascii(*v) && isdigit(*v))
1514 d->d_addr.sa.sa_family = atoi(v);
1515 #if _FFR_DAEMON_NETUNIX
1517 else if (sm_strcasecmp(v, "unix") == 0 ||
1518 sm_strcasecmp(v, "local") == 0)
1519 d->d_addr.sa.sa_family = AF_UNIX;
1520 # endif /* NETUNIX */
1521 #endif /* _FFR_DAEMON_NETUNIX */
1523 else if (sm_strcasecmp(v, "inet") == 0)
1524 d->d_addr.sa.sa_family = AF_INET;
1525 #endif /* NETINET */
1527 else if (sm_strcasecmp(v, "inet6") == 0)
1528 d->d_addr.sa.sa_family = AF_INET6;
1529 #endif /* NETINET6 */
1531 else if (sm_strcasecmp(v, "iso") == 0)
1532 d->d_addr.sa.sa_family = AF_ISO;
1535 else if (sm_strcasecmp(v, "ns") == 0)
1536 d->d_addr.sa.sa_family = AF_NS;
1539 else if (sm_strcasecmp(v, "x.25") == 0)
1540 d->d_addr.sa.sa_family = AF_CCITT;
1543 syserr("554 5.3.5 Unknown address family %s in Family=option",
1551 # endif /* !_FFR_DPO_CS */
1552 d->d_inputfilterlist = v;
1556 case 'L': /* listen queue size */
1559 #endif /* !_FFR_DPO_CS */
1560 d->d_listenqueue = atoi(v);
1563 case 'M': /* modifiers (flags) */
1566 #endif /* !_FFR_DPO_CS */
1567 d->d_mflags = getmodifiers(v, d->d_flags);
1570 case 'N': /* name */
1573 #endif /* !_FFR_DPO_CS */
1577 case 'P': /* port */
1580 #endif /* !_FFR_DPO_CS */
1585 d->d_queueLA = atoi(v);
1588 case 'R': /* receive buffer size */
1589 d->d_tcprcvbufsize = atoi(v);
1593 d->d_refuseLA = atoi(v);
1596 case 'S': /* send buffer size */
1599 #endif /* !_FFR_DPO_CS */
1600 d->d_tcpsndbufsize = atoi(v);
1603 #if _FFR_SS_PER_DAEMON
1604 case 'T': /* SuperSafe */
1605 if (tolower(*v) == 'i')
1606 d->d_supersafe = SAFE_INTERACTIVE;
1607 else if (tolower(*v) == 'p')
1609 d->d_supersafe = SAFE_REALLY_POSTMILTER;
1611 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1612 "Warning: SuperSafe=PostMilter requires Milter support (-DMILTER)\n");
1613 # endif /* MILTER */
1615 d->d_supersafe = atobool(v) ? SAFE_REALLY
1618 #endif /* _FFR_SS_PER_DAEMON */
1621 syserr("554 5.3.5 PortOptions parameter \"%s\" unknown",
1626 /* Check addr and port after finding family */
1629 switch (d->d_addr.sa.sa_family)
1631 #if _FFR_DAEMON_NETUNIX
1634 if (strlen(addr) >= sizeof(d->d_addr.sunix.sun_path))
1636 errno = ENAMETOOLONG;
1637 syserr("setsockaddroptions: domain socket name too long: %s > %d",
1638 addr, sizeof(d->d_addr.sunix.sun_path));
1642 /* file safety check done in opendaemonsocket() */
1643 (void) memset(&d->d_addr.sunix.sun_path, '\0',
1644 sizeof(d->d_addr.sunix.sun_path));
1645 (void) sm_strlcpy((char *)&d->d_addr.sunix.sun_path,
1647 sizeof(d->d_addr.sunix.sun_path));
1649 # endif /* NETUNIX */
1650 #endif /* _FFR_DAEMON_NETUNIX */
1653 if (!isascii(*addr) || !isdigit(*addr) ||
1654 ((d->d_addr.sin.sin_addr.s_addr = inet_addr(addr))
1657 register struct hostent *hp;
1659 hp = sm_gethostbyname(addr, AF_INET);
1661 syserr("554 5.3.0 host \"%s\" unknown",
1665 while (*(hp->h_addr_list) != NULL &&
1666 hp->h_addrtype != AF_INET)
1668 if (*(hp->h_addr_list) == NULL)
1669 syserr("554 5.3.0 host \"%s\" unknown",
1672 memmove(&d->d_addr.sin.sin_addr,
1678 # endif /* NETINET6 */
1682 #endif /* NETINET */
1686 if (anynet_pton(AF_INET6, addr,
1687 &d->d_addr.sin6.sin6_addr) != 1)
1689 register struct hostent *hp;
1691 hp = sm_gethostbyname(addr, AF_INET6);
1693 syserr("554 5.3.0 host \"%s\" unknown",
1697 while (*(hp->h_addr_list) != NULL &&
1698 hp->h_addrtype != AF_INET6)
1700 if (*(hp->h_addr_list) == NULL)
1701 syserr("554 5.3.0 host \"%s\" unknown",
1704 memmove(&d->d_addr.sin6.sin6_addr,
1712 #endif /* NETINET6 */
1715 syserr("554 5.3.5 address= option unsupported for family %d",
1716 d->d_addr.sa.sa_family);
1723 switch (d->d_addr.sa.sa_family)
1727 if (isascii(*port) && isdigit(*port))
1728 d->d_addr.sin.sin_port = htons((unsigned short)
1729 atoi((const char *) port));
1732 # ifdef NO_GETSERVBYNAME
1733 syserr("554 5.3.5 invalid port number: %s",
1735 # else /* NO_GETSERVBYNAME */
1736 register struct servent *sp;
1738 sp = getservbyname(port, "tcp");
1740 syserr("554 5.3.5 service \"%s\" unknown",
1743 d->d_addr.sin.sin_port = sp->s_port;
1744 # endif /* NO_GETSERVBYNAME */
1747 #endif /* NETINET */
1751 if (isascii(*port) && isdigit(*port))
1752 d->d_addr.sin6.sin6_port = htons((unsigned short)
1756 # ifdef NO_GETSERVBYNAME
1757 syserr("554 5.3.5 invalid port number: %s",
1759 # else /* NO_GETSERVBYNAME */
1760 register struct servent *sp;
1762 sp = getservbyname(port, "tcp");
1764 syserr("554 5.3.5 service \"%s\" unknown",
1767 d->d_addr.sin6.sin6_port = sp->s_port;
1768 # endif /* NO_GETSERVBYNAME */
1771 #endif /* NETINET6 */
1775 /* assume two byte transport selector */
1776 if (isascii(*port) && isdigit(*port))
1777 portno = htons((unsigned short) atoi(port));
1780 # ifdef NO_GETSERVBYNAME
1781 syserr("554 5.3.5 invalid port number: %s",
1783 # else /* NO_GETSERVBYNAME */
1784 register struct servent *sp;
1786 sp = getservbyname(port, "tcp");
1788 syserr("554 5.3.5 service \"%s\" unknown",
1791 portno = sp->s_port;
1792 # endif /* NO_GETSERVBYNAME */
1794 memmove(TSEL(&d->d_addr.siso),
1795 (char *) &portno, 2);
1800 syserr("554 5.3.5 Port= option unsupported for family %d",
1801 d->d_addr.sa.sa_family);
1807 ** SETDAEMONOPTIONS -- set options for running the MTA daemon
1810 ** p -- the options line.
1813 ** true if successful, false otherwise.
1816 ** increments number of daemons.
1819 #define DEF_LISTENQUEUE 10
1827 static struct dflags DaemonFlags[] =
1829 { "AUTHREQ", D_AUTHREQ },
1830 { "BINDIF", D_BINDIF },
1831 { "CANONREQ", D_CANONREQ },
1832 { "IFNHELO", D_IFNHELO },
1833 { "FQMAIL", D_FQMAIL },
1834 { "FQRCPT", D_FQRCPT },
1835 { "SMTPS", D_SMTPS },
1836 { "UNQUALOK", D_UNQUALOK },
1837 { "NOAUTH", D_NOAUTH },
1838 { "NOCANON", D_NOCANON },
1839 { "NOETRN", D_NOETRN },
1840 { "NOTLS", D_NOTLS },
1841 { "ETRNONLY", D_ETRNONLY },
1842 { "OPTIONAL", D_OPTIONAL },
1843 { "DISABLE", D_DISABLE },
1844 { "ISSET", D_ISSET },
1852 register struct dflags *df;
1855 for (df = DaemonFlags; df->d_name != NULL; df++)
1857 if (!bitnset(df->d_flag, d->d_flags))
1860 sm_dprintf("<%s", df->d_name);
1862 sm_dprintf(",%s", df->d_name);
1873 if (NDaemons >= MAXDAEMONS)
1875 Daemons[NDaemons].d_socket = -1;
1876 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1877 clrbitmap(Daemons[NDaemons].d_flags);
1878 setsockaddroptions(p, &Daemons[NDaemons]);
1881 if (Daemons[NDaemons].d_inputfilterlist != NULL)
1882 Daemons[NDaemons].d_inputfilterlist = newstr(Daemons[NDaemons].d_inputfilterlist);
1885 if (Daemons[NDaemons].d_name != NULL)
1886 Daemons[NDaemons].d_name = newstr(Daemons[NDaemons].d_name);
1891 (void) sm_snprintf(num, sizeof(num), "Daemon%d", NDaemons);
1892 Daemons[NDaemons].d_name = newstr(num);
1897 sm_dprintf("Daemon %s flags: ", Daemons[NDaemons].d_name);
1898 printdaemonflags(&Daemons[NDaemons]);
1905 ** INITDAEMON -- initialize daemon if not yet done.
1914 ** initializes structure for one daemon.
1922 Daemons[NDaemons].d_socket = -1;
1923 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1924 Daemons[NDaemons].d_name = "Daemon0";
1929 ** SETCLIENTOPTIONS -- set options for running the client
1932 ** p -- the options line.
1938 static DAEMON_T ClientSettings[AF_MAX + 1];
1947 memset(&d, '\0', sizeof(d));
1948 setsockaddroptions(p, &d);
1950 /* grab what we need */
1951 family = d.d_addr.sa.sa_family;
1952 STRUCTCOPY(d, ClientSettings[family]);
1953 setbitn(D_ISSET, ClientSettings[family].d_flags); /* mark as set */
1954 if (d.d_name != NULL)
1955 ClientSettings[family].d_name = newstr(d.d_name);
1960 (void) sm_snprintf(num, sizeof(num), "Client%d", family);
1961 ClientSettings[family].d_name = newstr(num);
1965 ** ADDR_FAMILY -- determine address family from address
1968 ** addr -- the string representation of the address
1971 ** AF_INET, AF_INET6 or AF_UNSPEC
1983 #endif /* NETINET6 */
1986 if (inet_addr(addr) != INADDR_NONE)
1989 sm_dprintf("addr_family(%s): INET\n", addr);
1992 #endif /* NETINET */
1994 if (anynet_pton(AF_INET6, addr, &clt_addr.sin6.sin6_addr) == 1)
1997 sm_dprintf("addr_family(%s): INET6\n", addr);
2000 #endif /* NETINET6 */
2001 #if _FFR_DAEMON_NETUNIX
2006 sm_dprintf("addr_family(%s): LOCAL\n", addr);
2009 # endif /* NETUNIX */
2010 #endif /* _FFR_DAEMON_NETUNIX */
2012 sm_dprintf("addr_family(%s): UNSPEC\n", addr);
2017 ** CHKCLIENTMODIFIERS -- check whether all clients have set a flag.
2020 ** flag -- the flag to test.
2023 ** true iff all configured clients have set the flag.
2027 chkclientmodifiers(flag)
2034 for (i = 0; i < AF_MAX; i++)
2036 if (bitnset(D_ISSET, ClientSettings[i].d_flags))
2038 if (!bitnset((char) flag, ClientSettings[i].d_flags))
2048 ** SETUP_DAEMON_FILTERS -- Parse per-socket filters
2058 setup_daemon_milters()
2062 if (OpMode == MD_SMTP)
2064 /* no need to configure the daemons */
2068 for (idx = 0; idx < NDaemons; idx++)
2070 if (Daemons[idx].d_inputfilterlist != NULL)
2072 milter_config(Daemons[idx].d_inputfilterlist,
2073 Daemons[idx].d_inputfilters,
2080 ** MAKECONNECTION -- make a connection to an SMTP socket on a machine.
2083 ** host -- the name of the host.
2084 ** port -- the port number to connect to.
2085 ** mci -- a pointer to the mail connection information
2086 ** structure to be filled in.
2087 ** e -- the current envelope.
2088 ** enough -- time at which to stop further connection attempts.
2089 ** (0 means no limit)
2092 ** An exit code telling whether the connection could be
2093 ** made and if not why not.
2099 static jmp_buf CtxConnectTimeout;
2101 SOCKADDR CurHostAddr; /* address of current host */
2104 makeconnection(host, port, mci, e, enough)
2106 volatile unsigned int port;
2111 register volatile int addrno = 0;
2113 register struct hostent *volatile hp = (struct hostent *) NULL;
2117 volatile SOCKADDR_LEN_T addrlen;
2118 volatile bool firstconnect = true;
2119 SM_EVENT *volatile ev = NULL;
2121 volatile bool v6found = false;
2122 #endif /* NETINET6 */
2123 volatile int family = InetMode;
2125 volatile SOCKADDR_LEN_T socksize = 0;
2126 volatile bool clt_bind;
2129 extern ENVELOPE BlankEnvelope;
2131 /* retranslate {daemon_flags} into bitmap */
2133 if ((p = macvalue(macid("{daemon_flags}"), e)) != NULL)
2135 for (; *p != '\0'; p++)
2137 if (!(isascii(*p) && isspace(*p)))
2138 setbitn(bitidx(*p), d_flags);
2144 #endif /* NETINET6 */
2147 /* Set up the address for outgoing connection. */
2148 if (bitnset(D_BINDIF, d_flags) &&
2149 (p = macvalue(macid("{if_addr}"), e)) != NULL &&
2153 char p6[INET6_ADDRSTRLEN];
2154 #endif /* NETINET6 */
2156 memset(&clt_addr, '\0', sizeof(clt_addr));
2158 /* infer the address family from the address itself */
2159 clt_addr.sa.sa_family = addr_family(p);
2160 switch (clt_addr.sa.sa_family)
2164 clt_addr.sin.sin_addr.s_addr = inet_addr(p);
2165 if (clt_addr.sin.sin_addr.s_addr != INADDR_NONE &&
2166 clt_addr.sin.sin_addr.s_addr !=
2167 htonl(INADDR_LOOPBACK))
2170 socksize = sizeof(struct sockaddr_in);
2173 #endif /* NETINET */
2177 if (inet_addr(p) != INADDR_NONE)
2178 (void) sm_snprintf(p6, sizeof(p6),
2179 "IPv6:::ffff:%s", p);
2181 (void) sm_strlcpy(p6, p, sizeof(p6));
2182 if (anynet_pton(AF_INET6, p6,
2183 &clt_addr.sin6.sin6_addr) == 1 &&
2184 !IN6_IS_ADDR_LOOPBACK(&clt_addr.sin6.sin6_addr))
2187 socksize = sizeof(struct sockaddr_in6);
2190 #endif /* NETINET6 */
2194 syserr("554 5.3.5 Address= option unsupported for family %d",
2195 clt_addr.sa.sa_family);
2200 family = clt_addr.sa.sa_family;
2203 /* D_BINDIF not set or not available, fallback to ClientPortOptions */
2206 STRUCTCOPY(ClientSettings[family].d_addr, clt_addr);
2207 switch (clt_addr.sa.sa_family)
2211 if (clt_addr.sin.sin_addr.s_addr == 0)
2212 clt_addr.sin.sin_addr.s_addr = LocalDaemon ?
2213 htonl(INADDR_LOOPBACK) : INADDR_ANY;
2216 if (clt_addr.sin.sin_port != 0)
2218 socksize = sizeof(struct sockaddr_in);
2220 #endif /* NETINET */
2223 if (IN6_IS_ADDR_UNSPECIFIED(&clt_addr.sin6.sin6_addr))
2224 clt_addr.sin6.sin6_addr =
2225 (LocalDaemon && V6LoopbackAddrFound) ?
2226 in6addr_loopback : in6addr_any;
2229 socksize = sizeof(struct sockaddr_in6);
2230 if (clt_addr.sin6.sin6_port != 0)
2233 #endif /* NETINET6 */
2236 socksize = sizeof(clt_addr.siso);
2246 ** Set up the address for the mailer.
2247 ** Accept "[a.b.c.d]" syntax for host name.
2252 memset(&CurHostAddr, '\0', sizeof(CurHostAddr));
2253 memset(&addr, '\0', sizeof(addr));
2254 SmtpPhase = mci->mci_phase = "initial connection";
2259 p = strchr(host, ']');
2263 unsigned long hid = INADDR_NONE;
2264 #endif /* NETINET */
2266 struct sockaddr_in6 hid6;
2267 #endif /* NETINET6 */
2271 memset(&hid6, '\0', sizeof(hid6));
2272 #endif /* NETINET6 */
2274 if (family == AF_INET &&
2275 (hid = inet_addr(&host[1])) != INADDR_NONE)
2277 addr.sin.sin_family = AF_INET;
2278 addr.sin.sin_addr.s_addr = hid;
2281 #endif /* NETINET */
2283 if (family == AF_INET6 &&
2284 anynet_pton(AF_INET6, &host[1],
2285 &hid6.sin6_addr) == 1)
2287 addr.sin6.sin6_family = AF_INET6;
2288 addr.sin6.sin6_addr = hid6.sin6_addr;
2291 #endif /* NETINET6 */
2293 /* try it as a host name (avoid MX lookup) */
2294 hp = sm_gethostbyname(&host[1], family);
2295 if (hp == NULL && p[-1] == '.')
2298 int oldopts = _res.options;
2300 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2301 #endif /* NAMED_BIND */
2303 hp = sm_gethostbyname(&host[1],
2307 _res.options = oldopts;
2308 #endif /* NAMED_BIND */
2317 extern char MsgBuf[];
2320 "553 Invalid numeric domain spec \"%s\"",
2322 mci_setstat(mci, EX_NOHOST, "5.1.2", MsgBuf);
2329 /* contortion to get around SGI cc complaints */
2331 p = &host[strlen(host) - 1];
2332 hp = sm_gethostbyname(host, family);
2333 if (hp == NULL && *p == '.')
2336 int oldopts = _res.options;
2338 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2339 #endif /* NAMED_BIND */
2341 hp = sm_gethostbyname(host, family);
2344 _res.options = oldopts;
2345 #endif /* NAMED_BIND */
2349 if (hp == NULL || hp->h_addr == NULL)
2352 /* check for name server timeouts */
2354 if (WorkAroundBrokenAAAA && family == AF_INET6 &&
2355 (h_errno == TRY_AGAIN || errno == ETIMEDOUT))
2358 ** An attempt with family AF_INET may
2359 ** succeed. By skipping the next section
2360 ** of code, we will try AF_INET before
2365 sm_dprintf("makeconnection: WorkAroundBrokenAAAA: Trying AF_INET lookup (AF_INET6 failed)\n");
2368 # endif /* NETINET6 */
2370 if (errno == ETIMEDOUT ||
2371 # if _FFR_GETHBN_ExFILE
2374 # endif /* EMFILE */
2377 # endif /* ENFILE */
2378 # endif /* _FFR_GETHBN_ExFILE */
2379 h_errno == TRY_AGAIN ||
2380 (errno == ECONNREFUSED && UseNameServer))
2383 mci_setstat(mci, EX_TEMPFAIL,
2389 #endif /* NAMED_BIND */
2392 ** Try v6 first, then fall back to v4.
2393 ** If we found a v6 address, but no v4
2394 ** addresses, then TEMPFAIL.
2397 if (family == AF_INET6)
2404 #endif /* NETINET6 */
2406 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2410 addr.sa.sa_family = hp->h_addrtype;
2411 switch (hp->h_addrtype)
2415 memmove(&addr.sin.sin_addr,
2419 #endif /* NETINET */
2423 memmove(&addr.sin6.sin6_addr,
2427 #endif /* NETINET6 */
2430 if (hp->h_length > sizeof(addr.sa.sa_data))
2432 syserr("makeconnection: long sa_data: family %d len %d",
2433 hp->h_addrtype, hp->h_length);
2434 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2438 memmove(addr.sa.sa_data, hp->h_addr, hp->h_length);
2445 ** Determine the port number.
2450 #ifdef NO_GETSERVBYNAME
2452 #else /* NO_GETSERVBYNAME */
2453 register struct servent *sp = getservbyname("smtp", "tcp");
2458 sm_syslog(LOG_ERR, NOQID,
2459 "makeconnection: service \"smtp\" unknown");
2464 #endif /* NO_GETSERVBYNAME */
2468 if (addr.sa.sa_family == AF_INET6 &&
2469 IN6_IS_ADDR_V4MAPPED(&addr.sin6.sin6_addr) &&
2470 ClientSettings[AF_INET].d_addr.sa.sa_family != 0)
2473 ** Ignore mapped IPv4 address since
2474 ** there is a ClientPortOptions setting
2480 #endif /* NETINET6 */
2482 switch (addr.sa.sa_family)
2486 addr.sin.sin_port = port;
2487 addrlen = sizeof(struct sockaddr_in);
2489 #endif /* NETINET */
2493 addr.sin6.sin6_port = port;
2494 addrlen = sizeof(struct sockaddr_in6);
2496 #endif /* NETINET6 */
2500 /* assume two byte transport selector */
2501 memmove(TSEL((struct sockaddr_iso *) &addr), (char *) &port, 2);
2502 addrlen = sizeof(struct sockaddr_iso);
2507 syserr("Can't connect to address family %d", addr.sa.sa_family);
2508 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2513 #endif /* NETINET6 */
2518 ** Try to actually open the connection.
2522 /* if too many connections, don't bother trying */
2523 if (!xla_noqueue_ok(host))
2528 # endif /* NETINET6 */
2536 sm_dprintf("makeconnection (%s [%s].%d (%d))\n",
2537 host, anynet_ntoa(&addr), ntohs(port),
2538 (int) addr.sa.sa_family);
2540 /* save for logging */
2544 if (bitnset(M_SECURE_PORT, mci->mci_mailer->m_flags))
2546 int rport = IPPORT_RESERVED - 1;
2548 s = rresvport(&rport);
2551 #endif /* HASRRESVPORT */
2553 s = socket(addr.sa.sa_family, SOCK_STREAM, 0);
2558 syserr("makeconnection: cannot create socket");
2562 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2566 #endif /* NETINET6 */
2572 if (ClientSettings[family].d_tcpsndbufsize > 0)
2574 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
2575 (char *) &ClientSettings[family].d_tcpsndbufsize,
2576 sizeof(ClientSettings[family].d_tcpsndbufsize)) < 0)
2577 syserr("makeconnection: setsockopt(SO_SNDBUF)");
2579 #endif /* SO_SNDBUF */
2581 if (ClientSettings[family].d_tcprcvbufsize > 0)
2583 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
2584 (char *) &ClientSettings[family].d_tcprcvbufsize,
2585 sizeof(ClientSettings[family].d_tcprcvbufsize)) < 0)
2586 syserr("makeconnection: setsockopt(SO_RCVBUF)");
2588 #endif /* SO_RCVBUF */
2591 sm_dprintf("makeconnection: fd=%d\n", s);
2593 /* turn on network debugging? */
2598 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
2599 (char *)&on, sizeof(on));
2601 if (e->e_xfp != NULL) /* for debugging */
2602 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
2603 errno = 0; /* for debugging */
2609 switch (clt_addr.sa.sa_family)
2613 if (clt_addr.sin.sin_port != 0)
2614 (void) setsockopt(s, SOL_SOCKET,
2619 #endif /* NETINET */
2623 if (clt_addr.sin6.sin6_port != 0)
2624 (void) setsockopt(s, SOL_SOCKET,
2629 #endif /* NETINET6 */
2632 if (bind(s, &clt_addr.sa, socksize) < 0)
2637 syserr("makeconnection: cannot bind socket [%s]",
2638 anynet_ntoa(&clt_addr));
2642 #endif /* NETINET6 */
2649 ** Linux seems to hang in connect for 90 minutes (!!!).
2650 ** Time out the connect to avoid this problem.
2653 if (setjmp(CtxConnectTimeout) == 0)
2657 if (e->e_ntries <= 0 && TimeOuts.to_iconnect != 0)
2658 ev = sm_setevent(TimeOuts.to_iconnect,
2660 else if (TimeOuts.to_connect != 0)
2661 ev = sm_setevent(TimeOuts.to_connect,
2666 switch (ConnectOnlyTo.sa.sa_family)
2670 addr.sin.sin_addr.s_addr = ConnectOnlyTo.sin.sin_addr.s_addr;
2671 addr.sa.sa_family = ConnectOnlyTo.sa.sa_family;
2673 #endif /* NETINET */
2677 memmove(&addr.sin6.sin6_addr,
2678 &ConnectOnlyTo.sin6.sin6_addr,
2681 #endif /* NETINET6 */
2684 sm_dprintf("Connecting to [%s]...\n", anynet_ntoa(&addr));
2685 i = connect(s, (struct sockaddr *) &addr, addrlen);
2695 /* couldn't connect.... figure out why */
2698 /* if running demand-dialed connection, try again */
2699 if (DialDelay > 0 && firstconnect &&
2700 bitnset(M_DIALDELAY, mci->mci_mailer->m_flags))
2703 sm_dprintf("Connect failed (%s); trying again...\n",
2704 sm_errstring(save_errno));
2705 firstconnect = false;
2706 (void) sleep(DialDelay);
2711 sm_syslog(LOG_INFO, e->e_id,
2712 "makeconnection (%s [%s]) failed: %s",
2713 host, anynet_ntoa(&addr),
2714 sm_errstring(save_errno));
2718 #endif /* NETINET6 */
2719 if (hp != NULL && hp->h_addr_list[addrno] != NULL &&
2720 (enough == 0 || curtime() < enough))
2723 sm_dprintf("Connect failed (%s); trying new address....\n",
2724 sm_errstring(save_errno));
2725 switch (addr.sa.sa_family)
2729 memmove(&addr.sin.sin_addr,
2730 hp->h_addr_list[addrno++],
2733 #endif /* NETINET */
2737 memmove(&addr.sin6.sin6_addr,
2738 hp->h_addr_list[addrno++],
2741 #endif /* NETINET6 */
2744 memmove(addr.sa.sa_data,
2745 hp->h_addr_list[addrno++],
2754 if (family == AF_INET6)
2757 sm_dprintf("Connect failed (%s); retrying with AF_INET....\n",
2758 sm_errstring(save_errno));
2769 #endif /* NETINET6 */
2770 /* couldn't open connection */
2772 /* Don't clobber an already saved errno from v4retry */
2774 #endif /* NETINET6 */
2777 sm_dprintf("Connect failed (%s)\n",
2778 sm_errstring(save_errno));
2782 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2786 #endif /* NETINET6 */
2797 #endif /* NETINET6 */
2799 /* connection ok, put it into canonical form */
2800 mci->mci_out = NULL;
2801 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2803 SM_IO_WRONLY_B, NULL)) == NULL ||
2805 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2807 SM_IO_RDONLY_B, NULL)) == NULL)
2810 syserr("cannot open SMTP client channel, fd=%d", s);
2811 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2812 if (mci->mci_out != NULL)
2813 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2818 sm_io_automode(mci->mci_out, mci->mci_in);
2820 /* set {client_flags} */
2821 if (ClientSettings[addr.sa.sa_family].d_mflags != NULL)
2823 macdefine(&mci->mci_macro, A_PERM,
2824 macid("{client_flags}"),
2825 ClientSettings[addr.sa.sa_family].d_mflags);
2828 macdefine(&mci->mci_macro, A_PERM,
2829 macid("{client_flags}"), "");
2831 /* "add" {client_flags} to bitmap */
2832 if (bitnset(D_IFNHELO, ClientSettings[addr.sa.sa_family].d_flags))
2834 /* look for just this one flag */
2835 setbitn(D_IFNHELO, d_flags);
2838 /* find out name for Interface through which we connect */
2840 if (getsockname(s, &addr.sa, &len) == 0)
2845 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2846 macid("{if_addr_out}"), anynet_ntoa(&addr));
2847 (void) sm_snprintf(family, sizeof(family), "%d",
2849 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2850 macid("{if_family_out}"), family);
2852 name = hostnamebyanyaddr(&addr);
2853 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2854 macid("{if_name_out}"), name);
2857 /* log connection information */
2858 sm_syslog(LOG_INFO, e->e_id,
2859 "SMTP outgoing connect on %.40s", name);
2861 if (bitnset(D_IFNHELO, d_flags))
2863 if (name[0] != '[' && strchr(name, '.') != NULL)
2864 mci->mci_heloname = newstr(name);
2869 macdefine(&BlankEnvelope.e_macro, A_PERM,
2870 macid("{if_name_out}"), NULL);
2871 macdefine(&BlankEnvelope.e_macro, A_PERM,
2872 macid("{if_addr_out}"), NULL);
2873 macdefine(&BlankEnvelope.e_macro, A_PERM,
2874 macid("{if_family_out}"), NULL);
2877 /* Use the configured HeloName as appropriate */
2878 if (HeloName != NULL && HeloName[0] != '\0')
2880 SM_FREE_CLR(mci->mci_heloname);
2881 mci->mci_heloname = newstr(HeloName);
2884 mci_setstat(mci, EX_OK, NULL, NULL);
2889 connecttimeout(ignore)
2893 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2894 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2899 longjmp(CtxConnectTimeout, 1);
2902 ** MAKECONNECTION_DS -- make a connection to a domain socket.
2905 ** mux_path -- the path of the socket to connect to.
2906 ** mci -- a pointer to the mail connection information
2907 ** structure to be filled in.
2910 ** An exit code telling whether the connection could be
2911 ** made and if not why not.
2919 makeconnection_ds(mux_path, mci)
2924 int rval, save_errno;
2925 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK;
2926 struct sockaddr_un unix_addr;
2928 /* if not safe, don't connect */
2929 rval = safefile(mux_path, RunAsUid, RunAsGid, RunAsUserName,
2930 sff, S_IRUSR|S_IWUSR, NULL);
2934 syserr("makeconnection_ds: unsafe domain socket %s",
2936 mci_setstat(mci, EX_TEMPFAIL, "4.3.5", NULL);
2941 /* prepare address structure */
2942 memset(&unix_addr, '\0', sizeof(unix_addr));
2943 unix_addr.sun_family = AF_UNIX;
2945 if (strlen(mux_path) >= sizeof(unix_addr.sun_path))
2947 syserr("makeconnection_ds: domain socket name %s too long",
2950 /* XXX why TEMPFAIL but 5.x.y ? */
2951 mci_setstat(mci, EX_TEMPFAIL, "5.3.5", NULL);
2952 errno = ENAMETOOLONG;
2953 return EX_UNAVAILABLE;
2955 (void) sm_strlcpy(unix_addr.sun_path, mux_path,
2956 sizeof(unix_addr.sun_path));
2958 /* initialize domain socket */
2959 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2963 syserr("makeconnection_ds: could not create domain socket %s",
2965 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2970 /* connect to server */
2971 if (connect(sock, (struct sockaddr *) &unix_addr,
2972 sizeof(unix_addr)) == -1)
2975 syserr("Could not connect to socket %s", mux_path);
2976 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2982 /* connection ok, put it into canonical form */
2983 mci->mci_out = NULL;
2984 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2985 (void *) &sock, SM_IO_WRONLY_B, NULL))
2987 || (sock = dup(sock)) < 0 ||
2988 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2989 (void *) &sock, SM_IO_RDONLY_B, NULL))
2993 syserr("cannot open SMTP client channel, fd=%d", sock);
2994 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2995 if (mci->mci_out != NULL)
2996 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
3001 sm_io_automode(mci->mci_out, mci->mci_in);
3003 mci_setstat(mci, EX_OK, NULL, NULL);
3007 #endif /* NETUNIX */
3009 ** SHUTDOWN_DAEMON -- Performs a clean shutdown of the daemon
3018 ** closes control socket, exits.
3027 sm_allsignals(true);
3029 reason = ShutdownRequest;
3030 ShutdownRequest = NULL;
3034 sm_syslog(LOG_INFO, CurEnv->e_id, "stopping daemon, reason=%s",
3035 reason == NULL ? "implicit call" : reason);
3038 closecontrolsocket(true);
3043 for (i = 0; i < NDaemons; i++)
3045 if (Daemons[i].d_socket >= 0)
3047 (void) close(Daemons[i].d_socket);
3048 Daemons[i].d_socket = -1;
3050 #if _FFR_DAEMON_NETUNIX
3052 /* Remove named sockets */
3053 if (Daemons[i].d_addr.sa.sa_family == AF_UNIX)
3056 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_MUSTOWN|SFF_EXECOK|SFF_CREAT;
3058 /* if not safe, don't use it */
3059 rval = safefile(Daemons[i].d_addr.sunix.sun_path,
3062 S_IRUSR|S_IWUSR, NULL);
3064 unlink(Daemons[i].d_addr.sunix.sun_path) < 0)
3066 sm_syslog(LOG_WARNING, NOQID,
3067 "Could not remove daemon %s socket: %s: %s",
3069 Daemons[i].d_addr.sunix.sun_path,
3070 sm_errstring(errno));
3073 # endif /* NETUNIX */
3074 #endif /* _FFR_DAEMON_NETUNIX */
3078 finis(false, true, EX_OK);
3081 ** RESTART_DAEMON -- Performs a clean restart of the daemon
3090 ** restarts the daemon or exits if restart fails.
3093 /* Make a non-DFL/IGN signal a noop */
3094 #define SM_NOOP_SIGNAL(sig, old) \
3097 (old) = sm_signal((sig), sm_signal_noop); \
3098 if ((old) == SIG_IGN || (old) == SIG_DFL) \
3099 (void) sm_signal((sig), (old)); \
3108 sigfunc_t ignore, oalrm, ousr1;
3109 extern int DtableSize;
3111 /* clear the events to turn off SIGALRMs */
3113 sm_allsignals(true);
3115 reason = RestartRequest;
3116 RestartRequest = NULL;
3119 if (SaveArgv[0][0] != '/')
3122 sm_syslog(LOG_INFO, NOQID,
3123 "could not restart: need full path");
3124 finis(false, true, EX_OSFILE);
3128 sm_syslog(LOG_INFO, NOQID, "restarting %s due to %s",
3130 reason == NULL ? "implicit call" : reason);
3132 closecontrolsocket(true);
3134 cleanup_shm(DaemonPid == getpid());
3135 #endif /* SM_CONF_SHM */
3137 /* close locked pid file */
3138 close_sendmail_pid();
3141 ** Want to drop to the user who started the process in all cases
3142 ** *but* when running as "smmsp" for the clientmqueue queue run
3143 ** daemon. In that case, UseMSP will be true, RunAsUid should not
3144 ** be root, and RealUid should be either 0 or RunAsUid.
3147 drop = !(UseMSP && RunAsUid != 0 &&
3148 (RealUid == 0 || RealUid == RunAsUid));
3150 if (drop_privileges(drop) != EX_OK)
3153 sm_syslog(LOG_ALERT, NOQID,
3154 "could not drop privileges: %s",
3155 sm_errstring(errno));
3156 finis(false, true, EX_OSERR);
3160 sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
3163 ** Need to allow signals before execve() to make them "harmless".
3164 ** However, the default action can be "terminate", so it isn't
3165 ** really harmless. Setting signals to IGN will cause them to be
3166 ** ignored in the new process to, so that isn't a good alternative.
3169 SM_NOOP_SIGNAL(SIGALRM, oalrm);
3170 SM_NOOP_SIGNAL(SIGCHLD, ignore);
3171 SM_NOOP_SIGNAL(SIGHUP, ignore);
3172 SM_NOOP_SIGNAL(SIGINT, ignore);
3173 SM_NOOP_SIGNAL(SIGPIPE, ignore);
3174 SM_NOOP_SIGNAL(SIGTERM, ignore);
3176 SM_NOOP_SIGNAL(SIGUSR1, ousr1);
3177 #endif /* SIGUSR1 */
3179 /* Turn back on signals */
3180 sm_allsignals(false);
3182 (void) execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
3185 /* block signals again and restore needed signals */
3186 sm_allsignals(true);
3188 /* For finis() events */
3189 (void) sm_signal(SIGALRM, oalrm);
3192 /* For debugging finis() */
3193 (void) sm_signal(SIGUSR1, ousr1);
3194 #endif /* SIGUSR1 */
3198 sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %s",
3199 SaveArgv[0], sm_errstring(errno));
3200 finis(false, true, EX_OSFILE);
3204 ** MYHOSTNAME -- return the name of this host.
3207 ** hostbuf -- a place to return the name of this host.
3208 ** size -- the size of hostbuf.
3211 ** A list of aliases for this host.
3214 ** Adds numeric codes to $=w.
3218 myhostname(hostbuf, size)
3222 register struct hostent *hp;
3224 if (gethostname(hostbuf, size) < 0 || hostbuf[0] == '\0')
3225 (void) sm_strlcpy(hostbuf, "localhost", size);
3226 hp = sm_gethostbyname(hostbuf, InetMode);
3227 #if NETINET && NETINET6
3228 if (hp == NULL && InetMode == AF_INET6)
3231 ** It's possible that this IPv6 enabled machine doesn't
3232 ** actually have any IPv6 interfaces and, therefore, no
3233 ** IPv6 addresses. Fall back to AF_INET.
3236 hp = sm_gethostbyname(hostbuf, AF_INET);
3238 #endif /* NETINET && NETINET6 */
3241 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
3242 (void) cleanstrcpy(hostbuf, hp->h_name, size);
3245 if (strchr(hostbuf, '.') == NULL)
3249 domainname = ni_propval("/locations", NULL, "resolver",
3251 if (domainname != NULL &&
3252 strlen(domainname) + strlen(hostbuf) + 1 < size)
3253 (void) sm_strlcat2(hostbuf, ".", domainname, size);
3255 #endif /* NETINFO */
3258 ** If there is still no dot in the name, try looking for a
3262 if (strchr(hostbuf, '.') == NULL)
3266 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
3268 if (strchr(*ha, '.') != NULL)
3270 (void) cleanstrcpy(hostbuf, *ha, size - 1);
3271 hostbuf[size - 1] = '\0';
3278 ** If _still_ no dot, wait for a while and try again -- it is
3279 ** possible that some service is starting up. This can result
3280 ** in excessive delays if the system is badly configured, but
3281 ** there really isn't a way around that, particularly given that
3282 ** the config file hasn't been read at this point.
3283 ** All in all, a bit of a mess.
3286 if (strchr(hostbuf, '.') == NULL &&
3287 !getcanonname(hostbuf, size, true, NULL))
3289 sm_syslog(LocalDaemon ? LOG_WARNING : LOG_CRIT, NOQID,
3290 "My unqualified host name (%s) unknown; sleeping for retry",
3292 message("My unqualified host name (%s) unknown; sleeping for retry",
3295 if (!getcanonname(hostbuf, size, true, NULL))
3297 sm_syslog(LocalDaemon ? LOG_WARNING : LOG_ALERT, NOQID,
3298 "unable to qualify my own domain name (%s) -- using short name",
3300 message("WARNING: unable to qualify my own domain name (%s) -- using short name",
3307 ** ADDRCMP -- compare two host addresses
3310 ** hp -- hostent structure for the first address
3311 ** ha -- actual first address
3312 ** sa -- second address
3315 ** 0 -- if ha and sa match
3316 ** else -- they don't match
3327 #endif /* NETINET6 */
3329 switch (sa->sa.sa_family)
3333 if (hp->h_addrtype == AF_INET)
3334 return memcmp(ha, (char *) &sa->sin.sin_addr, INADDRSZ);
3336 #endif /* NETINET */
3340 a = (unsigned char *) &sa->sin6.sin6_addr;
3342 /* Straight binary comparison */
3343 if (hp->h_addrtype == AF_INET6)
3344 return memcmp(ha, a, IN6ADDRSZ);
3346 /* If IPv4-mapped IPv6 address, compare the IPv4 section */
3347 if (hp->h_addrtype == AF_INET &&
3348 IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr))
3349 return memcmp(a + IN6ADDRSZ - INADDRSZ, ha, INADDRSZ);
3351 #endif /* NETINET6 */
3356 ** GETAUTHINFO -- get the real host name associated with a file descriptor
3358 ** Uses RFC1413 protocol to try to get info from the other end.
3361 ** fd -- the descriptor
3362 ** may_be_forged -- an outage that is set to true if the
3363 ** forward lookup of RealHostName does not match
3364 ** RealHostAddr; set to false if they do match.
3367 ** The user@host information associated with this descriptor.
3370 static jmp_buf CtxAuthTimeout;
3377 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
3378 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
3383 longjmp(CtxAuthTimeout, 1);
3387 getauthinfo(fd, may_be_forged)
3389 bool *may_be_forged;
3391 unsigned short SM_NONVOLATILE port = 0;
3392 SOCKADDR_LEN_T falen;
3393 register char *volatile p = NULL;
3395 SOCKADDR_LEN_T lalen;
3396 #ifndef NO_GETSERVBYNAME
3397 register struct servent *sp;
3399 static unsigned short port4 = 0;
3400 # endif /* NETINET */
3402 static unsigned short port6 = 0;
3403 # endif /* NETINET6 */
3404 #endif /* ! NO_GETSERVBYNAME */
3411 char *ostype = NULL;
3413 char ibuf[MAXNAME + 1];
3414 static char hbuf[MAXNAME + MAXAUTHINFO + 11];
3416 *may_be_forged = false;
3417 falen = sizeof(RealHostAddr);
3418 if (isatty(fd) || (i = getpeername(fd, &RealHostAddr.sa, &falen)) < 0 ||
3419 falen <= 0 || RealHostAddr.sa.sa_family == 0)
3424 ** ENOTSOCK is OK: bail on anything else, but reset
3425 ** errno in this case, so a mis-report doesn't
3429 if (errno != ENOTSOCK)
3433 (void) sm_strlcpyn(hbuf, sizeof(hbuf), 2, RealUserName,
3436 sm_dprintf("getauthinfo: %s\n", hbuf);
3440 if (RealHostName == NULL)
3442 /* translate that to a host name */
3443 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
3444 if (strlen(RealHostName) > MAXNAME)
3445 RealHostName[MAXNAME] = '\0'; /* XXX - 1 ? */
3448 /* cross check RealHostName with forward DNS lookup */
3449 if (anynet_ntoa(&RealHostAddr)[0] != '[' &&
3450 RealHostName[0] != '[')
3454 family = RealHostAddr.sa.sa_family;
3455 #if NETINET6 && NEEDSGETIPNODE
3457 ** If RealHostAddr is an IPv6 connection with an
3458 ** IPv4-mapped address, we need RealHostName's IPv4
3459 ** address(es) for addrcmp() to compare against
3462 ** Actually, we only need to do this for systems
3463 ** which NEEDSGETIPNODE since the real getipnodebyname()
3464 ** already does V4MAPPED address via the AI_V4MAPPEDCFG
3465 ** flag. A better fix to this problem is to add this
3466 ** functionality to our stub getipnodebyname().
3469 if (family == AF_INET6 &&
3470 IN6_IS_ADDR_V4MAPPED(&RealHostAddr.sin6.sin6_addr))
3472 #endif /* NETINET6 && NEEDSGETIPNODE */
3474 /* try to match the reverse against the forward lookup */
3475 hp = sm_gethostbyname(RealHostName, family);
3478 /* XXX: Could be a temporary error on forward lookup */
3479 *may_be_forged = true;
3483 for (ha = hp->h_addr_list; *ha != NULL; ha++)
3485 if (addrcmp(hp, *ha, &RealHostAddr) == 0)
3488 *may_be_forged = *ha == NULL;
3492 #endif /* NETINET6 */
3496 if (TimeOuts.to_ident == 0)
3500 switch (RealHostAddr.sa.sa_family)
3504 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3506 la.sa.sa_family != AF_INET)
3511 port = RealHostAddr.sin.sin_port;
3513 /* create ident query */
3514 (void) sm_snprintf(ibuf, sizeof(ibuf), "%d,%d\r\n",
3515 ntohs(RealHostAddr.sin.sin_port),
3516 ntohs(la.sin.sin_port));
3518 /* create local address */
3519 la.sin.sin_port = 0;
3521 /* create foreign address */
3522 # ifdef NO_GETSERVBYNAME
3523 RealHostAddr.sin.sin_port = htons(113);
3524 # else /* NO_GETSERVBYNAME */
3527 ** getservbyname() consumes about 5% of the time
3528 ** when receiving a small message (almost all of the time
3529 ** spent in this routine).
3530 ** Hence we store the port in a static variable
3531 ** to save this time.
3532 ** The portnumber shouldn't change very often...
3533 ** This code makes the assumption that the port number
3539 sp = getservbyname("auth", "tcp");
3545 RealHostAddr.sin.sin_port = port4;
3547 # endif /* NO_GETSERVBYNAME */
3548 #endif /* NETINET */
3552 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3554 la.sa.sa_family != AF_INET6)
3559 port = RealHostAddr.sin6.sin6_port;
3561 /* create ident query */
3562 (void) sm_snprintf(ibuf, sizeof(ibuf), "%d,%d\r\n",
3563 ntohs(RealHostAddr.sin6.sin6_port),
3564 ntohs(la.sin6.sin6_port));
3566 /* create local address */
3567 la.sin6.sin6_port = 0;
3569 /* create foreign address */
3570 # ifdef NO_GETSERVBYNAME
3571 RealHostAddr.sin6.sin6_port = htons(113);
3572 # else /* NO_GETSERVBYNAME */
3575 sp = getservbyname("auth", "tcp");
3581 RealHostAddr.sin6.sin6_port = port6;
3583 # endif /* NO_GETSERVBYNAME */
3584 #endif /* NETINET6 */
3591 if (setjmp(CtxAuthTimeout) != 0)
3598 /* put a timeout around the whole thing */
3599 ev = sm_setevent(TimeOuts.to_ident, authtimeout, 0);
3601 /* connect to foreign IDENT server using same address as SMTP socket */
3602 s = socket(la.sa.sa_family, SOCK_STREAM, 0);
3608 if (bind(s, &la.sa, lalen) < 0 ||
3609 connect(s, &RealHostAddr.sa, lalen) < 0)
3613 sm_dprintf("getauthinfo: sent %s", ibuf);
3616 if (write(s, ibuf, strlen(ibuf)) < 0)
3621 nleft = sizeof(ibuf) - 1;
3622 while ((i = read(s, p, nleft)) > 0)
3629 if ((s = strchr(ibuf, '\n')) != NULL)
3643 if (i < 0 || p == &ibuf[0])
3646 if (p >= &ibuf[2] && *--p == '\n' && *--p == '\r')
3651 sm_dprintf("getauthinfo: got %s\n", ibuf);
3654 p = strchr(ibuf, ':');
3657 /* malformed response */
3660 while (isascii(*++p) && isspace(*p))
3662 if (sm_strncasecmp(p, "userid", 6) != 0)
3664 /* presumably an error string */
3668 while (isascii(*p) && isspace(*p))
3672 /* either useridxx or malformed response */
3676 /* p now points to the OSTYPE field */
3677 while (isascii(*p) && isspace(*p))
3683 /* malformed response */
3691 charset = strchr(ostype, ',');
3692 if (charset != NULL)
3696 /* 1413 says don't do this -- but it's broken otherwise */
3697 while (isascii(*++p) && isspace(*p))
3700 /* p now points to the authenticated name -- copy carefully */
3701 if (sm_strncasecmp(ostype, "other", 5) == 0 &&
3702 (ostype[5] == ' ' || ostype[5] == '\0'))
3704 (void) sm_strlcpy(hbuf, "IDENT:", sizeof(hbuf));
3705 cleanstrcpy(&hbuf[6], p, MAXAUTHINFO);
3708 cleanstrcpy(hbuf, p, MAXAUTHINFO);
3710 (void) sm_strlcpyn(&hbuf[len], sizeof(hbuf) - len, 2, "@",
3711 RealHostName == NULL ? "localhost" : RealHostName);
3719 /* put back the original incoming port */
3720 switch (RealHostAddr.sa.sa_family)
3725 RealHostAddr.sin.sin_port = port;
3727 #endif /* NETINET */
3732 RealHostAddr.sin6.sin6_port = port;
3734 #endif /* NETINET6 */
3737 if (RealHostName == NULL)
3740 sm_dprintf("getauthinfo: NULL\n");
3743 (void) sm_strlcpy(hbuf, RealHostName, sizeof(hbuf));
3747 # ifndef GET_IPOPT_DST
3748 # define GET_IPOPT_DST(dst) (dst)
3749 # endif /* ! GET_IPOPT_DST */
3751 ** Extract IP source routing information.
3753 ** Format of output for a connection from site a through b
3755 ** loose: @site-c@site-b:site-a
3756 ** strict: !@site-c@site-b:site-a
3758 ** o - pointer within ipopt_list structure.
3759 ** q - pointer within ls/ss rr route data
3760 ** p - pointer to hbuf
3763 if (RealHostAddr.sa.sa_family == AF_INET)
3765 SOCKOPT_LEN_T ipoptlen;
3770 struct IPOPTION ipopt;
3772 ipoptlen = sizeof(ipopt);
3773 if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
3774 (char *) &ipopt, &ipoptlen) < 0)
3778 o = (unsigned char *) ipopt.IP_LIST;
3779 while (o != NULL && o < (unsigned char *) &ipopt + ipoptlen)
3795 ** o[0] is the option type (loose/strict).
3796 ** o[1] is the length of this option,
3797 ** including option type and
3799 ** o[2] is the pointer into the route
3801 ** o[3] begins the route data.
3804 p = &hbuf[strlen(hbuf)];
3805 l = sizeof(hbuf) - (hbuf - p) - 6;
3806 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
3808 *o == IPOPT_SSRR ? "!" : "",
3809 l > 240 ? 120 : l / 2,
3810 inet_ntoa(GET_IPOPT_DST(ipopt.IP_DST)));
3815 j = o[1] / sizeof(struct in_addr) - 1;
3817 /* q skips length and router pointer to data */
3819 for ( ; j >= 0; j--)
3821 struct in_addr addr;
3823 memcpy(&addr, q, sizeof(addr));
3824 (void) sm_snprintf(p,
3834 q += sizeof(struct in_addr);
3840 /* Skip over option */
3845 (void) sm_snprintf(p, SPACELEFT(hbuf, p), "]");
3850 #endif /* IP_SRCROUTE */
3851 if (RealHostName != NULL && RealHostName[0] != '[')
3853 p = &hbuf[strlen(hbuf)];
3854 (void) sm_snprintf(p, SPACELEFT(hbuf, p), " [%.100s]",
3855 anynet_ntoa(&RealHostAddr));
3859 p = &hbuf[strlen(hbuf)];
3860 (void) sm_strlcpy(p, " (may be forged)", SPACELEFT(hbuf, p));
3861 macdefine(&BlankEnvelope.e_macro, A_PERM,
3862 macid("{client_resolve}"), "FORGED");
3867 #endif /* IP_SRCROUTE */
3869 /* put back the original incoming port */
3870 switch (RealHostAddr.sa.sa_family)
3875 RealHostAddr.sin.sin_port = port;
3877 #endif /* NETINET */
3882 RealHostAddr.sin6.sin6_port = port;
3884 #endif /* NETINET6 */
3888 sm_dprintf("getauthinfo: %s\n", hbuf);
3892 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
3895 ** map -- a pointer to this map.
3896 ** name -- the (presumably unqualified) hostname.
3897 ** av -- unused -- for compatibility with other mapping
3899 ** statp -- an exit status (out parameter) -- set to
3900 ** EX_TEMPFAIL if the name server is unavailable.
3903 ** The mapping, if found.
3904 ** NULL if no mapping found.
3907 ** Looks up the host specified in hbuf. If it is not
3908 ** the canonical name for that host, return the canonical
3909 ** name (unless MF_MATCHONLY is set, which will cause the
3910 ** status only to be returned).
3914 host_map_lookup(map, name, av, statp)
3920 register struct hostent *hp;
3922 struct in_addr in_addr;
3923 #endif /* NETINET */
3925 struct in6_addr in6_addr;
3926 #endif /* NETINET6 */
3927 char *cp, *ans = NULL;
3931 time_t SM_NONVOLATILE retrans = 0;
3932 int SM_NONVOLATILE retry = 0;
3933 #endif /* NAMED_BIND */
3934 char hbuf[MAXNAME + 1];
3937 ** See if we have already looked up this name. If so, just
3938 ** return it (unless expired).
3942 s = stab(name, ST_NAMECANON, ST_ENTER);
3943 if (bitset(NCF_VALID, s->s_namecanon.nc_flags) &&
3944 s->s_namecanon.nc_exp >= now)
3947 sm_dprintf("host_map_lookup(%s) => CACHE %s\n",
3949 s->s_namecanon.nc_cname == NULL
3951 : s->s_namecanon.nc_cname);
3952 errno = s->s_namecanon.nc_errno;
3953 SM_SET_H_ERRNO(s->s_namecanon.nc_herrno);
3954 *statp = s->s_namecanon.nc_stat;
3955 if (*statp == EX_TEMPFAIL)
3957 CurEnv->e_status = "4.4.3";
3958 message("851 %s: Name server timeout",
3959 shortenstring(name, 33));
3961 if (*statp != EX_OK)
3963 if (s->s_namecanon.nc_cname == NULL)
3965 syserr("host_map_lookup(%s): bogus NULL cache entry, errno=%d, h_errno=%d",
3967 s->s_namecanon.nc_errno,
3968 s->s_namecanon.nc_herrno);
3971 if (bitset(MF_MATCHONLY, map->map_mflags))
3972 cp = map_rewrite(map, name, strlen(name), NULL);
3974 cp = map_rewrite(map,
3975 s->s_namecanon.nc_cname,
3976 strlen(s->s_namecanon.nc_cname),
3982 ** If we are running without a regular network connection (usually
3983 ** dial-on-demand) and we are just queueing, we want to avoid DNS
3984 ** lookups because those could try to connect to a server.
3987 if (CurEnv->e_sendmode == SM_DEFER &&
3988 bitset(MF_DEFER, map->map_mflags))
3991 sm_dprintf("host_map_lookup(%s) => DEFERRED\n", name);
3992 *statp = EX_TEMPFAIL;
3997 ** If first character is a bracket, then it is an address
3998 ** lookup. Address is copied into a temporary buffer to
3999 ** strip the brackets and to preserve name if address is
4004 sm_dprintf("host_map_lookup(%s) => ", name);
4006 if (map->map_timeout > 0)
4008 retrans = _res.retrans;
4009 _res.retrans = map->map_timeout;
4011 if (map->map_retry > 0)
4014 _res.retry = map->map_retry;
4016 #endif /* NAMED_BIND */
4018 /* set default TTL */
4019 s->s_namecanon.nc_exp = now + SM_DEFAULT_TTL;
4024 (void) sm_strlcpy(hbuf, name, sizeof(hbuf));
4025 if (getcanonname(hbuf, sizeof(hbuf) - 1, !HasWildcardMX, &ttl))
4029 s->s_namecanon.nc_exp = now + SM_MIN(ttl,
4035 if ((cp = strchr(name, ']')) == NULL)
4038 sm_dprintf("FAILED\n");
4045 if ((in_addr.s_addr = inet_addr(&name[1])) != INADDR_NONE)
4046 hp = sm_gethostbyaddr((char *)&in_addr,
4048 #endif /* NETINET */
4051 anynet_pton(AF_INET6, &name[1], &in6_addr) == 1)
4052 hp = sm_gethostbyaddr((char *)&in6_addr,
4053 IN6ADDRSZ, AF_INET6);
4054 #endif /* NETINET6 */
4059 /* found a match -- copy out */
4060 ans = denlstring((char *) hp->h_name, true, true);
4062 if (ans == hp->h_name)
4064 static char n[MAXNAME + 1];
4066 /* hp->h_name is about to disappear */
4067 (void) sm_strlcpy(n, ans, sizeof(n));
4072 #endif /* NETINET6 */
4076 if (map->map_timeout > 0)
4077 _res.retrans = retrans;
4078 if (map->map_retry > 0)
4080 #endif /* NAMED_BIND */
4082 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
4084 /* Found an answer */
4087 s->s_namecanon.nc_stat = *statp = EX_OK;
4088 if (s->s_namecanon.nc_cname != NULL)
4089 sm_free(s->s_namecanon.nc_cname);
4090 s->s_namecanon.nc_cname = sm_strdup_x(ans);
4091 if (bitset(MF_MATCHONLY, map->map_mflags))
4092 cp = map_rewrite(map, name, strlen(name), NULL);
4094 cp = map_rewrite(map, ans, strlen(ans), av);
4096 sm_dprintf("FOUND %s\n", ans);
4101 /* No match found */
4102 s->s_namecanon.nc_errno = errno;
4104 s->s_namecanon.nc_herrno = h_errno;
4106 sm_dprintf("FAIL (%d)\n", h_errno);
4112 CurEnv->e_status = "4.4.3";
4113 message("851 %s: Name server timeout",
4114 shortenstring(name, 33));
4116 *statp = EX_TEMPFAIL;
4119 case HOST_NOT_FOUND:
4125 *statp = EX_SOFTWARE;
4129 *statp = EX_UNAVAILABLE;
4132 #else /* NAMED_BIND */
4134 sm_dprintf("FAIL\n");
4136 #endif /* NAMED_BIND */
4137 s->s_namecanon.nc_stat = *statp;
4141 ** HOST_MAP_INIT -- initialize host class structures
4144 ** map -- a pointer to this map.
4145 ** args -- argument string.
4152 host_map_init(map, args)
4156 register char *p = args;
4160 while (isascii(*p) && isspace(*p))
4171 map->map_tapp = ++p;
4175 map->map_mflags |= MF_MATCHONLY;
4179 map->map_mflags |= MF_NODEFER;
4182 case 'S': /* only for consistency */
4183 map->map_spacesub = *++p;
4187 map->map_mflags |= MF_DEFER;
4194 while (isascii(*++p) && isspace(*p))
4199 map->map_timeout = convtime(p, 's');
4206 while (isascii(*++p) && isspace(*p))
4208 map->map_retry = atoi(p);
4211 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
4216 if (map->map_app != NULL)
4217 map->map_app = newstr(map->map_app);
4218 if (map->map_tapp != NULL)
4219 map->map_tapp = newstr(map->map_tapp);
4225 ** ANYNET_NTOP -- convert an IPv6 network address to printable form.
4228 ** s6a -- a pointer to an in6_addr structure.
4229 ** dst -- buffer to store result in
4230 ** dst_len -- size of dst buffer
4233 ** A printable version of that structure.
4237 anynet_ntop(s6a, dst, dst_len)
4238 struct in6_addr *s6a;
4244 if (IN6_IS_ADDR_V4MAPPED(s6a))
4245 ap = (char *) inet_ntop(AF_INET,
4246 &s6a->s6_addr[IN6ADDRSZ - INADDRSZ],
4253 /* Save pointer to beginning of string */
4256 /* Add IPv6: protocol tag */
4257 sz = sm_strlcpy(dst, "IPv6:", dst_len);
4263 ap = sm_inet6_ntop(s6a, dst, dst_len);
4264 # else /* _FFR_IPV6_FULL */
4265 ap = (char *) inet_ntop(AF_INET6, s6a, dst, dst_len);
4266 # endif /* _FFR_IPV6_FULL */
4268 /* Restore pointer to beginning of string */
4276 ** ANYNET_PTON -- convert printed form to network address.
4278 ** Wrapper for inet_pton() which handles IPv6: labels.
4281 ** family -- address family
4283 ** dst -- destination address structure
4286 ** 1 if the address was valid
4287 ** 0 if the address wasn't parseable
4292 anynet_pton(family, src, dst)
4297 if (family == AF_INET6 && sm_strncasecmp(src, "IPv6:", 5) == 0)
4299 return inet_pton(family, src, dst);
4301 #endif /* NETINET6 */
4303 ** ANYNET_NTOA -- convert a network address to printable form.
4306 ** sap -- a pointer to a sockaddr structure.
4309 ** A printable version of that sockaddr.
4312 #ifdef USE_SOCK_STREAM
4315 # include <net/if_dl.h>
4316 # endif /* NETLINK */
4320 register SOCKADDR *sap;
4325 static char buf[100];
4327 /* check for null/zero family */
4330 if (sap->sa.sa_family == 0)
4333 switch (sap->sa.sa_family)
4337 if (sap->sunix.sun_path[0] != '\0')
4338 (void) sm_snprintf(buf, sizeof(buf), "[UNIX: %.64s]",
4339 sap->sunix.sun_path);
4341 (void) sm_strlcpy(buf, "[UNIX: localhost]", sizeof(buf));
4343 # endif /* NETUNIX */
4347 return (char *) inet_ntoa(sap->sin.sin_addr);
4348 # endif /* NETINET */
4352 ap = anynet_ntop(&sap->sin6.sin6_addr, buf, sizeof(buf));
4356 # endif /* NETINET6 */
4360 (void) sm_snprintf(buf, sizeof(buf), "[LINK: %s]",
4361 link_ntoa((struct sockaddr_dl *) &sap->sa));
4363 # endif /* NETLINK */
4365 /* this case is needed when nothing is #defined */
4366 /* in order to keep the switch syntactically correct */
4370 /* unknown family -- just dump bytes */
4371 (void) sm_snprintf(buf, sizeof(buf), "Family %d: ", sap->sa.sa_family);
4372 bp = &buf[strlen(buf)];
4373 ap = sap->sa.sa_data;
4374 for (l = sizeof(sap->sa.sa_data); --l >= 0; )
4376 (void) sm_snprintf(bp, SPACELEFT(buf, bp), "%02x:",
4384 ** HOSTNAMEBYANYADDR -- return name of host based on address
4387 ** sap -- SOCKADDR pointer
4390 ** text representation of host name.
4397 hostnamebyanyaddr(sap)
4398 register SOCKADDR *sap;
4400 register struct hostent *hp;
4403 # endif /* NAMED_BIND */
4405 struct in6_addr in6_addr;
4406 # endif /* NETINET6 */
4409 /* shorten name server timeout to avoid higher level timeouts */
4410 saveretry = _res.retry;
4411 if (_res.retry * _res.retrans > 20)
4412 _res.retry = 20 / _res.retrans;
4413 if (_res.retry == 0)
4415 # endif /* NAMED_BIND */
4417 switch (sap->sa.sa_family)
4421 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
4424 # endif /* NETINET */
4428 hp = sm_gethostbyaddr((char *) &sap->sin6.sin6_addr,
4429 IN6ADDRSZ, AF_INET6);
4431 # endif /* NETINET6 */
4435 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
4436 sizeof(sap->siso.siso_addr), AF_ISO);
4438 # endif /* NETISO */
4444 # endif /* NETUNIX */
4447 hp = sm_gethostbyaddr(sap->sa.sa_data, sizeof(sap->sa.sa_data),
4453 _res.retry = saveretry;
4454 # endif /* NAMED_BIND */
4456 # if NETINET || NETINET6
4457 if (hp != NULL && hp->h_name[0] != '['
4459 && inet_pton(AF_INET6, hp->h_name, &in6_addr) != 1
4460 # endif /* NETINET6 */
4462 && inet_addr(hp->h_name) == INADDR_NONE
4463 # endif /* NETINET */
4468 name = denlstring((char *) hp->h_name, true, true);
4470 if (name == hp->h_name)
4472 static char n[MAXNAME + 1];
4474 /* Copy the string, hp->h_name is about to disappear */
4475 (void) sm_strlcpy(n, name, sizeof(n));
4479 # endif /* NETINET6 */
4482 # endif /* NETINET || NETINET6 */
4490 # endif /* NETINET6 */
4493 if (sap->sa.sa_family == AF_UNIX && sap->sunix.sun_path[0] == '\0')
4495 # endif /* NETUNIX */
4497 static char buf[203];
4499 (void) sm_snprintf(buf, sizeof(buf), "[%.200s]",
4504 #endif /* USE_SOCK_STREAM */