-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasswordFile.c
171 lines (130 loc) · 4.12 KB
/
PasswordFile.c
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
#include "PasswordFile.h"
#include "Entropy.h"
#include "Hash.h"
char *PasswordFileGenerateEntry(char *RetStr, const char *User, const char *PassType, const char *Password, const char *Extra)
{
char *Salt=NULL, *Hash=NULL, *Tempstr=NULL;
if (StrEnd(PassType) || (strcasecmp(PassType, "plain")==0) )
{
Salt=CopyStr(Salt, "");
Hash=CopyStr(Hash, Password);
}
else
{
Salt=GetRandomAlphabetStr(Salt, 20);
Tempstr=MCatStr(Tempstr, Salt, Password, NULL);
HashBytes(&Hash, PassType, Tempstr, StrLen(Tempstr), ENCODE_BASE64);
}
Tempstr=QuoteCharsInStr(Tempstr, Extra, ":");
RetStr=MCopyStr(RetStr, User, ":", PassType, ":", Salt, ":", Hash, ":", Tempstr, "\n", NULL);
Destroy(Tempstr);
Destroy(Hash);
Destroy(Salt);
return(RetStr);
}
int PasswordFileAdd(const char *Path, const char *PassType, const char *User, const char *Password, const char *Extra)
{
STREAM *Old, *New;
char *Tempstr=NULL, *Token=NULL, *Salt=NULL;
int RetVal=FALSE;
Old=STREAMOpen(Path, "r");
Tempstr=MCopyStr(Tempstr, Path, "+", NULL);
New=STREAMOpen(Tempstr, "w");
if (New)
{
if (Old)
{
Tempstr=STREAMReadLine(Tempstr, Old);
while (Tempstr)
{
GetToken(Tempstr, ":", &Token, 0);
if (strcmp(User, Token) !=0) STREAMWriteLine(Tempstr, New);
Tempstr=STREAMReadLine(Tempstr, Old);
}
STREAMClose(Old);
}
Tempstr=PasswordFileGenerateEntry(Tempstr, User, PassType, Password, Extra);
STREAMWriteLine(Tempstr, New);
rename(New->Path, Path);
STREAMClose(New);
RetVal=TRUE;
}
Destroy(Tempstr);
Destroy(Token);
Destroy(Salt);
return(RetVal);
}
int PasswordFileAppend(const char *Path, const char *PassType, const char *User, const char *Password, const char *Extra)
{
STREAM *F;
char *Tempstr=NULL, *Token=NULL;
int RetVal=FALSE;
F=STREAMOpen(Path, "a");
if (! F) return(FALSE);
STREAMSeek(F, 0, SEEK_END);
Tempstr=PasswordFileGenerateEntry(Tempstr, User, PassType, Password, Extra);
STREAMWriteLine(Tempstr, F);
STREAMClose(F);
Destroy(Tempstr);
Destroy(Token);
return(RetVal);
}
static int PasswordFileMatchItem(const char *Data, const char *User, const char *Password)
{
char *Tempstr=NULL, *Token=NULL, *Salt=NULL, *ProvidedCred=NULL, *StoredCred=NULL;
const char *ptr;
int result=FALSE;
ptr=GetToken(Data, ":", &Token, 0);
if (strcmp(Token, User)==0)
{
ptr=GetToken(ptr, ":", &Token, 0);
ptr=GetToken(ptr, ":", &Salt, 0);
ptr=GetToken(ptr, ":", &StoredCred, 0);
if ( (! StrValid(Token)) || (strcmp(Token, "plain")==0) ) ProvidedCred=CopyStr(ProvidedCred, Password);
else
{
Tempstr=MCopyStr(Tempstr, Salt, Password, NULL);
HashBytes(&ProvidedCred, Token, Tempstr, StrLen(Tempstr), ENCODE_BASE64);
}
if (strcmp(ProvidedCred, StoredCred) == 0) result=TRUE;
}
Destroy(Tempstr);
Destroy(StoredCred);
Destroy(ProvidedCred);
Destroy(Token);
Destroy(Salt);
return(result);
}
int PasswordFileCheck(const char *Path, const char *User, const char *Password, char **Extra)
{
STREAM *F;
char *Tempstr=NULL;
const char *ptr;
int result=FALSE;
if (! StrValid(Path)) return(FALSE);
if (! StrValid(User)) return(FALSE);
F=STREAMOpen(Path, "r");
if (F)
{
Tempstr=STREAMReadLine(Tempstr, F);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
result=PasswordFileMatchItem(Tempstr, User, Password);
if (result)
{
if (Extra)
{
ptr=strrchr(Tempstr, ':');
*Extra=UnQuoteStr(*Extra, ptr+1);
}
break;
}
Tempstr=STREAMReadLine(Tempstr, F);
}
STREAMClose(F);
}
else RaiseError(ERRFLAG_DEBUG, "PasswordFileCheck", "can't open %s", Path);
Destroy(Tempstr);
return(result);
}