summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2013-04-01 11:51:42 -0400
committerSimo Sorce <simo@redhat.com>2013-04-10 09:10:17 -0400
commit219cd176565419338b9cbcd24f3b8e31961fbc16 (patch)
treeac37eaf69dbcb33bd4cb5a9f0511a1c5f4b18bff
parenta28a79af21adbc8267d214d3341ab1e4a46b756b (diff)
downloadgss-proxy-219cd176565419338b9cbcd24f3b8e31961fbc16.tar.gz
gss-proxy-219cd176565419338b9cbcd24f3b8e31961fbc16.tar.xz
gss-proxy-219cd176565419338b9cbcd24f3b8e31961fbc16.zip
Move string formatting in a separate function
This way it can be reused for keytab path names too Signed-off-by: Simo Sorce <simo@redhat.com> Reviewed-by: Günther Deschner <gdeschner@redhat.com>
-rw-r--r--proxy/src/gp_creds.c123
1 files changed, 66 insertions, 57 deletions
diff --git a/proxy/src/gp_creds.c b/proxy/src/gp_creds.c
index 5cbb25b..7e0f492 100644
--- a/proxy/src/gp_creds.c
+++ b/proxy/src/gp_creds.c
@@ -108,105 +108,114 @@ struct gp_service *gp_creds_match_conn(struct gssproxy_ctx *gpctx,
return NULL;
}
-static char *gp_get_ccache_name(struct gp_service *svc,
- gssx_name *desired_name,
- gss_name_t *requested_name)
+#define PWBUFLEN 2048
+static char *get_formatted_string(const char *orig, uid_t target_uid)
{
- gss_name_t name = GSS_C_NO_NAME;
- gss_OID_desc name_type;
- uint32_t ret_maj = 0;
- uint32_t ret_min = 0;
- char buffer[2048];
- uid_t target_uid;
struct passwd pwd, *res = NULL;
- char *ccache;
+ char buffer[PWBUFLEN];
+ int len, left, right;
+ char *str;
char *tmp;
char *p;
- int len, left, right;
int ret;
- target_uid = svc->euid;
-
- if (desired_name) {
- gp_conv_gssx_to_oid(&desired_name->name_type, &name_type);
-
- if (svc->trusted &&
- (gss_oid_equal(&name_type, GSS_C_NT_STRING_UID_NAME) ||
- gss_oid_equal(&name_type, GSS_C_NT_MACHINE_UID_NAME))) {
- target_uid = atol(desired_name->display_name.octet_string_val);
- } else {
- ret_maj = gp_conv_gssx_to_name(&ret_min, desired_name, &name);
- if (ret_maj) {
- goto done;
- }
- *requested_name = name;
- }
- }
-
- if (svc->krb5.ccache == NULL) {
- ret = asprintf(&ccache, "%s/krb5cc_%u", CCACHE_PATH, target_uid);
- if (ret == -1) {
- ccache = NULL;
- goto done;
- }
-
- return ccache;
- }
-
- ccache = strdup(svc->krb5.ccache);
- if (!ccache) {
- goto done;
+ str = strdup(orig);
+ if (!str) {
+ return NULL;
}
- len = strlen(ccache);
+ len = strlen(str);
- p = ccache;
+ p = str;
while ((p = strchr(p, '%')) != NULL) {
p++;
switch (*p) {
case '%':
- left = p - ccache;
+ left = p - str;
memmove(p, p + 1, left - 1);
len--;
continue;
case 'U':
p++;
- left = p - ccache;
+ left = p - str;
right = len - left;
- len = asprintf(&tmp, "%.*s%d%s", left - 2, ccache, target_uid, p);
- safefree(ccache);
+ len = asprintf(&tmp, "%.*s%d%s", left - 2, str, target_uid, p);
+ safefree(str);
if (len == -1) {
goto done;
}
- ccache = tmp;
- p = ccache + (len - right);
+ str = tmp;
+ p = str + (len - right);
break;
case 'u':
if (!res) {
ret = getpwuid_r(target_uid, &pwd, buffer, 2048, &res);
if (ret || !res) {
- safefree(ccache);
+ safefree(str);
goto done;
}
}
p++;
- left = p - ccache;
+ left = p - str;
right = len - left;
- len = asprintf(&tmp, "%.*s%s%s", left - 2, ccache, pwd.pw_name, p);
- safefree(ccache);
+ len = asprintf(&tmp, "%.*s%s%s", left - 2, str, pwd.pw_name, p);
+ safefree(str);
if (len == -1) {
goto done;
}
- ccache = tmp;
- p = ccache + (len - right);
+ str = tmp;
+ p = str + (len - right);
break;
default:
GPDEBUG("Invalid format code '%%%c'\n", *p);
- safefree(ccache);
+ safefree(str);
goto done;
}
}
done:
+ return str;
+}
+
+static char *gp_get_ccache_name(struct gp_service *svc,
+ gssx_name *desired_name,
+ gss_name_t *requested_name)
+{
+ gss_name_t name = GSS_C_NO_NAME;
+ gss_OID_desc name_type;
+ uint32_t ret_maj = 0;
+ uint32_t ret_min = 0;
+ uid_t target_uid;
+ char *ccache = NULL;
+ int ret;
+
+ target_uid = svc->euid;
+
+ if (desired_name) {
+ gp_conv_gssx_to_oid(&desired_name->name_type, &name_type);
+
+ if (svc->trusted &&
+ (gss_oid_equal(&name_type, GSS_C_NT_STRING_UID_NAME) ||
+ gss_oid_equal(&name_type, GSS_C_NT_MACHINE_UID_NAME))) {
+ target_uid = atol(desired_name->display_name.octet_string_val);
+ } else {
+ ret_maj = gp_conv_gssx_to_name(&ret_min, desired_name, &name);
+ if (ret_maj) {
+ goto done;
+ }
+ *requested_name = name;
+ }
+ }
+
+ if (svc->krb5.ccache == NULL) {
+ ret = asprintf(&ccache, "%s/krb5cc_%u", CCACHE_PATH, target_uid);
+ if (ret == -1) {
+ ccache = NULL;
+ }
+ } else {
+ ccache = get_formatted_string(svc->krb5.ccache, target_uid);
+ }
+
+done:
if (!ccache) {
GPDEBUG("Failed to construct ccache string.\n");
}