Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
git/credential-store.c at master · freemint/git · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
freemint
git
Repository navigation
Code
Issues
3
(3)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
git
/
credential-store.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
194 lines (169 loc) · 5 KB
master
Breadcrumbs
git
/
credential-store.c
Copy path
Top
File metadata and controls
Code
Blame
194 lines (169 loc) · 5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include
"cache.h"
#include
"lockfile.h"
#include
"credential.h"
#include
"string-list.h"
#include
"parse-options.h"
static
struct
lock_file
credential_lock
;
static
int
parse_credential_file
(
const
char
*
fn
,
struct
credential
*
c
,
void
(
*
match_cb
)(
struct
credential
*
),
void
(
*
other_cb
)(
struct
strbuf
*
))
{
FILE
*
fh
;
struct
strbuf
line
=
STRBUF_INIT
;
struct
credential
entry
=
CREDENTIAL_INIT
;
int
found_credential
=
0
;
fh
=
fopen
(
fn
,
"r"
);
if
(!
fh
) {
if
(
errno
!=
ENOENT
&&
errno
!=
EACCES
)
die_errno
(
"unable to open %s"
,
fn
);
return
found_credential
;
}
while
(
strbuf_getline_lf
(
&
line
,
fh
)
!=
EOF
) {
credential_from_url
(
&
entry
,
line
.
buf
);
if
(
entry
.
username
&&
entry
.
password
&&
credential_match
(
c
,
&
entry
)) {
found_credential
=
1
;
if
(
match_cb
) {
match_cb
(
&
entry
);
break
;
}
}
else
if
(
other_cb
)
other_cb
(
&
line
);
}
credential_clear
(
&
entry
);
strbuf_release
(
&
line
);
fclose
(
fh
);
return
found_credential
;
}
static
void
print_entry
(
struct
credential
*
c
)
{
printf
(
"username=%s\n"
,
c
->
username
);
printf
(
"password=%s\n"
,
c
->
password
);
}
static
void
print_line
(
struct
strbuf
*
buf
)
{
strbuf_addch
(
buf
,
'\n'
);
write_or_die
(
get_lock_file_fd
(
&
credential_lock
),
buf
->
buf
,
buf
->
len
);
}
static
void
rewrite_credential_file
(
const
char
*
fn
,
struct
credential
*
c
,
struct
strbuf
*
extra
)
{
if
(
hold_lock_file_for_update
(
&
credential_lock
,
fn
,
0
)
<
0
)
die_errno
(
"unable to get credential storage lock"
);
if
(
extra
)
print_line
(
extra
);
parse_credential_file
(
fn
,
c
,
NULL
,
print_line
);
if
(
commit_lock_file
(
&
credential_lock
)
<
0
)
die_errno
(
"unable to write credential store"
);
}
static
void
store_credential_file
(
const
char
*
fn
,
struct
credential
*
c
)
{
struct
strbuf
buf
=
STRBUF_INIT
;
strbuf_addf
(
&
buf
,
"%s://"
,
c
->
protocol
);
strbuf_addstr_urlencode
(
&
buf
,
c
->
username
,
1
);
strbuf_addch
(
&
buf
,
':'
);
strbuf_addstr_urlencode
(
&
buf
,
c
->
password
,
1
);
strbuf_addch
(
&
buf
,
'@'
);
if
(
c
->
host
)
strbuf_addstr_urlencode
(
&
buf
,
c
->
host
,
1
);
if
(
c
->
path
) {
strbuf_addch
(
&
buf
,
'/'
);
strbuf_addstr_urlencode
(
&
buf
,
c
->
path
,
0
);
}
rewrite_credential_file
(
fn
,
c
,
&
buf
);
strbuf_release
(
&
buf
);
}
static
void
store_credential
(
const
struct
string_list
*
fns
,
struct
credential
*
c
)
{
struct
string_list_item
*
fn
;
/*
* Sanity check that what we are storing is actually sensible.
* In particular, we can't make a URL without a protocol field.
* Without either a host or pathname (depending on the scheme),
* we have no primary key. And without a username and password,
* we are not actually storing a credential.
*/
if
(!
c
->
protocol
||
!(
c
->
host
||
c
->
path
)
||
!
c
->
username
||
!
c
->
password
)
return
;
for_each_string_list_item
(
fn
,
fns
)
if
(!
access
(
fn
->
string
,
F_OK
)) {
store_credential_file
(
fn
->
string
,
c
);
return
;
}
/*
* Write credential to the filename specified by fns->items[0], thus
* creating it
*/
if
(
fns
->
nr
)
store_credential_file
(
fns
->
items
[
0
].
string
,
c
);
}
static
void
remove_credential
(
const
struct
string_list
*
fns
,
struct
credential
*
c
)
{
struct
string_list_item
*
fn
;
/*
* Sanity check that we actually have something to match
* against. The input we get is a restrictive pattern,
* so technically a blank credential means "erase everything".
* But it is too easy to accidentally send this, since it is equivalent
* to empty input. So explicitly disallow it, and require that the
* pattern have some actual content to match.
*/
if
(!
c
->
protocol
&&
!
c
->
host
&&
!
c
->
path
&&
!
c
->
username
)
return
;
for_each_string_list_item
(
fn
,
fns
)
if
(!
access
(
fn
->
string
,
F_OK
))
rewrite_credential_file
(
fn
->
string
,
c
,
NULL
);
}
static
void
lookup_credential
(
const
struct
string_list
*
fns
,
struct
credential
*
c
)
{
struct
string_list_item
*
fn
;
for_each_string_list_item
(
fn
,
fns
)
if
(
parse_credential_file
(
fn
->
string
,
c
,
print_entry
,
NULL
))
return
;
/* Found credential */
}
int
cmd_main
(
int
argc
,
const
char
*
*
argv
)
{
const
char
*
const
usage
[]
=
{
"git credential-store [<options>] <action>"
,
NULL
};
const
char
*
op
;
struct
credential
c
=
CREDENTIAL_INIT
;
struct
string_list
fns
=
STRING_LIST_INIT_DUP
;
char
*
file
=
NULL
;
struct
option
options
[]
=
{
OPT_STRING
(
0
,
"file"
,
&
file
,
"path"
,
"fetch and store credentials in <path>"
),
OPT_END
()
};
umask
(
077
);
argc
=
parse_options
(
argc
, (
const
char
*
*
)
argv
,
NULL
,
options
,
usage
,
0
);
if
(
argc
!=
1
)
usage_with_options
(
usage
,
options
);
op
=
argv
[
0
];
if
(
file
) {
string_list_append
(
&
fns
,
file
);
}
else
{
if
((
file
=
expand_user_path
(
"~/.git-credentials"
)))
string_list_append_nodup
(
&
fns
,
file
);
file
=
xdg_config_home
(
"credentials"
);
if
(
file
)
string_list_append_nodup
(
&
fns
,
file
);
}
if
(!
fns
.
nr
)
die
(
"unable to set up default path; use --file"
);
if
(
credential_read
(
&
c
,
stdin
)
<
0
)
die
(
"unable to read credential"
);
if
(!
strcmp
(
op
,
"get"
))
lookup_credential
(
&
fns
,
&
c
);
else
if
(!
strcmp
(
op
,
"erase"
))
remove_credential
(
&
fns
,
&
c
);
else
if
(!
strcmp
(
op
,
"store"
))
store_credential
(
&
fns
,
&
c
);
else
;
/* Ignore unknown operation. */
string_list_clear
(
&
fns
,
0
);
return
0
;
}
You can’t perform that action at this time.