Exemple #1
0
int generic_parser(const char * const file,
                   ConfigKeywords *config_keywords)
{
    char *linepnt;
    char *linepnt2;
    FILE *fp;
    ConfigKeywords *config_keywords_pnt;
    char line[LINE_MAX];

    if (file == NULL || (fp = fopen(file, "r")) == NULL) {
        return -1;
    }
    while (fgets(line, sizeof line, fp) != NULL) {
        line[sizeof line - 1U] = 0;       /* paranoia */
        linepnt = line;
        while (*linepnt != 0 && isspace((unsigned char) *linepnt)) {
            linepnt++;
        }
        if (ISCTRLCODE(*linepnt) || *linepnt == CONFIG_COMMENT) {
            continue;
        }
        config_keywords_pnt = config_keywords;
        do {
            size_t keyword_len;

            keyword_len = strlen(config_keywords_pnt->keyword);
            if (strncasecmp(config_keywords_pnt->keyword,
                            linepnt, keyword_len) == 0) {
                linepnt += keyword_len;
                while (*linepnt != 0 && isspace((unsigned char) *linepnt)) {
                    linepnt++;
                }
                if (*linepnt == 0) {
                    fclose(fp);
                    return -1;
                }
                linepnt2 = linepnt + strlen(linepnt) - 1U;
                while (linepnt2 != linepnt &&
                       (isspace((unsigned char) *linepnt2) ||
                        ISCTRLCODE(*linepnt2))) {
                    linepnt2--;
                }
                linepnt2[1] = 0;           /* No possible off-by-one here */
                if ((*config_keywords_pnt->value = strdup(linepnt)) == NULL) {
                    fclose(fp);
                    return -1;
                }
                break;
            }
            config_keywords_pnt++;
        } while (config_keywords_pnt->keyword != NULL);
        if (config_keywords_pnt->keyword == NULL) {
            fclose(fp);
            return -1;
        }
    }
    fclose(fp);

    return 0;
}
static const char *shell_escaped(const char * const s_)
{
    const unsigned char *s = (const unsigned char *) s_;        
    static char buf[MAXPATHLEN + 32U];
    const char * const bufend = &buf[sizeof buf - (size_t) 1U];
    char *bufpnt = buf;    
    
    while (*s != 0U) {
        if (ISCTRLCODE(*s)) {
            *bufpnt = '_';
        } else if (*s == '|' || *s == '\\') {
            if (bufpnt == bufend) {
                break;
            }
            *bufpnt++ = '\\';
            if (bufpnt == bufend) {
                bufpnt--;
                break;
            }
            *bufpnt = (char) *s;
        } else {
            *bufpnt = (char) *s;
        }
        if (bufpnt == bufend) {
            break;
        }
        bufpnt++;
        s++;
    }
    *bufpnt = 0;
    
    return buf;
}
static int readpipe(const int upload_file_fd,
                    char ** const r_who, char ** const r_file)
{
    static char who[MAX_USER_LENGTH + 1U];
    static char file[PATH_MAX + VHOST_PREFIX_MAX_LEN];
    const char * const whoend = &who[sizeof who];
    const char * const fileend = &file[sizeof file];
    char *whopnt = who;
    char *filepnt = file;
    int c;

    *r_who = NULL;
    *r_file = NULL;
    do {
        c = readchar(upload_file_fd);
        if (c == EOF) {
            return -1;
        }
    } while (c != 2);
    while (whopnt != whoend) {
        c = readchar(upload_file_fd);
        if (c == EOF || (c != 1 && ISCTRLCODE(c))) {
            return -1;
        }
        if (c == 1) {
            *whopnt = 0;
            break;
        }
        *whopnt = (char) c;
        whopnt++;
    }
    while (filepnt != fileend) {
        c = readchar(upload_file_fd);
        if (c == EOF || (c != 0 && ISCTRLCODE(c))) {
            return -1;
        }
        *filepnt = (char) c;
        if (c == 0) {
            break;
        }
        filepnt++;
    }
    *r_who = who;
    *r_file = file;

    return 0;
}
static char *xml_escaped(const char *const s_) {
    static char buf[MAXPATHLEN + 32U];
    const unsigned char *s = (const unsigned char *) s_;
    char *bufpnt = buf;
    size_t left = sizeof buf - (size_t) 1U;
    
    while (left > (size_t) 0U && *s != 0U) {
        if (ISCTRLCODE(*s)) {
            if (left <= (size_t) 0U) {
                *bufpnt = 0;
                return buf;
            }
            *bufpnt++ = '?';
            left--;
            goto next;
        }
        switch (*s) {
        case '<':
            if (left < sizeof "&lt;" - (size_t) 1U) {
                *bufpnt = 0;
                return buf;
            }
            *bufpnt++ = '&'; *bufpnt++ = 'l'; *bufpnt++ = 't'; *bufpnt++ = ';';
            left -= sizeof "&lt;" - (size_t) 1U;
            break;
        case '>':
            if (left < sizeof "&gt;" - (size_t) 1U) {
                *bufpnt = 0;
                return buf;
            }
            *bufpnt++ = '&'; *bufpnt++ = 'g'; *bufpnt++ = 't'; *bufpnt++ = ';';
            left -= sizeof "&gt;" - (size_t) 1U;
            break;
        case '&':
            if (left < sizeof "&amp;" - (size_t) 1U) {
                *bufpnt = 0;
                return buf;
            }
            *bufpnt++ = '&'; *bufpnt++ = 'a'; *bufpnt++ = 'm'; *bufpnt++ = 'p';
            *bufpnt++ = ';';
            left -= sizeof "&amp;" - (size_t) 1U;
            break;
        default:
            *bufpnt++ = (char) *s;
            left--;
        }
        next:
        s++;
    }
    *bufpnt = 0;
    
    return buf;    
}
Exemple #5
0
static int altlog_writexfer_xferlog(const int upload,
                    const char * const filename,
                    const off_t size,
                    const double duration)
{
    char date[sizeof "Mon Apr 13 12:34:56 1975"];
    struct tm *tm;
    char *alloca_line;
    const char *host_ = *host != 0 ? host : "-";
    const char *account_ = *account != 0 ? account : "-";
    char *quoted_filename;
    size_t filename_idx;
    time_t now;
    size_t line_size;
    size_t filename_size;
    char c;

    if ((now = time(NULL)) == (time_t) -1 ||
        (tm = localtime(&now)) == NULL ||
        tm->tm_mon > 11 || tm->tm_mon < 0 ||
        tm->tm_wday > 6 || tm->tm_wday < 0) {
        return -1;
    }
    if (SNCHECK(snprintf(date, sizeof date,
                         "%s %s %02d %02d:%02d:%02d %d",
                         week_days[tm->tm_wday], months[tm->tm_mon],
                         tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
                         tm->tm_year + 1900), sizeof date)) {
        return -1;
    }
    if ((filename_idx = strlen(filename)) <= (size_t) 0U) {
        return -1;
    }
    filename_size = filename_idx + (size_t) 1U;
    if ((quoted_filename = ALLOCA(filename_size)) == NULL) {
        return -1;
    }

    quoted_filename[filename_idx] = 0;
    do {
        filename_idx--;
        c = filename[filename_idx];
        if (isspace((unsigned char) c) || ISCTRLCODE(c)) {
            c = '_';
        }
        quoted_filename[filename_idx] = c;
    } while (filename_idx > (size_t) 0U);

    line_size = (sizeof date - 1U) + (sizeof " " - 1U) +
        (size_t) 16U /* duration */ + (sizeof " " - 1U) +
        strlen(host_) + (sizeof " " - 1U) +
        (size_t) 20U /* size */ + (sizeof " " - 1U) +
        (filename_size - 1U) + (sizeof " " - 1U) +
        (size_t) 1U /* type */ + (sizeof " _ " - 1U) +
        (size_t) 1U /* direction */ + (sizeof " " - 1U) +
        (size_t) 1U /* anonymous */ + (sizeof " " - 1U) +
        strlen(account_) + (sizeof " ftp 1 * c\n" - 1U) + (size_t) 1U;
    if ((alloca_line = ALLOCA(line_size)) == NULL) {
        ALLOCA_FREE(quoted_filename);
        return -1;
    }
    if (!SNCHECK(snprintf(alloca_line, line_size,
                          "%s %lu %s %llu %s %c _ %c %c %s ftp 1 * c\n",
                          date, (unsigned long) (duration + 0.5),
                          host_, (unsigned long long) size,
                          quoted_filename,
                          type == 1 ? 'a' : 'b',
                          upload != 0 ? 'i' : 'o',
                          loggedin != 0 ? 'r' : 'a',
                          account_), line_size)) {
        altlog_write(alloca_line);
    }
    ALLOCA_FREE(quoted_filename);
    ALLOCA_FREE(alloca_line);

    return 0;
}
Exemple #6
0
static char *sqlsubst(const char *orig_str, char * const query,
                      size_t query_len, const char * const user,
                      const char * const ip, const char * const port,
                      const char * const peer_ip,
                      const char * const decimal_ip)
{
    char *query_pnt = query;
    const char *orig_str_scan = orig_str;
    const size_t user_len = (user == NULL ? (size_t) 0U : strlen(user));
    const size_t ip_len = (ip == NULL ? (size_t) 0U : strlen(ip));
    const size_t port_len = (port == NULL ? (size_t) 0U : strlen(port));
    const size_t peer_ip_len = (peer_ip == NULL ? (size_t) 0U : strlen(peer_ip));
    const size_t decimal_ip_len = (decimal_ip == NULL ? (size_t) 0U : strlen(decimal_ip));

    while (*orig_str_scan != 0) {
        if (*orig_str_scan == '\\' && orig_str_scan[1] != 0) {
            orig_str_scan++;
            switch(tolower((unsigned char) *orig_str_scan)) {
            case 'l' :
                if (user_len >= query_len) {
                    return NULL;
                }
                if (user_len <= (size_t) 0U) {
                    goto nextone;
                }
                memcpy(query_pnt, user, user_len);
                query_pnt += user_len;
                query_len -= user_len;
                goto nextone;
            case 'i' :
                if (ip_len >= query_len) {
                    return NULL;
                }
                if (ip_len <= (size_t) 0U) {
                    goto nextone;
                }                
                memcpy(query_pnt, ip, ip_len);
                query_pnt += ip_len;
                query_len -= ip_len;
                goto nextone;
            case 'p' :             
                if (port_len >= query_len) {
                    return NULL;
                } 
                if (port_len <= (size_t) 0U) {
                    goto nextone;
                }               
                memcpy(query_pnt, port, port_len);
                query_pnt += port_len;
                query_len -= port_len;
                goto nextone;
            case 'r' :             
                if (peer_ip_len >= query_len) {
                    return NULL;
                } 
                if (peer_ip_len <= (size_t) 0U) {
                    goto nextone;
                }               
                memcpy(query_pnt, peer_ip, peer_ip_len);
                query_pnt += peer_ip_len;
                query_len -= peer_ip_len;
                goto nextone;
            case 'd' :             
                if (decimal_ip_len >= query_len) {
                    return NULL;
                } 
                if (decimal_ip_len <= (size_t) 0U) {
                    goto nextone;
                }               
                memcpy(query_pnt, decimal_ip, decimal_ip_len);
                query_pnt += decimal_ip_len;
                query_len -= decimal_ip_len;
                goto nextone;
            default :
                if (--query_len <= (size_t) 0U) {
                    return NULL;
                }
                *query_pnt++ = '\\';
            }
        }
        if (ISCTRLCODE(*orig_str_scan)) {
            goto nextone;
        }
        if (--query_len <= (size_t) 0U) {
            return NULL;
        }
        *query_pnt++ = *orig_str_scan;
        nextone:
        orig_str_scan++;
    }
    *query_pnt = 0;

    return query;
}
int main(int argc, char *argv[])
{
    int instamp = 0;
    int c;
    const char *file;
    FILE *fp;
    time_t date;
    struct tm *tm;
    char timestamp[42];

    if (argc != 2) {
        usage();
        return 1;
    }

#ifdef HAVE_SETLOCALE
# ifdef LC_MESSAGES
    (void) setlocale(LC_MESSAGES, "");
# endif
# ifdef LC_CTYPE
    (void) setlocale(LC_CTYPE, "");
# endif
# ifdef LC_COLLATE
    (void) setlocale(LC_COLLATE, "");
# endif
#endif

    file = argv[1];
    if (*file == '-' && file[1] == 0) {
        fp = stdin;
    } else {
        if ((fp = fopen(file, "r")) == NULL) {
            perror("Can't open file: ");
            return -1;
        }
    }
    while ((c = getc(fp)) != EOF) {
        if (instamp >= 0) {
            if (isdigit(c)) {
                if (instamp < (int) (sizeof timestamp - 1U)) {
                    timestamp[instamp] = (char) c;
                    instamp++;
                }
            } else {
                timestamp[instamp] = 0;
                instamp = -1;
                date = (time_t) strtoul(timestamp, NULL, 10);
                tm = localtime(&date);
                if (tm == NULL) {
                    printf("- - ");
                } else{
                    printf("%d/%02d/%02d %02d:%02d:%02d ",
                           tm->tm_year + 1900,
                           tm->tm_mon + 1,
                           tm->tm_mday,
                           tm->tm_hour,
                           tm->tm_min,
                           tm->tm_sec);
                }
            }
        } else {
            if (c == '\n' || !ISCTRLCODE(c)) {
                putchar(c);
            }
        }
        if (c == '\n') {
            fflush(fp);
            instamp = 0;
        }
    }
    fclose(fp);

    return 0;
}