1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 /*
26  * A test source file with alternative implementations of some functions. Those
27  * that are affected are generally ifdef'ed out in the original source file by
28  * TEST_CF_EXECD macro.
29  */
30 
31 #include <platform.h>
32 #include <cf-execd-runner.h>
33 
FeedSmtpDirectives(void * data)34 static void *FeedSmtpDirectives(void *data)
35 {
36     int socket = *(int *)data;
37 
38 #define COND_SEND(X) if (send(socket, X, strlen(X), 0) == -1) \
39     { \
40         Log(LOG_LEVEL_ERR, "Failed to write SMTP directive in %s:%i", __FILE__, __LINE__); \
41         return NULL; \
42     } \
43     else \
44     { \
45         fwrite(X, strlen(X), 1, stdout); \
46     }
47 
48 #define COND_RECV(X) if ((rcvd = recv(socket, X, sizeof(X), 0)) == -1) \
49     { \
50         Log(LOG_LEVEL_ERR, "Failed to read SMTP response in %s:%i", __FILE__, __LINE__); \
51         return NULL; \
52     } \
53     else \
54     { \
55         fwrite(X, rcvd, 1, stdout); \
56     }
57 
58     char recvbuf[CF_BUFSIZE];
59     int rcvd;
60     COND_SEND("220 test.com\r\n");
61     COND_RECV(recvbuf);
62     COND_SEND("250 Hello test.com, pleased to meet you\r\n");
63     COND_RECV(recvbuf);
64     COND_SEND("250 from@test.com... Sender ok\r\n");
65     COND_RECV(recvbuf);
66     COND_SEND("250 to@test.com... Recipient ok\r\n");
67     COND_RECV(recvbuf);
68     COND_SEND("354 Enter mail, end with \".\" on a line by itself\r\n");
69     while (true)
70     {
71         COND_RECV(recvbuf);
72         if ((rcvd == 3 && memcmp(recvbuf + rcvd - 3, ".\r\n", 3) == 0) ||
73             (rcvd > 3 && memcmp(recvbuf + rcvd - 4, "\n.\r\n", 4) == 0))
74         {
75             break;
76         }
77     }
78     COND_SEND("250 Message accepted for delivery\r\n");
79     COND_RECV(recvbuf);
80     COND_SEND("221 test.com closing connection\r\n");
81 
82 #undef COND_SEND
83 #undef COND_RECV
84 
85     cf_closesocket(socket);
86     free(data);
87 
88     return NULL;
89 }
90 
ConnectToSmtpSocket(ARG_UNUSED const ExecConfig * config)91 int ConnectToSmtpSocket(ARG_UNUSED const ExecConfig *config)
92 {
93     int sockets[2];
94     if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0)
95     {
96         return -1;
97     }
98 
99     int *thread_socket = xmalloc(sizeof(int));
100     *thread_socket = sockets[1];
101 
102     pthread_attr_t attr;
103     pthread_attr_init(&attr);
104     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
105     pthread_t thread;
106     int ret = pthread_create(&thread, &attr, &FeedSmtpDirectives, thread_socket);
107     pthread_attr_destroy(&attr);
108 
109     if (ret != 0)
110     {
111         free(thread_socket);
112         cf_closesocket(sockets[0]);
113         cf_closesocket(sockets[1]);
114         return -1;
115     }
116 
117     return sockets[0];
118 }
119