summaryrefslogtreecommitdiff
path: root/src/elogin/parse.c
blob: 16d1c7ebcd489ef05a10567948719565de80010f (plain) (blame)
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
// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// Copyright (c) 2014 - 2015 Evol developers

#include "common/hercules.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "login/account.h"
#include "login/lclif.h"
#include "login/login.h"

#include "plugins/HPMHooking.h"

#include "elogin/config.h"
#include "elogin/md5calc.h"
#include "elogin/parse.h"
#include "elogin/send.h"

int clientVersion = 0;

void login_parse_version(int fd)
{
    struct login_session_data* sd = (struct login_session_data*)sockt->session[fd]->session_data;
    if (!sd)
        return;

    clientVersion = RFIFOL(fd, 2);

    if (clientVersion < 2)
    {
        lclif->login_error(fd, 5);
        return;
    }

    send_update_host(fd);
    send_server_version(fd);
}

bool elogin_client_login_pre(int *fdPtr,
                             struct login_session_data **sdPtr __attribute__ ((unused)))
{
    int fd = *fdPtr;
    uint16 command = RFIFOW(fd,0);
    if (command != 0x64)
    {
        lclif->login_error(fd, 3);
        hookStop();
        return true;
    }
    char username[NAME_LENGTH];
    safestrncpy(username, RFIFOP(fd, 6), NAME_LENGTH);
    int len = (int)safestrnlen(username, NAME_LENGTH);
    if (clientVersion < 2)
    {
        lclif->login_error(fd, 5);
        hookStop();
        return true;
    }
    else if (len >= 2 && username[len - 2] == '_' && memchr("FfMm", username[len - 1], 4))
    {
        lclif->login_error(fd, 3);
        hookStop();
        return true;
    }

    short *ptr = (short*)RFIFOP(fd, 2);
    if (*ptr == 20)
        *ptr = clientVersion;
    return false;
}

bool elogin_client_login_post(bool retVal,
                              int fd,
                              struct login_session_data* sd)
{
    sd = (struct login_session_data*)sockt->session[fd]->session_data;
    if (sd)
        sd->version = clientVersion;
    return retVal;
}

void elogin_parse_client_login2(int fd)
{
    char username[NAME_LENGTH];
    char password[PASSWD_LEN];
    char email[40];
    uint8 clienttype;
    int result;

    safestrncpy(username, RFIFOP(fd, 2), NAME_LENGTH);

    int len = (int)safestrnlen(username, NAME_LENGTH);
    if (len < 2 || !(username[len - 2] == '_') || !memchr("FfMm", username[len - 1], 4))
    {
        lclif->login_error(fd, 3);
        return;
    }

    safestrncpy(password, RFIFOP(fd, 26), NAME_LENGTH);
    safestrncpy(email, RFIFOP(fd, 51), 40);
    clienttype = RFIFOB(fd, 50);

    struct login_session_data* sd = (struct login_session_data*)sockt->session[fd]->session_data;
    if (!sd)
        return;

    char ip[16];
    uint32 ipl = sockt->session[fd]->client_addr;
    sockt->ip2str(ipl, ip);
    sd->clienttype = clienttype;
    sd->version = clientVersion;
    sd->passwdenc = 0;
    safestrncpy(sd->userid, username, NAME_LENGTH);
    ShowStatus("Request for connection of %s (ip: %s).\n", sd->userid, ip);
    safestrncpy(sd->passwd, password, PASSWD_LEN);

    if (e_mail_check(email) == 0)
    {
        ShowNotice("Attempt to create an e-mail REFUSED - e-mail is invalid (ip: %s)\n", ip);
        lclif->login_error(fd, 11);
        return;
    }

    result = login->mmo_auth(sd, false);

    if (result == -1)
    {
        int account_id = sd->account_id;
        struct mmo_account acc;
        if (!login->accounts->load_num(login->accounts, &acc, account_id))
        {
            ShowNotice("Attempt to create an e-mail on an account REFUSED - account: %d, ip: %s).\n", account_id, ip);
        }
        else
        {
            memcpy(acc.email, email, 40);
            ShowNotice("Create an e-mail on an account with a default e-mail (account: %d, new e-mail: %s, ip: %s).\n", account_id, email, ip);
            // Save
            login->accounts->save(login->accounts, &acc);
        }

        login->auth_ok(sd);
    }
    else
    {
        login->auth_failed(sd, result);
    }

    return;
}

enum parsefunc_rcode elogin_parse_ping_pre(int *fd __attribute__ ((unused)),
                                           struct login_session_data **sdPtr)
{
    struct login_session_data *sd = *sdPtr;
    if (!sd)
    {
        hookStop();
        return PACKET_VALID;
    }
    struct online_login_data* data = (struct online_login_data*)idb_get(login->online_db, sd->account_id);
    if (data == NULL)
    {
        hookStop();
        return PACKET_VALID;
    }
    if (data->waiting_disconnect != INVALID_TIMER)
    {
        timer->delete(data->waiting_disconnect, login->waiting_disconnect_timer);
        data->waiting_disconnect = timer->add(timer->gettick() + 30000, login->waiting_disconnect_timer, sd->account_id, 0);
    }
    hookStop();
    return PACKET_VALID;
}

void elogin_parse_change_paassword(int fd)
{
    char actual_pass[24], new_pass[24];
    int  status = 0;
    struct mmo_account acc;
    const int accountId = RFIFOL (fd, 2);

    memcpy (actual_pass, RFIFOP (fd, 6), 24);
    actual_pass[23] = '\0';
    memcpy (new_pass, RFIFOP (fd, 30), 24);
    new_pass[23] = '\0';

    if (!login->accounts->load_num(login->accounts, &acc, accountId))
    {
        // account not found
        send_char_password_change_ack(fd, accountId, 0);
        return;
    }

    if (!strcmp(actual_pass, acc.pass) || pass_ok(actual_pass, acc.pass))
    {
        // changed ok
        status = 1;
        safestrncpy(acc.pass, new_pass, sizeof(acc.pass));
        login->accounts->save(login->accounts, &acc);
    }
    else
    {
        // wrong password
        status = 2;
    }
    send_char_password_change_ack(fd, accountId, status);
}

void elogin_parse_serverexit(int fd)
{
    const int code = RFIFOW(fd, 2);
    switch (code)
    {
        case 100:  // all exit
        case 101:  // all restart
        case 104:  // git pull and all restart
        case 105:  // build all
        case 106:  // rebuild all
        case 107:  // git pull and build all
        case 108:  // git pull and rebuild all
        case 109:  // build plugin
        case 110:  // git pull and build plugin
            core->shutdown_callback();
            break;
        case 102:  // restart char and map server
        case 103:  // restart map server
            break;
        default:
            ShowWarning("Unknown termination code: %d\n", code);
    }
}