MiniMagAsm

Artifact [04844bb592]
Login

Artifact 04844bb592fdf7cf2267e912a684539e97d403b6:


     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
include "%lib%/freshlib.inc"

@BinaryType console

options.ShowSkipped = 0
options.ShowSizes = 1
options.DebugMode = 0
options.AlignCode = 0

HeapManager  equ ASM


include "%lib%/freshlib.asm"

include 'save_response.asm'
include 'get.asm'
include 'post.asm'
include 'cgi.asm'

iglobal
  pHTMLTemplate dd cDefaultTemplate
endg


uglobal
  StartTime     dd ?

  hDocumentDir  dd ?

  hSource       dd ?

  hFileTitle    dd ?

  hSysMessage   dd ?

  hHoneypot     dd ?
  hFeedbackUser dd ?
  hFeedbackEmail dd ?

  hRemoteAddr   dd ?
  dwRemoteAddr  dd ?

  pBanList      dd ?
  dwBanListLen  dd ?

  fBanned       dd ?
endg

cSubject text "subject="
cFeedbackUser text "name="
cFeedbackEmail text "email="

cRemoteAddr text "REMOTE_ADDR"
cBanFilename text "banlist.inc"

;        rb 256          ; FASM ELF generator sometimes is buggy. Inserting some more bytes, fixes the problem.

start:
        stdcall GetTimestamp
        mov     [StartTime], eax

        InitializeAll

        stdcall InitScriptVariables
        stdcall GetPostData
        jc      .no_post_data

        stdcall GetQueryItem, [pPostContent], cSubject, 0
        mov     [hHoneypot], eax

        stdcall GetQueryItem, [pPostContent], cFeedbackUser, cDefaultNULL
        push    eax
        stdcall StrEncodeHTML, eax
        stdcall StrDel; from the stack
        mov     [hFeedbackUser], eax

        stdcall GetQueryItem, [pPostContent], cFeedbackEmail, cDefaultNULL
        mov     [hFeedbackEmail], eax

        stdcall StrClipSpacesR, eax
        stdcall StrClipSpacesL, eax

; it is work around. The function StrLCase does not work properly here.
        stdcall StrPtr, eax
        mov     ecx, [eax+string.len]
        jecxz   .email_ok
.lcase:
        cmp     byte [eax], 'A'
        jb      .next
        cmp     byte [eax], 'Z'
        ja      .next

        or      byte [eax], $40

.next:
        inc     eax
        loop    .lcase

.email_ok:
        stdcall StrClipSpacesR, [hSource]
        stdcall StrLen, [hSource]
        test    eax, eax
        jz      .finish

        stdcall StrClipSpacesR, [hFeedbackUser]
        stdcall StrClipSpacesL, [hFeedbackUser]
        stdcall StrLen, [hFeedbackUser]
        test    eax, eax
        jz      .finish

        stdcall GetEnvVariable, cRemoteAddr
        jc      .finish
        mov     [hRemoteAddr], eax

        stdcall StrIP2Num, eax
        jc      .finish

        mov     [dwRemoteAddr], eax

; check the ban list.

        stdcall StrDup, [hPath]
        stdcall StrCat, eax, cBanFilename
        push    eax

        stdcall LoadBinaryFile, eax
        stdcall StrDel; from the stack
        test    ecx, ecx
        jnz     .check_banlist

        stdcall GetMem, 16
        mov     [pBanList], eax
        mov     [dwBanListLen], ecx
        jmp     .not_banned

.check_banlist:
        mov     [pBanList], eax
        mov     edi, eax
        mov     eax, [dwRemoteAddr]
        shr     ecx, 2
        mov     [dwBanListLen], ecx

        jecxz   .not_banned
        repne scasd
        je    .finish            ; this address is banned.

.not_banned:
; check the honeypot
        cmp     [hHoneypot], 0
        je      .save_it
        stdcall StrLen, [hHoneypot]
        test    eax, eax
        jnz     .ban_it

        mov     [fBanned], FALSE

; save the feedback

.save_it:
; create the filename
        stdcall StrDup, [hPath]
        stdcall StrCat, eax, [hPageName]
        stdcall StrCharCat, eax, '.inc'
        mov     ebx, eax

