Remove ncat() in favor of just counting bytes and pre-allocating what is

needed.
This commit is contained in:
Todd C. Miller
2005-06-27 00:10:06 +00:00
parent 73730b872b
commit 4e86b1d74a

89
ldap.c
View File

@@ -366,47 +366,6 @@ sudo_ldap_parse_options(ld, entry)
ldap_value_free(v); ldap_value_free(v);
} }
/*
* Concatenate strings, dynamically growing them as necessary.
* Strings can be arbitrarily long and are allocated/reallocated on
* the fly. Make sure to free them when you are done.
*
* Usage:
*
* char *s=NULL;
* size_t sz;
*
* ncat(&s,&sz,"This ");
* ncat(&s,&sz,"is ");
* ncat(&s,&sz,"an ");
* ncat(&s,&sz,"arbitrarily ");
* ncat(&s,&sz,"long ");
* ncat(&s,&sz,"string!");
*
* printf("String Value='%s', but has %d bytes allocated\n",s,sz);
*
*/
void
ncat(s, sz, src)
char **s;
size_t *sz;
char *src;
{
size_t nsz;
/* handle initial alloc */
if (*s == NULL) {
*s = estrdup(src);
*sz = strlen(src) + 1;
return;
}
/* handle realloc */
nsz = strlen(*s) + strlen(src) + 1;
if (*sz < nsz)
*s = erealloc((void *) *s, *sz = nsz * 2);
strlcat(*s, src, *sz);
}
/* /*
* builds together a filter to check against ldap * builds together a filter to check against ldap
*/ */
@@ -416,41 +375,47 @@ sudo_ldap_build_pass1(pw)
{ {
struct group *grp; struct group *grp;
size_t sz; size_t sz;
char *b = NULL; char *buf;
int i; int i;
/* global OR */ /* Start with (|(sudoUser=USERNAME)(sudoUser=ALL)) + NUL */
ncat(&b, &sz, "(|"); sz = 29 + strlen(pw->pw_name);
/* build filter sudoUser=user_name */ /* Add space for groups */
ncat(&b, &sz, "(sudoUser="); if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL)
ncat(&b, &sz, pw->pw_name); sz += 12 + strlen(grp->gr_name); /* primary group */
ncat(&b, &sz, ")"); for (i = 0; i < user_ngroups; i++) {
if ((grp = sudo_getgrgid(user_groups[i])) != NULL)
sz += 12 + strlen(grp->gr_name); /* supplementary group */
}
buf = emalloc(sz);
/* Global OR + sudoUser=user_name filter */
(void) strlcpy(buf, "(|(sudoUser=", sz);
(void) strlcat(buf, pw->pw_name, sz);
(void) strlcat(buf, ")", sz);
/* Append primary group */ /* Append primary group */
grp = sudo_getgrgid(pw->pw_gid); if ((grp = sudo_getgrgid(pw->pw_gid)) != NULL) {
if (grp != NULL) { (void) strlcat(buf, "(sudoUser=%", sz);
ncat(&b, &sz, "(sudoUser=%"); (void) strlcat(buf, grp->gr_name, sz);
ncat(&b, &sz, grp->gr_name); (void) strlcat(buf, ")", sz);
ncat(&b, &sz, ")");
} }
/* Append supplementary groups */ /* Append supplementary groups */
for (i = 0; i < user_ngroups; i++) { for (i = 0; i < user_ngroups; i++) {
if ((grp = sudo_getgrgid(user_groups[i])) != NULL) { if ((grp = sudo_getgrgid(user_groups[i])) != NULL) {
ncat(&b, &sz, "(sudoUser=%"); (void) strlcat(buf, "(sudoUser=%", sz);
ncat(&b, &sz, grp->gr_name); (void) strlcat(buf, grp->gr_name, sz);
ncat(&b, &sz, ")"); (void) strlcat(buf, ")", sz);
} }
} }
/* Add ALL to list */ /* Add ALL to list and end the global OR */
ncat(&b, &sz, "(sudoUser=ALL)"); if (strlcat(buf, "(sudoUser=ALL))", sz) >= sz)
errorx(1, "sudo_ldap_build_pass1 allocation mismatch");
/* End of OR List */ return(buf);
ncat(&b, &sz, ")");
return(b);
} }
/* /*