summaryrefslogtreecommitdiff
path: root/sqmail-4.3.07/src/dkimbase.cpp
blob: f6abf45284fd7bcd78950fe9c7b3dc40938b9228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*****************************************************************************
*  Copyright 2005 Alt-N Technologies, Ltd. 
*
*  Licensed under the Apache License, Version 2.0 (the "License"); 
*  you may not use this file except in compliance with the License. 
*  You may obtain a copy of the License at 
*
*      http://www.apache.org/licenses/LICENSE-2.0 
*
*  This code incorporates intellectual property owned by Yahoo! and licensed 
*  pursuant to the Yahoo! DomainKeys Patent License Agreement.
*
*  Unless required by applicable law or agreed to in writing, software 
*  distributed under the License is distributed on an "AS IS" BASIS, 
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
*  See the License for the specific language governing permissions and 
*  limitations under the License.
*
*  Changes done by ¢feh@fehcom.de obeying the above license
*
*****************************************************************************/
#include <string.h>
#include <algorithm>
#include "dkim.h"
#include "dkimbase.h"


CDKIMBase::CDKIMBase()
{
  m_From = NULL;
  m_Sender = NULL;
  m_hTag = NULL;
  m_hTagSize = 0;
  m_hTagPos = 0;
  m_Line = NULL;
  m_LineSize = 0;
  m_LinePos = 0;
  m_InHeaders = true;
}

CDKIMBase::~CDKIMBase()  // delete
{
  Free(m_Line);
  Free(m_From);
  Free(m_Sender);
  Free(m_hTag);
}

