forked from ibillxia/xsmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_user.cpp
142 lines (127 loc) · 4.46 KB
/
module_user.cpp
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
//
// Copyright (C) 2011
// Bill Xia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
//
// This is the use management module, includes user login, authentication and quit.
//
//
#include "module_user.hpp"
#include "module_mail.hpp"
using namespace std;
// check username and passwd from file
bool check_name_pass(const char *const name, const char *const pass);
bool check_user(const char *const from_user)
{
const auto strlen_from_user = strlen(from_user);
ifstream file(userinfo_file);
if (!file.is_open())
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
for (string line; getline(file, line);)
if (strncmp(from_user, line.c_str(), strlen_from_user) == 0) // valid user
return true;
return false;
}
void auth(const int sockfd, int &mail_stat)
{
send_data(sockfd, reply_code[25]); // require username
sleep(3);
vector<char> name;
{
array<char, 50> encrypted_name;
if (recv(sockfd, encrypted_name.data(), sizeof(encrypted_name), 0) <= 0)
send_data(sockfd, reply_code[16]);
cout << "Request stream: " << encrypted_name.data() << "\n";
name = base64_decode(encrypted_name.data());
cout << "Decoded username: " << name.data() << "\n";
send_data(sockfd, reply_code[26]); // require passwd
sleep(3);
}
vector<char> passwd;
{
array<char, 50> encrypted_passwd;
if (recv(sockfd, encrypted_passwd.data(), sizeof(encrypted_passwd), 0) <= 0)
send_data(sockfd, reply_code[16]);
cout << "Request stream: " << encrypted_passwd.data() << "\n";
passwd = base64_decode(encrypted_passwd.data());
cout << "Decoded password: " << passwd.data() << "\n";
}
if (check_name_pass(name.data(), passwd.data())) // check username and passwd
{
mail_stat = 13; // changing mail status
send_data(sockfd, reply_code[27]);
}
else
send_data(sockfd, reply_code[16]);
}
void user_quit(const char *const from_user)
{
const auto strlen_from_user = strlen(from_user);
fstream file(userstat_file, ios::in | ios::out | ios::trunc); // same as fopen(userstat_file, "w+")
if (!file.is_open())
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
for (string line; getline(file, line);)
{
if (strncmp(line.c_str(), from_user, strlen_from_user) == 0)
{
long len = strlen(line.c_str());
if (getline(file, line))
{
len = -len;
file.seekp(len, ios::cur);
// fseek(fp, len, SEEK_CUR);
file << line; // file.write(line.data(), line.size());
// fputs(data, fp);
// len = strlen(data);
file.seekg(line.size(), ios::cur);
// fseek(fp, len, SEEK_CUR);
}
}
}
}
bool check_name_pass(const char *const name, const char *const pass)
{
fstream file;
file.open(userinfo_file, ios::in); // fopen(userinfo_file, "r")
if (!file.is_open())
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
for (string line; getline(file, line);)
{
if (strncmp(line.c_str(), name, strlen(name)) != 0) // find username
continue;
if (strncmp(strchr(line.c_str(), ' ') + 1, pass, strlen(pass)) != 0)
return false; // invalid passwd
// valid passwd
file.close();
file.open(userstat_file, ios::in | ios::out | ios::trunc); // same as fopen(userstat_file, "w+")
if (!file.is_open())
{
perror("File opening failed");
exit(EXIT_FAILURE);
}
const string new_status = name + " on"s; // change the status of the user to ON
file << new_status;
// fwrite(new_status.c_str(), 1, strlen(new_status.c_str()), fp);
return true; // success
}
return false; // invalid username
}