2 * Copyright (c) 1998-2003 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.613.2.14 2003/02/11 17:17:22 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 # if _FFR_MILTER_PERDAEMON
78 char *d_inputfilterlist;
79 struct milter *d_inputfilters[MAXFILTERS];
80 # endif /* _FFR_MILTER_PERDAEMON */
84 typedef struct daemon DAEMON_T;
86 static void connecttimeout __P((void));
87 static int opendaemonsocket __P((DAEMON_T *, bool));
88 static unsigned short setupdaemon __P((SOCKADDR *));
89 static void getrequests_checkdiskspace __P((ENVELOPE *e));
92 ** DAEMON.C -- routines to use when running as a daemon.
94 ** This entire file is highly dependent on the 4.2 BSD
95 ** interprocess communication primitives. No attempt has
96 ** been made to make this file portable to Version 7,
97 ** Version 6, MPX files, etc. If you should try such a
98 ** thing yourself, I recommend chucking the entire file
99 ** and starting from scratch. Basic semantics are:
102 ** Opens a port and initiates a connection.
103 ** Returns in a child. Must set InChannel and
104 ** OutChannel appropriately.
106 ** Close any open files associated with getting
107 ** the connection; this is used when running the queue,
108 ** etc., to avoid having extra file descriptors during
109 ** the queue run and to avoid confusing the network
110 ** code (if it cares).
111 ** makeconnection(host, port, mci, e, enough)
112 ** Make a connection to the named host on the given
113 ** port. Returns zero on success, else an exit status
114 ** describing the error.
115 ** host_map_lookup(map, hbuf, avp, pstat)
116 ** Convert the entry in hbuf into a canonical form.
119 static DAEMON_T Daemons[MAXDAEMONS];
120 static int NDaemons = 0; /* actual number of daemons */
122 static time_t NextDiskSpaceCheck = 0;
125 ** GETREQUESTS -- open mail IPC port and get requests.
128 ** e -- the current envelope.
134 ** Waits until some interesting activity occurs. When
135 ** it does, a child is created to process it, and the
136 ** parent waits for completion. Return from this
137 ** routine is always in the child. The file pointers
138 ** "InChannel" and "OutChannel" should be set to point
139 ** to the communication channel.
140 ** May restart persistent queue runners if they have ended
149 int idx, curdaemon = -1;
150 int i, olddaemon = 0;
154 char status[MAXLINE];
156 SOCKADDR_LEN_T len = sizeof sa;
157 #if _FFR_QUEUE_RUN_PARANOIA
159 #endif /* _FFR_QUEUE_RUN_PARANOIA */
161 extern int ControlSocket;
162 # endif /* NETUNIX */
163 extern ENVELOPE BlankEnvelope;
164 extern bool refuseconnections __P((char *, ENVELOPE *, int, bool));
167 for (idx = 0; idx < NDaemons; idx++)
169 Daemons[idx].d_port = setupdaemon(&(Daemons[idx].d_addr));
170 Daemons[idx].d_firsttime = true;
171 Daemons[idx].d_refuse_connections_until = (time_t) 0;
175 ** Try to actually open the connection.
180 for (idx = 0; idx < NDaemons; idx++)
182 sm_dprintf("getrequests: daemon %s: port %d\n",
184 ntohs(Daemons[idx].d_port));
188 /* get a socket for the SMTP connection */
189 for (idx = 0; idx < NDaemons; idx++)
190 Daemons[idx].d_socksize = opendaemonsocket(&Daemons[idx], true);
192 if (opencontrolsocket() < 0)
193 sm_syslog(LOG_WARNING, NOQID,
194 "daemon could not open control socket %s: %s",
195 ControlSocketName, sm_errstring(errno));
197 /* If there are any queue runners released reapchild() co-ord's */
198 (void) sm_signal(SIGCHLD, reapchild);
200 /* write the pid to file, command line args to syslog */
205 char jbuf[MAXHOSTNAMELEN];
207 expand("\201j", jbuf, sizeof jbuf, e);
208 j_has_dot = strchr(jbuf, '.') != NULL;
212 /* Add parent process as first item */
213 proc_list_add(CurrentPid, "Sendmail daemon", PROC_DAEMON, 0, -1);
217 for (idx = 0; idx < NDaemons; idx++)
218 sm_dprintf("getrequests: daemon %s: %d\n",
220 Daemons[idx].d_socket);
226 auto SOCKADDR_LEN_T lotherend;
227 bool timedout = false;
228 bool control = false;
234 #endif /* STARTTLS */
236 /* see if we are rejecting connections */
237 (void) sm_blocksignal(SIGALRM);
239 if (ShutdownRequest != NULL)
241 else if (RestartRequest != NULL)
243 else if (RestartWorkGroup)
244 restart_marked_work_groups();
246 for (idx = 0; idx < NDaemons; idx++)
249 ** XXX do this call outside the loop?
250 ** no: refuse_connections may sleep().
254 if (now < Daemons[idx].d_refuse_connections_until)
256 if (bitnset(D_DISABLE, Daemons[idx].d_flags))
258 if (refuseconnections(Daemons[idx].d_name, e, idx,
261 if (Daemons[idx].d_socket >= 0)
263 /* close socket so peer fails quickly */
264 (void) close(Daemons[idx].d_socket);
265 Daemons[idx].d_socket = -1;
268 /* refuse connections for next 15 seconds */
269 Daemons[idx].d_refuse_connections_until = now + 15;
271 else if (Daemons[idx].d_socket < 0 ||
272 Daemons[idx].d_firsttime)
274 if (!Daemons[idx].d_firsttime && LogLevel > 8)
275 sm_syslog(LOG_INFO, NOQID,
276 "accepting connections again for daemon %s",
277 Daemons[idx].d_name);
279 /* arrange to (re)open the socket if needed */
280 (void) opendaemonsocket(&Daemons[idx], false);
281 Daemons[idx].d_firsttime = false;
285 /* May have been sleeping above, check again */
286 if (ShutdownRequest != NULL)
288 else if (RestartRequest != NULL)
290 else if (RestartWorkGroup)
291 restart_marked_work_groups();
293 getrequests_checkdiskspace(e);
296 /* check for disaster */
298 char jbuf[MAXHOSTNAMELEN];
300 expand("\201j", jbuf, sizeof jbuf, e);
301 if (!wordinclass(jbuf, 'w'))
303 dumpstate("daemon lost $j");
304 sm_syslog(LOG_ALERT, NOQID,
305 "daemon process doesn't have $j in $=w; see syslog");
308 else if (j_has_dot && strchr(jbuf, '.') == NULL)
310 dumpstate("daemon $j lost dot");
311 sm_syslog(LOG_ALERT, NOQID,
312 "daemon process $j lost dot; see syslog");
320 ** Andrew Sun <asun@ieps-sun.ml.com> claims that this will
321 ** fix the SVr4 problem. But it seems to have gone away,
322 ** so is it worth doing this?
325 if (DaemonSocket >= 0 &&
326 SetNonBlocking(DaemonSocket, false) < 0)
329 (void) sm_releasesignal(SIGALRM);
333 bool setproc = false;
336 struct timeval timeout;
338 if (ShutdownRequest != NULL)
340 else if (RestartRequest != NULL)
342 else if (RestartWorkGroup)
343 restart_marked_work_groups();
347 for (idx = 0; idx < NDaemons; idx++)
349 /* wait for a connection */
350 if (Daemons[idx].d_socket >= 0)
354 Daemons[idx].d_flags))
356 sm_setproctitle(true, e,
357 "accepting connections");
360 if (Daemons[idx].d_socket > highest)
361 highest = Daemons[idx].d_socket;
362 SM_FD_SET(Daemons[idx].d_socket,
368 if (ControlSocket >= 0)
370 if (ControlSocket > highest)
371 highest = ControlSocket;
372 SM_FD_SET(ControlSocket, &readfds);
379 t = select(highest + 1, FDSET_CAST &readfds,
380 NULL, NULL, &timeout);
382 /* Did someone signal while waiting? */
383 if (ShutdownRequest != NULL)
385 else if (RestartRequest != NULL)
387 else if (RestartWorkGroup)
388 restart_marked_work_groups();
395 (void) runqueue(true, false, false, false);
396 #if _FFR_QUEUE_RUN_PARANOIA
398 #endif /* _FFR_QUEUE_RUN_PARANOIA */
400 #if _FFR_QUEUE_RUN_PARANOIA
401 else if (QueueIntvl > 0 &&
402 lastrun + QueueIntvl + 60 < now)
406 ** set lastrun unconditionally to avoid
407 ** calling checkqueuerunner() all the time.
408 ** That's also why we currently ignore the
409 ** result of the function call.
412 (void) checkqueuerunner();
415 #endif /* _FFR_QUEUE_RUN_PARANOIA */
426 /* look "round-robin" for an active socket */
427 if ((idx = olddaemon + 1) >= NDaemons)
429 for (i = 0; i < NDaemons; i++)
431 if (Daemons[idx].d_socket >= 0 &&
432 SM_FD_ISSET(Daemons[idx].d_socket,
435 lotherend = Daemons[idx].d_socksize;
436 memset(&RealHostAddr, '\0',
437 sizeof RealHostAddr);
438 t = accept(Daemons[idx].d_socket,
439 (struct sockaddr *)&RealHostAddr,
443 ** If remote side closes before
444 ** accept() finishes, sockaddr
445 ** might not be fully filled in.
450 # ifdef BSD4_4_SOCKADDR
451 RealHostAddr.sa.sa_len == 0 ||
452 # endif /* BSD4_4_SOCKADDR */
453 RealHostAddr.sa.sa_family != Daemons[idx].d_addr.sa.sa_family))
459 olddaemon = curdaemon = idx;
462 if (++idx >= NDaemons)
466 if (curdaemon == -1 && ControlSocket >= 0 &&
467 SM_FD_ISSET(ControlSocket, &readfds))
469 struct sockaddr_un sa_un;
471 lotherend = sizeof sa_un;
472 memset(&sa_un, '\0', sizeof sa_un);
473 t = accept(ControlSocket,
474 (struct sockaddr *)&sa_un,
478 ** If remote side closes before
479 ** accept() finishes, sockaddr
480 ** might not be fully filled in.
485 # ifdef BSD4_4_SOCKADDR
486 sa_un.sun_len == 0 ||
487 # endif /* BSD4_4_SOCKADDR */
488 sa_un.sun_family != AF_UNIX))
500 /* No daemon to service */
504 if (t >= 0 || errno != EINTR)
513 (void) sm_blocksignal(SIGALRM);
517 syserr("getrequests: accept");
519 /* arrange to re-open the socket next time around */
520 (void) close(Daemons[curdaemon].d_socket);
521 Daemons[curdaemon].d_socket = -1;
522 #if SO_REUSEADDR_IS_BROKEN
524 ** Give time for bound socket to be released.
525 ** This creates a denial-of-service if you can
526 ** force accept() to fail on affected systems.
529 Daemons[curdaemon].d_refuse_connections_until = curtime() + 15;
530 #endif /* SO_REUSEADDR_IS_BROKEN */
536 /* set some daemon related macros */
537 switch (Daemons[curdaemon].d_addr.sa.sa_family)
540 macdefine(&BlankEnvelope.e_macro, A_PERM,
541 macid("{daemon_family}"), "unspec");
543 #if _FFR_DAEMON_NETUNIX
546 macdefine(&BlankEnvelope.e_macro, A_PERM,
547 macid("{daemon_family}"), "local");
549 # endif /* NETUNIX */
550 #endif /* _FFR_DAEMON_NETUNIX */
553 macdefine(&BlankEnvelope.e_macro, A_PERM,
554 macid("{daemon_family}"), "inet");
559 macdefine(&BlankEnvelope.e_macro, A_PERM,
560 macid("{daemon_family}"), "inet6");
562 #endif /* NETINET6 */
565 macdefine(&BlankEnvelope.e_macro, A_PERM,
566 macid("{daemon_family}"), "iso");
571 macdefine(&BlankEnvelope.e_macro, A_PERM,
572 macid("{daemon_family}"), "ns");
577 macdefine(&BlankEnvelope.e_macro, A_PERM,
578 macid("{daemon_family}"), "x.25");
582 macdefine(&BlankEnvelope.e_macro, A_PERM,
583 macid("{daemon_name}"),
584 Daemons[curdaemon].d_name);
585 if (Daemons[curdaemon].d_mflags != NULL)
586 macdefine(&BlankEnvelope.e_macro, A_PERM,
587 macid("{daemon_flags}"),
588 Daemons[curdaemon].d_mflags);
590 macdefine(&BlankEnvelope.e_macro, A_PERM,
591 macid("{daemon_flags}"), "");
595 ** Create a subprocess to process the mail.
599 sm_dprintf("getrequests: forking (fd = %d)\n", t);
602 ** Advance state of PRNG.
603 ** This is necessary because otherwise all child processes
604 ** will produce the same PRN sequence and hence the selection
605 ** of a queue directory (and other things, e.g., MX selection)
606 ** are not "really" random.
609 /* XXX get some better "random" data? */
611 RAND_seed((void *) &NextDiskSpaceCheck,
612 sizeof NextDiskSpaceCheck);
613 RAND_seed((void *) &now, sizeof now);
614 RAND_seed((void *) &seed, sizeof seed);
617 #endif /* STARTTLS */
621 ** Update MX records for FallBackMX.
622 ** Let's hope this is fast otherwise we screw up the
626 if (FallBackMX != NULL)
627 (void) getfallbackmxrr(FallBackMX);
628 #endif /* NAMED_BIND */
632 /* don't fork, handle connection in this process */
634 pipefd[0] = pipefd[1] = -1;
639 ** Create a pipe to keep the child from writing to
640 ** the socket until after the parent has closed
641 ** it. Otherwise the parent may hang if the child
642 ** has closed it first.
645 if (pipe(pipefd) < 0)
646 pipefd[0] = pipefd[1] = -1;
648 (void) sm_blocksignal(SIGCHLD);
652 syserr("daemon: cannot fork");
655 (void) close(pipefd[0]);
656 (void) close(pipefd[1]);
658 (void) sm_releasesignal(SIGCHLD);
668 SM_FILE_T *inchannel, *outchannel = NULL;
671 ** CHILD -- return to caller.
672 ** Collect verified idea of sending host.
673 ** Verify calling user id if possible here.
676 /* Reset global flags */
677 RestartRequest = NULL;
678 RestartWorkGroup = false;
679 ShutdownRequest = NULL;
681 CurrentPid = getpid();
683 (void) sm_releasesignal(SIGALRM);
684 (void) sm_releasesignal(SIGCHLD);
685 (void) sm_signal(SIGCHLD, SIG_DFL);
686 (void) sm_signal(SIGHUP, SIG_DFL);
687 (void) sm_signal(SIGTERM, intsig);
689 /* turn on profiling */
693 ** Initialize exception stack and default exception
694 ** handler for child process.
697 sm_exc_newthread(fatal_error);
701 macdefine(&BlankEnvelope.e_macro, A_TEMP,
702 macid("{daemon_addr}"),
703 anynet_ntoa(&Daemons[curdaemon].d_addr));
704 (void) sm_snprintf(status, sizeof status, "%d",
705 ntohs(Daemons[curdaemon].d_port));
706 macdefine(&BlankEnvelope.e_macro, A_TEMP,
707 macid("{daemon_port}"), status);
710 for (idx = 0; idx < NDaemons; idx++)
712 if (Daemons[idx].d_socket >= 0)
713 (void) close(Daemons[idx].d_socket);
714 Daemons[idx].d_socket = -1;
718 /* Avoid SMTP daemon actions if control command */
721 /* Add control socket process */
722 proc_list_add(CurrentPid,
723 "console socket child",
724 PROC_CONTROL_CHILD, 0, -1);
730 /* clean up background delivery children */
731 (void) sm_signal(SIGCHLD, reapchild);
733 /* Add parent process as first child item */
734 proc_list_add(CurrentPid, "daemon child",
735 PROC_DAEMON_CHILD, 0, -1);
737 /* don't schedule queue runs if ETRN */
740 sm_setproctitle(true, e, "startup with %s",
741 anynet_ntoa(&RealHostAddr));
749 ** Wait for the parent to close the write end
750 ** of the pipe, which we will see as an EOF.
751 ** This guarantees that we won't write to the
752 ** socket until after the parent has closed
756 /* close the write end of the pipe */
757 (void) close(pipefd[1]);
759 /* we shouldn't be interrupted, but ... */
760 while (read(pipefd[0], &c, 1) < 0 &&
763 (void) close(pipefd[0]);
766 /* control socket processing */
769 control_command(t, e);
774 /* determine host name */
775 p = hostnamebyanyaddr(&RealHostAddr);
776 if (strlen(p) > MAXNAME) /* XXX - 1 ? */
778 RealHostName = newstr(p);
779 if (RealHostName[0] == '[')
781 macdefine(&BlankEnvelope.e_macro, A_PERM,
782 macid("{client_resolve}"),
783 h_errno == TRY_AGAIN ? "TEMP" : "FAIL");
786 macdefine(&BlankEnvelope.e_macro, A_PERM,
787 macid("{client_resolve}"), "OK");
788 sm_setproctitle(true, e, "startup with %s", p);
789 markstats(e, NULL, STATS_CONNECT);
791 if ((inchannel = sm_io_open(SmFtStdiofd,
797 (outchannel = sm_io_open(SmFtStdiofd,
803 syserr("cannot open SMTP server channel, fd=%d",
805 finis(false, true, EX_OK);
807 sm_io_automode(inchannel, outchannel);
809 InChannel = inchannel;
810 OutChannel = outchannel;
811 DisConnected = false;
814 if (!xla_host_ok(RealHostName))
816 message("421 4.4.5 Too many SMTP sessions for this host");
817 finis(false, true, EX_OK);
820 /* find out name for interface of connection */
821 if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
822 NULL), &sa.sa, &len) == 0)
824 p = hostnamebyanyaddr(&sa);
826 sm_dprintf("getreq: got name %s\n", p);
827 macdefine(&BlankEnvelope.e_macro, A_TEMP,
828 macid("{if_name}"), p);
831 ** Do this only if it is not the loopback
840 addr = anynet_ntoa(&sa);
841 (void) sm_snprintf(family,
843 "%d", sa.sa.sa_family);
844 macdefine(&BlankEnvelope.e_macro,
846 macid("{if_addr}"), addr);
847 macdefine(&BlankEnvelope.e_macro,
849 macid("{if_family}"), family);
851 sm_dprintf("getreq: got addr %s and family %s\n",
856 macdefine(&BlankEnvelope.e_macro,
858 macid("{if_addr}"), NULL);
859 macdefine(&BlankEnvelope.e_macro,
861 macid("{if_family}"), NULL);
867 sm_dprintf("getreq: getsockname failed\n");
868 macdefine(&BlankEnvelope.e_macro, A_PERM,
869 macid("{if_name}"), NULL);
870 macdefine(&BlankEnvelope.e_macro, A_PERM,
871 macid("{if_addr}"), NULL);
872 macdefine(&BlankEnvelope.e_macro, A_PERM,
873 macid("{if_family}"), NULL);
878 /* parent -- keep track of children */
881 (void) sm_snprintf(status, sizeof status,
882 "control socket server child");
883 proc_list_add(pid, status, PROC_CONTROL, 0, -1);
887 (void) sm_snprintf(status, sizeof status,
888 "SMTP server child for %s",
889 anynet_ntoa(&RealHostAddr));
890 proc_list_add(pid, status, PROC_DAEMON, 0, -1);
892 (void) sm_releasesignal(SIGCHLD);
894 /* close the read end of the synchronization pipe */
897 (void) close(pipefd[0]);
901 /* close the port so that others will hang (for a while) */
904 /* release the child by closing the read end of the sync pipe */
907 (void) close(pipefd[1]);
912 sm_dprintf("getreq: returning\n");
915 # if _FFR_MILTER_PERDAEMON
916 /* set the filters for this daemon */
917 if (Daemons[curdaemon].d_inputfilterlist != NULL)
921 Daemons[curdaemon].d_inputfilters[i] != NULL);
924 InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
927 InputFilters[i] = NULL;
929 # endif /* _FFR_MILTER_PERDAEMON */
931 return &Daemons[curdaemon].d_flags;
935 ** GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
944 ** Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
948 getrequests_checkdiskspace(e)
956 if (now < NextDiskSpaceCheck)
959 /* Check if there is available disk space in all queue groups. */
960 if (!enoughdiskspace(0, NULL))
962 for (idx = 0; idx < NDaemons; ++idx)
964 if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
967 /* log only if not logged before */
971 sm_syslog(LOG_INFO, NOQID,
972 "rejecting new messages: min free: %ld",
974 sm_setproctitle(true, e,
975 "rejecting new messages: min free: %ld",
979 setbitn(D_ETRNONLY, Daemons[idx].d_flags);
984 for (idx = 0; idx < NDaemons; ++idx)
986 if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
989 /* log only if not logged before */
993 sm_syslog(LOG_INFO, NOQID,
994 "accepting new messages (again)");
998 /* title will be set later */
999 clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
1003 /* only check disk space once a minute */
1004 NextDiskSpaceCheck = now + 60;
1008 ** OPENDAEMONSOCKET -- open SMTP socket
1010 ** Deals with setting all appropriate options.
1013 ** d -- the structure for the daemon to open.
1014 ** firsttime -- set if this is the initial open.
1017 ** Size in bytes of the daemon socket addr.
1020 ** Leaves DaemonSocket set to the open socket.
1021 ** Exits if the socket cannot be created.
1024 #define MAXOPENTRIES 10 /* maximum number of tries to open connection */
1027 opendaemonsocket(d, firsttime)
1033 SOCKADDR_LEN_T socksize = 0;
1038 sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
1044 if (firsttime || d->d_socket < 0)
1046 #if _FFR_DAEMON_NETUNIX
1048 if (d->d_addr.sa.sa_family == AF_UNIX)
1051 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
1053 /* if not safe, don't use it */
1054 rval = safefile(d->d_addr.sunix.sun_path,
1057 S_IRUSR|S_IWUSR, NULL);
1061 syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
1063 d->d_addr.sunix.sun_path);
1067 /* Don't try to overtake an existing socket */
1068 (void) unlink(d->d_addr.sunix.sun_path);
1070 # endif /* NETUNIX */
1071 #endif /* _FFR_DOMAIN_NETUNIX */
1072 d->d_socket = socket(d->d_addr.sa.sa_family,
1074 if (d->d_socket < 0)
1077 syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
1080 if (bitnset(D_OPTIONAL, d->d_flags) &&
1081 (!transienterror(save_errno) ||
1082 ntries >= MAXOPENTRIES - 1))
1084 syserr("opendaemonsocket: daemon %s: optional socket disabled",
1086 setbitn(D_DISABLE, d->d_flags);
1092 sm_syslog(LOG_ALERT, NOQID,
1093 "daemon %s: problem creating SMTP socket",
1099 if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
1101 save_errno = EINVAL;
1102 syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
1103 d->d_name, d->d_socket);
1107 /* turn on network debugging? */
1109 (void) setsockopt(d->d_socket, SOL_SOCKET,
1110 SO_DEBUG, (char *)&on,
1113 (void) setsockopt(d->d_socket, SOL_SOCKET,
1114 SO_REUSEADDR, (char *)&on, sizeof on);
1115 (void) setsockopt(d->d_socket, SOL_SOCKET,
1116 SO_KEEPALIVE, (char *)&on, sizeof on);
1119 if (d->d_tcprcvbufsize > 0)
1121 if (setsockopt(d->d_socket, SOL_SOCKET,
1123 (char *) &d->d_tcprcvbufsize,
1124 sizeof(d->d_tcprcvbufsize)) < 0)
1125 syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
1127 #endif /* SO_RCVBUF */
1129 if (d->d_tcpsndbufsize > 0)
1131 if (setsockopt(d->d_socket, SOL_SOCKET,
1133 (char *) &d->d_tcpsndbufsize,
1134 sizeof(d->d_tcpsndbufsize)) < 0)
1135 syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
1137 #endif /* SO_SNDBUF */
1139 if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
1140 fcntl(d->d_socket, F_SETFD,
1141 fdflags | FD_CLOEXEC) == -1)
1144 syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
1146 fdflags == -1 ? "get" : "set",
1147 sm_errstring(save_errno));
1148 (void) close(d->d_socket);
1152 switch (d->d_addr.sa.sa_family)
1154 #if _FFR_DAEMON_NETUNIX
1157 socksize = sizeof d->d_addr.sunix;
1159 # endif /* NETUNIX */
1160 #endif /* _FFR_DAEMON_NETUNIX */
1163 socksize = sizeof d->d_addr.sin;
1165 #endif /* NETINET */
1169 socksize = sizeof d->d_addr.sin6;
1171 #endif /* NETINET6 */
1175 socksize = sizeof d->d_addr.siso;
1180 socksize = sizeof d->d_addr;
1184 if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
1186 /* probably another daemon already */
1188 syserr("opendaemonsocket: daemon %s: cannot bind",
1190 (void) close(d->d_socket);
1195 listen(d->d_socket, d->d_listenqueue) < 0)
1198 syserr("opendaemonsocket: daemon %s: cannot listen",
1200 (void) close(d->d_socket);
1204 } while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
1205 syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
1208 return -1; /* avoid compiler warning on IRIX */
1211 ** SETUPDAEMON -- setup socket for daemon
1214 ** daemonaddr -- socket for daemon
1217 ** port number on which daemon should run
1221 static unsigned short
1222 setupdaemon(daemonaddr)
1223 SOCKADDR *daemonaddr;
1225 unsigned short port;
1228 ** Set up the address for the mailer.
1231 if (daemonaddr->sa.sa_family == AF_UNSPEC)
1233 memset(daemonaddr, '\0', sizeof *daemonaddr);
1235 daemonaddr->sa.sa_family = AF_INET;
1236 #endif /* NETINET */
1239 switch (daemonaddr->sa.sa_family)
1243 if (daemonaddr->sin.sin_addr.s_addr == 0)
1244 daemonaddr->sin.sin_addr.s_addr = INADDR_ANY;
1245 port = daemonaddr->sin.sin_port;
1247 #endif /* NETINET */
1251 if (IN6_IS_ADDR_UNSPECIFIED(&daemonaddr->sin6.sin6_addr))
1252 daemonaddr->sin6.sin6_addr = in6addr_any;
1253 port = daemonaddr->sin6.sin6_port;
1255 #endif /* NETINET6 */
1258 /* unknown protocol */
1264 #ifdef NO_GETSERVBYNAME
1266 #else /* NO_GETSERVBYNAME */
1268 register struct servent *sp;
1270 sp = getservbyname("smtp", "tcp");
1273 syserr("554 5.3.5 service \"smtp\" unknown");
1279 #endif /* NO_GETSERVBYNAME */
1282 switch (daemonaddr->sa.sa_family)
1286 daemonaddr->sin.sin_port = port;
1288 #endif /* NETINET */
1292 daemonaddr->sin6.sin6_port = port;
1294 #endif /* NETINET6 */
1297 /* unknown protocol */
1303 ** CLRDAEMON -- reset the daemon connection
1312 ** releases any resources used by the passive daemon.
1320 for (i = 0; i < NDaemons; i++)
1322 if (Daemons[i].d_socket >= 0)
1323 (void) close(Daemons[i].d_socket);
1324 Daemons[i].d_socket = -1;
1329 ** GETMODIFIERS -- get modifier flags
1332 ** v -- the modifiers (input text line).
1333 ** modifiers -- pointer to flag field to represent modifiers.
1336 ** (xallocat()ed) string representation of modifiers.
1339 ** fills in modifiers.
1343 getmodifiers(v, modifiers)
1345 BITMAP256 modifiers;
1348 char *h, *f, *flags;
1350 /* maximum length of flags: upper case Option -> "OO " */
1351 l = 3 * strlen(v) + 3;
1353 /* is someone joking? */
1354 if (l < 0 || l > 256)
1357 sm_syslog(LOG_ERR, NOQID,
1358 "getmodifiers too long, ignored");
1363 clrbitmap(modifiers);
1364 for (h = v; *h != '\0'; h++)
1366 if (isascii(*h) && !isspace(*h) && isprint(*h))
1368 setbitn(*h, modifiers);
1381 ** CHKDAEMONMODIFIERS -- check whether all daemons have set a flag.
1384 ** flag -- the flag to test.
1387 ** true iff all daemons have set flag.
1391 chkdaemonmodifiers(flag)
1396 for (i = 0; i < NDaemons; i++)
1397 if (!bitnset((char) flag, Daemons[i].d_flags))
1403 ** SETSOCKADDROPTIONS -- set options for SOCKADDR (daemon or client)
1406 ** p -- the options line.
1407 ** d -- the daemon structure to fill in.
1414 setsockaddroptions(p, d)
1425 if (d->d_addr.sa.sa_family == AF_UNSPEC)
1426 d->d_addr.sa.sa_family = AF_INET;
1427 #endif /* NETINET */
1434 while (isascii(*p) && isspace(*p))
1445 while (isascii(*++v) && isspace(*v))
1447 if (isascii(*f) && islower(*f))
1452 case 'F': /* address family */
1453 if (isascii(*v) && isdigit(*v))
1454 d->d_addr.sa.sa_family = atoi(v);
1455 #if _FFR_DAEMON_NETUNIX
1457 else if (sm_strcasecmp(v, "unix") == 0 ||
1458 sm_strcasecmp(v, "local") == 0)
1459 d->d_addr.sa.sa_family = AF_UNIX;
1460 # endif /* NETUNIX */
1461 #endif /* _FFR_DAEMON_NETUNIX */
1463 else if (sm_strcasecmp(v, "inet") == 0)
1464 d->d_addr.sa.sa_family = AF_INET;
1465 #endif /* NETINET */
1467 else if (sm_strcasecmp(v, "inet6") == 0)
1468 d->d_addr.sa.sa_family = AF_INET6;
1469 #endif /* NETINET6 */
1471 else if (sm_strcasecmp(v, "iso") == 0)
1472 d->d_addr.sa.sa_family = AF_ISO;
1475 else if (sm_strcasecmp(v, "ns") == 0)
1476 d->d_addr.sa.sa_family = AF_NS;
1479 else if (sm_strcasecmp(v, "x.25") == 0)
1480 d->d_addr.sa.sa_family = AF_CCITT;
1483 syserr("554 5.3.5 Unknown address family %s in Family=option",
1487 case 'A': /* address */
1492 # if _FFR_MILTER_PERDAEMON
1494 d->d_inputfilterlist = v;
1496 # endif /* _FFR_MILTER_PERDAEMON */
1499 case 'P': /* port */
1503 case 'L': /* listen queue size */
1504 d->d_listenqueue = atoi(v);
1507 case 'M': /* modifiers (flags) */
1508 d->d_mflags = getmodifiers(v, d->d_flags);
1511 case 'S': /* send buffer size */
1512 d->d_tcpsndbufsize = atoi(v);
1515 case 'R': /* receive buffer size */
1516 d->d_tcprcvbufsize = atoi(v);
1519 case 'N': /* name */
1524 syserr("554 5.3.5 PortOptions parameter \"%s\" unknown",
1529 /* Check addr and port after finding family */
1532 switch (d->d_addr.sa.sa_family)
1534 #if _FFR_DAEMON_NETUNIX
1537 if (strlen(addr) >= sizeof(d->d_addr.sunix.sun_path))
1539 errno = ENAMETOOLONG;
1540 syserr("setsockaddroptions: domain socket name too long: %s > %d",
1541 addr, sizeof(d->d_addr.sunix.sun_path));
1545 /* file safety check done in opendaemonsocket() */
1546 (void) memset(&d->d_addr.sunix.sun_path, '\0',
1547 sizeof(d->d_addr.sunix.sun_path));
1548 (void) sm_strlcpy((char *)&d->d_addr.sunix.sun_path,
1550 sizeof(d->d_addr.sunix.sun_path));
1552 # endif /* NETUNIX */
1553 #endif /* _FFR_DAEMON_NETUNIX */
1556 if (!isascii(*addr) || !isdigit(*addr) ||
1557 ((d->d_addr.sin.sin_addr.s_addr = inet_addr(addr))
1560 register struct hostent *hp;
1562 hp = sm_gethostbyname(addr, AF_INET);
1564 syserr("554 5.3.0 host \"%s\" unknown",
1568 while (*(hp->h_addr_list) != NULL &&
1569 hp->h_addrtype != AF_INET)
1571 if (*(hp->h_addr_list) == NULL)
1572 syserr("554 5.3.0 host \"%s\" unknown",
1575 memmove(&d->d_addr.sin.sin_addr,
1581 # endif /* NETINET6 */
1585 #endif /* NETINET */
1589 if (anynet_pton(AF_INET6, addr,
1590 &d->d_addr.sin6.sin6_addr) != 1)
1592 register struct hostent *hp;
1594 hp = sm_gethostbyname(addr, AF_INET6);
1596 syserr("554 5.3.0 host \"%s\" unknown",
1600 while (*(hp->h_addr_list) != NULL &&
1601 hp->h_addrtype != AF_INET6)
1603 if (*(hp->h_addr_list) == NULL)
1604 syserr("554 5.3.0 host \"%s\" unknown",
1607 memmove(&d->d_addr.sin6.sin6_addr,
1615 #endif /* NETINET6 */
1618 syserr("554 5.3.5 address= option unsupported for family %d",
1619 d->d_addr.sa.sa_family);
1626 switch (d->d_addr.sa.sa_family)
1630 if (isascii(*port) && isdigit(*port))
1631 d->d_addr.sin.sin_port = htons((unsigned short)
1632 atoi((const char *) port));
1635 # ifdef NO_GETSERVBYNAME
1636 syserr("554 5.3.5 invalid port number: %s",
1638 # else /* NO_GETSERVBYNAME */
1639 register struct servent *sp;
1641 sp = getservbyname(port, "tcp");
1643 syserr("554 5.3.5 service \"%s\" unknown",
1646 d->d_addr.sin.sin_port = sp->s_port;
1647 # endif /* NO_GETSERVBYNAME */
1650 #endif /* NETINET */
1654 if (isascii(*port) && isdigit(*port))
1655 d->d_addr.sin6.sin6_port = htons((unsigned short)
1659 # ifdef NO_GETSERVBYNAME
1660 syserr("554 5.3.5 invalid port number: %s",
1662 # else /* NO_GETSERVBYNAME */
1663 register struct servent *sp;
1665 sp = getservbyname(port, "tcp");
1667 syserr("554 5.3.5 service \"%s\" unknown",
1670 d->d_addr.sin6.sin6_port = sp->s_port;
1671 # endif /* NO_GETSERVBYNAME */
1674 #endif /* NETINET6 */
1678 /* assume two byte transport selector */
1679 if (isascii(*port) && isdigit(*port))
1680 portno = htons((unsigned short) atoi(port));
1683 # ifdef NO_GETSERVBYNAME
1684 syserr("554 5.3.5 invalid port number: %s",
1686 # else /* NO_GETSERVBYNAME */
1687 register struct servent *sp;
1689 sp = getservbyname(port, "tcp");
1691 syserr("554 5.3.5 service \"%s\" unknown",
1694 portno = sp->s_port;
1695 # endif /* NO_GETSERVBYNAME */
1697 memmove(TSEL(&d->d_addr.siso),
1698 (char *) &portno, 2);
1703 syserr("554 5.3.5 Port= option unsupported for family %d",
1704 d->d_addr.sa.sa_family);
1710 ** SETDAEMONOPTIONS -- set options for running the MTA daemon
1713 ** p -- the options line.
1716 ** true if successful, false otherwise.
1719 ** increments number of daemons.
1722 #define DEF_LISTENQUEUE 10
1730 static struct dflags DaemonFlags[] =
1732 { "AUTHREQ", D_AUTHREQ },
1733 { "BINDIF", D_BINDIF },
1734 { "CANONREQ", D_CANONREQ },
1735 { "IFNHELO", D_IFNHELO },
1736 { "FQMAIL", D_FQMAIL },
1737 { "FQRCPT", D_FQRCPT },
1739 { "SMTPS", D_SMTPS },
1740 #endif /* _FFR_SMTP_SSL */
1741 { "UNQUALOK", D_UNQUALOK },
1742 { "NOAUTH", D_NOAUTH },
1743 { "NOCANON", D_NOCANON },
1744 { "NOETRN", D_NOETRN },
1745 { "NOTLS", D_NOTLS },
1746 { "ETRNONLY", D_ETRNONLY },
1747 { "OPTIONAL", D_OPTIONAL },
1748 { "DISABLE", D_DISABLE },
1749 { "ISSET", D_ISSET },
1757 register struct dflags *df;
1760 for (df = DaemonFlags; df->d_name != NULL; df++)
1762 if (!bitnset(df->d_flag, d->d_flags))
1765 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, "<%s",
1768 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, ",%s",
1773 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, ">");
1780 if (NDaemons >= MAXDAEMONS)
1782 Daemons[NDaemons].d_socket = -1;
1783 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1784 clrbitmap(Daemons[NDaemons].d_flags);
1785 setsockaddroptions(p, &Daemons[NDaemons]);
1788 # if _FFR_MILTER_PERDAEMON
1789 if (Daemons[NDaemons].d_inputfilterlist != NULL)
1790 Daemons[NDaemons].d_inputfilterlist = newstr(Daemons[NDaemons].d_inputfilterlist);
1791 # endif /* _FFR_MILTER_PERDAEMON */
1794 if (Daemons[NDaemons].d_name != NULL)
1795 Daemons[NDaemons].d_name = newstr(Daemons[NDaemons].d_name);
1800 (void) sm_snprintf(num, sizeof num, "Daemon%d", NDaemons);
1801 Daemons[NDaemons].d_name = newstr(num);
1806 sm_dprintf("Daemon %s flags: ", Daemons[NDaemons].d_name);
1807 printdaemonflags(&Daemons[NDaemons]);
1814 ** INITDAEMON -- initialize daemon if not yet done.
1823 ** initializes structure for one daemon.
1831 Daemons[NDaemons].d_socket = -1;
1832 Daemons[NDaemons].d_listenqueue = DEF_LISTENQUEUE;
1833 Daemons[NDaemons].d_name = "Daemon0";
1838 ** SETCLIENTOPTIONS -- set options for running the client
1841 ** p -- the options line.
1847 static DAEMON_T ClientSettings[AF_MAX + 1];
1856 memset(&d, '\0', sizeof d);
1857 setsockaddroptions(p, &d);
1859 /* grab what we need */
1860 family = d.d_addr.sa.sa_family;
1861 STRUCTCOPY(d, ClientSettings[family]);
1862 setbitn(D_ISSET, ClientSettings[family].d_flags); /* mark as set */
1863 if (d.d_name != NULL)
1864 ClientSettings[family].d_name = newstr(d.d_name);
1869 (void) sm_snprintf(num, sizeof num, "Client%d", family);
1870 ClientSettings[family].d_name = newstr(num);
1874 ** ADDR_FAMILY -- determine address family from address
1877 ** addr -- the string representation of the address
1880 ** AF_INET, AF_INET6 or AF_UNSPEC
1892 #endif /* NETINET6 */
1895 if (inet_addr(addr) != INADDR_NONE)
1898 sm_dprintf("addr_family(%s): INET\n", addr);
1901 #endif /* NETINET */
1903 if (anynet_pton(AF_INET6, addr, &clt_addr.sin6.sin6_addr) == 1)
1906 sm_dprintf("addr_family(%s): INET6\n", addr);
1909 #endif /* NETINET6 */
1910 #if _FFR_DAEMON_NETUNIX
1915 sm_dprintf("addr_family(%s): LOCAL\n", addr);
1918 # endif /* NETUNIX */
1919 #endif /* _FFR_DAEMON_NETUNIX */
1921 sm_dprintf("addr_family(%s): UNSPEC\n", addr);
1926 ** CHKCLIENTMODIFIERS -- check whether all clients have set a flag.
1929 ** flag -- the flag to test.
1932 ** true iff all configured clients have set the flag.
1936 chkclientmodifiers(flag)
1943 for (i = 0; i < AF_MAX; i++)
1945 if (bitnset(D_ISSET, ClientSettings[i].d_flags))
1947 if (!bitnset((char) flag, ClientSettings[i].d_flags))
1956 # if _FFR_MILTER_PERDAEMON
1958 ** SETUP_DAEMON_FILTERS -- Parse per-socket filters
1968 setup_daemon_milters()
1972 if (OpMode == MD_SMTP)
1974 /* no need to configure the daemons */
1978 for (idx = 0; idx < NDaemons; idx++)
1980 if (Daemons[idx].d_inputfilterlist != NULL)
1982 milter_config(Daemons[idx].d_inputfilterlist,
1983 Daemons[idx].d_inputfilters,
1988 # endif /* _FFR_MILTER_PERDAEMON */
1991 ** MAKECONNECTION -- make a connection to an SMTP socket on a machine.
1994 ** host -- the name of the host.
1995 ** port -- the port number to connect to.
1996 ** mci -- a pointer to the mail connection information
1997 ** structure to be filled in.
1998 ** e -- the current envelope.
1999 ** enough -- time at which to stop further connection attempts.
2000 ** (0 means no limit)
2003 ** An exit code telling whether the connection could be
2004 ** made and if not why not.
2010 static jmp_buf CtxConnectTimeout;
2012 SOCKADDR CurHostAddr; /* address of current host */
2015 makeconnection(host, port, mci, e, enough)
2017 volatile unsigned int port;
2022 register volatile int addrno = 0;
2024 register struct hostent *volatile hp = (struct hostent *) NULL;
2028 volatile SOCKADDR_LEN_T addrlen;
2029 volatile bool firstconnect;
2030 SM_EVENT *volatile ev = NULL;
2032 volatile bool v6found = false;
2033 #endif /* NETINET6 */
2034 volatile int family = InetMode;
2036 volatile SOCKADDR_LEN_T socksize = 0;
2037 volatile bool clt_bind;
2040 extern ENVELOPE BlankEnvelope;
2042 /* retranslate {daemon_flags} into bitmap */
2044 if ((p = macvalue(macid("{daemon_flags}"), e)) != NULL)
2046 for (; *p != '\0'; p++)
2048 if (!(isascii(*p) && isspace(*p)))
2049 setbitn(bitidx(*p), d_flags);
2055 #endif /* NETINET6 */
2058 /* Set up the address for outgoing connection. */
2059 if (bitnset(D_BINDIF, d_flags) &&
2060 (p = macvalue(macid("{if_addr}"), e)) != NULL &&
2064 char p6[INET6_ADDRSTRLEN];
2065 #endif /* NETINET6 */
2067 memset(&clt_addr, '\0', sizeof clt_addr);
2069 /* infer the address family from the address itself */
2070 clt_addr.sa.sa_family = addr_family(p);
2071 switch (clt_addr.sa.sa_family)
2075 clt_addr.sin.sin_addr.s_addr = inet_addr(p);
2076 if (clt_addr.sin.sin_addr.s_addr != INADDR_NONE &&
2077 clt_addr.sin.sin_addr.s_addr != INADDR_LOOPBACK)
2080 socksize = sizeof (struct sockaddr_in);
2083 #endif /* NETINET */
2087 if (inet_addr(p) != INADDR_NONE)
2088 (void) sm_snprintf(p6, sizeof p6,
2089 "IPv6:::ffff:%s", p);
2091 (void) sm_strlcpy(p6, p, sizeof p6);
2092 if (anynet_pton(AF_INET6, p6,
2093 &clt_addr.sin6.sin6_addr) == 1 &&
2094 !IN6_IS_ADDR_LOOPBACK(&clt_addr.sin6.sin6_addr))
2097 socksize = sizeof (struct sockaddr_in6);
2100 #endif /* NETINET6 */
2104 syserr("554 5.3.5 Address= option unsupported for family %d",
2105 clt_addr.sa.sa_family);
2110 family = clt_addr.sa.sa_family;
2113 /* D_BINDIF not set or not available, fallback to ClientPortOptions */
2116 STRUCTCOPY(ClientSettings[family].d_addr, clt_addr);
2117 switch (clt_addr.sa.sa_family)
2121 if (clt_addr.sin.sin_addr.s_addr == 0)
2122 clt_addr.sin.sin_addr.s_addr = INADDR_ANY;
2125 if (clt_addr.sin.sin_port != 0)
2127 socksize = sizeof (struct sockaddr_in);
2129 #endif /* NETINET */
2132 if (IN6_IS_ADDR_UNSPECIFIED(&clt_addr.sin6.sin6_addr))
2133 clt_addr.sin6.sin6_addr = in6addr_any;
2136 socksize = sizeof (struct sockaddr_in6);
2137 if (clt_addr.sin6.sin6_port != 0)
2140 #endif /* NETINET6 */
2143 socksize = sizeof clt_addr.siso;
2153 ** Set up the address for the mailer.
2154 ** Accept "[a.b.c.d]" syntax for host name.
2159 memset(&CurHostAddr, '\0', sizeof CurHostAddr);
2160 memset(&addr, '\0', sizeof addr);
2161 SmtpPhase = mci->mci_phase = "initial connection";
2166 p = strchr(host, ']');
2170 unsigned long hid = INADDR_NONE;
2171 #endif /* NETINET */
2173 struct sockaddr_in6 hid6;
2174 #endif /* NETINET6 */
2178 memset(&hid6, '\0', sizeof hid6);
2179 #endif /* NETINET6 */
2181 if (family == AF_INET &&
2182 (hid = inet_addr(&host[1])) != INADDR_NONE)
2184 addr.sin.sin_family = AF_INET;
2185 addr.sin.sin_addr.s_addr = hid;
2188 #endif /* NETINET */
2190 if (family == AF_INET6 &&
2191 anynet_pton(AF_INET6, &host[1],
2192 &hid6.sin6_addr) == 1)
2194 addr.sin6.sin6_family = AF_INET6;
2195 addr.sin6.sin6_addr = hid6.sin6_addr;
2198 #endif /* NETINET6 */
2200 /* try it as a host name (avoid MX lookup) */
2201 hp = sm_gethostbyname(&host[1], family);
2202 if (hp == NULL && p[-1] == '.')
2205 int oldopts = _res.options;
2207 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2208 #endif /* NAMED_BIND */
2210 hp = sm_gethostbyname(&host[1],
2214 _res.options = oldopts;
2215 #endif /* NAMED_BIND */
2224 extern char MsgBuf[];
2227 "553 Invalid numeric domain spec \"%s\"",
2229 mci_setstat(mci, EX_NOHOST, "5.1.2", MsgBuf);
2236 /* contortion to get around SGI cc complaints */
2238 p = &host[strlen(host) - 1];
2239 hp = sm_gethostbyname(host, family);
2240 if (hp == NULL && *p == '.')
2243 int oldopts = _res.options;
2245 _res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
2246 #endif /* NAMED_BIND */
2248 hp = sm_gethostbyname(host, family);
2251 _res.options = oldopts;
2252 #endif /* NAMED_BIND */
2259 /* check for name server timeouts */
2261 if (WorkAroundBrokenAAAA && family == AF_INET6 &&
2265 ** An attempt with family AF_INET may
2266 ** succeed By skipping the next section
2267 ** of code, we will try AF_INET before
2272 sm_dprintf("makeconnection: WorkAroundBrokenAAAA: Trying AF_INET lookup (AF_INET6 failed)\n");
2275 # endif /* NETINET6 */
2277 if (errno == ETIMEDOUT ||
2278 h_errno == TRY_AGAIN ||
2279 (errno == ECONNREFUSED && UseNameServer))
2282 mci_setstat(mci, EX_TEMPFAIL,
2288 #endif /* NAMED_BIND */
2291 ** Try v6 first, then fall back to v4.
2292 ** If we found a v6 address, but no v4
2293 ** addresses, then TEMPFAIL.
2296 if (family == AF_INET6)
2303 #endif /* NETINET6 */
2305 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2309 addr.sa.sa_family = hp->h_addrtype;
2310 switch (hp->h_addrtype)
2314 memmove(&addr.sin.sin_addr,
2318 #endif /* NETINET */
2322 memmove(&addr.sin6.sin6_addr,
2326 #endif /* NETINET6 */
2329 if (hp->h_length > sizeof addr.sa.sa_data)
2331 syserr("makeconnection: long sa_data: family %d len %d",
2332 hp->h_addrtype, hp->h_length);
2333 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2337 memmove(addr.sa.sa_data, hp->h_addr, hp->h_length);
2344 ** Determine the port number.
2349 #ifdef NO_GETSERVBYNAME
2351 #else /* NO_GETSERVBYNAME */
2352 register struct servent *sp = getservbyname("smtp", "tcp");
2357 sm_syslog(LOG_ERR, NOQID,
2358 "makeconnection: service \"smtp\" unknown");
2363 #endif /* NO_GETSERVBYNAME */
2367 if (addr.sa.sa_family == AF_INET6 &&
2368 IN6_IS_ADDR_V4MAPPED(&addr.sin6.sin6_addr) &&
2369 ClientSettings[AF_INET].d_addr.sa.sa_family != 0)
2372 ** Ignore mapped IPv4 address since
2373 ** there is a ClientPortOptions setting
2379 #endif /* NETINET6 */
2381 switch (addr.sa.sa_family)
2385 addr.sin.sin_port = port;
2386 addrlen = sizeof (struct sockaddr_in);
2388 #endif /* NETINET */
2392 addr.sin6.sin6_port = port;
2393 addrlen = sizeof (struct sockaddr_in6);
2395 #endif /* NETINET6 */
2399 /* assume two byte transport selector */
2400 memmove(TSEL((struct sockaddr_iso *) &addr), (char *) &port, 2);
2401 addrlen = sizeof (struct sockaddr_iso);
2406 syserr("Can't connect to address family %d", addr.sa.sa_family);
2407 mci_setstat(mci, EX_NOHOST, "5.1.2", NULL);
2412 #endif /* NETINET6 */
2417 ** Try to actually open the connection.
2421 /* if too many connections, don't bother trying */
2422 if (!xla_noqueue_ok(host))
2427 # endif /* NETINET6 */
2432 firstconnect = true;
2436 sm_dprintf("makeconnection (%s [%s].%d (%d))\n",
2437 host, anynet_ntoa(&addr), ntohs(port),
2438 (int) addr.sa.sa_family);
2440 /* save for logging */
2444 if (bitnset(M_SECURE_PORT, mci->mci_mailer->m_flags))
2446 int rport = IPPORT_RESERVED - 1;
2448 s = rresvport(&rport);
2451 #endif /* HASRRESVPORT */
2453 s = socket(addr.sa.sa_family, SOCK_STREAM, 0);
2458 syserr("makeconnection: cannot create socket");
2462 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2466 #endif /* NETINET6 */
2472 if (ClientSettings[family].d_tcpsndbufsize > 0)
2474 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
2475 (char *) &ClientSettings[family].d_tcpsndbufsize,
2476 sizeof(ClientSettings[family].d_tcpsndbufsize)) < 0)
2477 syserr("makeconnection: setsockopt(SO_SNDBUF)");
2479 #endif /* SO_SNDBUF */
2481 if (ClientSettings[family].d_tcprcvbufsize > 0)
2483 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
2484 (char *) &ClientSettings[family].d_tcprcvbufsize,
2485 sizeof(ClientSettings[family].d_tcprcvbufsize)) < 0)
2486 syserr("makeconnection: setsockopt(SO_RCVBUF)");
2488 #endif /* SO_RCVBUF */
2491 sm_dprintf("makeconnection: fd=%d\n", s);
2493 /* turn on network debugging? */
2498 (void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
2499 (char *)&on, sizeof on);
2501 if (e->e_xfp != NULL) /* for debugging */
2502 (void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
2503 errno = 0; /* for debugging */
2509 switch (clt_addr.sa.sa_family)
2513 if (clt_addr.sin.sin_port != 0)
2514 (void) setsockopt(s, SOL_SOCKET,
2519 #endif /* NETINET */
2523 if (clt_addr.sin6.sin6_port != 0)
2524 (void) setsockopt(s, SOL_SOCKET,
2529 #endif /* NETINET6 */
2532 if (bind(s, &clt_addr.sa, socksize) < 0)
2537 syserr("makeconnection: cannot bind socket [%s]",
2538 anynet_ntoa(&clt_addr));
2542 #endif /* NETINET6 */
2549 ** Linux seems to hang in connect for 90 minutes (!!!).
2550 ** Time out the connect to avoid this problem.
2553 if (setjmp(CtxConnectTimeout) == 0)
2557 if (e->e_ntries <= 0 && TimeOuts.to_iconnect != 0)
2558 ev = sm_setevent(TimeOuts.to_iconnect,
2560 else if (TimeOuts.to_connect != 0)
2561 ev = sm_setevent(TimeOuts.to_connect,
2566 switch (ConnectOnlyTo.sa.sa_family)
2570 addr.sin.sin_addr.s_addr = ConnectOnlyTo.sin.sin_addr.s_addr;
2572 #endif /* NETINET */
2576 memmove(&addr.sin6.sin6_addr,
2577 &ConnectOnlyTo.sin6.sin6_addr,
2580 #endif /* NETINET6 */
2582 i = connect(s, (struct sockaddr *) &addr, addrlen);
2592 /* couldn't connect.... figure out why */
2595 /* if running demand-dialed connection, try again */
2596 if (DialDelay > 0 && firstconnect &&
2597 bitnset(M_DIALDELAY, mci->mci_mailer->m_flags))
2600 sm_dprintf("Connect failed (%s); trying again...\n",
2601 sm_errstring(save_errno));
2602 firstconnect = false;
2603 (void) sleep(DialDelay);
2608 sm_syslog(LOG_INFO, e->e_id,
2609 "makeconnection (%s [%s]) failed: %s",
2610 host, anynet_ntoa(&addr),
2611 sm_errstring(save_errno));
2615 #endif /* NETINET6 */
2616 if (hp != NULL && hp->h_addr_list[addrno] != NULL &&
2617 (enough == 0 || curtime() < enough))
2620 sm_dprintf("Connect failed (%s); trying new address....\n",
2621 sm_errstring(save_errno));
2622 switch (addr.sa.sa_family)
2626 memmove(&addr.sin.sin_addr,
2627 hp->h_addr_list[addrno++],
2630 #endif /* NETINET */
2634 memmove(&addr.sin6.sin6_addr,
2635 hp->h_addr_list[addrno++],
2638 #endif /* NETINET6 */
2641 memmove(addr.sa.sa_data,
2642 hp->h_addr_list[addrno++],
2651 if (family == AF_INET6)
2654 sm_dprintf("Connect failed (%s); retrying with AF_INET....\n",
2655 sm_errstring(save_errno));
2666 #endif /* NETINET6 */
2667 /* couldn't open connection */
2669 /* Don't clobber an already saved errno from v4retry */
2671 #endif /* NETINET6 */
2674 sm_dprintf("Connect failed (%s)\n",
2675 sm_errstring(save_errno));
2679 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2683 #endif /* NETINET6 */
2694 #endif /* NETINET6 */
2696 /* connection ok, put it into canonical form */
2697 mci->mci_out = NULL;
2698 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2700 SM_IO_WRONLY, NULL)) == NULL ||
2702 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2704 SM_IO_RDONLY, NULL)) == NULL)
2707 syserr("cannot open SMTP client channel, fd=%d", s);
2708 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2709 if (mci->mci_out != NULL)
2710 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2715 sm_io_automode(mci->mci_out, mci->mci_in);
2717 /* set {client_flags} */
2718 if (ClientSettings[addr.sa.sa_family].d_mflags != NULL)
2720 macdefine(&mci->mci_macro, A_PERM,
2721 macid("{client_flags}"),
2722 ClientSettings[addr.sa.sa_family].d_mflags);
2725 macdefine(&mci->mci_macro, A_PERM,
2726 macid("{client_flags}"), "");
2728 /* "add" {client_flags} to bitmap */
2729 if (bitnset(D_IFNHELO, ClientSettings[addr.sa.sa_family].d_flags))
2731 /* look for just this one flag */
2732 setbitn(D_IFNHELO, d_flags);
2735 /* find out name for Interface through which we connect */
2737 if (getsockname(s, &addr.sa, &len) == 0)
2742 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2743 macid("{if_addr_out}"), anynet_ntoa(&addr));
2744 (void) sm_snprintf(family, sizeof(family), "%d",
2746 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2747 macid("{if_family_out}"), family);
2749 name = hostnamebyanyaddr(&addr);
2750 macdefine(&BlankEnvelope.e_macro, A_TEMP,
2751 macid("{if_name_out}"), name);
2754 /* log connection information */
2755 sm_syslog(LOG_INFO, e->e_id,
2756 "SMTP outgoing connect on %.40s", name);
2758 if (bitnset(D_IFNHELO, d_flags))
2760 if (name[0] != '[' && strchr(name, '.') != NULL)
2761 mci->mci_heloname = newstr(name);
2766 macdefine(&BlankEnvelope.e_macro, A_PERM,
2767 macid("{if_name_out}"), NULL);
2768 macdefine(&BlankEnvelope.e_macro, A_PERM,
2769 macid("{if_addr_out}"), NULL);
2770 macdefine(&BlankEnvelope.e_macro, A_PERM,
2771 macid("{if_family_out}"), NULL);
2773 mci_setstat(mci, EX_OK, NULL, NULL);
2781 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
2782 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
2787 longjmp(CtxConnectTimeout, 1);
2790 ** MAKECONNECTION_DS -- make a connection to a domain socket.
2793 ** mux_path -- the path of the socket to connect to.
2794 ** mci -- a pointer to the mail connection information
2795 ** structure to be filled in.
2798 ** An exit code telling whether the connection could be
2799 ** made and if not why not.
2807 makeconnection_ds(mux_path, mci)
2812 int rval, save_errno;
2813 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK;
2814 struct sockaddr_un unix_addr;
2816 /* if not safe, don't connect */
2817 rval = safefile(mux_path, RunAsUid, RunAsGid, RunAsUserName,
2818 sff, S_IRUSR|S_IWUSR, NULL);
2822 syserr("makeconnection_ds: unsafe domain socket");
2823 mci_setstat(mci, EX_TEMPFAIL, "4.3.5", NULL);
2828 /* prepare address structure */
2829 memset(&unix_addr, '\0', sizeof unix_addr);
2830 unix_addr.sun_family = AF_UNIX;
2832 if (strlen(mux_path) >= sizeof unix_addr.sun_path)
2834 syserr("makeconnection_ds: domain socket name too long");
2836 /* XXX why TEMPFAIL but 5.x.y ? */
2837 mci_setstat(mci, EX_TEMPFAIL, "5.3.5", NULL);
2838 errno = ENAMETOOLONG;
2839 return EX_UNAVAILABLE;
2841 (void) sm_strlcpy(unix_addr.sun_path, mux_path,
2842 sizeof unix_addr.sun_path);
2844 /* initialize domain socket */
2845 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2849 syserr("makeconnection_ds: could not create domain socket");
2850 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2855 /* connect to server */
2856 if (connect(sock, (struct sockaddr *) &unix_addr,
2857 sizeof(unix_addr)) == -1)
2860 syserr("Could not connect to socket %s", mux_path);
2861 mci_setstat(mci, EX_TEMPFAIL, "4.4.1", NULL);
2867 /* connection ok, put it into canonical form */
2868 mci->mci_out = NULL;
2869 if ((mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2870 (void *) &sock, SM_IO_WRONLY, NULL))
2872 || (sock = dup(sock)) < 0 ||
2873 (mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
2874 (void *) &sock, SM_IO_RDONLY, NULL))
2878 syserr("cannot open SMTP client channel, fd=%d", sock);
2879 mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
2880 if (mci->mci_out != NULL)
2881 (void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
2886 sm_io_automode(mci->mci_out, mci->mci_in);
2888 mci_setstat(mci, EX_OK, NULL, NULL);
2892 #endif /* NETUNIX */
2894 ** SHUTDOWN_DAEMON -- Performs a clean shutdown of the daemon
2903 ** closes control socket, exits.
2912 sm_allsignals(true);
2914 reason = ShutdownRequest;
2915 ShutdownRequest = NULL;
2919 sm_syslog(LOG_DEBUG, CurEnv->e_id, "interrupt (%s)",
2920 reason == NULL ? "implicit call" : reason);
2923 closecontrolsocket(true);
2928 for (i = 0; i < NDaemons; i++)
2930 if (Daemons[i].d_socket >= 0)
2932 (void) close(Daemons[i].d_socket);
2933 Daemons[i].d_socket = -1;
2935 #if _FFR_DAEMON_NETUNIX
2937 /* Remove named sockets */
2938 if (Daemons[i].d_addr.sa.sa_family == AF_UNIX)
2941 long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_MUSTOWN|SFF_EXECOK|SFF_CREAT;
2943 /* if not safe, don't use it */
2944 rval = safefile(Daemons[i].d_addr.sunix.sun_path,
2947 S_IRUSR|S_IWUSR, NULL);
2949 unlink(Daemons[i].d_addr.sunix.sun_path) < 0)
2951 sm_syslog(LOG_WARNING, NOQID,
2952 "Could not remove daemon %s socket: %s: %s",
2954 Daemons[i].d_addr.sunix.sun_path,
2955 sm_errstring(errno));
2958 # endif /* NETUNIX */
2959 #endif /* _FFR_DAEMON_NETUNIX */
2963 finis(false, true, EX_OK);
2966 ** RESTART_DAEMON -- Performs a clean restart of the daemon
2975 ** restarts the daemon or exits if restart fails.
2978 /* Make a non-DFL/IGN signal a noop */
2979 #define SM_NOOP_SIGNAL(sig, old) \
2982 (old) = sm_signal((sig), sm_signal_noop); \
2983 if ((old) == SIG_IGN || (old) == SIG_DFL) \
2984 (void) sm_signal((sig), (old)); \
2994 sigfunc_t ignore, oalrm, ousr1;
2995 extern int DtableSize;
2997 /* clear the events to turn off SIGALRMs */
2999 sm_allsignals(true);
3001 reason = RestartRequest;
3002 RestartRequest = NULL;
3005 if (SaveArgv[0][0] != '/')
3008 sm_syslog(LOG_INFO, NOQID,
3009 "could not restart: need full path");
3010 finis(false, true, EX_OSFILE);
3014 sm_syslog(LOG_INFO, NOQID, "restarting %s due to %s",
3016 reason == NULL ? "implicit call" : reason);
3018 closecontrolsocket(true);
3020 cleanup_shm(DaemonPid == getpid());
3021 #endif /* SM_CONF_SHM */
3024 ** Want to drop to the user who started the process in all cases
3025 ** *but* when running as "smmsp" for the clientmqueue queue run
3026 ** daemon. In that case, UseMSP will be true, RunAsUid should not
3027 ** be root, and RealUid should be either 0 or RunAsUid.
3030 drop = !(UseMSP && RunAsUid != 0 &&
3031 (RealUid == 0 || RealUid == RunAsUid));
3033 if (drop_privileges(drop) != EX_OK)
3036 sm_syslog(LOG_ALERT, NOQID,
3037 "could not drop privileges: %s",
3038 sm_errstring(errno));
3039 finis(false, true, EX_OSERR);
3043 /* arrange for all the files to be closed */
3044 for (i = 3; i < DtableSize; i++)
3048 if ((j = fcntl(i, F_GETFD, 0)) != -1)
3049 (void) fcntl(i, F_SETFD, j | FD_CLOEXEC);
3053 ** Need to allow signals before execve() to make them "harmless".
3054 ** However, the default action can be "terminate", so it isn't
3055 ** really harmless. Setting signals to IGN will cause them to be
3056 ** ignored in the new process to, so that isn't a good alternative.
3059 SM_NOOP_SIGNAL(SIGALRM, oalrm);
3060 SM_NOOP_SIGNAL(SIGCHLD, ignore);
3061 SM_NOOP_SIGNAL(SIGHUP, ignore);
3062 SM_NOOP_SIGNAL(SIGINT, ignore);
3063 SM_NOOP_SIGNAL(SIGPIPE, ignore);
3064 SM_NOOP_SIGNAL(SIGTERM, ignore);
3066 SM_NOOP_SIGNAL(SIGUSR1, ousr1);
3067 #endif /* SIGUSR1 */
3069 /* Turn back on signals */
3070 sm_allsignals(false);
3072 (void) execve(SaveArgv[0], (ARGV_T) SaveArgv, (ARGV_T) ExternalEnviron);
3075 /* block signals again and restore needed signals */
3076 sm_allsignals(true);
3078 /* For finis() events */
3079 (void) sm_signal(SIGALRM, oalrm);
3082 /* For debugging finis() */
3083 (void) sm_signal(SIGUSR1, ousr1);
3084 #endif /* SIGUSR1 */
3088 sm_syslog(LOG_ALERT, NOQID, "could not exec %s: %s",
3089 SaveArgv[0], sm_errstring(errno));
3090 finis(false, true, EX_OSFILE);
3094 ** MYHOSTNAME -- return the name of this host.
3097 ** hostbuf -- a place to return the name of this host.
3098 ** size -- the size of hostbuf.
3101 ** A list of aliases for this host.
3104 ** Adds numeric codes to $=w.
3108 myhostname(hostbuf, size)
3112 register struct hostent *hp;
3114 if (gethostname(hostbuf, size) < 0 || hostbuf[0] == '\0')
3115 (void) sm_strlcpy(hostbuf, "localhost", size);
3116 hp = sm_gethostbyname(hostbuf, InetMode);
3117 #if NETINET && NETINET6
3118 if (hp == NULL && InetMode == AF_INET6)
3121 ** It's possible that this IPv6 enabled machine doesn't
3122 ** actually have any IPv6 interfaces and, therefore, no
3123 ** IPv6 addresses. Fall back to AF_INET.
3126 hp = sm_gethostbyname(hostbuf, AF_INET);
3128 #endif /* NETINET && NETINET6 */
3131 if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
3132 (void) cleanstrcpy(hostbuf, hp->h_name, size);
3135 if (strchr(hostbuf, '.') == NULL)
3139 domainname = ni_propval("/locations", NULL, "resolver",
3141 if (domainname != NULL &&
3142 strlen(domainname) + strlen(hostbuf) + 1 < size)
3143 (void) sm_strlcat2(hostbuf, ".", domainname, size);
3145 #endif /* NETINFO */
3148 ** If there is still no dot in the name, try looking for a
3152 if (strchr(hostbuf, '.') == NULL)
3156 for (ha = hp->h_aliases; ha != NULL && *ha != NULL; ha++)
3158 if (strchr(*ha, '.') != NULL)
3160 (void) cleanstrcpy(hostbuf, *ha, size - 1);
3161 hostbuf[size - 1] = '\0';
3168 ** If _still_ no dot, wait for a while and try again -- it is
3169 ** possible that some service is starting up. This can result
3170 ** in excessive delays if the system is badly configured, but
3171 ** there really isn't a way around that, particularly given that
3172 ** the config file hasn't been read at this point.
3173 ** All in all, a bit of a mess.
3176 if (strchr(hostbuf, '.') == NULL &&
3177 !getcanonname(hostbuf, size, true, NULL))
3179 sm_syslog(LOG_CRIT, NOQID,
3180 "My unqualified host name (%s) unknown; sleeping for retry",
3182 message("My unqualified host name (%s) unknown; sleeping for retry",
3185 if (!getcanonname(hostbuf, size, true, NULL))
3187 sm_syslog(LOG_ALERT, NOQID,
3188 "unable to qualify my own domain name (%s) -- using short name",
3190 message("WARNING: unable to qualify my own domain name (%s) -- using short name",
3197 ** ADDRCMP -- compare two host addresses
3200 ** hp -- hostent structure for the first address
3201 ** ha -- actual first address
3202 ** sa -- second address
3205 ** 0 -- if ha and sa match
3206 ** else -- they don't match
3217 #endif /* NETINET6 */
3219 switch (sa->sa.sa_family)
3223 if (hp->h_addrtype == AF_INET)
3224 return memcmp(ha, (char *) &sa->sin.sin_addr, INADDRSZ);
3226 #endif /* NETINET */
3230 a = (unsigned char *) &sa->sin6.sin6_addr;
3232 /* Straight binary comparison */
3233 if (hp->h_addrtype == AF_INET6)
3234 return memcmp(ha, a, IN6ADDRSZ);
3236 /* If IPv4-mapped IPv6 address, compare the IPv4 section */
3237 if (hp->h_addrtype == AF_INET &&
3238 IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr))
3239 return memcmp(a + IN6ADDRSZ - INADDRSZ, ha, INADDRSZ);
3241 #endif /* NETINET6 */
3246 ** GETAUTHINFO -- get the real host name associated with a file descriptor
3248 ** Uses RFC1413 protocol to try to get info from the other end.
3251 ** fd -- the descriptor
3252 ** may_be_forged -- an outage that is set to true if the
3253 ** forward lookup of RealHostName does not match
3254 ** RealHostAddr; set to false if they do match.
3257 ** The user@host information associated with this descriptor.
3260 static jmp_buf CtxAuthTimeout;
3266 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
3267 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
3272 longjmp(CtxAuthTimeout, 1);
3276 getauthinfo(fd, may_be_forged)
3278 bool *may_be_forged;
3280 unsigned short SM_NONVOLATILE port = 0;
3281 SOCKADDR_LEN_T falen;
3282 register char *volatile p = NULL;
3284 SOCKADDR_LEN_T lalen;
3285 #ifndef NO_GETSERVBYNAME
3286 register struct servent *sp;
3288 static unsigned short port4 = 0;
3289 # endif /* NETINET */
3291 static unsigned short port6 = 0;
3292 # endif /* NETINET6 */
3293 #endif /* ! NO_GETSERVBYNAME */
3300 char *ostype = NULL;
3302 char ibuf[MAXNAME + 1];
3303 static char hbuf[MAXNAME + MAXAUTHINFO + 11];
3305 *may_be_forged = false;
3306 falen = sizeof RealHostAddr;
3307 if (isatty(fd) || (i = getpeername(fd, &RealHostAddr.sa, &falen)) < 0 ||
3308 falen <= 0 || RealHostAddr.sa.sa_family == 0)
3313 ** ENOTSOCK is OK: bail on anything else, but reset
3314 ** errno in this case, so a mis-report doesn't
3318 if (errno != ENOTSOCK)
3322 (void) sm_strlcpyn(hbuf, sizeof hbuf, 2, RealUserName,
3325 sm_dprintf("getauthinfo: %s\n", hbuf);
3329 if (RealHostName == NULL)
3331 /* translate that to a host name */
3332 RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
3333 if (strlen(RealHostName) > MAXNAME)
3334 RealHostName[MAXNAME] = '\0'; /* XXX - 1 ? */
3337 /* cross check RealHostName with forward DNS lookup */
3338 if (anynet_ntoa(&RealHostAddr)[0] != '[' &&
3339 RealHostName[0] != '[')
3343 family = RealHostAddr.sa.sa_family;
3344 #if NETINET6 && NEEDSGETIPNODE
3346 ** If RealHostAddr is an IPv6 connection with an
3347 ** IPv4-mapped address, we need RealHostName's IPv4
3348 ** address(es) for addrcmp() to compare against
3351 ** Actually, we only need to do this for systems
3352 ** which NEEDSGETIPNODE since the real getipnodebyname()
3353 ** already does V4MAPPED address via the AI_V4MAPPEDCFG
3354 ** flag. A better fix to this problem is to add this
3355 ** functionality to our stub getipnodebyname().
3358 if (family == AF_INET6 &&
3359 IN6_IS_ADDR_V4MAPPED(&RealHostAddr.sin6.sin6_addr))
3361 #endif /* NETINET6 && NEEDSGETIPNODE */
3363 /* try to match the reverse against the forward lookup */
3364 hp = sm_gethostbyname(RealHostName, family);
3366 *may_be_forged = true;
3369 for (ha = hp->h_addr_list; *ha != NULL; ha++)
3371 if (addrcmp(hp, *ha, &RealHostAddr) == 0)
3374 *may_be_forged = *ha == NULL;
3378 #endif /* NETINET6 */
3382 if (TimeOuts.to_ident == 0)
3386 switch (RealHostAddr.sa.sa_family)
3390 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3392 la.sa.sa_family != AF_INET)
3397 port = RealHostAddr.sin.sin_port;
3399 /* create ident query */
3400 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3401 ntohs(RealHostAddr.sin.sin_port),
3402 ntohs(la.sin.sin_port));
3404 /* create local address */
3405 la.sin.sin_port = 0;
3407 /* create foreign address */
3408 # ifdef NO_GETSERVBYNAME
3409 RealHostAddr.sin.sin_port = htons(113);
3410 # else /* NO_GETSERVBYNAME */
3413 ** getservbyname() consumes about 5% of the time
3414 ** when receiving a small message (almost all of the time
3415 ** spent in this routine).
3416 ** Hence we store the port in a static variable
3417 ** to save this time.
3418 ** The portnumber shouldn't change very often...
3419 ** This code makes the assumption that the port number
3425 sp = getservbyname("auth", "tcp");
3431 RealHostAddr.sin.sin_port = port4;
3433 # endif /* NO_GETSERVBYNAME */
3434 #endif /* NETINET */
3438 if (getsockname(fd, &la.sa, &lalen) < 0 ||
3440 la.sa.sa_family != AF_INET6)
3445 port = RealHostAddr.sin6.sin6_port;
3447 /* create ident query */
3448 (void) sm_snprintf(ibuf, sizeof ibuf, "%d,%d\r\n",
3449 ntohs(RealHostAddr.sin6.sin6_port),
3450 ntohs(la.sin6.sin6_port));
3452 /* create local address */
3453 la.sin6.sin6_port = 0;
3455 /* create foreign address */
3456 # ifdef NO_GETSERVBYNAME
3457 RealHostAddr.sin6.sin6_port = htons(113);
3458 # else /* NO_GETSERVBYNAME */
3461 sp = getservbyname("auth", "tcp");
3467 RealHostAddr.sin6.sin6_port = port6;
3469 # endif /* NO_GETSERVBYNAME */
3470 #endif /* NETINET6 */
3477 if (setjmp(CtxAuthTimeout) != 0)
3484 /* put a timeout around the whole thing */
3485 ev = sm_setevent(TimeOuts.to_ident, authtimeout, 0);
3487 /* connect to foreign IDENT server using same address as SMTP socket */
3488 s = socket(la.sa.sa_family, SOCK_STREAM, 0);
3494 if (bind(s, &la.sa, lalen) < 0 ||
3495 connect(s, &RealHostAddr.sa, lalen) < 0)
3499 sm_dprintf("getauthinfo: sent %s", ibuf);
3502 if (write(s, ibuf, strlen(ibuf)) < 0)
3507 nleft = sizeof ibuf - 1;
3508 while ((i = read(s, p, nleft)) > 0)
3513 if (strchr(ibuf, '\n') != NULL || nleft <= 0)
3518 if (i < 0 || p == &ibuf[0])
3521 if (p >= &ibuf[2] && *--p == '\n' && *--p == '\r')
3526 sm_dprintf("getauthinfo: got %s\n", ibuf);
3529 p = strchr(ibuf, ':');
3532 /* malformed response */
3535 while (isascii(*++p) && isspace(*p))
3537 if (sm_strncasecmp(p, "userid", 6) != 0)
3539 /* presumably an error string */
3543 while (isascii(*p) && isspace(*p))
3547 /* either useridxx or malformed response */
3551 /* p now points to the OSTYPE field */
3552 while (isascii(*p) && isspace(*p))
3558 /* malformed response */
3566 charset = strchr(ostype, ',');
3567 if (charset != NULL)
3571 /* 1413 says don't do this -- but it's broken otherwise */
3572 while (isascii(*++p) && isspace(*p))
3575 /* p now points to the authenticated name -- copy carefully */
3576 if (sm_strncasecmp(ostype, "other", 5) == 0 &&
3577 (ostype[5] == ' ' || ostype[5] == '\0'))
3579 (void) sm_strlcpy(hbuf, "IDENT:", sizeof hbuf);
3580 cleanstrcpy(&hbuf[6], p, MAXAUTHINFO);
3583 cleanstrcpy(hbuf, p, MAXAUTHINFO);
3585 (void) sm_strlcpyn(&hbuf[len], sizeof hbuf - len, 2, "@",
3586 RealHostName == NULL ? "localhost" : RealHostName);
3594 /* put back the original incoming port */
3595 switch (RealHostAddr.sa.sa_family)
3600 RealHostAddr.sin.sin_port = port;
3602 #endif /* NETINET */
3607 RealHostAddr.sin6.sin6_port = port;
3609 #endif /* NETINET6 */
3612 if (RealHostName == NULL)
3615 sm_dprintf("getauthinfo: NULL\n");
3618 (void) sm_strlcpy(hbuf, RealHostName, sizeof hbuf);
3622 # ifndef GET_IPOPT_DST
3623 # define GET_IPOPT_DST(dst) (dst)
3624 # endif /* ! GET_IPOPT_DST */
3626 ** Extract IP source routing information.
3628 ** Format of output for a connection from site a through b
3630 ** loose: @site-c@site-b:site-a
3631 ** strict: !@site-c@site-b:site-a
3633 ** o - pointer within ipopt_list structure.
3634 ** q - pointer within ls/ss rr route data
3635 ** p - pointer to hbuf
3638 if (RealHostAddr.sa.sa_family == AF_INET)
3640 SOCKOPT_LEN_T ipoptlen;
3645 struct IPOPTION ipopt;
3647 ipoptlen = sizeof ipopt;
3648 if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
3649 (char *) &ipopt, &ipoptlen) < 0)
3653 o = (unsigned char *) ipopt.IP_LIST;
3654 while (o != NULL && o < (unsigned char *) &ipopt + ipoptlen)
3670 ** o[0] is the option type (loose/strict).
3671 ** o[1] is the length of this option,
3672 ** including option type and
3674 ** o[2] is the pointer into the route
3676 ** o[3] begins the route data.
3679 p = &hbuf[strlen(hbuf)];
3680 l = sizeof hbuf - (hbuf - p) - 6;
3681 (void) sm_snprintf(p, SPACELEFT(hbuf, p),
3683 *o == IPOPT_SSRR ? "!" : "",
3684 l > 240 ? 120 : l / 2,
3685 inet_ntoa(GET_IPOPT_DST(ipopt.IP_DST)));
3690 j = o[1] / sizeof(struct in_addr) - 1;
3692 /* q skips length and router pointer to data */
3694 for ( ; j >= 0; j--)
3696 struct in_addr addr;
3698 memcpy(&addr, q, sizeof(addr));
3699 (void) sm_snprintf(p,
3709 q += sizeof(struct in_addr);
3715 /* Skip over option */
3720 (void) sm_snprintf(p, SPACELEFT(hbuf, p), "]");
3725 #endif /* IP_SRCROUTE */
3726 if (RealHostName != NULL && RealHostName[0] != '[')
3728 p = &hbuf[strlen(hbuf)];
3729 (void) sm_snprintf(p, SPACELEFT(hbuf, p), " [%.100s]",
3730 anynet_ntoa(&RealHostAddr));
3734 p = &hbuf[strlen(hbuf)];
3735 (void) sm_strlcpy(p, " (may be forged)", SPACELEFT(hbuf, p));
3736 macdefine(&BlankEnvelope.e_macro, A_PERM,
3737 macid("{client_resolve}"), "FORGED");
3742 #endif /* IP_SRCROUTE */
3744 /* put back the original incoming port */
3745 switch (RealHostAddr.sa.sa_family)
3750 RealHostAddr.sin.sin_port = port;
3752 #endif /* NETINET */
3757 RealHostAddr.sin6.sin6_port = port;
3759 #endif /* NETINET6 */
3763 sm_dprintf("getauthinfo: %s\n", hbuf);
3767 ** HOST_MAP_LOOKUP -- turn a hostname into canonical form
3770 ** map -- a pointer to this map.
3771 ** name -- the (presumably unqualified) hostname.
3772 ** av -- unused -- for compatibility with other mapping
3774 ** statp -- an exit status (out parameter) -- set to
3775 ** EX_TEMPFAIL if the name server is unavailable.
3778 ** The mapping, if found.
3779 ** NULL if no mapping found.
3782 ** Looks up the host specified in hbuf. If it is not
3783 ** the canonical name for that host, return the canonical
3784 ** name (unless MF_MATCHONLY is set, which will cause the
3785 ** status only to be returned).
3789 host_map_lookup(map, name, av, statp)
3795 register struct hostent *hp;
3797 struct in_addr in_addr;
3798 #endif /* NETINET */
3800 struct in6_addr in6_addr;
3801 #endif /* NETINET6 */
3802 char *cp, *ans = NULL;
3806 time_t SM_NONVOLATILE retrans = 0;
3807 int SM_NONVOLATILE retry = 0;
3808 #endif /* NAMED_BIND */
3809 char hbuf[MAXNAME + 1];
3812 ** See if we have already looked up this name. If so, just
3813 ** return it (unless expired).
3817 s = stab(name, ST_NAMECANON, ST_ENTER);
3818 if (bitset(NCF_VALID, s->s_namecanon.nc_flags) &&
3819 s->s_namecanon.nc_exp >= now)
3822 sm_dprintf("host_map_lookup(%s) => CACHE %s\n",
3824 s->s_namecanon.nc_cname == NULL
3826 : s->s_namecanon.nc_cname);
3827 errno = s->s_namecanon.nc_errno;
3828 SM_SET_H_ERRNO(s->s_namecanon.nc_herrno);
3829 *statp = s->s_namecanon.nc_stat;
3830 if (*statp == EX_TEMPFAIL)
3832 CurEnv->e_status = "4.4.3";
3833 message("851 %s: Name server timeout",
3834 shortenstring(name, 33));
3836 if (*statp != EX_OK)
3838 if (s->s_namecanon.nc_cname == NULL)
3840 syserr("host_map_lookup(%s): bogus NULL cache entry, errno = %d, h_errno = %d",
3842 s->s_namecanon.nc_errno,
3843 s->s_namecanon.nc_herrno);
3846 if (bitset(MF_MATCHONLY, map->map_mflags))
3847 cp = map_rewrite(map, name, strlen(name), NULL);
3849 cp = map_rewrite(map,
3850 s->s_namecanon.nc_cname,
3851 strlen(s->s_namecanon.nc_cname),
3857 ** If we are running without a regular network connection (usually
3858 ** dial-on-demand) and we are just queueing, we want to avoid DNS
3859 ** lookups because those could try to connect to a server.
3862 if (CurEnv->e_sendmode == SM_DEFER &&
3863 bitset(MF_DEFER, map->map_mflags))
3866 sm_dprintf("host_map_lookup(%s) => DEFERRED\n", name);
3867 *statp = EX_TEMPFAIL;
3872 ** If first character is a bracket, then it is an address
3873 ** lookup. Address is copied into a temporary buffer to
3874 ** strip the brackets and to preserve name if address is
3879 sm_dprintf("host_map_lookup(%s) => ", name);
3881 if (map->map_timeout > 0)
3883 retrans = _res.retrans;
3884 _res.retrans = map->map_timeout;
3886 if (map->map_retry > 0)
3889 _res.retry = map->map_retry;
3891 #endif /* NAMED_BIND */
3893 /* set default TTL */
3894 s->s_namecanon.nc_exp = now + SM_DEFAULT_TTL;
3899 (void) sm_strlcpy(hbuf, name, sizeof hbuf);
3900 if (getcanonname(hbuf, sizeof hbuf - 1, !HasWildcardMX, &ttl))
3904 s->s_namecanon.nc_exp = now + SM_MIN(ttl,
3910 if ((cp = strchr(name, ']')) == NULL)
3913 sm_dprintf("FAILED\n");
3920 if ((in_addr.s_addr = inet_addr(&name[1])) != INADDR_NONE)
3921 hp = sm_gethostbyaddr((char *)&in_addr,
3923 #endif /* NETINET */
3926 anynet_pton(AF_INET6, &name[1], &in6_addr) == 1)
3927 hp = sm_gethostbyaddr((char *)&in6_addr,
3928 IN6ADDRSZ, AF_INET6);
3929 #endif /* NETINET6 */
3934 /* found a match -- copy out */
3935 ans = denlstring((char *) hp->h_name, true, true);
3937 if (ans == hp->h_name)
3939 static char n[MAXNAME + 1];
3941 /* hp->h_name is about to disappear */
3942 (void) sm_strlcpy(n, ans, sizeof n);
3947 #endif /* NETINET6 */
3951 if (map->map_timeout > 0)
3952 _res.retrans = retrans;
3953 if (map->map_retry > 0)
3955 #endif /* NAMED_BIND */
3957 s->s_namecanon.nc_flags |= NCF_VALID; /* will be soon */
3959 /* Found an answer */
3962 s->s_namecanon.nc_stat = *statp = EX_OK;
3963 if (s->s_namecanon.nc_cname != NULL)
3964 sm_free(s->s_namecanon.nc_cname);
3965 s->s_namecanon.nc_cname = sm_strdup_x(ans);
3966 if (bitset(MF_MATCHONLY, map->map_mflags))
3967 cp = map_rewrite(map, name, strlen(name), NULL);
3969 cp = map_rewrite(map, ans, strlen(ans), av);
3971 sm_dprintf("FOUND %s\n", ans);
3976 /* No match found */
3977 s->s_namecanon.nc_errno = errno;
3979 s->s_namecanon.nc_herrno = h_errno;
3981 sm_dprintf("FAIL (%d)\n", h_errno);
3987 CurEnv->e_status = "4.4.3";
3988 message("851 %s: Name server timeout",
3989 shortenstring(name, 33));
3991 *statp = EX_TEMPFAIL;
3994 case HOST_NOT_FOUND:
4000 *statp = EX_SOFTWARE;
4004 *statp = EX_UNAVAILABLE;
4007 #else /* NAMED_BIND */
4009 sm_dprintf("FAIL\n");
4011 #endif /* NAMED_BIND */
4012 s->s_namecanon.nc_stat = *statp;
4016 ** HOST_MAP_INIT -- initialize host class structures
4019 ** map -- a pointer to this map.
4020 ** args -- argument string.
4027 host_map_init(map, args)
4031 register char *p = args;
4035 while (isascii(*p) && isspace(*p))
4046 map->map_tapp = ++p;
4050 map->map_mflags |= MF_MATCHONLY;
4054 map->map_mflags |= MF_NODEFER;
4057 case 'S': /* only for consistency */
4058 map->map_spacesub = *++p;
4062 map->map_mflags |= MF_DEFER;
4069 while (isascii(*++p) && isspace(*p))
4074 map->map_timeout = convtime(p, 's');
4081 while (isascii(*++p) && isspace(*p))
4083 map->map_retry = atoi(p);
4086 while (*p != '\0' && !(isascii(*p) && isspace(*p)))
4091 if (map->map_app != NULL)
4092 map->map_app = newstr(map->map_app);
4093 if (map->map_tapp != NULL)
4094 map->map_tapp = newstr(map->map_tapp);
4100 ** ANYNET_NTOP -- convert an IPv6 network address to printable form.
4103 ** s6a -- a pointer to an in6_addr structure.
4104 ** dst -- buffer to store result in
4105 ** dst_len -- size of dst buffer
4108 ** A printable version of that structure.
4112 anynet_ntop(s6a, dst, dst_len)
4113 struct in6_addr *s6a;
4119 if (IN6_IS_ADDR_V4MAPPED(s6a))
4120 ap = (char *) inet_ntop(AF_INET,
4121 &s6a->s6_addr[IN6ADDRSZ - INADDRSZ],
4128 /* Save pointer to beginning of string */
4131 /* Add IPv6: protocol tag */
4132 sz = sm_strlcpy(dst, "IPv6:", dst_len);
4137 ap = (char *) inet_ntop(AF_INET6, s6a, dst, dst_len);
4139 /* Restore pointer to beginning of string */
4147 ** ANYNET_PTON -- convert printed form to network address.
4149 ** Wrapper for inet_pton() which handles IPv6: labels.
4152 ** family -- address family
4154 ** dst -- destination address structure
4157 ** 1 if the address was valid
4158 ** 0 if the address wasn't parseable
4163 anynet_pton(family, src, dst)
4168 if (family == AF_INET6 && sm_strncasecmp(src, "IPv6:", 5) == 0)
4170 return inet_pton(family, src, dst);
4172 #endif /* NETINET6 */
4174 ** ANYNET_NTOA -- convert a network address to printable form.
4177 ** sap -- a pointer to a sockaddr structure.
4180 ** A printable version of that sockaddr.
4183 #ifdef USE_SOCK_STREAM
4186 # include <net/if_dl.h>
4187 # endif /* NETLINK */
4191 register SOCKADDR *sap;
4196 static char buf[100];
4198 /* check for null/zero family */
4201 if (sap->sa.sa_family == 0)
4204 switch (sap->sa.sa_family)
4208 if (sap->sunix.sun_path[0] != '\0')
4209 (void) sm_snprintf(buf, sizeof buf, "[UNIX: %.64s]",
4210 sap->sunix.sun_path);
4212 (void) sm_strlcpy(buf, "[UNIX: localhost]", sizeof buf);
4214 # endif /* NETUNIX */
4218 return (char *) inet_ntoa(sap->sin.sin_addr);
4219 # endif /* NETINET */
4223 ap = anynet_ntop(&sap->sin6.sin6_addr, buf, sizeof buf);
4227 # endif /* NETINET6 */
4231 (void) sm_snprintf(buf, sizeof buf, "[LINK: %s]",
4232 link_ntoa((struct sockaddr_dl *) &sap->sa));
4234 # endif /* NETLINK */
4236 /* this case is needed when nothing is #defined */
4237 /* in order to keep the switch syntactically correct */
4241 /* unknown family -- just dump bytes */
4242 (void) sm_snprintf(buf, sizeof buf, "Family %d: ", sap->sa.sa_family);
4243 bp = &buf[strlen(buf)];
4244 ap = sap->sa.sa_data;
4245 for (l = sizeof sap->sa.sa_data; --l >= 0; )
4247 (void) sm_snprintf(bp, SPACELEFT(buf, bp), "%02x:",
4255 ** HOSTNAMEBYANYADDR -- return name of host based on address
4258 ** sap -- SOCKADDR pointer
4261 ** text representation of host name.
4268 hostnamebyanyaddr(sap)
4269 register SOCKADDR *sap;
4271 register struct hostent *hp;
4274 # endif /* NAMED_BIND */
4276 struct in6_addr in6_addr;
4277 # endif /* NETINET6 */
4280 /* shorten name server timeout to avoid higher level timeouts */
4281 saveretry = _res.retry;
4282 if (_res.retry * _res.retrans > 20)
4283 _res.retry = 20 / _res.retrans;
4284 # endif /* NAMED_BIND */
4286 switch (sap->sa.sa_family)
4290 hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
4293 # endif /* NETINET */
4297 hp = sm_gethostbyaddr((char *) &sap->sin6.sin6_addr,
4298 IN6ADDRSZ, AF_INET6);
4300 # endif /* NETINET6 */
4304 hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
4305 sizeof sap->siso.siso_addr, AF_ISO);
4307 # endif /* NETISO */
4313 # endif /* NETUNIX */
4316 hp = sm_gethostbyaddr(sap->sa.sa_data, sizeof sap->sa.sa_data,
4322 _res.retry = saveretry;
4323 # endif /* NAMED_BIND */
4325 # if NETINET || NETINET6
4326 if (hp != NULL && hp->h_name[0] != '['
4328 && inet_pton(AF_INET6, hp->h_name, &in6_addr) != 1
4329 # endif /* NETINET6 */
4331 && inet_addr(hp->h_name) == INADDR_NONE
4332 # endif /* NETINET */
4337 name = denlstring((char *) hp->h_name, true, true);
4339 if (name == hp->h_name)
4341 static char n[MAXNAME + 1];
4343 /* Copy the string, hp->h_name is about to disappear */
4344 (void) sm_strlcpy(n, name, sizeof n);
4348 # endif /* NETINET6 */
4351 # endif /* NETINET || NETINET6 */
4359 # endif /* NETINET6 */
4362 if (sap->sa.sa_family == AF_UNIX && sap->sunix.sun_path[0] == '\0')
4364 # endif /* NETUNIX */
4366 static char buf[203];
4368 (void) sm_snprintf(buf, sizeof buf, "[%.200s]",
4373 #endif /* USE_SOCK_STREAM */