Changeset 482 in openpam
- Timestamp:
- Nov 3, 2011, 4:33:02 PM (9 years ago)
- Location:
- trunk/lib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/openpam_configure.c
r481 r482 270 270 * Parse an option. 271 271 * 272 * Returns a pointer to a dynamically allocated pam_opt_t with the name 273 * and value of the next module option, or NULL if the end of the string 274 * was reached or a disallowed non-whitespace character was encountered. 275 * 276 * An option consists of an option name optionally followed by an equal 277 * sign and an option value. 278 * 279 * The structure and strings are allocated as a single object, so a single 280 * free(3) call is sufficient to release the allocated memory. 272 * Returns a dynamically allocated string containing the next module 273 * option, or NULL if the end of the string was reached or a disallowed 274 * non-whitespace character was encountered. 281 275 * 282 276 * If parse_option() is successful, it updates *line to point one … … 285 279 * all other cases, it leaves *line unmodified. 286 280 * 287 * If parse_option() fails to allocate memory for the option structure, it288 * will return NULL and seterrno to a non-zero value.281 * If parse_option() fails to allocate memory, it will return NULL and set 282 * errno to a non-zero value. 289 283 * 290 284 * Allowed characters for option names are all characters in the POSIX … … 295 289 * Note that the entire value must be quoted, not just part of it. 296 290 */ 297 static pam_opt_t*291 static char * 298 292 parse_option(char **line) 299 293 { 300 294 char *nb, *ne, *vb, *ve; 301 295 unsigned char q = 0; 302 pam_opt_t *opt; 303 size_t size; 296 char *option; 304 297 305 298 errno = 0; … … 332 325 vb = ve = ne; 333 326 } 334 size = sizeof *opt; 335 size += ne - nb + 1; 336 size += ve - vb + 1; 337 if ((opt = malloc(size)) == NULL) 327 if ((option = malloc((ne - nb) + 1 + (ve - vb) + 1)) == NULL) 338 328 return (NULL); 339 opt->name = (char *)opt + sizeof *opt;340 strlcpy(opt->name, nb, ne - nb + 1);341 opt->value = opt->name + (ne - nb) + 1;342 strlcpy(opt->value, vb, ve - vb + 1);329 strncpy(option, nb, ne - nb); 330 option[ne - nb] = '='; 331 strncpy(option + (ne - nb), vb, ve - vb); 332 option[(ne - nb) + 1 + (ve - vb) + 1] = '\0'; 343 333 *line = q ? ve + 1 : ve; 344 return (opt );334 return (option); 345 335 } 346 336 … … 381 371 pam_facility_t fclt; 382 372 pam_control_t ctlf; 383 pam_opt_t *opt;384 373 char *line, *str, *name; 374 char *option, **optv; 385 375 int len, ret; 386 376 FILE *f; … … 471 461 472 462 /* get module options */ 473 /* XXX quick and dirty, may waste a few hundred bytes */ 474 if ((this->optv = malloc(sizeof *opt * strlen(line))) == NULL) 475 goto syserr; 476 while ((opt = parse_option(&line)) != NULL) 477 this->optv[this->optc++] = opt; 478 this->optv[this->optc] = NULL; 463 this->optv = NULL; 464 this->optc = 0; 465 while ((option = parse_option(&line)) != NULL) { 466 optv = realloc(this->optv, 467 (this->optc + 2) * sizeof *optv); 468 if (optv == NULL) 469 goto syserr; 470 this->optv = optv; 471 this->optv[this->optc++] = option; 472 this->optv[this->optc] = NULL; 473 } 479 474 if (*line != '\0') { 480 475 openpam_log(PAM_LOG_ERROR, -
trunk/lib/openpam_get_option.c
r478 r482 60 60 { 61 61 pam_chain_t *cur; 62 size_t len; 62 63 int i; 63 64 … … 66 67 RETURNS(NULL); 67 68 cur = pamh->current; 68 for (i = 0; i < cur->optc; ++i) 69 if (strcmp(cur->optv[i]->name, option) == 0) 70 RETURNS(cur->optv[i]->value); 69 len = strlen(option); 70 for (i = 0; i < cur->optc; ++i) { 71 if (strncmp(cur->optv[i], option, len) == 0) { 72 if (cur->optv[i][len] == '\0') 73 RETURNS(&cur->optv[i][len]); 74 else if (cur->optv[i][len] == '=') 75 RETURNS(&cur->optv[i][len + 1]); 76 } 77 } 71 78 RETURNS(NULL); 72 79 } -
trunk/lib/openpam_impl.h
r479 r482 72 72 } pam_facility_t; 73 73 74 typedef struct pam_opt pam_opt_t;75 struct pam_opt {76 char *name;77 char *value;78 char str[];79 };80 81 74 typedef struct pam_chain pam_chain_t; 82 75 struct pam_chain { … … 84 77 int flag; 85 78 int optc; 86 pam_opt_t**optv;79 char **optv; 87 80 pam_chain_t *next; 88 81 }; -
trunk/lib/openpam_set_option.c
r478 r482 63 63 { 64 64 pam_chain_t *cur; 65 pam_opt_t *opt, **optv; 66 int i, ol, vl; 65 char *opt, **optv; 66 size_t len; 67 int i; 67 68 68 69 ENTERS(option); … … 70 71 RETURNC(PAM_SYSTEM_ERR); 71 72 cur = pamh->current; 72 for ( i = 0; i < cur->optc; ++i)73 if ( strcmp(cur->optv[i]->name, option) == 0)73 for (len = 0; option[len] != '\0'; ++len) 74 if (option[len] == '=') 74 75 break; 76 for (i = 0; i < cur->optc; ++i) { 77 if (strncmp(cur->optv[i], option, len) == 0 && 78 (cur->optv[i][len] == '\0' || cur->optv[i][len] == '=')) 79 break; 80 } 75 81 if (value == NULL) { 76 82 /* remove */ … … 80 86 cur->optv[i] = cur->optv[i + 1]; 81 87 cur->optv[i] = NULL; 82 cur->optc--;83 88 RETURNC(PAM_SUCCESS); 84 89 } 85 ol = strlen(option) + 1; 86 vl = strlen(value) + 1; 87 if ((opt = malloc(sizeof *opt + ol + vl)) == NULL) 90 if (asprintf(&opt, "%.*s=%s", (int)len, option, value) < 0) 88 91 RETURNC(PAM_BUF_ERR); 89 opt->name = (char *)opt + sizeof *opt;90 strlcpy(opt->name, option, ol);91 opt->value = opt->name + ol;92 strlcpy(opt->value, value, vl);93 92 if (i == cur->optc) { 94 93 /* add */ 95 optv = realloc(cur->optv, sizeof *optv* (cur->optc + 2));94 optv = realloc(cur->optv, sizeof(char *) * (cur->optc + 2)); 96 95 if (optv == NULL) { 97 96 FREE(opt);
Note: See TracChangeset
for help on using the changeset viewer.