1 /**
2 * \file smtp.c
3 * \brief This file has SMTP related code
4 *
5 * \author Fernando J. Pereda <ferdy@ferdyx.org>
6 * \author Ricardo Cervera Navarro <ricardo@zonasiete.org>
7 *
8 * This is part of nbsmtp. nbsmtp is free software;
9 * you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * nbsmtp is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with nbsmtp; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * See COPYING for details.
24 */
25
26 #include <stdio.h>
27 #include <syslog.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "fdutil.h"
32 #include "smtp.h"
33 #include "util.h"
34
35 /**
36 * \brief Las message from the server
37 */
38 char *smtp_stored_message;
39
40 /**
41 * \brief Calls fd_puts appending CR LF
42 *
43 * \param[in] serverinfo A pointer to a servinfo_t struct with the socket information
44 * \param[in] str A pointer to the string to be written
45 * \returns We return what fd_puts returns
46 * \see fd_puts()
47 */
smtp_write(servinfo_t * serverinfo,char * str)48 ssize_t smtp_write(servinfo_t *serverinfo, char *str)
49 {
50 char *buf;
51 ssize_t ret;
52
53 asprintf(&buf,"%s\r\n",str);
54
55 ret = fd_puts(serverinfo,buf,strlen(buf));
56 log_msg(LOG_DEBUG,"[->] %s",buf);
57 free(buf);
58
59 return ret;
60 }
61
62 /**
63 * \brief Returns the last message from the server
64 *
65 * \returns A pointer to the string with the last message from the server
66 */
smtp_last_message()67 char *smtp_last_message()
68 {
69 return smtp_stored_message;
70 }
71
72 /**
73 * \brief Gets a line from a socket an returns its first digit ( borrowed from sSMTP )
74 *
75 * \param[in] serverinfo A pointer to a servinfo_t struct with the socket information
76 * \param[out] response A pointer where we will store the line read
77 * \returns The first digit of the line we read
78 * \see fd_gets()
79 */
smtp_read(servinfo_t * serverinfo,char * response)80 int smtp_read(servinfo_t *serverinfo, char *response)
81 {
82 char *p;
83
84 do
85 {
86 if (fd_gets(response,BUF_SIZE,serverinfo)==NULL)
87 {
88 return 0;
89 }
90
91 log_msg(LOG_DEBUG,"[<-] %s",response);
92 } while (response[3]=='-');
93
94
95 /* Free last message if neccesary */
96 if (smtp_stored_message!=NULL)
97 {
98 free(smtp_stored_message);
99 }
100
101
102 /* Find the first space (numbers finish there) */
103 p = strchr(response,' ');
104
105 /* Avoid possible missbehavior */
106 if (p != NULL)
107 {
108 p++;
109
110 smtp_stored_message = (char *)strdup(p);
111 }
112 else
113 {
114 smtp_stored_message = (char *)strdup("");
115 }
116
117 return(atoi(response)/100); /* response[0] ? */
118 }
119
120 /**
121 * \brief Gets a line using smtp_read and if it returns 2 we return 1 (borrowed from sSMTP)
122 *
123 * \param[in] serverinfo A pointer to a servinfo_t struct with the socket info
124 * \return 1 if smtp_read returns 2 and 0 in other case
125 * \see smtp_read()
126 */
smtp_okay(servinfo_t * serverinfo)127 int smtp_okay(servinfo_t *serverinfo)
128 {
129 char response[BUF_SIZE];
130
131 return((smtp_read(serverinfo,response)==2) ? 1 : 0);
132 }
133
134 /**
135 * \brief Writes data to the socket (actually it writes the message body and headers)
136 *
137 * \param[in] serverinfo A pointer to a servinfo_t structure with the information needed
138 * \param[in] msg A pointer to the buffer with the message
139 * \param[in] length An integer with the number of bytes to be written
140 * \return 1 in case of error, 0 if everything goes allright
141 */
smtp_write_data(servinfo_t * serverinfo,char * msg,int length)142 int smtp_write_data(servinfo_t *serverinfo, char *msg, int length)
143 {
144 char *p;
145 int i = 0; /* Avoid segfault if msg[0]=='\n' or msg[0]=='.' */
146 char r = '\r';
147 char dot = '.';
148
149 for ( p = msg ; *p && i < length; p++ )
150 {
151 if (*p=='\n')
152 {
153 if (i>0 && *(p-1)!='\r')
154 {
155 if (fd_putc(serverinfo,&r)<1)
156 {
157 return 1;
158 }
159 }
160 else if (i==0)
161 {
162 if (fd_putc(serverinfo,&r)<1)
163 {
164 return 1;
165 }
166 }
167 }
168 else if (*p=='.' && i>0 && *(p-1)=='\n')
169 {
170 if (fd_putc(serverinfo,&dot)<1)
171 {
172 return 1;
173 }
174 }
175
176 if (fd_putc(serverinfo,p)<1)
177 {
178 return 1;
179 }
180
181 i++;
182 }
183
184 return 0;
185 }
186
187 /**
188 * \brief Cleans up the smtp module
189 */
smtp_exit(void)190 void smtp_exit(void)
191 {
192 if (smtp_stored_message)
193 {
194 free(smtp_stored_message);
195 }
196
197 return;
198 }
199