Пример #1
0
/* read_user_passwd LOCALPART DOMAIN
 * Read the password hash from the proper flat file for the given LOCALPART and
 * DOMAIN. Returns the password or NULL if not found. The files are structured
 * with colon-separated fields, where the first field is the local-part and the
 * second field to the password hash. Any subsequent fields are ignored. */
static char *read_user_passwd(const char *local_part, const char *domain) {
    FILE *fp = NULL;
    char *filename = NULL, *result = NULL;
    struct sverr err;
    static char *buf, *pwhash;
    static size_t buflen;
    size_t i, linenum;
    int c;

    if (!(filename = substitute_variables(user_passwd_file_template, &err, 1, "domain", domain))) {
        log_print(LOG_ERR, _("read_user_passwd: %s near `%.16s'"), err.msg, user_passwd_file_template + err.offset);
        goto fail;
    }

    if (!(fp = fopen(filename, "rt"))) {
        log_print(LOG_ERR, _("read_user_passwd: flat file %s: %m"), filename);
        goto fail;
    }

    /* Read lines from the file. */
    if (!buf)
        buf = xmalloc(buflen = 1024);
    
    linenum = 0;
    while (1) {
        char *user, *end;
        
        i = 0;
        while ((c = getc(fp)) != EOF) {
            if (c == '\n')
                break;
            buf[i++] = (char)c;
            if (i == buflen)
                buf = xrealloc(buf, buflen *= 2);
        }

        buf[i] = 0;

        if (c == EOF) {
            if (ferror(fp)) {
                /* Read error. */
                log_print(LOG_ERR, _("read_user_passwd: flat file %s: %m"), filename);
                goto fail;
            } else if (i == 0)
                /* Read nothing at end of file. */
                break;
        }

        /* OK, have a line. */
        user = buf;
        pwhash = strchr(buf, ':');
        if (!pwhash) {
            log_print(LOG_WARNING, _("read_user_passwd: flat file %s: line %u: bad format (missing :)"), filename, (unsigned)linenum);
            continue;
        }
        
        *pwhash++ = 0;

        /* Check username. */
        if (strcmp(user, local_part) != 0)
            continue;

        if ((end = strchr(pwhash, ':')))
            *end = 0;

        result = pwhash;

        break;
    }
    
fail:
    if (fp)
        fclose(fp);
    
    if (filename)
        xfree(filename);

    return result;
}
Пример #2
0
 std::string getWithSubst(const std::string & key) const
 {
     return substitute_variables(get<std::string>(key));
 }