summaryrefslogtreecommitdiff
path: root/src/login/parse.c
blob: 6cbc77b89fcd3c5c25ce04d8c6ac6469b97c21a6 (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
// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// Copyright (c) 2014 Evol developers

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

#include "../../../common/HPMi.h"
#include "../../../common/malloc.h"
#include "../../../common/mmo.h"
#include "../../../common/socket.h"
#include "../../../common/strlib.h"
#include "../../../login/account.h"
#include "../../../login/login.h"

#include "common/ip.h"
#include "login/config.h"
#include "login/parse.h"
#include "login/send.h"

int clientVersion = 0;

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

    clientVersion = RFIFOL(fd, 2);

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

    send_update_host(fd);
    send_server_version(fd);
}

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

    return 0;
}

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

    safestrncpy(username, (const char*)RFIFOP(fd, 2), NAME_LENGTH);

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

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

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

    char ip[16];
    uint32 ipl = session[fd]->client_addr;
    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);
        login->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;
}

void elogin_parse_request_connection(int *fd, struct login_session_data* sd, const char *const ip)
{
    if (!inter_server_ip || !ip)
        return;
    if (!checkAllowedIp(inter_server_ip, ip))
    {
        hookStop();
        login->char_server_connection_status(*fd, sd, 3);
        ShowNotice("Connection of the char-server from ip %s REFUSED.\n", ip);
    }
}