Changeset 482 in openpam for trunk


Ignore:
Timestamp:
Nov 3, 2011, 4:33:02 PM (8 years ago)
Author:
Dag-Erling Smørgrav
Message:

Revert large parts of r478. I had forgotten that the module arguments
are actually passed to each service function in the classic (argc,
argv) form. The only place where the compiler could have caught this
used a type cast, and it did not show up in testing either because all
of the modules I tested use openpam_get_option(3) instead of
manipulating argv directly.

The cleaned-up policy parsing code remains in place, but options are
once more stored as strings, pretty much the way they appear in the
policy file, except that quotes are stripped.

Location:
trunk/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/openpam_configure.c

    r481 r482  
    270270 * Parse an option.
    271271 *
    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.
    281275 *
    282276 * If parse_option() is successful, it updates *line to point one
     
    285279 * all other cases, it leaves *line unmodified.
    286280 *
    287  * If parse_option() fails to allocate memory for the option structure, it
    288  * will return NULL and set errno 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.
    289283 *
    290284 * Allowed characters for option names are all characters in the POSIX
     
    295289 * Note that the entire value must be quoted, not just part of it.
    296290 */
    297 static pam_opt_t *
     291static char *
    298292parse_option(char **line)
    299293{
    300294        char *nb, *ne, *vb, *ve;
    301295        unsigned char q = 0;
    302         pam_opt_t *opt;
    303         size_t size;
     296        char *option;
    304297
    305298        errno = 0;
     
    332325                vb = ve = ne;
    333326        }
    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)
    338328                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';
    343333        *line = q ? ve + 1 : ve;
    344         return (opt);
     334        return (option);
    345335}
    346336
     
    381371        pam_facility_t fclt;
    382372        pam_control_t ctlf;
    383         pam_opt_t *opt;
    384373        char *line, *str, *name;
     374        char *option, **optv;
    385375        int len, ret;
    386376        FILE *f;
     
    471461
    472462                /* 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                }
    479474                if (*line != '\0') {
    480475                        openpam_log(PAM_LOG_ERROR,
  • trunk/lib/openpam_get_option.c

    r478 r482  
    6060{
    6161        pam_chain_t *cur;
     62        size_t len;
    6263        int i;
    6364
     
    6667                RETURNS(NULL);
    6768        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        }
    7178        RETURNS(NULL);
    7279}
  • trunk/lib/openpam_impl.h

    r479 r482  
    7272} pam_facility_t;
    7373
    74 typedef struct pam_opt pam_opt_t;
    75 struct pam_opt {
    76         char            *name;
    77         char            *value;
    78         char             str[];
    79 };
    80 
    8174typedef struct pam_chain pam_chain_t;
    8275struct pam_chain {
     
    8477        int              flag;
    8578        int              optc;
    86         pam_opt_t      **optv;
     79        char           **optv;
    8780        pam_chain_t     *next;
    8881};
  • trunk/lib/openpam_set_option.c

    r478 r482  
    6363{
    6464        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;
    6768
    6869        ENTERS(option);
     
    7071                RETURNC(PAM_SYSTEM_ERR);
    7172        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] == '=')
    7475                        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        }
    7581        if (value == NULL) {
    7682                /* remove */
     
    8086                        cur->optv[i] = cur->optv[i + 1];
    8187                cur->optv[i] = NULL;
    82                 cur->optc--;
    8388                RETURNC(PAM_SUCCESS);
    8489        }
    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)
    8891                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);
    9392        if (i == cur->optc) {
    9493                /* add */
    95                 optv = realloc(cur->optv, sizeof *optv * (cur->optc + 2));
     94                optv = realloc(cur->optv, sizeof(char *) * (cur->optc + 2));
    9695                if (optv == NULL) {
    9796                        FREE(opt);
Note: See TracChangeset for help on using the changeset viewer.