2 * Copyright (c) 1998-2006 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: daemon.c,v 8.665 2006/03/02 19:12:00 ca Exp $")
18 #if defined(SOCK_STREAM) || defined(__GNU_LIBRARY__)
19 # define USE_SOCK_STREAM 1
20 #endif /* defined(SOCK_STREAM) || defined(__GNU_LIBRARY__) */
22 #if defined(USE_SOCK_STREAM)
23 # if NETINET || NETINET6
24 # include <arpa/inet.h>
25 # endif /* NETINET || NETINET6 */
28 # define NO_DATA NO_ADDRESS
29 # endif /* ! NO_DATA */
30 # endif /* NAMED_BIND */
31 #endif /* defined(USE_SOCK_STREAM) */
34 # include <openssl/rand.h>
39 #if IP_SRCROUTE && NETINET
40 # include <netinet/in_systm.h>
41 # include <netinet/ip.h>
43 # include <netinet/in.h>
45 # define IPOPTION ip_opts
46 # define IP_LIST ip_opts
47 # define IP_DST ip_dst
48 # endif /* ! IPOPTION */
50 # include <netinet/ip_var.h>
52 # define IPOPTION ipoption
53 # define IP_LIST ipopt_list
54 # define IP_DST ipopt_dst
55 # endif /* ! IPOPTION */
56 # endif /* HAS_IN_H */
57 #endif /* IP_SRCROUTE && NETINET */
61 /* structure to describe a daemon or a client */
64 int d_socket; /* fd for socket */
65 SOCKADDR d_addr; /* socket for incoming */
66 unsigned short d_port; /* port number */
67 int d_listenqueue; /* size of listen queue */
68 int d_tcprcvbufsize; /* size of TCP receive buffer */
69 int d_tcpsndbufsize; /* size of TCP send buffer */
70 time_t d_refuse_connections_until;
73 BITMAP256 d_flags; /* flags; see sendmail.h */
74 char *d_mflags; /* flags for use in macro */
75 char *d_name; /* user-supplied name */
77 char *d_inputfilterlist;
78 struct milter *d_inputfilters[MAXFILTERS];
80 #if _FFR_SS_PER_DAEMON
82 #endif /* _FFR_SS_PER_DAEMON */
83 #if _FFR_DM_PER_DAEMON
84 int d_dm; /* DeliveryMode */
85 #endif /* _FFR_DM_PER_DAEMON */
88 typedef struct daemon DAEMON_T;
90 #define SAFE_NOTSET (-1) /* SuperSafe (per daemon) option not set */
91 /* see also sendmail.h: SuperSafe values */
93 static void connecttimeout __P((int));
94 static int opendaemonsocket __P((DAEMON_T *, bool));
95 static unsigned short setupdaemon __P((SOCKADDR *));
96 static void getrequests_checkdiskspace __P((ENVELOPE *e));
97 static void setsockaddroptions __P((char *, DAEMON_T *));
98 static void printdaemonflags __P((DAEMON_T *));
99 static int addr_family __P((char *));
100 static int addrcmp __P((struct hostent *, char *, SOCKADDR *));
101 static void authtimeout __P((int));
104 ** DAEMON.C -- routines to use when running as a daemon.
106 ** This entire file is highly dependent on the 4.2 BSD
107 ** interprocess communication primitives. No attempt has
108 ** been made to make this file portable to Version 7,
109 ** Version 6, MPX files, etc. If you should try such a
110 ** thing yourself, I recommend chucking the entire file
111 ** and starting from scratch. Basic semantics are:
114 ** Opens a port and initiates a connection.
115 ** Returns in a child. Must set InChannel and
116 ** OutChannel appropriately.
118 ** Close any open files associated with getting
119 ** the connection; this is used when running the queue,
120 ** etc., to avoid having extra file descriptors during
121 ** the queue run and to avoid confusing the network
122 ** code (if it cares).
123 ** makeconnection(host, port, mci, e, enough)
124 ** Make a connection to the named host on the given
125 ** port. Returns zero on success, else an exit status
126 ** describing the error.
127 ** host_map_lookup(map, hbuf, avp, pstat)
128 ** Convert the entry in hbuf into a canonical form.
131 static DAEMON_T Daemons[MAXDAEMONS];
132 static int NDaemons = 0; /* actual number of daemons */
134 static time_t NextDiskSpaceCheck = 0;
137 ** GETREQUESTS -- open mail IPC port and get requests.
140 ** e -- the current envelope.
146 ** Waits until some interesting activity occurs. When
147 ** it does, a child is created to process it, and the
148 ** parent waits for completion. Return from this
149 ** routine is always in the child. The file pointers
150 ** "InChannel" and "OutChannel" should be set to point
151 ** to the communication channel.
152 ** May restart persistent queue runners if they have ended
161 int idx, curdaemon = -1;
162 int i, olddaemon = 0;
166 char status[MAXLINE];
168 SOCKADDR_LEN_T len = sizeof sa;
169 #if _FFR_QUEUE_RUN_PARANOIA
171 #endif /* _FFR_QUEUE_RUN_PARANOIA */
173 extern int ControlSocket;
174 # endif /* NETUNIX */
175 extern ENVELOPE BlankEnvelope;
176 extern bool refuseconnections __P((char *, ENVELOPE *, int, bool));
179 /* initialize data for function that generates queue ids */
181 for (idx = 0; idx < NDaemons; idx++)
183 Daemons[idx].d_port = setupdaemon(&(Daemons[idx].d_addr));
184 Daemons[idx].d_firsttime = true;
185 Daemons[idx].d_refuse_connections_until = (time_t) 0;
189 ** Try to actually open the connection.
194 for (idx = 0; idx < NDaemons; idx++)
196 sm_dprintf("getrequests: daemon %s: port %d\n",
198 ntohs(Daemons[idx].d_port));
202 /* get a socket for the SMTP connection */
203 for (idx = 0; idx < NDaemons; idx++)
204 Daemons[idx].d_socksize = opendaemonsocket(&Daemons[idx], true);
206 if (opencontrolsocket() < 0)
207 sm_syslog(LOG_WARNING, NOQID,
208 "daemon could not open control socket %s: %s",
209 ControlSocketName, sm_errstring(errno));
211 /* If there are any queue runners released reapchild() co-ord's */
212 (void) sm_signal(SIGCHLD, reapchild);
214 /* write the pid to file, command line args to syslog */
219 char jbuf[MAXHOSTNAMELEN];
221 expand("\201j", jbuf, sizeof jbuf, e);
222 j_has_dot = strchr(jbuf, '.') != NULL;
226 /* Add parent process as first item */
227 proc_list_add(CurrentPid, "Sendmail daemon", PROC_DAEMON, 0, -1, NULL);
231 for (idx = 0; idx < NDaemons; idx++)
232 sm_dprintf("getrequests: daemon %s: %d\n",
234 Daemons[idx].d_socket);
240 auto SOCKADDR_LEN_T lotherend;
241 bool timedout = false;
242 bool control = false;
248 #endif /* STARTTLS */
250 /* see if we are rejecting connections */
251 (void) sm_blocksignal(SIGALRM);
254 for (idx = 0; idx < NDaemons; idx++)
257 ** XXX do this call outside the loop?
258 ** no: refuse_connections may sleep().
262 if (now < Daemons[idx].d_refuse_connections_until)
264 if (bitnset(D_DISABLE, Daemons[idx].d_flags))
266 if (refuseconnections(Daemons[idx].d_name, e, idx,
269 if (Daemons[idx].d_socket >= 0)
271 /* close socket so peer fails quickly */
272 (void) close(Daemons[idx].d_socket);
273 Daemons[idx].d_socket = -1;
276 /* refuse connections for next 15 seconds */
277 Daemons[idx].d_refuse_connections_until = now + 15;
279 else if (Daemons[idx].d_socket < 0 ||
280 Daemons[idx].d_firsttime)
282 if (!Daemons[idx].d_firsttime && LogLevel > 8)
283 sm_syslog(LOG_INFO, NOQID,
284 "accepting connections again for daemon %s",
285 Daemons[idx].d_name);
287 /* arrange to (re)open the socket if needed */
288 (void) opendaemonsocket(&Daemons[idx], false);
289 Daemons[idx].d_firsttime = false;
293 /* May have been sleeping above, check again */
296 getrequests_checkdiskspace(e);
299 /* check for disaster */
301 char jbuf[MAXHOSTNAMELEN];
303 expand("\201j", jbuf, sizeof jbuf, e);
304 if (!wordinclass(jbuf, 'w'))
306 dumpstate("daemon lost $j");
307 sm_syslog(LOG_ALERT, NOQID,
308 "daemon process doesn't have $j in $=w; see syslog");
311 else if (j_has_dot && strchr(jbuf, '.') == NULL)
313 dumpstate("daemon $j lost dot");
314 sm_syslog(LOG_ALERT, NOQID,
315 "daemon process $j lost dot; see syslog");
323 ** Andrew Sun <asun@ieps-sun.ml.com> claims that this will
324 ** fix the SVr4 problem. But it seems to have gone away,
325 ** so is it worth doing this?
328 if (DaemonSocket >= 0 &&
329 SetNonBlocking(DaemonSocket, false) < 0)
332 (void) sm_releasesignal(SIGALRM);
336 bool setproc = false;
339 struct timeval timeout;
343 for (idx = 0; idx < NDaemons; idx++)
345 /* wait for a connection */
346 if (Daemons[idx].d_socket >= 0)
350 Daemons[idx].d_flags))
352 sm_setproctitle(true, e,
353 "accepting connections");
356 if (Daemons[idx].d_socket > highest)
357 highest = Daemons[idx].d_socket;
358 SM_FD_SET(Daemons[idx].d_socket,
364 if (ControlSocket >= 0)
366 if (ControlSocket > highest)
367 highest = ControlSocket;
368 SM_FD_SET(ControlSocket, &readfds);
375 t = select(highest + 1, FDSET_CAST &readfds,
376 NULL, NULL, &timeout);
378 /* Did someone signal while waiting? */
384 (void) runqueue(true, false, false, false);
385 #if _FFR_QUEUE_RUN_PARANOIA
387 #endif /* _FFR_QUEUE_RUN_PARANOIA */
389 #if _FFR_QUEUE_RUN_PARANOIA
390 else if (CheckQueueRunners > 0 && QueueIntvl > 0 &&
391 lastrun + QueueIntvl + CheckQueueRunners < now)
395 ** set lastrun unconditionally to avoid
396 ** calling checkqueuerunner() all the time.
397 ** That's also why we currently ignore the
398 ** result of the function call.
401 (void) checkqueuerunner();
404 #endif /* _FFR_QUEUE_RUN_PARANOIA */
415 /* look "round-robin" for an active socket */
416 if ((idx = olddaemon + 1) >= NDaemons)
418 for (i = 0; i < NDaemons; i++)
420 if (Daemons[idx].d_socket >= 0 &&
421 SM_FD_ISSET(Daemons[idx].d_socket,
424 lotherend = Daemons[idx].d_socksize;
425 memset(&RealHostAddr, '\0',
426 sizeof RealHostAddr);
427 t = accept(Daemons[idx].d_socket,
428 (struct sockaddr *)&RealHostAddr,
432 ** If remote side closes before
433 ** accept() finishes, sockaddr
434 ** might not be fully filled in.
439 # ifdef BSD4_4_SOCKADDR
440 RealHostAddr.sa.sa_len == 0 ||
441 # endif /* BSD4_4_SOCKADDR */
442 RealHostAddr.sa.sa_family != Daemons[idx].d_addr.sa.sa_family))
448 olddaemon = curdaemon = idx;
451 if (++idx >= NDaemons)
455 if (curdaemon == -1 && ControlSocket >= 0 &&
456 SM_FD_ISSET(ControlSocket, &readfds))
458 struct sockaddr_un sa_un;
460 lotherend = sizeof sa_un;
461 memset(&sa_un, '\0', sizeof sa_un);
462 t = accept(ControlSocket,
463 (struct sockaddr *)&sa_un,
467 ** If remote side closes before
468 ** accept() finishes, sockaddr
469 ** might not be fully filled in.
474 # ifdef BSD4_4_SOCKADDR
475 sa_un.sun_len == 0 ||
476 # endif /* BSD4_4_SOCKADDR */
477 sa_un.sun_family != AF_UNIX))
489 /* No daemon to service */
493 if (t >= 0 || errno != EINTR)
502 (void) sm_blocksignal(SIGALRM);
507 /* let's ignore these temporary errors */
508 if (save_errno == EINTR
510 || save_errno == EAGAIN
513 || save_errno == ECONNABORTED
514 #endif /* ECONNABORTED */
516 || save_errno == EWOULDBLOCK
517 #endif /* EWOULDBLOCK */
521 syserr("getrequests: accept");
523 /* arrange to re-open the socket next time around */
524 (void) close(Daemons[curdaemon].d_socket);
525 Daemons[curdaemon].d_socket = -1;
526 #if SO_REUSEADDR_IS_BROKEN
528 ** Give time for bound socket to be released.
529 ** This creates a denial-of-service if you can
530 ** force accept() to fail on affected systems.
533 Daemons[curdaemon].d_refuse_connections_until = curtime() + 15;
534 #endif /* SO_REUSEADDR_IS_BROKEN */
540 /* set some daemon related macros */
541 switch (Daemons[curdaemon].d_addr.sa.sa_family)
544 macdefine(&BlankEnvelope.e_macro, A_PERM,
545 macid("{daemon_family}"), "unspec");
547 #if _FFR_DAEMON_NETUNIX
550 macdefine(&BlankEnvelope.e_macro, A_PERM,
551 macid("{daemon_family}"), "local");
553 # endif /* NETUNIX */
554 #endif /* _FFR_DAEMON_NETUNIX */
557 macdefine(&BlankEnvelope.e_macro, A_PERM,
558 macid("{daemon_family}"), "inet");
563 macdefine(&BlankEnvelope.e_macro, A_PERM,
564 macid("{daemon_family}"), "inet6");
566 #endif /* NETINET6 */
569 macdefine(&BlankEnvelope.e_macro, A_PERM,
570 macid("{daemon_family}"), "iso");
575 macdefine(&BlankEnvelope.e_macro, A_PERM,
576 macid("{daemon_family}"), "ns");
581 macdefine(&BlankEnvelope.e_macro, A_PERM,
582 macid("{daemon_family}"), "x.25");
586 macdefine(&BlankEnvelope.e_macro, A_PERM,
587 macid("{daemon_name}"),
588 Daemons[curdaemon].d_name);
589 if (Daemons[curdaemon].d_mflags != NULL)
590 macdefine(&BlankEnvelope.e_macro, A_PERM,
591 macid("{daemon_flags}"),
592 Daemons[curdaemon].d_mflags);
594 macdefine(&BlankEnvelope.e_macro, A_PERM,
595 macid("{daemon_flags}"), "");
599 ** If connection rate is exceeded here, connection shall be
600 ** refused later by a new call after fork() by the
601 ** validate_connection() function. Closing the connection
602 ** at this point violates RFC 2821.
603 ** Do NOT remove this call, its side effects are needed.
606 connection_rate_check(&RealHostAddr, NULL);
609 ** Create a subprocess to process the mail.
613 sm_dprintf("getrequests: forking (fd = %d)\n", t);
616 ** Advance state of PRNG.
617 ** This is necessary because otherwise all child processes
618 ** will produce the same PRN sequence and hence the selection
619 ** of a queue directory (and other things, e.g., MX selection)
620 ** are not "really" random.
623 /* XXX get some better "random" data? */
625 RAND_seed((void *) &NextDiskSpaceCheck,
626 sizeof NextDiskSpaceCheck);
627 RAND_seed((void *) &now, sizeof now);
628 RAND_seed((void *) &seed, sizeof seed);
631 #endif /* STARTTLS */
635 ** Update MX records for FallbackMX.
636 ** Let's hope this is fast otherwise we screw up the
640 if (FallbackMX != NULL)
641 (void) getfallbackmxrr(FallbackMX);
642 #endif /* NAMED_BIND */
646 /* don't fork, handle connection in this process */
648 pipefd[0] = pipefd[1] = -1;
653 ** Create a pipe to keep the child from writing to
654 ** the socket until after the parent has closed
655 ** it. Otherwise the parent may hang if the child
656 ** has closed it first.
659 if (pipe(pipefd) < 0)
660 pipefd[0] = pipefd[1] = -1;
662 (void) sm_blocksignal(SIGCHLD);
666 syserr("daemon: cannot fork");
669 (void) close(pipefd[0]);
670 (void) close(pipefd[1]);
672 (void) sm_releasesignal(SIGCHLD);
682 SM_FILE_T *inchannel, *outchannel = NULL;
685 ** CHILD -- return to caller.
686 ** Collect verified idea of sending host.
687 ** Verify calling user id if possible here.
690 /* Reset global flags */
691 RestartRequest = NULL;
692 RestartWorkGroup = false;
693 ShutdownRequest = NULL;
695 CurrentPid = getpid();
696 close_sendmail_pid();
698 (void) sm_releasesignal(SIGALRM);
699 (void) sm_releasesignal(SIGCHLD);
700 (void) sm_signal(SIGCHLD, SIG_DFL);
701 (void) sm_signal(SIGHUP, SIG_DFL);
702 (void) sm_signal(SIGTERM, intsig);
704 /* turn on profiling */
708 ** Initialize exception stack and default exception
709 ** handler for child process.
712 sm_exc_newthread(fatal_error);
716 macdefine(&BlankEnvelope.e_macro, A_TEMP,
717 macid("{daemon_addr}"),
718 anynet_ntoa(&Daemons[curdaemon].d_addr));
719 (void) sm_snprintf(status, sizeof status, "%d",
720 ntohs(Daemons[curdaemon].d_port));
721 macdefine(&BlankEnvelope.e_macro, A_TEMP,
722 macid("{daemon_port}"), status);
725 for (idx = 0; idx < NDaemons; idx++)
727 if (Daemons[idx].d_socket >= 0)
728 (void) close(Daemons[idx].d_socket);
729 Daemons[idx].d_socket = -1;
733 /* Avoid SMTP daemon actions if control command */
736 /* Add control socket process */
737 proc_list_add(CurrentPid,
738 "console socket child",
739 PROC_CONTROL_CHILD, 0, -1, NULL);
745 /* clean up background delivery children */
746 (void) sm_signal(SIGCHLD, reapchild);
748 /* Add parent process as first child item */
749 proc_list_add(CurrentPid, "daemon child",
750 PROC_DAEMON_CHILD, 0, -1, NULL);
752 /* don't schedule queue runs if ETRN */
754 #if _FFR_SS_PER_DAEMON
755 if (Daemons[curdaemon].d_supersafe !=
757 SuperSafe = Daemons[curdaemon].d_supersafe;
758 #endif /* _FFR_SS_PER_DAEMON */
759 #if _FFR_DM_PER_DAEMON
760 if (Daemons[curdaemon].d_dm != DM_NOTSET)
762 Daemons[curdaemon].d_dm, e);
763 #endif /* _FFR_DM_PER_DAEMON */
765 sm_setproctitle(true, e, "startup with %s",
766 anynet_ntoa(&RealHostAddr));
774 ** Wait for the parent to close the write end
775 ** of the pipe, which we will see as an EOF.
776 ** This guarantees that we won't write to the
777 ** socket until after the parent has closed
781 /* close the write end of the pipe */
782 (void) close(pipefd[1]);
784 /* we shouldn't be interrupted, but ... */
785 while (read(pipefd[0], &c, 1) < 0 &&
788 (void) close(pipefd[0]);
791 /* control socket processing */
794 control_command(t, e);
799 /* determine host name */
800 p = hostnamebyanyaddr(&RealHostAddr);
801 if (strlen(p) > MAXNAME) /* XXX - 1 ? */
803 RealHostName = newstr(p);
804 if (RealHostName[0] == '[')
806 macdefine(&BlankEnvelope.e_macro, A_PERM,
807 macid("{client_resolve}"),
808 h_errno == TRY_AGAIN ? "TEMP" : "FAIL");
812 macdefine(&BlankEnvelope.e_macro, A_PERM,
813 macid("{client_resolve}"), "OK");
815 sm_setproctitle(true, e, "startup with %s", p);
816 markstats(e, NULL, STATS_CONNECT);
818 if ((inchannel = sm_io_open(SmFtStdiofd,
824 (outchannel = sm_io_open(SmFtStdiofd,
830 syserr("cannot open SMTP server channel, fd=%d",
832 finis(false, true, EX_OK);
834 sm_io_automode(inchannel, outchannel);
836 InChannel = inchannel;
837 OutChannel = outchannel;
838 DisConnected = false;
841 if (!xla_host_ok(RealHostName))
843 message("421 4.4.5 Too many SMTP sessions for this host");
844 finis(false, true, EX_OK);
847 /* find out name for interface of connection */
848 if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
849 NULL), &sa.sa, &len) == 0)
851 p = hostnamebyanyaddr(&sa);
853 sm_dprintf("getreq: got name %s\n", p);
854 macdefine(&BlankEnvelope.e_macro, A_TEMP,
855 macid("{if_name}"), p);
858 ** Do this only if it is not the loopback
867 addr = anynet_ntoa(&sa);
868 (void) sm_snprintf(family,
870 "%d", sa.sa.sa_family);
871 macdefine(&BlankEnvelope.e_macro,
873 macid("{if_addr}"), addr);
874 macdefine(&BlankEnvelope.e_macro,
876 macid("{if_family}"), family);
878 sm_dprintf("getreq: got addr %s and family %s\n",
883 macdefine(&BlankEnvelope.e_macro,
885 macid("{if_addr}"), NULL);
886 macdefine(&BlankEnvelope.e_macro,
888 macid("{if_family}"), NULL);
894 sm_dprintf("getreq: getsockname failed\n");
895 macdefine(&BlankEnvelope.e_macro, A_PERM,
896 macid("{if_name}"), NULL);
897 macdefine(&BlankEnvelope.e_macro, A_PERM,
898 macid("{if_addr}"), NULL);
899 macdefine(&BlankEnvelope.e_macro, A_PERM,
900 macid("{if_family}"), NULL);
905 /* parent -- keep track of children */
908 (void) sm_snprintf(status, sizeof status,
909 "control socket server child");
910 proc_list_add(pid, status, PROC_CONTROL, 0, -1, NULL);
914 (void) sm_snprintf(status, sizeof status,
915 "SMTP server child for %s",
916 anynet_ntoa(&RealHostAddr));
917 proc_list_add(pid, status, PROC_DAEMON, 0, -1,
920 (void) sm_releasesignal(SIGCHLD);
922 /* close the read end of the synchronization pipe */
925 (void) close(pipefd[0]);
929 /* close the port so that others will hang (for a while) */
932 /* release the child by closing the read end of the sync pipe */
935 (void) close(pipefd[1]);
940 sm_dprintf("getreq: returning\n");
943 /* set the filters for this daemon */
944 if (Daemons[curdaemon].d_inputfilterlist != NULL)
948 Daemons[curdaemon].d_inputfilters[i] != NULL);
951 InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
954 InputFilters[i] = NULL;
957 return &Daemons[curdaemon].d_flags;
961 ** GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
970 ** Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
974 getrequests_checkdiskspace(e)
982 if (now < NextDiskSpaceCheck)
985 /* Check if there is available disk space in all queue groups. */
986 if (!enoughdiskspace(0, NULL))
988 for (idx = 0; idx < NDaemons; ++idx)
990 if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
993 /* log only if not logged before */
997 sm_syslog(LOG_INFO, NOQID,
998 "rejecting new messages: min free: %ld",
1000 sm_setproctitle(true, e,
1001 "rejecting new messages: min free: %ld",
1005 setbitn(D_ETRNONLY, Daemons[idx].d_flags);
1010 for (idx = 0; idx < NDaemons; ++idx)
1012 if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
1015 /* log only if not logged before */
1019 sm_syslog(LOG_INFO, NOQID,
1020 "accepting new messages (again)");
1024 /* title will be set later */
1025 clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
1029 /* only check disk space once a minute */
1030 NextDiskSpaceCheck = now + 60;
1034 ** OPENDAEMONSOCKET -- open SMTP socket
1036 ** Deals with setting all appropriate options.
1039 ** d -- the structure for the daemon to open.
1040 ** firsttime -- set if this is the initial open.
1043 ** Size in bytes of the daemon socket addr.
1046 ** Leaves DaemonSocket set to the open socket.
1047 ** Exits if the socket cannot be created.
1050 #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
1053 opendaemonsocket(d, firsttime)
1059 SOCKADDR_LEN_T socksize = 0;
1064 sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
1070 if (firsttime || d->d_socket < 0)
1072 #if _FFR_DAEMON_NETUNIX
1074 if (d->d_addr.sa.sa_family == AF_UNIX)
1077 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
1079 /* if not safe, don't use it */
1080 rval = safefile(d->d_addr.sunix.sun_path,
1083 S_IRUSR|S_IWUSR, NULL);
1087 syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
1089 d->d_addr.sunix.sun_path);
1093 /* Don't try to overtake an existing socket */
1094 (void) unlink(d->d_addr.sunix.sun_path);
1096 # endif /* NETUNIX */
1097 #endif /* _FFR_DOMAIN_NETUNIX */
1098 d->d_socket = socket(d->d_addr.sa.sa_family,
1100 if (d->d_socket < 0)
1103 syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
1106 if (bitnset(D_OPTIONAL, d->d_flags) &&
1107 (!transienterror(save_errno) ||
1108 ntries >= MAXOPENTRIES - 1))
1110 syserr("opendaemonsocket: daemon %s: optional socket disabled",
1112 setbitn(D_DISABLE, d->d_flags);
1118 sm_syslog(LOG_ALERT, NOQID,
1119 "daemon %s: problem creating SMTP socket",
1125 if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
1127 save_errno = EINVAL;
1128 syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
1129 d->d_name, d->d_socket);
1133 /* turn on network debugging? */
1135 (void) setsockopt(d->d_socket, SOL_SOCKET,
1136 SO_DEBUG, (char *)&on,
1139 (void) setsockopt(d->d_socket, SOL_SOCKET,
1140 SO_REUSEADDR, (char *)&on, sizeof on);
1141 (void) setsockopt(d->d_socket, SOL_SOCKET,
1142 SO_KEEPALIVE, (char *)&on, sizeof on);
1145 if (d->d_tcprcvbufsize > 0)
1147 if (setsockopt(d->d_socket, SOL_SOCKET,
1149 (char *) &d->d_tcprcvbufsize,
1150 sizeof(d->d_tcprcvbufsize)) < 0)
1151 syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
1153 #endif /* SO_RCVBUF */
1155 if (d->d_tcpsndbufsize > 0)
1157 if (setsockopt(d->d_socket, SOL_SOCKET,
1159 (char *) &d->d_tcpsndbufsize,
1160 sizeof(d->d_tcpsndbufsize)) < 0)
1161 syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
1163 #endif /* SO_SNDBUF */
1165 if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
1166 fcntl(d->d_socket, F_SETFD,
1167 fdflags | FD_CLOEXEC) == -1)
1170 syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
1172 fdflags == -1 ? "get" : "set",
1173 sm_errstring(save_errno));
1174 (void) close(d->d_socket);
1178 switch (d->d_addr.sa.sa_family)
1180 #if _FFR_DAEMON_NETUNIX
1183 socksize = sizeof d->d_addr.sunix;
1185 # endif /* NETUNIX */
1186 #endif /* _FFR_DAEMON_NETUNIX */
1189 socksize = sizeof d->d_addr.sin;
1191 #endif /* NETINET */
1195 socksize = sizeof d->d_addr.sin6;
1197 #endif /* NETINET6 */
1201 socksize = sizeof d->d_addr.siso;
1206 socksize = sizeof d->d_addr;
1210 if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
1212 /* probably another daemon already */
1214 syserr("opendaemonsocket: daemon %s: cannot bind",
1216 (void) close(d->d_socket);
1221 listen(d->d_socket, d->d_listenqueue) < 0)
1224 syserr("opendaemonsocket: daemon %s: cannot listen",
1226 (void) close(d->d_socket);
1230 } while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
1231 syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
1234 return -1; /* avoid compiler warning on IRIX */
1237 ** SETUPDAEMON -- setup socket for daemon
1240 ** daemonaddr -- socket for daemon
1243 ** port number on which daemon should run
1247 static unsigned short
1248 setupdaemon(daemonaddr)
1249 SOCKADDR *daemonaddr;
1251 unsigned short port;
1254 ** Set up the address for the mailer.
1257 if (daemonaddr->sa.sa_family == AF_UNSPEC)
1259 memset(daemonaddr, '\0', sizeof *daemonaddr);
1261 daemonaddr->sa.sa_family = AF_INET;
1262 #endif /* NETINET */
1265 switch (daemonaddr->sa.sa_family)
1269 if (daemonaddr->sin.sin_addr.s_addr == 0)
1270 daemonaddr->sin.sin_addr.s_addr = INADDR_ANY;
1271 port = daemonaddr->sin.sin_port;
1273 #endif /* NETINET */
1277 if (IN6_IS_ADDR_UNSPECIFIED(&daemonaddr->sin6.sin6_addr))
1278 daemonaddr->sin6.sin6_addr = in6addr_any;
1279 port = daemonaddr->sin6.sin6_port;
1281 #endif /* NETINET6 */
1284 /* unknown protocol */
1290 #ifdef NO_GETSERVBYNAME
1292 #else /* NO_GETSERVBYNAME */
1294 register struct servent *sp;
1296 sp = getservbyname("smtp", "tcp");
1299 syserr("554 5.3.5 service \"smtp\" unknown");
1305 #endif /* NO_GETSERVBYNAME */
1308 switch (daemonaddr->sa.sa_family)
1312 daemonaddr->sin.sin_port = port;
1314 #endif /* NETINET */
1318 daemonaddr->sin6.sin6_port = port;
1320 #endif /* NETINET6 */
1323 /* unknown protocol */
1329 ** CLRDAEMON -- reset the daemon connection
1338 ** releases any resources used by the passive daemon.
1346 for (i = 0; i < NDaemons; i++)
1348 if (Daemons[i].d_socket >= 0)
1349 (void) close(Daemons[i].d_socket);
1350 Daemons[i].d_socket = -1;
1355 ** GETMODIFIERS -- get modifier flags
1358 ** v -- the modifiers (input text line).
1359 ** modifiers -- pointer to flag field to represent modifiers.
1362 ** (xallocat()ed) string representation of modifiers.
1365 ** fills in modifiers.
1369 getmodifiers(v, modifiers)
1371 BITMAP256 modifiers;
1374 char *h, *f, *flags;
1376 /* maximum length of flags: upper case Option -> "OO " */
1377 l = 3 * strlen(v) + 3;
1379 /* is someone joking? */
1380 if (l < 0 || l > 256)
1383 sm_syslog(LOG_ERR, NOQID,
1384 "getmodifiers too long, ignored");
1389 clrbitmap(modifiers);
1390 for (h = v; *h != '\0'; h++)
1392 if (isascii(*h) && !isspace(*h) && isprint(*h))
1394 setbitn(*h, modifiers);
1407 ** CHKDAEMONMODIFIERS -- check whether all daemons have set a flag.
1410 ** flag -- the flag to test.
1413 ** true iff all daemons have set flag.
1417 chkdaemonmodifiers(flag)
1422 for (i = 0; i < NDaemons; i++)
1423 if (!bitnset((char) flag, Daemons[i].d_flags))
1429 ** SETSOCKADDROPTIONS -- set options for SOCKADDR (daemon or client)
1432 ** p -- the options line.
1433 ** d -- the daemon structure to fill in.
1440 setsockaddroptions(p, d)
1451 if (d->d_addr.sa.sa_family == AF_UNSPEC)
1452 d->d_addr.sa.sa_family = AF_INET;
1453 #endif /* NETINET */
1454 #if _FFR_SS_PER_DAEMON
1455 d->d_supersafe = SAFE_NOTSET;
1456 #endif /* _FFR_SS_PER_DAEMON */
1457 #if _FFR_DM_PER_DAEMON
1458 d->d_dm = DM_NOTSET;
1459 #endif /* _FFR_DM_PER_DAEMON */
1466 while (isascii(*p) && isspace(*p))
1477 while (isascii(*++v) && isspace(*v))
1479 if (isascii(*f) && islower(*f))
1484 case 'A': /* address */
1488 #if _FFR_DM_PER_DAEMON
1489 case 'D': /* DeliveryMode */
1499 syserr("554 5.3.5 Unknown delivery mode %c",
1504 #endif /* _FFR_DM_PER_DAEMON */
1506 case 'F': /* address family */
1507 if (isascii(*v) && isdigit(*v))
1508 d->d_addr.sa.sa_family = atoi(v);
1509 #if _FFR_DAEMON_NETUNIX
1511 else if (sm_strcasecmp(v, "unix") == 0 ||
1512 sm_strcasecmp(v, "local") == 0)
1513 d->d_addr.sa.sa_family = AF_UNIX;
1514 # endif /* NETUNIX */
1515 #endif /* _FFR_DAEMON_NETUNIX */
1517 else if (sm_strcasecmp(v, "inet") == 0)
1518 d->d_addr.sa.sa_family = AF_INET;
1519 #endif /* NETINET */
1521 else if (sm_strcasecmp(v, "inet6") == 0)
1522 d->d_addr.sa.sa_family = AF_INET6;
1523 #endif /* NETINET6 */
1525 else if (sm_strcasecmp(v, "iso") == 0)
1526 d->d_addr.sa.sa_family = AF_ISO;
1529 else if (sm_strcasecmp(v, "ns") == 0)
1530 d->d_addr.sa.sa_family = AF_NS;
1533 else if (sm_strcasecmp(v, "x.25") == 0)
1534 d->d_addr.sa.sa_family = AF_CCITT;
1537 syserr("554 5.3.5 Unknown address family %s in Family=option",
1543 d->d_inputfilterlist = v;
1547 case 'L': /* listen queue size */
1548 d->d_listenqueue = atoi(v);
1551 case 'M': /* modifiers (flags) */
1552 d->d_mflags = getmodifiers(v, d->d_flags);
1555 case 'N': /* name */
1559 case 'P': /* port */
1563 case 'R': /* receive buffer size */
1564 d->d_tcprcvbufsize = atoi(v);
1567 case 'S': /* send buffer size */
1568 d->d_tcpsndbufsize = atoi(v);
1571 #if _FFR_SS_PER_DAEMON
1572 case 'T': /* SuperSafe */
1573 if (tolower(*v) == 'i')
1574 d->d_supersafe = SAFE_INTERACTIVE;
1575 else if (tolower(*v) == 'p')
1577 d->d_supersafe = SAFE_REALLY_POSTMILTER;
1579 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
1580 "Warning: SuperSafe=PostMilter requires Milter support (-DMILTER)\n");
1581 # endif /* MILTER */
1583 d->d_supersafe = atobool(v) ? SAFE_REALLY
1586 #endif /* _FFR_SS_PER_DAEMON */
1589 syserr("554 5.3.5 PortOptions parameter \"%s\" unknown",
1594 /* Check addr and port after finding family */
1597 switch (d->d_addr.sa.sa_family)
1599 #if _FFR_DAEMON_NETUNIX
1602 if (strlen(addr) >= sizeof(d->d_addr.sunix.sun_path))
1604 errno = ENAMETOOLONG;
1605 syserr("setsockaddroptions: domain socket name too long: %s > %d",
1606 addr, sizeof(d->d_addr.sunix.sun_path));
1610 /* file safety check done in opendaemonsocket() */
1611 (void) memset(&d->d_addr.sunix.sun_path, '\0',
1612 sizeof(d->d_addr.sunix.sun_path));
1613 (void) sm_strlcpy((char *)&d->d_addr.sunix.sun_path,
1615 sizeof(d->d_addr.sunix.sun_path));
1617 # endif /* NETUNIX */
1618 #endif /* _FFR_DAEMON_NETUNIX */
1621 if (!isascii(*addr) || !isdigit(*addr) ||
1622 ((d->d_addr.sin.sin_addr.s_addr = inet_addr(addr))
1625 register struct hostent *hp;
1627 hp = sm_gethostbyname(addr, AF_INET);
1629 syserr("554 5.3.0 host \"%s\" unknown",
1633 while (*(hp->h_addr_list) != NULL &&
1634 hp->h_addrtype != AF_INET)
1636 if (*(hp->h_addr_list) == NULL)
1637 syserr("554 5.3.0 host \"%s\" unknown",
1640 memmove(&d->d_addr.sin.sin_addr,
1646 # endif /* NETINET6 */
1650 #endif /* NETINET */
1654 if (anynet_pton(AF_INET6, addr,
1655 &d->d_addr.sin6.sin6_addr) != 1)
1657 register struct hostent *hp;
1659 hp = sm_gethostbyname(addr, AF_INET6);
1661 syserr("554 5.3.0 host \"%s\" unknown",
1665 while (*(hp->h_addr_list) != NULL &&
1666 hp->h_addrtype != AF_INET6)
1668 if (*(hp->h_addr_list) == NULL)
1669 syserr("554 5.3.0 host \"%s\" unknown",
1672 memmove(&d->d_addr.sin6.sin6_addr,
1680 #endif /* NETINET6 */
1683 syserr("554 5.3.5 address= option unsupported for family %d",
1684 d->d_addr.sa.sa_family);
1691 switch (d->d_addr.sa.sa_family)
1695 if (isascii(*port) && isdigit(*port))
1696 d->d_addr.sin.sin_port = htons((unsigned short)
1697 atoi((const char *) port));
1700 # ifdef NO_GETSERVBYNAME
1701 syserr("554 5.3.5 invalid port number: %s",
1703 # else /* NO_GETSERVBYNAME */
1704 register struct servent *sp;
1706 sp = getservbyname(port, "tcp");
1708 syserr("554 5.3.5 service \"%s\" unknown",
1711 d->d_addr.sin.sin_port = sp->s_port;
1712 # endif /* NO_GETSERVBYNAME */
1715 #endif /* NETINET */
1719 if (isascii(*port) && isdigit(*port))
1720 d->d_addr.sin6.sin6_port = htons((unsigned short)
1724 # ifdef NO_GETSERVBYNAME
1725 syserr("554 5.3.5 invalid port number: %s",
1727 # else /* NO_GETSERVBYNAME */
1728 register struct servent *sp;
1730 sp = getservbyname(port, "tcp");
1732 syserr("554 5.3.5 service \"%s\" unknown",
1735 d->d_addr.sin6.sin6_port = sp->s_port;
1736 # endif /* NO_GETSERVBYNAME */
1739 #endif /* NETINET6 */
1743 /* assume two byte transport selector */
1744 if (isascii(*port) && isdigit(*port))
1745 portno = htons((unsigned short) atoi(port));
1748 # ifdef NO_GETSERVBYNAME
1749 syserr("554 5.3.5 invalid port number: %s",
1751 # else /* NO_GETSERVBYNAME */
1752 register struct servent *sp;
1754 sp = getservbyname(port, "tcp");
1756 syserr("554 5.3.5 service \"%s\" unknown",
1759 portno = sp->s_port;
1760 # endif /* NO_GETSERVBYNAME */
1762 memmove(TSEL(&d->d_addr.siso),
1763 (char *) &portno, 2);
1768 syserr("554 5.3.5 Port= option unsupported for family %d",
1769 d->d_addr.sa.sa_family);
1775 ** SETDAEMONOPTIONS -- set options for running the MTA daemon
1778 ** p -- the options line.
1781 ** true if successful, false otherwise.
1784 ** increments number of daemons.
1787 #define DEF_LISTENQUEUE 10
1795 static struct dflags DaemonFlags[] =
1797 { "AUTHREQ", D_AUTHREQ },
1798 { "BINDIF", D_BINDIF },
1799 { "CANONREQ", D_CANONREQ },
1800 { "IFNHELO", D_IFNHELO },
1801 { "FQMAIL", D_FQMAIL },
1802 { "FQRCPT", D_FQRCPT },
1803 { "SMTPS", D_SMTPS },
1804 { "UNQUALOK", D_UNQUALOK },
1805 { "NOAUTH", D_NOAUTH },
1806 { "NOCANON", D_NOCANON },
1807 { "NOETRN", D_NOETRN },
1808 { "NOTLS", D_NOTLS },
1809 { "ETRNONLY", D_ETRNONLY },
1810 { "OPTIONAL", D_OPTIONAL },
1811 { "DISABLE", D_DISABLE },
1812 { "ISSET", D_ISSET },
1820 register struct dflags *df;
1823 for (df = DaemonFlags; df->d_name != NULL; df++)
1825 if (!bitnset(df->d_flag, d->d_flags))
1828 sm_dprintf("<%s", df->d_name);
1830 sm_dprintf(",%s", df->d_name);
1841 if (NDaemons >= MAXDAEMONS)
1843 Daemons[NDaemons].d_socket = -1;
1844 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1845 clrbitmap(Daemons[NDaemons].d_flags);
1846 setsockaddroptions(p, &Daemons[NDaemons]);
1849 if (Daemons[NDaemons].d_inputfilterlist != NULL)
1850 Daemons[NDaemons].d_inputfilterlist = newstr(Daemons[NDaemons].d_inputfilterlist);
1853 if (Daemons[NDaemons].d_name != NULL)
1854 Daemons[NDaemons].d_name = newstr(Daemons[NDaemons].d_name);
1859 (void) sm_snprintf(num, sizeof num, "Daemon%d", NDaemons);
1860 Daemons[NDaemons].d_name = newstr(num);
1865 sm_dprintf("Daemon %s flags: ", Daemons[NDaemons].d_name);
1866 printdaemonflags(&Daemons[NDaemons]);
1873 ** INITDAEMON -- initialize daemon if not yet done.
1882 ** initializes structure for one daemon.
1890 Daemons[NDaemons].d_socket = -1;
1891 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1892 Daemons[NDaemons].d_name = "Daemon0";
1897 ** SETCLIENTOPTIONS -- set options for running the client
1900 ** p -- the options line.
1906 static DAEMON_T ClientSettings[AF_MAX + 1];
1915 memset(&d, '\0', sizeof d);
1916 setsockaddroptions(p, &d);
1918 /* grab what we need */
1919 family = d.d_addr.sa.sa_family;
1920 STRUCTCOPY(d, ClientSettings[family]);
1921 setbitn(D_ISSET, ClientSettings[family].d_flags); /* mark as set */
1922 if (d.d_name != NULL)
1923 ClientSettings[family].d_name = newstr(d.d_name);
1928 (void) sm_snprintf(num, sizeof num, "Client%d", family);
1929 ClientSettings[family].d_name = newstr(num);
1933 ** ADDR_FAMILY -- determine address family from address
1936 ** addr -- the string representation of the address
1939 ** AF_INET, AF_INET6 or AF_UNSPEC
1951 #endif /* NETINET6 */
1954 if (inet_addr(addr) != INADDR_NONE)
1957 sm_dprintf("addr_family(%s): INET\n", addr);
1960 #endif /* NETINET */
1962 if (anynet_pton(AF_INET6, addr, &clt_addr.sin6.sin6_addr) == 1)
1965 sm_dprintf("addr_family(%s): INET6\n", addr);
1968 #endif /* NETINET6 */
1969 #if _FFR_DAEMON_NETUNIX
1974 sm_dprintf("addr_family(%s): LOCAL\n", addr);
1977 # endif /* NETUNIX */
1978 #endif /* _FFR_DAEMON_NETUNIX */
1980 sm_dprintf("addr_family(%s): UNSPEC\n", addr);
1985 ** CHKCLIENTMODIFIERS -- check whether all clients have set a flag.
1988 ** flag -- the flag to test.
1991 ** true iff all configured clients have set the flag.
1995 chkclientmodifiers(flag)
2002 for (i = 0; i < AF_MAX; i++)
2004 if (bitnset(D_ISSET, ClientSettings[i].d_flags))
2006 if (!bitnset((char) flag, ClientSettings[i].d_flags))
2016 ** SETUP_DAEMON_FILTERS -- Parse per-socket filters
2026 setup_daemon_milters()
2030 if (OpMode == MD_SMTP)
2032 /* no need to configure the daemons */
2036 for (idx = 0; idx < NDaemons; idx++)
2038 if (Daemons[idx].d_inputfilterlist != NULL)
2040 milter_config(Daemons[idx].d_inputfilterlist,
2041 Daemons[idx].d_inputfilters,
2048 ** MAKECONNECTION -- make a connection to an SMTP socket on a machine.
2051 ** host -- the name of the host.
2052 ** port -- the port number to connect to.
2053 ** mci -- a pointer to the mail connection information
2054 ** structure to be filled in.
2055 ** e -- the current envelope.
2056 ** enough -- time at which to stop further connection attempts.
2057 ** (0 means no limit)
2060 ** An exit code telling whether the connection could be
2061 ** made and if not why not.
2067 static jmp_buf CtxConnectTimeout;
2069 SOCKADDR CurHostAddr; /* address of current host */
2072 makeconnection(host, port, mci, e, enough)
2074 volatile unsigned int port;
2079 register volatile int addrno = 0;
2081 register struct hostent *volatile hp = (struct hostent *) NULL;
2085 volatile SOCKADDR_LEN_T addrlen;
2086 volatile bool firstconnect;
2087 SM_EVENT *volatile ev = NULL;
2089 volatile bool v6found = false;
2090 #endif /* NETINET6 */
2091 volatile int family = InetMode;
2093 volatile SOCKADDR_LEN_T socksize = 0;
2094 volatile bool clt_bind;
2097 extern ENVELOPE BlankEnvelope;
2099 /* retranslate {daemon_flags} into bitmap */
2101 if ((p = macvalue(macid("{daemon_flags}"), e)) != NULL)
2103 for (; *p != '\0'; p++)
2105 if (!(isascii(*p) && isspace(*p)))
2106 setbitn(bitidx(*p), d_flags);
2112 #endif /* NETINET6 */
2115 /* Set up the address for outgoing connection. */
2116 if (bitnset(D_BINDIF, d_flags) &&
2117 (p = macvalue(macid("{if_addr}"), e)) != NULL &&
2121 char p6[INET6_ADDRSTRLEN];
2122 #endif /* NETINET6 */
2124 memset(&clt_addr, '\0', sizeof clt_addr);
2126 /* infer the address family from the address itself */
2127 clt_addr.sa.sa_family = addr_family(p);
2128 switch (clt_addr.sa.sa_family)
2132 clt_addr.sin.sin_addr.s_addr = inet_addr(p);
2133 if (clt_addr.sin.sin_addr.s_addr != INADDR_NONE &&
2134 clt_addr.sin.sin_addr.s_addr != INADDR_LOOPBACK)
2137 socksize = sizeof (struct sockaddr_in);
2140 #endif /* NETINET */
2144 if (inet_addr(p) != INADDR_NONE)
2145 (void) sm_snprintf(p6, sizeof p6,
2146 "IPv6:::ffff:%s", p);
2148 (void) sm_strlcpy(p6, p, sizeof p6);
2149 if (anynet_pton(AF_INET6, p6,
2150 &clt_addr.sin6.sin6_addr) == 1 &&
2151 !IN6_IS_ADDR_LOOPBACK(&clt_addr.sin6.sin6_addr))
2154 socksize = sizeof (struct sockaddr_in6);
2157 #endif /* NETINET6 */
2161 syserr("554 5.3.5 Address= option unsupported for family %d",
2162 clt_addr.sa.sa_family);
2167 family = clt_addr.sa.sa_family;
2170 /* D_BINDIF not set or not available, fallback to ClientPortOptions */
2173 STRUCTCOPY(ClientSettings[family].d_addr, clt_addr);
2174 switch (clt_addr.sa.sa_family)
2178 if (clt_addr.sin.sin_addr.s_addr == 0)
2179 clt_addr.sin.sin_addr.s_addr = INADDR_ANY;
2182 if (clt_addr.sin.sin_port != 0)
2184 socksize = sizeof (struct sockaddr_in);
2186 #endif /* NETINET */
2189 if (IN6_IS_ADDR_UNSPECIFIED(&clt_addr.sin6.sin6_addr))
2190 clt_addr.sin6.sin6_addr = in6addr_any;
2193 socksize = sizeof (struct sockaddr_in6);
2194 if (clt_addr.sin6.sin6_port != 0)
2197 #endif /* NETINET6 */
2200 socksize = sizeof clt_addr.siso;
2210 ** Set up the address for the mailer.
2211 ** Accept "[a.b.c.d]" syntax for host name.
2216 memset(&CurHostAddr, '\0', sizeof CurHostAddr);
2217 memset(&addr, '\0', sizeof addr);
2218 SmtpPhase = mci->mci_phase = "initial connection";
2223 p = strchr(host, ']');
2227 unsigned long hid = INADDR_NONE;
2228 #endif /* NETINET */
2230 struct sockaddr_in6 hid6;
2231 #endif /* NETINET6 */
2235 memset(&hid6, '\0', sizeof hid6);
2236 #endif /* NETINET6 */
2238 if (family == AF_INET &&
2239 (hid = inet_addr(&host[1])) != INADDR_NONE)
2241 addr.sin.sin_family = AF_INET;
2242 addr.sin.sin_addr.s_addr = hid;
2245 #endif /* NETINET */
2247 if (family == AF_INET6 &&
2248 anynet_pton(AF_INET6, &host[1],
2249 &hid6.sin6_addr) == 1)
2251 addr.sin6.sin6_family = AF_INET6;
2252 addr.sin6.sin6_addr = hid6.sin6_addr;
2255 #endif /* NETINET6 */
2257 /* try it as a host name (avoid MX lookup) */
2258 hp = sm_gethostbyname(&host[1], family);
2259 if (hp == NULL && p[-1] == '.')
2262 int oldopts = _res.options;
2264 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2265 #endif /* NAMED_BIND */
2267 hp = sm_gethostbyname(&host[1],
2271 _res.options = oldopts;
2272 #endif /* NAMED_BIND */
2281 extern char MsgBuf[];
2284 "553 Invalid numeric domain spec \"%s\"",
2286 mci_setstat(mci, EX_NOHOST, "5.1.2", MsgBuf);
2293 /* contortion to get around SGI cc complaints */
2295 p = &host[strlen(host) - 1];
2296 hp = sm_gethostbyname(host, family);
2297 if (hp == NULL && *p == '.')
2300 int oldopts = _res.options;
2302 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2303 #endif /* NAMED_BIND */
2305 hp = sm_gethostbyname(host, family);
2308 _res.options = oldopts;
2309 #endif /* NAMED_BIND */
2316 /* check for name server timeouts */
2318 if (WorkAroundBrokenAAAA && family == AF_INET6 &&
2322 ** An attempt with family AF_INET may
2323 ** succeed By skipping the next section
2324 ** of code, we will try AF_INET before
2329 sm_dprintf("makeconnection: WorkAroundBrokenAAAA: Trying AF_INET lookup (AF_INET6 failed)\n");
2332 # endif /* NETINET6 */
2334 if (errno == ETIMEDOUT ||
2335 h_errno == TRY_AGAIN ||
2336 (errno == ECONNREFUSED && UseNameServer))
2339 mci_setstat(mci, EX_TEMPFAIL,
2345 #endif /* NAMED_BIND */
2348 ** Try v6 first, then fall back to v4.
2349 ** If we found a v6 address, but no v4
2350 ** addresses, then TEMPFAIL.
2353 if (family == AF_INET6)
2360 #endif /* NETINET6 */
2362 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2366 addr.sa.sa_family = hp->h_addrtype;
2367 switch (hp->h_addrtype)
2371 memmove(&addr.sin.sin_addr,
2375 #endif /* NETINET */
2379 memmove(&addr.sin6.sin6_addr,
2383 #endif /* NETINET6 */
2386 if (hp->h_length > sizeof addr.sa.sa_data)
2388 syserr("makeconnection: long sa_data: family %d len %d",
2389 hp->h_addrtype, hp->h_length);
2390 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2394 memmove(addr.sa.sa_data, hp->h_addr, hp->h_length);
2401 ** Determine the port number.
2406 #ifdef NO_GETSERVBYNAME
2408 #else /* NO_GETSERVBYNAME */
2409 register struct servent *sp = getservbyname("smtp", "tcp");
2414 sm_syslog(LOG_ERR, NOQID,
2415 "makeconnection: service \"smtp\" unknown");
2420 #endif /* NO_GETSERVBYNAME */
2424 if (addr.sa.sa_family == AF_INET6 &&
2425 IN6_IS_ADDR_V4MAPPED(&addr.sin6.sin6_addr) &&
2426 ClientSettings[AF_INET].d_addr.sa.sa_family != 0)
2429 ** Ignore mapped IPv4 address since
2430 ** there is a ClientPortOptions setting
2436 #endif /* NETINET6 */
2438 switch (addr.sa.sa_family)
2442 addr.sin.sin_port = port;
2443 addrlen = sizeof (struct sockaddr_in);
2445 #endif /* NETINET */
2449 addr.sin6.sin6_port = port;
2450 addrlen = sizeof (struct sockaddr_in6);
2452 #endif /* NETINET6 */
2456 /* assume two byte transport selector */
2457 memmove(TSEL((struct sockaddr_iso *) &addr), (char *) &port, 2);
2458 addrlen = sizeof (struct sockaddr_iso);
2463 syserr("Can't connect to address family %d", addr.sa.sa_family);
2464 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2469 #endif /* NETINET6 */
2474 ** Try to actually open the connection.
2478 /* if too many connections, don't bother trying */
2479 if (!xla_noqueue_ok(host))
2484 # endif /* NETINET6 */
2489 firstconnect = true;
2493 sm_dprintf("makeconnection (%s [%s].%d (%d))\n",
2494 host, anynet_ntoa(&addr), ntohs(port),
2495 (int) addr.sa.sa_family);
2497 /* save for logging */
2501 if (bitnset(M_SECURE_PORT, mci->mci_mailer->m_flags))
2503 int rport = IPPORT_RESERVED - 1;
2505 s = rresvport(&rport);
2508 #endif /* HASRRESVPORT */
2510 s = socket(addr.sa.sa_family, SOCK_STREAM, 0);
2515 syserr("makeconnection: cannot create socket");
2519 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2523 #endif /* NETINET6 */
2529 if (ClientSettings[family].d_tcpsndbufsize > 0)
2531 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
2532 (char *) &ClientSettings[family].d_tcpsndbufsize,
2533 sizeof(ClientSettings[family].d_tcpsndbufsize)) < 0)
2534 syserr("makeconnection: setsockopt(SO_SNDBUF)");
2536 #endif /* SO_SNDBUF */
2538 if (ClientSettings[family].d_tcprcvbufsize > 0)
2540 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
2541 (char *) &ClientSettings[family].d_tcprcvbufsize,
2542 sizeof(ClientSettings[family].d_tcprcvbufsize)) < 0)
2543 syserr("makeconnection: setsockopt(SO_RCVBUF)");
2545 #endif /* SO_RCVBUF */
2548 sm_dprintf("makeconnection: fd=%d\n", s);
2550 /* turn on network debugging? */
2555 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
2556 (char *)&on, sizeof on);
2558 if (e->e_xfp != NULL) /* for debugging */
2559 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
2560 errno = 0; /* for debugging */
2566 switch (clt_addr.sa.sa_family)
2570 if (clt_addr.sin.sin_port != 0)
2571 (void) setsockopt(s, SOL_SOCKET,
2576 #endif /* NETINET */
2580 if (clt_addr.sin6.sin6_port != 0)
2581 (void) setsockopt(s, SOL_SOCKET,
2586 #endif /* NETINET6 */
2589 if (bind(s, &clt_addr.sa, socksize) < 0)
2594 syserr("makeconnection: cannot bind socket [%s]",
2595 anynet_ntoa(&clt_addr));
2599 #endif /* NETINET6 */
2606 ** Linux seems to hang in connect for 90 minutes (!!!).
2607 ** Time out the connect to avoid this problem.
2610 if (setjmp(CtxConnectTimeout) == 0)
2614 if (e->e_ntries <= 0 && TimeOuts.to_iconnect != 0)
2615 ev = sm_setevent(TimeOuts.to_iconnect,
2617 else if (TimeOuts.to_connect != 0)
2618 ev = sm_setevent(TimeOuts.to_connect,
2623 switch (ConnectOnlyTo.sa.sa_family)
2627 addr.sin.sin_addr.s_addr = ConnectOnlyTo.sin.sin_addr.s_addr;
2629 #endif /* NETINET */
2633 memmove(&addr.sin6.sin6_addr,
2634 &ConnectOnlyTo.sin6.sin6_addr,
2637 #endif /* NETINET6 */
2640 sm_dprintf("Connecting to [%s]...\n", anynet_ntoa(&addr));
2641 i = connect(s, (struct sockaddr *) &addr, addrlen);
2651 /* couldn't connect.... figure out why */
2654 /* if running demand-dialed connection, try again */
2655 if (DialDelay > 0 && firstconnect &&
2656 bitnset(M_DIALDELAY, mci->mci_mailer->m_flags))
2659 sm_dprintf("Connect failed (%s); trying again...\n",
2660 sm_errstring(save_errno));
2661 firstconnect = false;
2662 (void) sleep(DialDelay);
2667 sm_syslog(LOG_INFO, e->e_id,
2668 "makeconnection (%s [%s]) failed: %s",
2669 host, anynet_ntoa(&addr),
2670 sm_errstring(save_errno));
2674 #endif /* NETINET6 */
2675 if (hp != NULL && hp->h_addr_list[addrno] != NULL &&
2676 (enough == 0 || curtime() < enough))
2679 sm_dprintf("Connect failed (%s); trying new address....\n",
2680 sm_errstring(save_errno));
2681 switch (addr.sa.sa_family)
2685 memmove(&addr.sin.sin_addr,
2686 hp->h_addr_list[addrno++],
2689 #endif /* NETINET */
2693 memmove(&addr.sin6.sin6_addr,
2694 hp->h_addr_list[addrno++],
2697 #endif /* NETINET6 */
2700 memmove(addr.sa.sa_data,
2701 hp->h_addr_list[addrno++],
2710 if (family == AF_INET6)
2713 sm_dprintf("Connect failed (%s); retrying with AF_INET....\n",
2714 sm_errstring(save_errno));
2725 #endif /* NETINET6 */
2726 /* couldn't open connection */
2728 /* Don't clobber an already saved errno from v4retry */
2730 #endif /* NETINET6 */
2733 sm_dprintf("Connect failed (%s)\n",
2734 sm_errstring(save_errno));
2738 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2742 #endif /* NETINET6 */
2753 #endif /* NETINET6 */
2755 /* connection ok, put it into canonical form */
2756 mci->mci_out = NULL;
2757 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2759 SM_IO_WRONLY_B, NULL)) == NULL ||
2761 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2763 SM_IO_RDONLY_B, NULL)) == NULL)
2766 syserr("cannot open SMTP client channel, fd=%d", s);
2767 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2768 if (mci->mci_out != NULL)
2769 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2774 sm_io_automode(mci->mci_out, mci->mci_in);
2776 /* set {client_flags} */
2777 if (ClientSettings[addr.sa.sa_family].d_mflags != NULL)
2779 macdefine(&mci->mci_macro, A_PERM,
2780 macid("{client_flags}"),
2781 ClientSettings[addr.sa.sa_family].d_mflags);
2784 macdefine(&mci->mci_macro, A_PERM,
2785 macid("{client_flags}"), "");
2787 /* "add" {client_flags} to bitmap */
2788 if (bitnset(D_IFNHELO, ClientSettings[addr.sa.sa_family].d_flags))
2790 /* look for just this one flag */
2791 setbitn(D_IFNHELO, d_flags);
2794 /* find out name for Interface through which we connect */
2796 if (getsockname(s, &addr.sa, &len) == 0)
2801 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2802 macid("{if_addr_out}"), anynet_ntoa(&addr));
2803 (void) sm_snprintf(family, sizeof(family), "%d",
2805 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2806 macid("{if_family_out}"), family);
2808 name = hostnamebyanyaddr(&addr);
2809 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2810 macid("{if_name_out}"), name);
2813 /* log connection information */
2814 sm_syslog(LOG_INFO, e->e_id,
2815 "SMTP outgoing connect on %.40s", name);
2817 if (bitnset(D_IFNHELO, d_flags))
2819 if (name[0] != '[' && strchr(name, '.') != NULL)
2820 mci->mci_heloname = newstr(name);
2825 macdefine(&BlankEnvelope.e_macro, A_PERM,
2826 macid("{if_name_out}"), NULL);
2827 macdefine(&BlankEnvelope.e_macro, A_PERM,
2828 macid("{if_addr_out}"), NULL);
2829 macdefine(&BlankEnvelope.e_macro, A_PERM,
2830 macid("{if_family_out}"), NULL);
2834 /* Use the configured HeloName as appropriate */
2835 if (HeloName != NULL && HeloName[0] != '\0')
2836 mci->mci_heloname = newstr(HeloName);
2837 #endif /* _FFR_HELONAME */
2839 mci_setstat(mci, EX_OK, NULL, NULL);
2844 connecttimeout(ignore)
2848 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2849 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2854 longjmp(CtxConnectTimeout, 1);
2857 ** MAKECONNECTION_DS -- make a connection to a domain socket.
2860 ** mux_path -- the path of the socket to connect to.
2861 ** mci -- a pointer to the mail connection information
2862 ** structure to be filled in.
2865 ** An exit code telling whether the connection could be
2866 ** made and if not why not.
2874 makeconnection_ds(mux_path, mci)
2879 int rval, save_errno;
2880 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK;
2881 struct sockaddr_un unix_addr;
2883 /* if not safe, don't connect */
2884 rval = safefile(mux_path, RunAsUid, RunAsGid, RunAsUserName,
2885 sff, S_IRUSR|S_IWUSR, NULL);
2889 syserr("makeconnection_ds: unsafe domain socket %s",
2891 mci_setstat(mci, EX_TEMPFAIL, "4.3.5", NULL);
2896 /* prepare address structure */
2897 memset(&unix_addr, '\0', sizeof unix_addr);
2898 unix_addr.sun_family = AF_UNIX;
2900 if (strlen(mux_path) >= sizeof unix_addr.sun_path)
2902 syserr("makeconnection_ds: domain socket name %s too long",
2905 /* XXX why TEMPFAIL but 5.x.y ? */
2906 mci_setstat(mci, EX_TEMPFAIL, "5.3.5", NULL);
2907 errno = ENAMETOOLONG;
2908 return EX_UNAVAILABLE;
2910 (void) sm_strlcpy(unix_addr.sun_path, mux_path,
2911 sizeof unix_addr.sun_path);
2913 /* initialize domain socket */
2914 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2918 syserr("makeconnection_ds: could not create domain socket %s",
2920 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2925 /* connect to server */
2926 if (connect(sock, (struct sockaddr *) &unix_addr,
2927 sizeof(unix_addr)) == -1)
2930 syserr("Could not connect to socket %s", mux_path);
2931 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2937 /* connection ok, put it into canonical form */
2938 mci->mci_out = NULL;
2939 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2940 (void *) &sock, SM_IO_WRONLY_B, NULL))
2942 || (sock = dup(sock)) < 0 ||
2943 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2944 (void *) &sock, SM_IO_RDONLY_B, NULL))
2948 syserr("cannot open SMTP client channel, fd=%d", sock);
2949 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2950 if (mci->mci_out != NULL)
2951 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2956 sm_io_automode(mci->mci_out, mci->mci_in);
2958 mci_setstat(mci, EX_OK, NULL, NULL);
2962 #endif /* NETUNIX */
2964 ** SHUTDOWN_DAEMON -- Performs a clean shutdown of the daemon
2973 ** closes control socket, exits.
2982 sm_allsignals(true);
2984 reason = ShutdownRequest;
2985 ShutdownRequest = NULL;
2989 sm_syslog(LOG_INFO, CurEnv->e_id, "stopping daemon, reason=%s",
2990 reason == NULL ? "implicit call" : reason);
2993 closecontrolsocket(true);
2998 for (i = 0; i < NDaemons; i++)
3000 if (Daemons[i].d_socket >= 0)
3002 (void) close(Daemons[i].d_socket);
3003 Daemons[i].d_socket = -1;
3005 #if _FFR_DAEMON_NETUNIX
3007 /* Remove named sockets */
3008 if (Daemons[i].d_addr.sa.sa_family == AF_UNIX)
3011 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_MUSTOWN|SFF_EXECOK|SFF_CREAT;
3013 /* if not safe, don't use it */
3014 rval = safefile(Daemons[i].d_addr.sunix.sun_path,
3017 S_IRUSR|S_IWUSR, NULL);
3019 unlink(Daemons[i].d_addr.sunix.sun_path) < 0)
3021 sm_syslog(LOG_WARNING, NOQID,
3022 "Could not remove daemon %s socket: %s: %s",
3024 Daemons[i].d_addr.sunix.sun_path,
3025 sm_errstring(errno));
3028 # endif /* NETUNIX */
3029 #endif /* _FFR_DAEMON_NETUNIX */
3033 finis(false, true, EX_OK);
3036 ** RESTART_DAEMON -- Performs a clean restart of the daemon
3045 ** restarts the daemon or exits if restart fails.
3048 /* Make a non-DFL/IGN signal a noop */
3049 #define SM_NOOP_SIGNAL(sig, old) \
3052 (old) = sm_signal((sig), sm_signal_noop); \
3053 if ((old) == SIG_IGN || (old) == SIG_DFL) \
3054 (void) sm_signal((sig), (old)); \
3063 sigfunc_t ignore, oalrm, ousr1;
3064 extern int DtableSize;
3066 /* clear the events to turn off SIGALRMs */
3068 sm_allsignals(true);
3070 reason = RestartRequest;
3071 RestartRequest = NULL;
3074 if (SaveArgv[0][0] != '/')
3077 sm_syslog(LOG_INFO, NOQID,
3078 "could not restart: need full path");
3079 finis(false, true, EX_OSFILE);
3083 sm_syslog(LOG_INFO, NOQID, "restarting %s due to %s",
3085 reason == NULL ? "implicit call" : reason);
3087 closecontrolsocket(true);
3089 cleanup_shm(DaemonPid == getpid());
3090 #endif /* SM_CONF_SHM */
3092 /* close locked pid file */
3093 close_sendmail_pid();
3096 ** Want to drop to the user who started the process in all cases
3097 ** *but* when running as "smmsp" for the clientmqueue queue run
3098 ** daemon. In that case, UseMSP will be true, RunAsUid should not
3099 ** be root, and RealUid should be either 0 or RunAsUid.
3102 drop = !(UseMSP && RunAsUid != 0 &&
3103 (RealUid == 0 || RealUid == RunAsUid));
3105 if (drop_privileges(drop) != EX_OK)
3108 sm_syslog(LOG_ALERT, NOQID,
3109 "could not drop privileges: %s",
3110 sm_errstring(errno));
3111 finis(false, true, EX_OSERR);
3115 sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
3118 ** Need to allow signals before execve() to make them "harmless".
3119 ** However, the default action can be "terminate", so it isn't
3120 ** really harmless. Setting signals to IGN will cause them to be
3121 ** ignored in the new process to, so that isn't a good alternative.
3124 SM_NOOP_SIGNAL(SIGALRM, oalrm);
3125 SM_NOOP_SIGNAL(SIGCHLD, ignore);
3126 SM_NOOP_SIGNAL(SIGHUP, ignore);
3127 SM_NOOP_SIGNAL(SIGINT, ignore);
3128 SM_NOOP_SIGNAL(SIGPIPE, ignore);
3129 SM_NOOP_SIGNAL(SIGTERM, ignore);
3131 SM_NOOP_SIGNAL(SIGUSR1, ousr1);
3132 #endif /* SIGUSR1 */
3134 /* Turn back on signals */
3135 sm_allsignals(false);
3137 (void) execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
3140 /* block signals again and restore needed signals */
3141 sm_allsignals(true);
3143 /* For finis() events */
3144 (void) sm_signal(SIGALRM, oalrm);
3147 /* For debugging finis() */
3148 (void) sm_signal(SIGUSR1, ousr1);
3149 #endif /* SIGUSR1 */
3153 sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %s",
3154 SaveArgv[0], sm_errstring(errno));
3155 finis(false, true, EX_OSFILE);
3159 ** MYHOSTNAME -- return the name of this host.
3162 ** hostbuf -- a place to return the name of this host.
3163 ** size -- the size of hostbuf.
3166 ** A list of aliases for this host.
3169 ** Adds numeric codes to $=w.
3173 myhostname(hostbuf, size)
3177 register struct hostent *hp;
3179 if (gethostname(hostbuf, size) < 0 || hostbuf[0] == '\0')
3180 (void) sm_strlcpy(hostbuf, "localhost", size);
3181 hp = sm_gethostbyname(hostbuf, InetMode);
3182 #if NETINET && NETINET6
3183 if (hp == NULL && InetMode == AF_INET6)
3186 ** It's possible that this IPv6 enabled machine doesn't
3187 ** actually have any IPv6 interfaces and, therefore, no
3188 ** IPv6 addresses. Fall back to AF_INET.
3191 hp = sm_gethostbyname(hostbuf, AF_INET);
3193 #endif /* NETINET && NETINET6 */
3196 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
3197 (void) cleanstrcpy(hostbuf, hp->h_name, size);
3200 if (strchr(hostbuf, '.') == NULL)
3204 domainname = ni_propval("/locations", NULL, "resolver",
3206 if (domainname != NULL &&
3207 strlen(domainname) + strlen(hostbuf) + 1 < size)
3208 (void) sm_strlcat2(hostbuf, ".", domainname, size);
3210 #endif /* NETINFO */
3213 ** If there is still no dot in the name, try looking for a
3217 if (strchr(hostbuf, '.') == NULL)
3221 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
3223 if (strchr(*ha, '.') != NULL)
3225 (void) cleanstrcpy(hostbuf, *ha, size - 1);
3226 hostbuf[size - 1] = '\0';
3233 ** If _still_ no dot, wait for a while and try again -- it is
3234 ** possible that some service is starting up. This can result
3235 ** in excessive delays if the system is badly configured, but
3236 ** there really isn't a way around that, particularly given that
3237 ** the config file hasn't been read at this point.
3238 ** All in all, a bit of a mess.
3241 if (strchr(hostbuf, '.') == NULL &&
3242 !getcanonname(hostbuf, size, true, NULL))
3244 sm_syslog(LOG_CRIT, NOQID,
3245 "My unqualified host name (%s) unknown; sleeping for retry",
3247 message("My unqualified host name (%s) unknown; sleeping for retry",
3250 if (!getcanonname(hostbuf, size, true, NULL))
3252 sm_syslog(LOG_ALERT, NOQID,
3253 "unable to qualify my own domain name (%s) -- using short name",
3255 message("WARNING: unable to qualify my own domain name (%s) -- using short name",
3262 ** ADDRCMP -- compare two host addresses
3265 ** hp -- hostent structure for the first address
3266 ** ha -- actual first address
3267 ** sa -- second address
3270 ** 0 -- if ha and sa match
3271 ** else -- they don't match
3282 #endif /* NETINET6 */
3284 switch (sa->sa.sa_family)
3288 if (hp->h_addrtype == AF_INET)
3289 return memcmp(ha, (char *) &sa->sin.sin_addr, INADDRSZ);
3291 #endif /* NETINET */
3295 a = (unsigned char *) &sa->sin6.sin6_addr;
3297 /* Straight binary comparison */
3298 if (hp->h_addrtype == AF_INET6)
3299 return memcmp(ha, a, IN6ADDRSZ);
3301 /* If IPv4-mapped IPv6 address, compare the IPv4 section */
3302 if (hp->h_addrtype == AF_INET &&
3303 IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr))
3304 return memcmp(a + IN6ADDRSZ - INADDRSZ, ha, INADDRSZ);
3306 #endif /* NETINET6 */
3311 ** GETAUTHINFO -- get the real host name associated with a file descriptor
3313 ** Uses RFC1413 protocol to try to get info from the other end.
3316 ** fd -- the descriptor
3317 ** may_be_forged -- an outage that is set to true if the
3318 ** forward lookup of RealHostName does not match
3319 ** RealHostAddr; set to false if they do match.
3322 ** The user@host information associated with this descriptor.
3325 static jmp_buf CtxAuthTimeout;
3332 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
3333 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
3338 longjmp(CtxAuthTimeout, 1);
3342 getauthinfo(fd, may_be_forged)
3344 bool *may_be_forged;
3346 unsigned short SM_NONVOLATILE port = 0;
3347 SOCKADDR_LEN_T falen;
3348 register char *volatile p = NULL;
3350 SOCKADDR_LEN_T lalen;
3351 #ifndef NO_GETSERVBYNAME
3352 register struct servent *sp;
3354 static unsigned short port4 = 0;
3355 # endif /* NETINET */
3357 static unsigned short port6 = 0;
3358 # endif /* NETINET6 */
3359 #endif /* ! NO_GETSERVBYNAME */
3366 char *ostype = NULL;
3368 char ibuf[MAXNAME + 1];
3369 static char hbuf[MAXNAME + MAXAUTHINFO + 11];
3371 *may_be_forged = false;
3372 falen = sizeof RealHostAddr;
3373 if (isatty(fd) || (i = getpeername(fd, &RealHostAddr.sa, &falen)) < 0 ||
3374 falen <= 0 || RealHostAddr.sa.sa_family == 0)
3379 ** ENOTSOCK is OK: bail on anything else, but reset
3380 ** errno in this case, so a mis-report doesn't
3384 if (errno != ENOTSOCK)
3388 (void) sm_strlcpyn(hbuf, sizeof hbuf, 2, RealUserName,
3391 sm_dprintf("getauthinfo: %s\n", hbuf);
3395 if (RealHostName == NULL)
3397 /* translate that to a host name */
3398 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
3399 if (strlen(RealHostName) > MAXNAME)
3400 RealHostName[MAXNAME] = '\0'; /* XXX - 1 ? */
3403 /* cross check RealHostName with forward DNS lookup */
3404 if (anynet_ntoa(&RealHostAddr)[0] != '[' &&
3405 RealHostName[0] != '[')
3409 family = RealHostAddr.sa.sa_family;
3410 #if NETINET6 && NEEDSGETIPNODE
3412 ** If RealHostAddr is an IPv6 connection with an
3413 ** IPv4-mapped address, we need RealHostName's IPv4
3414 ** address(es) for addrcmp() to compare against
3417 ** Actually, we only need to do this for systems
3418 ** which NEEDSGETIPNODE since the real getipnodebyname()
3419 ** already does V4MAPPED address via the AI_V4MAPPEDCFG
3420 ** flag. A better fix to this problem is to add this
3421 ** functionality to our stub getipnodebyname().
3424 if (family == AF_INET6 &&
3425 IN6_IS_ADDR_V4MAPPED(&RealHostAddr.sin6.sin6_addr))
3427 #endif /* NETINET6 && NEEDSGETIPNODE */
3429 /* try to match the reverse against the forward lookup */
3430 hp = sm_gethostbyname(RealHostName, family);
3433 /* XXX: Could be a temporary error on forward lookup */
3434 *may_be_forged = true;
3438 for (ha = hp->h_addr_list; *ha != NULL; ha++)
3440 if (addrcmp(hp, *ha, &RealHostAddr) == 0)
3443 *may_be_forged = *ha == NULL;
3447 #endif /* NETINET6 */
3451 if (TimeOuts.to_ident == 0)
3455 switch (RealHostAddr.sa.sa_family)
3459 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3461 la.sa.sa_family != AF_INET)
3466 port = RealHostAddr.sin.sin_port;
3468 /* create ident query */
3469 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3470 ntohs(RealHostAddr.sin.sin_port),
3471 ntohs(la.sin.sin_port));
3473 /* create local address */
3474 la.sin.sin_port = 0;
3476 /* create foreign address */
3477 # ifdef NO_GETSERVBYNAME
3478 RealHostAddr.sin.sin_port = htons(113);
3479 # else /* NO_GETSERVBYNAME */
3482 ** getservbyname() consumes about 5% of the time
3483 ** when receiving a small message (almost all of the time
3484 ** spent in this routine).
3485 ** Hence we store the port in a static variable
3486 ** to save this time.
3487 ** The portnumber shouldn't change very often...
3488 ** This code makes the assumption that the port number
3494 sp = getservbyname("auth", "tcp");
3500 RealHostAddr.sin.sin_port = port4;
3502 # endif /* NO_GETSERVBYNAME */
3503 #endif /* NETINET */
3507 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3509 la.sa.sa_family != AF_INET6)
3514 port = RealHostAddr.sin6.sin6_port;
3516 /* create ident query */
3517 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3518 ntohs(RealHostAddr.sin6.sin6_port),
3519 ntohs(la.sin6.sin6_port));
3521 /* create local address */
3522 la.sin6.sin6_port = 0;
3524 /* create foreign address */
3525 # ifdef NO_GETSERVBYNAME
3526 RealHostAddr.sin6.sin6_port = htons(113);
3527 # else /* NO_GETSERVBYNAME */
3530 sp = getservbyname("auth", "tcp");
3536 RealHostAddr.sin6.sin6_port = port6;
3538 # endif /* NO_GETSERVBYNAME */
3539 #endif /* NETINET6 */
3546 if (setjmp(CtxAuthTimeout) != 0)
3553 /* put a timeout around the whole thing */
3554 ev = sm_setevent(TimeOuts.to_ident, authtimeout, 0);
3556 /* connect to foreign IDENT server using same address as SMTP socket */
3557 s = socket(la.sa.sa_family, SOCK_STREAM, 0);
3563 if (bind(s, &la.sa, lalen) < 0 ||
3564 connect(s, &RealHostAddr.sa, lalen) < 0)
3568 sm_dprintf("getauthinfo: sent %s", ibuf);
3571 if (write(s, ibuf, strlen(ibuf)) < 0)
3576 nleft = sizeof ibuf - 1;
3577 while ((i = read(s, p, nleft)) > 0)
3584 if ((s = strchr(ibuf, '\n')) != NULL)
3598 if (i < 0 || p == &ibuf[0])
3601 if (p >= &ibuf[2] && *--p == '\n' && *--p == '\r')
3606 sm_dprintf("getauthinfo: got %s\n", ibuf);
3609 p = strchr(ibuf, ':');
3612 /* malformed response */
3615 while (isascii(*++p) && isspace(*p))
3617 if (sm_strncasecmp(p, "userid", 6) != 0)
3619 /* presumably an error string */
3623 while (isascii(*p) && isspace(*p))
3627 /* either useridxx or malformed response */
3631 /* p now points to the OSTYPE field */
3632 while (isascii(*p) && isspace(*p))
3638 /* malformed response */
3646 charset = strchr(ostype, ',');
3647 if (charset != NULL)
3651 /* 1413 says don't do this -- but it's broken otherwise */
3652 while (isascii(*++p) && isspace(*p))
3655 /* p now points to the authenticated name -- copy carefully */
3656 if (sm_strncasecmp(ostype, "other", 5) == 0 &&
3657 (ostype[5] == ' ' || ostype[5] == '\0'))
3659 (void) sm_strlcpy(hbuf, "IDENT:", sizeof hbuf);
3660 cleanstrcpy(&hbuf[6], p, MAXAUTHINFO);
3663 cleanstrcpy(hbuf, p, MAXAUTHINFO);
3665 (void) sm_strlcpyn(&hbuf[len], sizeof hbuf - len, 2, "@",
3666 RealHostName == NULL ? "localhost" : RealHostName);
3674 /* put back the original incoming port */
3675 switch (RealHostAddr.sa.sa_family)
3680 RealHostAddr.sin.sin_port = port;
3682 #endif /* NETINET */
3687 RealHostAddr.sin6.sin6_port = port;
3689 #endif /* NETINET6 */
3692 if (RealHostName == NULL)
3695 sm_dprintf("getauthinfo: NULL\n");
3698 (void) sm_strlcpy(hbuf, RealHostName, sizeof hbuf);
3702 # ifndef GET_IPOPT_DST
3703 # define GET_IPOPT_DST(dst) (dst)
3704 # endif /* ! GET_IPOPT_DST */
3706 ** Extract IP source routing information.
3708 ** Format of output for a connection from site a through b
3710 ** loose: @site-c@site-b:site-a
3711 ** strict: !@site-c@site-b:site-a
3713 ** o - pointer within ipopt_list structure.
3714 ** q - pointer within ls/ss rr route data
3715 ** p - pointer to hbuf
3718 if (RealHostAddr.sa.sa_family == AF_INET)
3720 SOCKOPT_LEN_T ipoptlen;
3725 struct IPOPTION ipopt;
3727 ipoptlen = sizeof ipopt;
3728 if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
3729 (char *) &ipopt, &ipoptlen) < 0)
3733 o = (unsigned char *) ipopt.IP_LIST;
3734 while (o != NULL && o < (unsigned char *) &ipopt + ipoptlen)
3750 ** o[0] is the option type (loose/strict).
3751 ** o[1] is the length of this option,
3752 ** including option type and
3754 ** o[2] is the pointer into the route
3756 ** o[3] begins the route data.
3759 p = &hbuf[strlen(hbuf)];
3760 l = sizeof hbuf - (hbuf - p) - 6;
3761 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
3763 *o == IPOPT_SSRR ? "!" : "",
3764 l > 240 ? 120 : l / 2,
3765 inet_ntoa(GET_IPOPT_DST(ipopt.IP_DST)));
3770 j = o[1] / sizeof(struct in_addr) - 1;
3772 /* q skips length and router pointer to data */
3774 for ( ; j >= 0; j--)
3776 struct in_addr addr;
3778 memcpy(&addr, q, sizeof(addr));
3779 (void) sm_snprintf(p,
3789 q += sizeof(struct in_addr);
3795 /* Skip over option */
3800 (void) sm_snprintf(p, SPACELEFT(hbuf, p), "]");
3805 #endif /* IP_SRCROUTE */
3806 if (RealHostName != NULL && RealHostName[0] != '[')
3808 p = &hbuf[strlen(hbuf)];
3809 (void) sm_snprintf(p, SPACELEFT(hbuf, p), " [%.100s]",
3810 anynet_ntoa(&RealHostAddr));
3814 p = &hbuf[strlen(hbuf)];
3815 (void) sm_strlcpy(p, " (may be forged)", SPACELEFT(hbuf, p));
3816 macdefine(&BlankEnvelope.e_macro, A_PERM,
3817 macid("{client_resolve}"), "FORGED");
3822 #endif /* IP_SRCROUTE */
3824 /* put back the original incoming port */
3825 switch (RealHostAddr.sa.sa_family)
3830 RealHostAddr.sin.sin_port = port;
3832 #endif /* NETINET */
3837 RealHostAddr.sin6.sin6_port = port;
3839 #endif /* NETINET6 */
3843 sm_dprintf("getauthinfo: %s\n", hbuf);
3847 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
3850 ** map -- a pointer to this map.
3851 ** name -- the (presumably unqualified) hostname.
3852 ** av -- unused -- for compatibility with other mapping
3854 ** statp -- an exit status (out parameter) -- set to
3855 ** EX_TEMPFAIL if the name server is unavailable.
3858 ** The mapping, if found.
3859 ** NULL if no mapping found.
3862 ** Looks up the host specified in hbuf. If it is not
3863 ** the canonical name for that host, return the canonical
3864 ** name (unless MF_MATCHONLY is set, which will cause the
3865 ** status only to be returned).
3869 host_map_lookup(map, name, av, statp)
3875 register struct hostent *hp;
3877 struct in_addr in_addr;
3878 #endif /* NETINET */
3880 struct in6_addr in6_addr;
3881 #endif /* NETINET6 */
3882 char *cp, *ans = NULL;
3886 time_t SM_NONVOLATILE retrans = 0;
3887 int SM_NONVOLATILE retry = 0;
3888 #endif /* NAMED_BIND */
3889 char hbuf[MAXNAME + 1];
3892 ** See if we have already looked up this name. If so, just
3893 ** return it (unless expired).
3897 s = stab(name, ST_NAMECANON, ST_ENTER);
3898 if (bitset(NCF_VALID, s->s_namecanon.nc_flags) &&
3899 s->s_namecanon.nc_exp >= now)
3902 sm_dprintf("host_map_lookup(%s) => CACHE %s\n",
3904 s->s_namecanon.nc_cname == NULL
3906 : s->s_namecanon.nc_cname);
3907 errno = s->s_namecanon.nc_errno;
3908 SM_SET_H_ERRNO(s->s_namecanon.nc_herrno);
3909 *statp = s->s_namecanon.nc_stat;
3910 if (*statp == EX_TEMPFAIL)
3912 CurEnv->e_status = "4.4.3";
3913 message("851 %s: Name server timeout",
3914 shortenstring(name, 33));
3916 if (*statp != EX_OK)
3918 if (s->s_namecanon.nc_cname == NULL)
3920 syserr("host_map_lookup(%s): bogus NULL cache entry, errno=%d, h_errno=%d",
3922 s->s_namecanon.nc_errno,
3923 s->s_namecanon.nc_herrno);
3926 if (bitset(MF_MATCHONLY, map->map_mflags))
3927 cp = map_rewrite(map, name, strlen(name), NULL);
3929 cp = map_rewrite(map,
3930 s->s_namecanon.nc_cname,
3931 strlen(s->s_namecanon.nc_cname),
3937 ** If we are running without a regular network connection (usually
3938 ** dial-on-demand) and we are just queueing, we want to avoid DNS
3939 ** lookups because those could try to connect to a server.
3942 if (CurEnv->e_sendmode == SM_DEFER &&
3943 bitset(MF_DEFER, map->map_mflags))
3946 sm_dprintf("host_map_lookup(%s) => DEFERRED\n", name);
3947 *statp = EX_TEMPFAIL;
3952 ** If first character is a bracket, then it is an address
3953 ** lookup. Address is copied into a temporary buffer to
3954 ** strip the brackets and to preserve name if address is
3959 sm_dprintf("host_map_lookup(%s) => ", name);
3961 if (map->map_timeout > 0)
3963 retrans = _res.retrans;
3964 _res.retrans = map->map_timeout;
3966 if (map->map_retry > 0)
3969 _res.retry = map->map_retry;
3971 #endif /* NAMED_BIND */
3973 /* set default TTL */
3974 s->s_namecanon.nc_exp = now + SM_DEFAULT_TTL;
3979 (void) sm_strlcpy(hbuf, name, sizeof hbuf);
3980 if (getcanonname(hbuf, sizeof hbuf - 1, !HasWildcardMX, &ttl))
3984 s->s_namecanon.nc_exp = now + SM_MIN(ttl,
3990 if ((cp = strchr(name, ']')) == NULL)
3993 sm_dprintf("FAILED\n");
4000 if ((in_addr.s_addr = inet_addr(&name[1])) != INADDR_NONE)
4001 hp = sm_gethostbyaddr((char *)&in_addr,
4003 #endif /* NETINET */
4006 anynet_pton(AF_INET6, &name[1], &in6_addr) == 1)
4007 hp = sm_gethostbyaddr((char *)&in6_addr,
4008 IN6ADDRSZ, AF_INET6);
4009 #endif /* NETINET6 */
4014 /* found a match -- copy out */
4015 ans = denlstring((char *) hp->h_name, true, true);
4017 if (ans == hp->h_name)
4019 static char n[MAXNAME + 1];
4021 /* hp->h_name is about to disappear */
4022 (void) sm_strlcpy(n, ans, sizeof n);
4027 #endif /* NETINET6 */
4031 if (map->map_timeout > 0)
4032 _res.retrans = retrans;
4033 if (map->map_retry > 0)
4035 #endif /* NAMED_BIND */
4037 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
4039 /* Found an answer */
4042 s->s_namecanon.nc_stat = *statp = EX_OK;
4043 if (s->s_namecanon.nc_cname != NULL)
4044 sm_free(s->s_namecanon.nc_cname);
4045 s->s_namecanon.nc_cname = sm_strdup_x(ans);
4046 if (bitset(MF_MATCHONLY, map->map_mflags))
4047 cp = map_rewrite(map, name, strlen(name), NULL);
4049 cp = map_rewrite(map, ans, strlen(ans), av);
4051 sm_dprintf("FOUND %s\n", ans);
4056 /* No match found */
4057 s->s_namecanon.nc_errno = errno;
4059 s->s_namecanon.nc_herrno = h_errno;
4061 sm_dprintf("FAIL (%d)\n", h_errno);
4067 CurEnv->e_status = "4.4.3";
4068 message("851 %s: Name server timeout",
4069 shortenstring(name, 33));
4071 *statp = EX_TEMPFAIL;
4074 case HOST_NOT_FOUND:
4080 *statp = EX_SOFTWARE;
4084 *statp = EX_UNAVAILABLE;
4087 #else /* NAMED_BIND */
4089 sm_dprintf("FAIL\n");
4091 #endif /* NAMED_BIND */
4092 s->s_namecanon.nc_stat = *statp;
4096 ** HOST_MAP_INIT -- initialize host class structures
4099 ** map -- a pointer to this map.
4100 ** args -- argument string.
4107 host_map_init(map, args)
4111 register char *p = args;
4115 while (isascii(*p) && isspace(*p))
4126 map->map_tapp = ++p;
4130 map->map_mflags |= MF_MATCHONLY;
4134 map->map_mflags |= MF_NODEFER;
4137 case 'S': /* only for consistency */
4138 map->map_spacesub = *++p;
4142 map->map_mflags |= MF_DEFER;
4149 while (isascii(*++p) && isspace(*p))
4154 map->map_timeout = convtime(p, 's');
4161 while (isascii(*++p) && isspace(*p))
4163 map->map_retry = atoi(p);
4166 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
4171 if (map->map_app != NULL)
4172 map->map_app = newstr(map->map_app);
4173 if (map->map_tapp != NULL)
4174 map->map_tapp = newstr(map->map_tapp);
4180 ** ANYNET_NTOP -- convert an IPv6 network address to printable form.
4183 ** s6a -- a pointer to an in6_addr structure.
4184 ** dst -- buffer to store result in
4185 ** dst_len -- size of dst buffer
4188 ** A printable version of that structure.
4192 anynet_ntop(s6a, dst, dst_len)
4193 struct in6_addr *s6a;
4199 if (IN6_IS_ADDR_V4MAPPED(s6a))
4200 ap = (char *) inet_ntop(AF_INET,
4201 &s6a->s6_addr[IN6ADDRSZ - INADDRSZ],
4208 /* Save pointer to beginning of string */
4211 /* Add IPv6: protocol tag */
4212 sz = sm_strlcpy(dst, "IPv6:", dst_len);
4217 ap = (char *) inet_ntop(AF_INET6, s6a, dst, dst_len);
4219 /* Restore pointer to beginning of string */
4227 ** ANYNET_PTON -- convert printed form to network address.
4229 ** Wrapper for inet_pton() which handles IPv6: labels.
4232 ** family -- address family
4234 ** dst -- destination address structure
4237 ** 1 if the address was valid
4238 ** 0 if the address wasn't parseable
4243 anynet_pton(family, src, dst)
4248 if (family == AF_INET6 && sm_strncasecmp(src, "IPv6:", 5) == 0)
4250 return inet_pton(family, src, dst);
4252 #endif /* NETINET6 */
4254 ** ANYNET_NTOA -- convert a network address to printable form.
4257 ** sap -- a pointer to a sockaddr structure.
4260 ** A printable version of that sockaddr.
4263 #ifdef USE_SOCK_STREAM
4266 # include <net/if_dl.h>
4267 # endif /* NETLINK */
4271 register SOCKADDR *sap;
4276 static char buf[100];
4278 /* check for null/zero family */
4281 if (sap->sa.sa_family == 0)
4284 switch (sap->sa.sa_family)
4288 if (sap->sunix.sun_path[0] != '\0')
4289 (void) sm_snprintf(buf, sizeof buf, "[UNIX: %.64s]",
4290 sap->sunix.sun_path);
4292 (void) sm_strlcpy(buf, "[UNIX: localhost]", sizeof buf);
4294 # endif /* NETUNIX */
4298 return (char *) inet_ntoa(sap->sin.sin_addr);
4299 # endif /* NETINET */
4303 ap = anynet_ntop(&sap->sin6.sin6_addr, buf, sizeof buf);
4307 # endif /* NETINET6 */
4311 (void) sm_snprintf(buf, sizeof buf, "[LINK: %s]",
4312 link_ntoa((struct sockaddr_dl *) &sap->sa));
4314 # endif /* NETLINK */
4316 /* this case is needed when nothing is #defined */
4317 /* in order to keep the switch syntactically correct */
4321 /* unknown family -- just dump bytes */
4322 (void) sm_snprintf(buf, sizeof buf, "Family %d: ", sap->sa.sa_family);
4323 bp = &buf[strlen(buf)];
4324 ap = sap->sa.sa_data;
4325 for (l = sizeof sap->sa.sa_data; --l >= 0; )
4327 (void) sm_snprintf(bp, SPACELEFT(buf, bp), "%02x:",
4335 ** HOSTNAMEBYANYADDR -- return name of host based on address
4338 ** sap -- SOCKADDR pointer
4341 ** text representation of host name.
4348 hostnamebyanyaddr(sap)
4349 register SOCKADDR *sap;
4351 register struct hostent *hp;
4354 # endif /* NAMED_BIND */
4356 struct in6_addr in6_addr;
4357 # endif /* NETINET6 */
4360 /* shorten name server timeout to avoid higher level timeouts */
4361 saveretry = _res.retry;
4362 if (_res.retry * _res.retrans > 20)
4363 _res.retry = 20 / _res.retrans;
4364 # endif /* NAMED_BIND */
4366 switch (sap->sa.sa_family)
4370 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
4373 # endif /* NETINET */
4377 hp = sm_gethostbyaddr((char *) &sap->sin6.sin6_addr,
4378 IN6ADDRSZ, AF_INET6);
4380 # endif /* NETINET6 */
4384 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
4385 sizeof sap->siso.siso_addr, AF_ISO);
4387 # endif /* NETISO */
4393 # endif /* NETUNIX */
4396 hp = sm_gethostbyaddr(sap->sa.sa_data, sizeof sap->sa.sa_data,
4402 _res.retry = saveretry;
4403 # endif /* NAMED_BIND */
4405 # if NETINET || NETINET6
4406 if (hp != NULL && hp->h_name[0] != '['
4408 && inet_pton(AF_INET6, hp->h_name, &in6_addr) != 1
4409 # endif /* NETINET6 */
4411 && inet_addr(hp->h_name) == INADDR_NONE
4412 # endif /* NETINET */
4417 name = denlstring((char *) hp->h_name, true, true);
4419 if (name == hp->h_name)
4421 static char n[MAXNAME + 1];
4423 /* Copy the string, hp->h_name is about to disappear */
4424 (void) sm_strlcpy(n, name, sizeof n);
4428 # endif /* NETINET6 */
4431 # endif /* NETINET || NETINET6 */
4439 # endif /* NETINET6 */
4442 if (sap->sa.sa_family == AF_UNIX && sap->sunix.sun_path[0] == '\0')
4444 # endif /* NETUNIX */
4446 static char buf[203];
4448 (void) sm_snprintf(buf, sizeof buf, "[%.200s]",
4453 #endif /* USE_SOCK_STREAM */