.save_it_here:
; open the feedback file
        stdcall LoadBinaryFile, ebx
        mov     esi, eax
        mov     edx, ecx

        stdcall FileCreate, ebx
        jc      .file_create_error
        mov     edi, eax

                                                        cAvatarImg text '<img class="avatar" alt=":)" src="http://gravatar.com/avatar/'
                                                        cAvatarImgEnd text '?d=monsterid&amp;s=60" />'

        stdcall FileWriteString, edi, cAvatarImg

        stdcall StrMD5, [hFeedbackEmail]
        push    eax
        stdcall FileWriteString, edi, eax
        stdcall FileWriteString, edi, cAvatarImgEnd
        stdcall StrDel ; from the stack.

        stdcall FileWriteString, edi, [hFeedbackUser]
        stdcall FileWriteString, edi, cCRLF
        stdcall FileWriteString, edi, cFeedbackCommentEmail
        stdcall FileWriteString, edi, [hFeedbackEmail]
        stdcall FileWriteString, edi, cCRLF
        stdcall FileWriteString, edi, cFeedbackCommentIP
        stdcall FileWriteString, edi, [hRemoteAddr]
        stdcall FileWriteString, edi, cCRLF
        stdcall FileWriteString, edi, [hSource]
        stdcall FileWriteString, edi, cCRLF
        stdcall FileWriteString, edi, cFeedbackSeparator

cFeedbackCommentEmail   text ';;;; User e-mail: '
cFeedbackCommentIP      text ';;;; User IP: '
cFeedbackSeparator text $03, $0d, $0a

        test    edx, edx
        jz      .old_ok
        stdcall FileWrite, edi, esi, edx
.old_ok:

        stdcall FileClose, edi

        cmp     [fBanned], FALSE
        jne     .log_banned

cCommentEvent text 'comment added.'
        stdcall LogEvent, cCommentEvent, [hPageName], [hFileTitle]

cCommentSuccess text 'Comment was added.'
        stdcall StrDup, cCommentSuccess
        mov     [hSysMessage], eax
        jmp     .finish

.log_banned:

cBannedEvent text 'bot banned from commenting.'
        stdcall LogEvent, cBannedEvent, [hPageName], [hFileTitle]

cBannedSuccess text 'Comment was not added.'
        stdcall StrDup, cBannedSuccess
        mov     [hSysMessage], eax

.finish:
        stdcall ReadTemplate, cTemplateFileName
        jc      .template_ok
        mov     [pHTMLTemplate], eax

.template_ok:
        stdcall FileWrite, [STDOUT], cContentType, cContentType.length
        stdcall WriteTemplate, [pHTMLTemplate], [STDOUT], SaveVarCallback

        push    eax
        FinalizeAll
        stdcall Terminate

;..............................................................................


.no_post_data:
        stdcall StrDup, cErrorNoPost
        mov     [hSysMessage], eax
        jmp     .finish


.file_write_error:
        push    eax
        stdcall FileClose, ebx
        pop     eax

.error_read:
.file_create_error:
        stdcall StrDup, 'Error file read/write.'
        mov     [hSysMessage], eax
        jmp     .finish

.ban_it:
        mov     ecx, [dwBanListLen]
        mov     esi, [pBanList]
        mov     eax, [dwRemoteAddr]
        mov     [esi+4*ecx], eax
        inc     ecx

        shl     ecx, 2

        stdcall StrDup, [hPath]
        stdcall StrCat, eax, cBanFilename
        push    eax
        stdcall SaveBinaryFile, eax, esi, ecx
        stdcall StrDel; from the stack

        stdcall StrDup, [hPath]
        stdcall StrCat, eax, cBanedFeedbacks
        mov     ebx, eax
        mov     [fBanned], TRUE
        jmp     .save_it_here

cBanedFeedbacks  text 'BanedFeedbackPosts.inc'

cCRLF             text $0d, $0a
cTemplateFileName text 'feedback.inc'
cDefaultTemplate  text '<html><head></head><body></body></html>'


cContentType text 'Content-type: text/html', 13, 10, 13, 10


cErrorNoPost text 'Error! feedback.cgi needs post data.'