int CDKIMBase::Init(void)
{
  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// Alloc - allocate buffer
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::Alloc(char*& szBuffer,int nRequiredSize)
{
  szBuffer = new char[nRequiredSize];

  return (szBuffer == NULL) ? DKIM_OUT_OF_MEMORY : DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// ReAlloc - extend buffer if necessary, leaving room for future expansion
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::ReAlloc(char*& szBuffer,int& nBufferSize,int nRequiredSize)
{
  if (nRequiredSize > nBufferSize) {
    char* newp;
    int nNewSize = nRequiredSize + BUFFER_ALLOC_INCREMENT; 

    if (Alloc(newp,nNewSize) == DKIM_SUCCESS) {
      if (szBuffer != NULL && nBufferSize > 0) {
        memcpy(newp,szBuffer,nBufferSize);
        delete[] szBuffer;
      }
      szBuffer = newp;
      nBufferSize  = nNewSize;
    } else {
      return DKIM_OUT_OF_MEMORY; // memory alloc error!
    }
  }

  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// Process - split buffers into lines without any CRs or LFs at the end.
//
////////////////////////////////////////////////////////////////////////////////
void CDKIMBase::Free(char* szBuffer)
{
  if (szBuffer)
    delete[] szBuffer;
}

////////////////////////////////////////////////////////////////////////////////
// 
// Process - split buffers into lines without any CRs or LFs at the end.
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::Process(const char* szBuffer,int nBufLength,bool bEOF)
{ const char* p = szBuffer;
  const char* e = szBuffer + nBufLength;

  while (p < e) {
    if (*p != '\n' || m_LinePos == 0 || m_Line[m_LinePos - 1] != '\r') {
      // add char to line
      if (m_LinePos >= m_LineSize) {
        int nRet = ReAlloc(m_Line,m_LineSize,m_LinePos + 1);
        if (nRet != DKIM_SUCCESS) return nRet;
      }
      m_Line[m_LinePos++] = *p;
    } else {
      // back up past the CR
      m_LinePos--;

      if (m_InHeaders) {
        // process header line
        if (m_LinePos == 0) {
          m_InHeaders = false;
          int Result = ProcessHeaders();
          if (Result != DKIM_SUCCESS)
            return Result;
        } else {
          // append the header to the headers list
          if (m_Line[0] != ' ' && m_Line[0] != '\t') {
            HeaderList.push_back(string(m_Line,m_LinePos));
//            fprintf(stderr," dkimbase.cpp:Process:Input:  %s \n",m_Line);
          } else {
            if (!HeaderList.empty()) {
              HeaderList.back().append("\r\n",2).append(m_Line,m_LinePos);
            } else {
              // no header to append to...
            }
          }
        }
      } else {
        // process body line
        int Result = ProcessBody(m_Line,m_LinePos,bEOF);
        if (Result != DKIM_SUCCESS) {
          m_LinePos = 0;
          return Result;
        }
      }

      m_LinePos = 0;
    }

    p++;
  }

  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// ProcessFinal - process leftovers if stopping before the body or mid-line
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::ProcessFinal(void)
{
  if (m_LinePos > 0) {
    Process("\r\n",2,true);
  }

  if (m_InHeaders) {
    m_InHeaders = false;
    ProcessHeaders();
    /* type conversion should be safe as length is zero */
    ProcessBody((char *)"",0,true);
  }

  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// ProcessHeaders - process the headers (to be implemented by derived class)
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::ProcessHeaders()
{
  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// ProcessBody - process body line (to be implemented by derived class)
//
////////////////////////////////////////////////////////////////////////////////
int CDKIMBase::ProcessBody(char* szBuffer, int nBufLength, bool bEOF)
{
  return DKIM_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
// 
// RemoveSWSP - remove streaming white space from buffer/string inline
//
////////////////////////////////////////////////////////////////////////////////

struct isswsp
{
  bool operator()(char ch) { return(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); }
};

void CDKIMBase::RemoveSWSP(char* szBuffer)
{
  *remove_if(szBuffer,szBuffer + strlen(szBuffer),isswsp()) = '\0';
}

void CDKIMBase::RemoveSWSP(char* pBuffer,int& nBufLength)
{
  nBufLength = remove_if(pBuffer,pBuffer+nBufLength,isswsp()) - pBuffer;
}

void CDKIMBase::RemoveSWSP(string& sBuffer)
{
  sBuffer.erase(remove_if(sBuffer.begin(),sBuffer.end(),isswsp()),sBuffer.end());
}

//////////////////////////////////////////////////////////////////////////////////////////
// 
// CompressSWSP - compress streaming white space into single spaces from buffer/string inline
//
//////////////////////////////////////////////////////////////////////////////////////////

void CDKIMBase::CompressSWSP(char* pBuffer,int& nBufLength)
{
  char* pSrc = pBuffer;
  char* pDst = pBuffer;
  char* pEnd = pBuffer + nBufLength;

  while (pSrc != pEnd) {
    if (isswsp()(*pSrc)) {

      do {
        ++pSrc;
      } while (pSrc != pEnd && isswsp()(*pSrc));

      if (pSrc == pEnd)
        break;

      *pDst++ = ' ';
    }

    *pDst++ = *pSrc++;
  }

  nBufLength = pDst - pBuffer;
}

void CDKIMBase::CompressSWSP(string& sBuffer)
{
  string::iterator iSrc = sBuffer.begin();
  string::iterator iDst = sBuffer.begin();
  string::iterator iEnd = sBuffer.end();

  while (iSrc != iEnd) {
    if (isswsp()(*iSrc)) {

      do {
        ++iSrc;
      } while (iSrc != iEnd && isswsp()(*iSrc));

      if (iSrc == iEnd)
        break;

      *iDst++ = ' ';
    }

    *iDst++ = *iSrc++;
  }

  sBuffer.erase(iDst, iEnd);
}

//////////////////////////////////////////////////////////////////////////////////////////
// 
// RelaxHeader - relax a header field (lower case the name, remove swsp before and after :)
//
// modified 4/21/06 STB to remove white space before colon
//
//////////////////////////////////////////////////////////////////////////////////////////

string CDKIMBase::RelaxHeader(const string& sHeader)
{
  string sTemp = sHeader;

  CompressSWSP(sTemp);

  string::size_type cpos = sTemp.find(':');

  if (cpos == string::npos) {
    // no colon?!
  } else {
    // lower case the header field name
    for (unsigned i = 0; i < cpos; i++) {
      if (sTemp[i] >= 'A' && sTemp[i] <= 'Z')
        sTemp[i] += 'a'-'A';
    }

    // remove the space after the :
    if (cpos + 1 < sTemp.length() && sTemp[cpos+1] == ' ')
      sTemp.erase(cpos + 1, 1);

    // remove the space before the :
    if (cpos > 0 && sTemp[cpos - 1] == ' ')
      sTemp.erase(cpos - 1,1);
  }

  return sTemp;
}