From 5a36d207b1e79b53cd0e440e5b5229148ad23772 Mon Sep 17 00:00:00 2001 From: Greg Hudson Date: Thu, 30 Oct 2008 19:32:50 +0000 Subject: [PATCH] Use the k5buf module instead of strcpy/strcat in several places ticket: 6200 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20941 dc483132-0cff-0310-8789-dd5450dbe970 --- src/appl/gssftp/ftpd/ftpcmd.y | 12 ++-- src/clients/ksu/authorization.c | 23 +++---- src/lib/crypto/t_hmac.c | 10 +-- src/lib/gssapi/generic/gssapiP_generic.h | 1 + src/lib/gssapi/mechglue/oid_ops.c | 65 +++++-------------- src/lib/kadm5/alt_prof.c | 38 ++++------- src/lib/kadm5/str_conv.c | 42 ++++-------- src/lib/krb5/krb/preauth.c | 25 +++---- src/lib/krb5/krb/srv_rcache.c | 60 ++++------------- src/lib/krb5/os/sendto_kdc.c | 22 ++++--- .../kdb/ldap/libkdb_ldap/ldap_principal.c | 65 ++++--------------- 11 files changed, 114 insertions(+), 249 deletions(-) diff --git a/src/appl/gssftp/ftpd/ftpcmd.y b/src/appl/gssftp/ftpd/ftpcmd.y index ae1d11d2f..f304541a9 100644 --- a/src/appl/gssftp/ftpd/ftpcmd.y +++ b/src/appl/gssftp/ftpd/ftpcmd.y @@ -66,6 +66,7 @@ static char sccsid[] = "@(#)ftpcmd.y 5.24 (Berkeley) 2/25/91"; #include #include #include +#include #include "ftpd_var.h" @@ -1470,6 +1471,7 @@ help(ctab, s) if (s == 0) { register int i, j, w; int columns, lines; + struct k5buf buf; lreply(214, "The following %scommands are recognized %s.", ftype, "(* =>'s unimplemented)"); @@ -1478,16 +1480,18 @@ help(ctab, s) columns = 1; lines = (NCMDS + columns - 1) / columns; for (i = 0; i < lines; i++) { - strcpy(str, " "); + krb5int_buf_init_fixed(&buf, str, sizeof(str)); + krb5int_buf_add(&buf, " "); for (j = 0; j < columns; j++) { c = ctab + j * lines + i; - sprintf(&str[strlen(str)], "%s%c", c->name, - c->implemented ? ' ' : '*'); + krb5int_buf_add_fmt(&buf, "%s%c", c->name, + c->implemented ? ' ' + : '*'); if (c + lines >= &ctab[NCMDS]) break; w = strlen(c->name) + 1; while (w < width) { - strcat(str, " "); + krb5int_buf_add(&buf, " "); w++; } } diff --git a/src/clients/ksu/authorization.c b/src/clients/ksu/authorization.c index b5713c103..829251d81 100644 --- a/src/clients/ksu/authorization.c +++ b/src/clients/ksu/authorization.c @@ -498,13 +498,9 @@ krb5_boolean find_first_cmd_that_exists(fcmd_arr, cmd_out, err_out) int i = 0; krb5_boolean retbool= FALSE; int j =0; - char * err; - unsigned int max_ln=0; - unsigned int tln=0; + struct k5buf buf; while(fcmd_arr[i]){ - tln = strlen(fcmd_arr[i]); - if ( tln > max_ln) max_ln = tln; if (!stat (fcmd_arr[i], &st_temp )){ *cmd_out = xstrdup(fcmd_arr[i]); retbool = TRUE; @@ -514,15 +510,16 @@ krb5_boolean find_first_cmd_that_exists(fcmd_arr, cmd_out, err_out) } if (retbool == FALSE ){ - err = (char *) xcalloc((80 + (max_ln+2)*i) ,sizeof(char)); - strcpy(err,"Error: not found -> "); - for(j= 0; j < i; j ++){ - strcat(err, " "); - strcat(err, fcmd_arr[j]); - strcat(err, " "); + krb5int_buf_init_dynamic(&buf); + krb5int_buf_add(&buf, "Error: not found -> "); + for(j= 0; j < i; j ++) + krb5int_buf_add_fmt(&buf, " %s ", fcmd_arr[j]); + krb5int_buf_add(&buf, "\n"); + *err_out = krb5int_buf_cstr(&buf); + if (*err_out == NULL) { + perror(prog_name); + exit(1); } - strcat(err, "\n"); - *err_out = err; } diff --git a/src/lib/crypto/t_hmac.c b/src/lib/crypto/t_hmac.c index 000e64b85..63b90c76f 100644 --- a/src/lib/crypto/t_hmac.c +++ b/src/lib/crypto/t_hmac.c @@ -135,6 +135,7 @@ static void test_hmac() krb5_error_code err; int i, j; int lose = 0; + struct k5buf buf; /* RFC 2202 test vector. */ static const struct hmac_test md5tests[] = { @@ -240,11 +241,12 @@ static void test_hmac() exit(1); } - if (sizeof(stroutbuf) - 3 < 2 * out.length) - abort(); - strcpy(stroutbuf, "0x"); + krb5int_buf_init_fixed(&buf, stroutbuf, sizeof(stroutbuf)); + krb5int_buf_add(&buf, "0x"); for (j = 0; j < out.length; j++) - sprintf(stroutbuf + strlen(stroutbuf), "%02x", 0xff & outbuf[j]); + krb5int_buf_add_fmt(&buf, "%02x", 0xff & outbuf[j]); + if (krb5int_buf_cstr(&buf) == NULL) + abort(); if (strcmp(stroutbuf, md5tests[i].hexdigest)) { printf("*** CHECK FAILED!\n" "\tReturned: %s.\n" diff --git a/src/lib/gssapi/generic/gssapiP_generic.h b/src/lib/gssapi/generic/gssapiP_generic.h index 747c6fe6c..03f4a1305 100644 --- a/src/lib/gssapi/generic/gssapiP_generic.h +++ b/src/lib/gssapi/generic/gssapiP_generic.h @@ -45,6 +45,7 @@ #include #include "k5-platform.h" +#include "k5-buf.h" typedef UINT64_TYPE gssint_uint64; /** helper macros **/ diff --git a/src/lib/gssapi/mechglue/oid_ops.c b/src/lib/gssapi/mechglue/oid_ops.c index 11a509984..44d79f361 100644 --- a/src/lib/gssapi/mechglue/oid_ops.c +++ b/src/lib/gssapi/mechglue/oid_ops.c @@ -219,13 +219,11 @@ generic_gss_oid_to_str(minor_status, oid, oid_str) const gss_OID_desc * const oid; gss_buffer_t oid_str; { - char numstr[128]; OM_uint32 number; - int numshift; - OM_uint32 string_length; OM_uint32 i; unsigned char *cp; char *bp; + struct k5buf buf; if (minor_status != NULL) *minor_status = 0; @@ -243,60 +241,29 @@ generic_gss_oid_to_str(minor_status, oid, oid_str) /* Decoded according to krb5/gssapi_krb5.c */ - /* First determine the size of the string */ - string_length = 0; - number = 0; - numshift = 0; cp = (unsigned char *) oid->elements; number = (unsigned long) cp[0]; - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number/40); - string_length += strlen(numstr); - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number%40); - string_length += strlen(numstr); + krb5int_buf_init_dynamic(&buf); + krb5int_buf_add_fmt(&buf, "{ %lu %lu ", (unsigned long)number/40, + (unsigned long)number%40); + number = 0; + cp = (unsigned char *) oid->elements; for (i=1; ilength; i++) { - if ((OM_uint32) (numshift+7) < (sizeof (OM_uint32)*8)) {/* XXX */ - number = (number << 7) | (cp[i] & 0x7f); - numshift += 7; - } - else { - return(GSS_S_FAILURE); - } + number = (number << 7) | (cp[i] & 0x7f); if ((cp[i] & 0x80) == 0) { - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number); - string_length += strlen(numstr); + krb5int_buf_add_fmt(&buf, "%lu ", (unsigned long)number); number = 0; - numshift = 0; } } - /* - * If we get here, we've calculated the length of "n n n ... n ". Add 4 - * here for "{ " and "}\0". - */ - string_length += 4; - if ((bp = (char *) malloc(string_length))) { - strcpy(bp, "{ "); - number = (OM_uint32) cp[0]; - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number/40); - strcat(bp, numstr); - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number%40); - strcat(bp, numstr); - number = 0; - cp = (unsigned char *) oid->elements; - for (i=1; ilength; i++) { - number = (number << 7) | (cp[i] & 0x7f); - if ((cp[i] & 0x80) == 0) { - snprintf(numstr, sizeof(numstr), "%lu ", (unsigned long)number); - strcat(bp, numstr); - number = 0; - } - } - strcat(bp, "}"); - oid_str->length = strlen(bp)+1; - oid_str->value = (void *) bp; - return(GSS_S_COMPLETE); + krb5int_buf_add(&buf, "}"); + bp = krb5int_buf_cstr(&buf); + if (bp == NULL) { + *minor_status = ENOMEM; + return(GSS_S_FAILURE); } - *minor_status = ENOMEM; - return(GSS_S_FAILURE); + oid_str->length = krb5int_buf_len(&buf)+1; + oid_str->value = (void *) bp; + return(GSS_S_COMPLETE); } OM_uint32 diff --git a/src/lib/kadm5/alt_prof.c b/src/lib/kadm5/alt_prof.c index cfcbd79aa..8fa742c2e 100644 --- a/src/lib/kadm5/alt_prof.c +++ b/src/lib/kadm5/alt_prof.c @@ -73,42 +73,28 @@ krb5_aprof_init(fname, envname, acontextp) krb5_error_code kret; profile_t profile; const char *kdc_config; - size_t krb5_config_len, kdc_config_len; char *profile_path; char **filenames; int i; + struct k5buf buf; kret = krb5_get_default_config_files (&filenames); if (kret) return kret; - krb5_config_len = 0; - for (i = 0; filenames[i] != NULL; i++) - krb5_config_len += strlen(filenames[i]) + 1; - if (i > 0) - krb5_config_len--; - if (envname == NULL - || (kdc_config = getenv(envname)) == NULL) + if (envname == NULL || (kdc_config = getenv(envname)) == NULL) kdc_config = fname; - if (kdc_config == NULL) - kdc_config_len = 0; - else - kdc_config_len = strlen(kdc_config); - profile_path = malloc(2 + krb5_config_len + kdc_config_len); - if (profile_path == NULL) { - krb5_free_config_files(filenames); - return ENOMEM; + krb5int_buf_init_dynamic(&buf); + if (kdc_config) + krb5int_buf_add(&buf, kdc_config); + for (i = 0; filenames[i] != NULL; i++) { + if (krb5int_buf_len(&buf) > 0) + krb5int_buf_add(&buf, ":"); + krb5int_buf_add(&buf, filenames[i]); } - if (kdc_config_len) - strcpy(profile_path, kdc_config); - else - profile_path[0] = 0; - if (krb5_config_len) - for (i = 0; filenames[i] != NULL; i++) { - if (kdc_config_len || i) - strcat(profile_path, ":"); - strcat(profile_path, filenames[i]); - } krb5_free_config_files(filenames); + profile_path = krb5int_buf_cstr(&buf); + if (profile_path == NULL) + return ENOMEM; profile = (profile_t) NULL; kret = profile_init_path(profile_path, &profile); free(profile_path); diff --git a/src/lib/kadm5/str_conv.c b/src/lib/kadm5/str_conv.c index b9e58aac6..7ffcc8e11 100644 --- a/src/lib/kadm5/str_conv.c +++ b/src/lib/kadm5/str_conv.c @@ -173,45 +173,29 @@ krb5_flags_to_string(flags, sep, buffer, buflen) int i; krb5_flags pflags; const char *sepstring; - char *op; - int initial; - krb5_error_code retval; + struct k5buf buf; - retval = 0; - op = buffer; pflags = 0; - initial = 1; sepstring = (sep) ? sep : flags_default_sep; + krb5int_buf_init_fixed(&buf, buffer, buflen); /* Blast through the table matching all we can */ for (i=0; i 0) + krb5int_buf_add(&buf, sepstring); + krb5int_buf_add(&buf, flags_table[i].fl_output); /* Keep track of what we matched */ pflags |= flags_table[i].fl_flags; } } - if (!retval) { - /* See if there's any leftovers */ - if (flags & ~pflags) - retval = EINVAL; - else if (initial) - *buffer = '\0'; - } - return(retval); + if (krb5int_buf_cstr(&buf) == NULL) + return(ENOMEM); + + /* See if there's any leftovers */ + if (flags & ~pflags) + return(EINVAL); + + return(0); } krb5_error_code diff --git a/src/lib/krb5/krb/preauth.c b/src/lib/krb5/krb/preauth.c index 0e7c279e1..32249df87 100644 --- a/src/lib/krb5/krb/preauth.c +++ b/src/lib/krb5/krb/preauth.c @@ -397,10 +397,7 @@ char *handle_sam_labels(krb5_sam_challenge *sc) unsigned int prompt_len = sc->sam_response_prompt.length; char *challenge = sc->sam_challenge.data; unsigned int challenge_len = sc->sam_challenge.length; - char *prompt1, *p; - char *sep1 = ": ["; - char *sep2 = "]\n"; - char *sep3 = ": "; + struct k5buf buf; if (sc->sam_cksum.length == 0) { /* or invalid -- but lets just handle presence now XXX */ @@ -438,20 +435,16 @@ char *handle_sam_labels(krb5_sam_challenge *sc) Challenge for Digital Pathways mechanism: [134591] Passcode: */ - p = prompt1 = malloc(label_len + strlen(sep1) + - challenge_len + strlen(sep2) + - prompt_len+ strlen(sep3) + 1); - if (p == NULL) - return NULL; + krb5int_buf_init_dynamic(&buf); if (challenge_len) { - strncpy(p, label, label_len); p += label_len; - strcpy(p, sep1); p += strlen(sep1); - strncpy(p, challenge, challenge_len); p += challenge_len; - strcpy(p, sep2); p += strlen(sep2); + krb5int_buf_add_len(&buf, label, label_len); + krb5int_buf_add(&buf, ": ["); + krb5int_buf_add_len(&buf, challenge, challenge_len); + krb5int_buf_add(&buf, "]\n"); } - strncpy(p, prompt, prompt_len); p += prompt_len; - strcpy(p, sep3); /* p += strlen(sep3); */ - return prompt1; + krb5int_buf_add_len(&buf, prompt, prompt_len); + krb5int_buf_add(&buf, ": "); + return krb5int_buf_cstr(&buf); } /* diff --git a/src/lib/krb5/krb/srv_rcache.c b/src/lib/krb5/krb/srv_rcache.c index f3ea3ee5a..4ce073301 100644 --- a/src/lib/krb5/krb/srv_rcache.c +++ b/src/lib/krb5/krb/srv_rcache.c @@ -39,13 +39,10 @@ krb5_get_server_rcache(krb5_context context, const krb5_data *piece, { krb5_rcache rcache = 0; char *cachename = 0, *cachetype; - char tmp[4]; krb5_error_code retval; - unsigned int p, i; - unsigned int len; - + unsigned int i; + struct k5buf buf; #ifdef HAVE_GETEUID - unsigned long tens; unsigned long uid = geteuid(); #endif @@ -54,55 +51,24 @@ krb5_get_server_rcache(krb5_context context, const krb5_data *piece, cachetype = krb5_rc_default_type(context); - len = piece->length + 3 + 1; + krb5int_buf_init_dynamic(&buf); + krb5int_buf_add(&buf, cachetype); + krb5int_buf_add(&buf, ":"); for (i = 0; i < piece->length; i++) { if (piece->data[i] == '-') - len++; + krb5int_buf_add(&buf, "--"); else if (!isvalidrcname((int) piece->data[i])) - len += 3; + krb5int_buf_add_fmt(&buf, "-%03o", piece->data[i]); + else + krb5int_buf_add_len(&buf, &piece->data[i], 1); } - #ifdef HAVE_GETEUID - len += 2; /* _ */ - for (tens = 1; (uid / tens) > 9 ; tens *= 10) - len++; + krb5int_buf_add_fmt(&buf, "_%lu", uid); #endif - - cachename = malloc(strlen(cachetype) + 5 + len); - if (!cachename) { - retval = ENOMEM; - goto cleanup; - } - strcpy(cachename, cachetype); - p = strlen(cachename); - cachename[p++] = ':'; - for (i = 0; i < piece->length; i++) { - if (piece->data[i] == '-') { - cachename[p++] = '-'; - cachename[p++] = '-'; - continue; - } - if (!isvalidrcname((int) piece->data[i])) { - snprintf(tmp, sizeof(tmp), "%03o", piece->data[i]); - cachename[p++] = '-'; - cachename[p++] = tmp[0]; - cachename[p++] = tmp[1]; - cachename[p++] = tmp[2]; - continue; - } - cachename[p++] = piece->data[i]; - } - -#ifdef HAVE_GETEUID - cachename[p++] = '_'; - while (tens) { - cachename[p++] = '0' + ((uid / tens) % 10); - tens /= 10; - } -#endif - - cachename[p++] = '\0'; + cachename = krb5int_buf_cstr(&buf); + if (cachename == NULL) + return ENOMEM; retval = krb5_rc_resolve_full(context, &rcache, cachename); if (retval) { diff --git a/src/lib/krb5/os/sendto_kdc.c b/src/lib/krb5/os/sendto_kdc.c index e95be3af0..46ed6f6af 100644 --- a/src/lib/krb5/os/sendto_kdc.c +++ b/src/lib/krb5/os/sendto_kdc.c @@ -116,6 +116,7 @@ krb5int_debug_fprint (const char *fmt, ...) #define max(a,b) ((a) > (b) ? (a) : (b)) #endif char tmpbuf[max(NI_MAXHOST + NI_MAXSERV + 30, 200)]; + struct k5buf buf; if (!krb5int_debug_sendto_kdc) return; @@ -221,26 +222,27 @@ krb5int_debug_fprint (const char *fmt, ...) case 'A': /* %A => addrinfo */ ai = va_arg(args, struct addrinfo *); + krb5int_buf_init_dynamic(&buf); if (ai->ai_socktype == SOCK_DGRAM) - strlcpy(tmpbuf, "dgram", sizeof(tmpbuf)); + krb5int_buf_add(&buf, "dgram"); else if (ai->ai_socktype == SOCK_STREAM) - strlcpy(tmpbuf, "stream", sizeof(tmpbuf)); + krb5int_buf_add(&buf, "stream"); else - snprintf(tmpbuf, sizeof(tmpbuf), "socktype%d", ai->ai_socktype); + krb5int_buf_add_fmt(&buf, "socktype%d", ai->ai_socktype); + if (0 != getnameinfo (ai->ai_addr, ai->ai_addrlen, addrbuf, sizeof (addrbuf), portbuf, sizeof (portbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { if (ai->ai_addr->sa_family == AF_UNSPEC) - strcpy(tmpbuf + strlen(tmpbuf), " AF_UNSPEC"); + krb5int_buf_add(&buf, " AF_UNSPEC"); else - snprintf(tmpbuf + strlen(tmpbuf), - sizeof(tmpbuf)-strlen(tmpbuf), - " af%d", ai->ai_addr->sa_family); + krb5int_buf_add_fmt(&buf, " af%d", ai->ai_addr->sa_family); } else - snprintf(tmpbuf + strlen(tmpbuf), sizeof(tmpbuf)-strlen(tmpbuf), - " %s.%s", addrbuf, portbuf); - putstr(tmpbuf); + krb5int_buf_add_fmt(&buf, " %s.%s", addrbuf, portbuf); + if (krb5int_buf_cstr(&buf)) + putstr(krb5int_buf_cstr(&buf)); + krb5int_free_buf(&buf); break; case 'D': /* %D => krb5_data * */ diff --git a/src/plugins/kdb/ldap/libkdb_ldap/ldap_principal.c b/src/plugins/kdb/ldap/libkdb_ldap/ldap_principal.c index ef6786c68..26f498453 100644 --- a/src/plugins/kdb/ldap/libkdb_ldap/ldap_principal.c +++ b/src/plugins/kdb/ldap/libkdb_ldap/ldap_principal.c @@ -452,62 +452,25 @@ krb5_ldap_parse_principal_name(i_princ_name, o_princ_name) char *i_princ_name; char **o_princ_name; { - char *tmp_princ_name = NULL, *princ_name = NULL, *at_rlm_name = NULL; - int l = 0, m = 0, tmp_princ_name_len = 0, princ_name_len = 0, at_count = 0; - krb5_error_code st = 0; + const char *at_rlm_name, *p; + struct k5buf buf; at_rlm_name = strrchr(i_princ_name, '@'); - if (!at_rlm_name) { *o_princ_name = strdup(i_princ_name); - if (!o_princ_name) { - st = ENOMEM; - goto cleanup; - } + if (!o_princ_name) + return ENOMEM; } else { - tmp_princ_name_len = at_rlm_name - i_princ_name; - - tmp_princ_name = (char *) malloc ((unsigned) tmp_princ_name_len + 1); - if (!tmp_princ_name) { - st = ENOMEM; - goto cleanup; - } - memset(tmp_princ_name, 0, (unsigned) tmp_princ_name_len + 1); - memcpy(tmp_princ_name, i_princ_name, (unsigned) tmp_princ_name_len); - - l = 0; - while (tmp_princ_name[l]) { - if (tmp_princ_name[l++] == '@') - at_count++; - } - - princ_name_len = strlen(i_princ_name) + at_count + 1; - princ_name = (char *) malloc ((unsigned) princ_name_len); - if (!princ_name) { - st = ENOMEM; - goto cleanup; + krb5int_buf_init_dynamic(&buf); + for (p = i_princ_name; p < at_rlm_name; p++) { + if (*p == '@') + krb5int_buf_add(&buf, "\\"); + krb5int_buf_add_len(&buf, p, 1); } - memset(princ_name, 0, (unsigned) princ_name_len); - - l = 0; - m = 0; - while (tmp_princ_name[l]) { - if (tmp_princ_name[l] == '@') { - princ_name[m++]='\\'; - } - princ_name[m++]=tmp_princ_name[l++]; - } - strcat(princ_name, at_rlm_name); - - *o_princ_name = princ_name; + krb5int_buf_add(&buf, at_rlm_name); + *o_princ_name = krb5int_buf_cstr(&buf); + if (!*o_princ_name) + return ENOMEM; } - -cleanup: - - if (tmp_princ_name) { - free(tmp_princ_name); - tmp_princ_name = NULL; - } - - return st; + return 0; } -- 2.26.2