Skip to content

Commit 485a2eb

Browse files
simo5Günther Deschner
authored andcommitted
Move uid to name resolution in its own function.
Reviewed-by: Günther Deschner <[email protected]>
1 parent ee2a157 commit 485a2eb

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

proxy/src/gp_creds.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,24 @@ struct gp_service *gp_creds_match_conn(struct gssproxy_ctx *gpctx,
123123
return NULL;
124124
}
125125

126-
#define PWBUFLEN 2048
127-
static char *get_formatted_string(const char *orig, uid_t target_uid)
126+
static char *uid_to_name(uid_t uid)
128127
{
129128
struct passwd pwd, *res = NULL;
130129
char buffer[PWBUFLEN];
130+
int ret;
131+
132+
ret = getpwuid_r(uid, &pwd, buffer, PWBUFLEN, &res);
133+
if (ret || !res) {
134+
return NULL;
135+
}
136+
return strdup(pwd.pw_name);
137+
}
138+
139+
#define PWBUFLEN 2048
140+
static char *get_formatted_string(const char *orig, uid_t target_uid)
141+
{
131142
int len, left, right;
143+
char *user = NULL;
132144
char *str;
133145
char *tmp;
134146
char *p;
@@ -162,17 +174,17 @@ static char *get_formatted_string(const char *orig, uid_t target_uid)
162174
p = str + (len - right);
163175
break;
164176
case 'u':
165-
if (!res) {
166-
ret = getpwuid_r(target_uid, &pwd, buffer, 2048, &res);
167-
if (ret || !res) {
177+
if (!user) {
178+
user = uid_to_name(target_uid);
179+
if (!user) {
168180
safefree(str);
169181
goto done;
170182
}
171183
}
172184
p++;
173185
left = p - str;
174186
right = len - left;
175-
len = asprintf(&tmp, "%.*s%s%s", left - 2, str, pwd.pw_name, p);
187+
len = asprintf(&tmp, "%.*s%s%s", left - 2, str, user, p);
176188
safefree(str);
177189
if (len == -1) {
178190
goto done;
@@ -188,6 +200,7 @@ static char *get_formatted_string(const char *orig, uid_t target_uid)
188200
}
189201

190202
done:
203+
safefree(user);
191204
return str;
192205
}
193206

0 commit comments

Comments
 (0)