pure-pw.c

来自「功能强大的ftp服务器源代码」· C语言 代码 · 共 1,575 行 · 第 1/4 页

C
1,575
字号
                if ((dbfile_ = getenv(ENV_DEFAULT_PW_DB)) != NULL && *dbfile_ != 0) {            dbfile = dbfile_;        } else {                    dbfile = DEFAULT_PW_DB;        }    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }        if ((fp = fopen(file, "r")) == NULL) {        perror("Unable to open the passwd file");        return PW_ERROR_MISSING_PASSWD_FILE;    }    sizeof_index_dbfile = strlen(dbfile) + sizeof NEWPASSWD_INDEX_SUFFIX;    if ((index_dbfile = ALLOCA(sizeof_index_dbfile)) == NULL) {        fclose(fp);        no_mem();    }    sizeof_data_dbfile = strlen(dbfile) + sizeof NEWPASSWD_DATA_SUFFIX;    if ((data_dbfile = ALLOCA(sizeof_data_dbfile)) == NULL) {        fclose(fp);        ALLOCA_FREE(index_dbfile);        no_mem();    }    snprintf(index_dbfile, sizeof_index_dbfile, "%s%s",             dbfile, NEWPASSWD_INDEX_SUFFIX);    snprintf(data_dbfile, sizeof_data_dbfile, "%s%s",             dbfile, NEWPASSWD_DATA_SUFFIX);    if (puredbw_open(&dbw, index_dbfile, data_dbfile, dbfile) != 0) {        perror("Unable to create the database");        goto err;    }    while (fgets(line, (int) sizeof line - 1U, fp) != NULL) {        strip_lf(line);        if (*line == PW_LINE_COMMENT) {            continue;        }        if (*line == 0 || (s = strchr(line, *PW_LINE_SEP)) == NULL ||            s[1] == 0) {            continue;        }        *s++ = 0;        if (puredbw_add_s(&dbw, line, s) != 0) {            perror("Error while indexing a new entry");            goto err;        }    }    if (puredbw_close(&dbw) != 0) {        perror("Unable to close the database");    } else {        ret = 0;    }    err:    puredbw_free(&dbw);    ALLOCA_FREE(index_dbfile);    ALLOCA_FREE(data_dbfile);        fclose(fp);        return ret;}static void init_zrand(void){    struct timeval tv;    struct timezone tz;        gettimeofday(&tv, &tz);#ifdef HAVE_SRANDOMDEV    srandomdev();#endif    #ifdef HAVE_RANDOM    srandom((unsigned int) (tv.tv_sec ^ tv.tv_usec));#else    srand((unsigned int) (tv.tv_sec ^ tv.tv_usec));#endif}int main(int argc, char *argv[]){    const char *action;    char *file = NULL;    char *dbfile = NULL;    PWInfo pwinfo;    int fodder;    int ret = 0;    int with_chroot = 1;    int with_mkdb = 0;            if (argc < 2) {        help();    }#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    #ifdef PROBE_RANDOM_AT_RUNTIME    pw_zrand_probe();#endif        pwinfo.pwd = NULL;    pwinfo.gecos = NULL;    pwinfo.home = NULL;    pwinfo.allow_local_ip = pwinfo.deny_local_ip = NULL;    pwinfo.allow_client_ip = pwinfo.deny_client_ip = NULL;            pwinfo.has_bw_dl = 0;    pwinfo.has_bw_ul = 0;    pwinfo.has_quota_files = 0;    pwinfo.has_quota_size = 0;    pwinfo.has_ul_ratio = 0;    pwinfo.has_dl_ratio = 0;    pwinfo.has_time = 0;    pwinfo.time_begin = pwinfo.time_end = 0U;    pwinfo.has_per_user_max = 0;    pwinfo.per_user_max = 0U;#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)    pwinfo.uid = (uid_t) 42U;    pwinfo.gid = (gid_t) 42U;#else    pwinfo.uid = (uid_t) 0U;    pwinfo.gid = (gid_t) 0U;#endif        argv++;    argc--;    action = *argv;    if (argc > 1) {        argv++;        argc--;        pwinfo.login = *argv;    } else {        pwinfo.login = NULL;    }    filter_pw_line_sep(pwinfo.login);    while ((fodder = getopt(argc, argv,                             "c:d:D:f:g:hi:I:mn:N:q:Q:r:R:t:T:u:y:z:")) != -1) {        switch(fodder) {        case 'c' : {            if ((pwinfo.gecos = strdup(optarg)) == NULL) {                no_mem();            }            filter_pw_line_sep(pwinfo.gecos);            break;        }        case 'D' :            with_chroot = 0;        case 'd' : {            char *optarg_copy;            size_t sizeof_home;            size_t optarg_len;                        if ((optarg_copy = strdup(optarg)) == NULL) {                no_mem();            }            again:            optarg_len = strlen(optarg_copy);            if (optarg_len < (size_t) 1U) {                fprintf(stderr, "home directory is missing\n");                exit(EXIT_FAILURE);            }            if (optarg_copy[optarg_len - 1U] == '/') {                optarg_len--;                optarg_copy[optarg_len] = 0;                goto again;            }            sizeof_home = optarg_len + sizeof "/./";            if ((pwinfo.home = malloc(sizeof_home)) == NULL) {                no_mem();            }            snprintf(pwinfo.home, sizeof_home, "%s%s", optarg_copy,                     with_chroot != 0 ? "/./" : "");            filter_pw_line_sep(pwinfo.home);                        break;        }        case 'f' : {            if ((file = strdup(optarg)) == NULL) {                no_mem();            }            break;        }        case 'g' : {            struct group *gr;                        if (pwinfo.gid > (gid_t) 0 && pwinfo.uid <= (uid_t) 0) {                fprintf(stderr, "You already gave a gid\n");                exit(EXIT_FAILURE);            }                            if ((gr = getgrnam(optarg)) != NULL) {                pwinfo.gid = gr->gr_gid;            } else {                pwinfo.gid = (gid_t) strtoul(optarg, NULL, 10);            }                        break;        }        case 'h' : {            help();            /* doesn't return */        }        case 'i' : {            if ((pwinfo.allow_local_ip = strdup(optarg)) == NULL) {                no_mem();            }            break;        }        case 'I' : {            if ((pwinfo.deny_local_ip = strdup(optarg)) == NULL) {                no_mem();            }            break;        }        case 'm' : {            with_mkdb = 1;            break;        }        case 'n' : {            if (*optarg == 0) {                pwinfo.has_quota_files = -1;            } else {                pwinfo.quota_files = strtoull(optarg, NULL, 10);                pwinfo.has_quota_files = 1;            }            break;        }        case 'N' : {            if (*optarg == 0) {                pwinfo.has_quota_size = -1;            } else {                pwinfo.quota_size = strtoull(optarg, NULL, 10) *                     (1024ULL * 1024ULL);                pwinfo.has_quota_size = 1;            }            break;        }        case 'q' : {            if (*optarg == 0) {                pwinfo.has_ul_ratio = -1;            } else {                pwinfo.ul_ratio = (unsigned int) strtoul(optarg, NULL, 10);                if (pwinfo.ul_ratio < 1U) {                    fprintf(stderr, "Illegal upload ratio\n");                    exit(EXIT_FAILURE);                }                pwinfo.has_ul_ratio = 1;            }            break;        }                    case 'Q' : {            if (*optarg == 0) {                pwinfo.has_dl_ratio = -1;            } else {                pwinfo.dl_ratio = (unsigned int) strtoul(optarg, NULL, 10);                if (pwinfo.dl_ratio < 1U) {                    fprintf(stderr, "Illegal download ratio\n");                    exit(EXIT_FAILURE);                }                            pwinfo.has_dl_ratio = 1;            }            break;        }        case 'r' : {            if ((pwinfo.allow_client_ip = strdup(optarg)) == NULL) {                no_mem();            }            break;        }        case 'R' : {            if ((pwinfo.deny_client_ip = strdup(optarg)) == NULL) {                no_mem();            }            break;        }                    case 't' : {            if (*optarg == 0) {                pwinfo.has_bw_dl = -1;            } else {                if ((pwinfo.bw_dl = strtoul(optarg, NULL, 10)) > 0UL) {                    pwinfo.bw_dl *= 1024UL;                                        pwinfo.has_bw_dl = 1;                }            }            break;        }        case 'T' : {            if (*optarg == 0) {                pwinfo.has_bw_ul = -1;            } else {                if ((pwinfo.bw_ul = strtoul(optarg, NULL, 10)) > 0UL) {                    pwinfo.bw_ul *= 1024UL;                    pwinfo.has_bw_ul = 1;                }            }            break;        }                    case 'u' : {            struct passwd *pw;                                        if (pwinfo.uid > (uid_t) 0) {                fprintf(stderr, "You already gave an uid\n");                exit(EXIT_FAILURE);            }            if ((pw = getpwnam(optarg)) != NULL) {                pwinfo.uid = pw->pw_uid;                if (pwinfo.gid <= (gid_t) 0) {                    pwinfo.gid = pw->pw_gid;                }            } else {                pwinfo.uid = (uid_t) strtoul(optarg, NULL, 10);            }            break;        }	case 'y' : {	    if ((pwinfo.per_user_max = (unsigned int) strtoul(optarg, NULL, 10)) <= 0U) {                pwinfo.has_per_user_max = -1;            } else {                pwinfo.has_per_user_max = 1;            }	    break;	}        case 'z' : {            if (sscanf(optarg, "%u-%u",                        &pwinfo.time_begin, &pwinfo.time_end) == 2 &&                pwinfo.time_begin < 2360 && (pwinfo.time_begin % 100) < 60 &&                pwinfo.time_end < 2360 && (pwinfo.time_end % 100) < 60) {                pwinfo.has_time = 1;            } else if (*optarg != 0) {                fprintf(stderr, "Time should be given as hhmm-hhmm\n"                        "Example : 0900-1800 (9 am to 6 pm)\n");                exit(EXIT_FAILURE);                                } else {                pwinfo.has_time = -1;            }            break;        }        case '?' :            help();        }    }    if (file == NULL) {        char *file_;                if ((file_ = getenv(ENV_DEFAULT_PW_FILE)) != NULL && *file_ != 0) {            file = file_;        } else if ((file = strdup(DEFAULT_PW_FILE)) == NULL) {            no_mem();        }    }    (void) umask(0177);    init_zrand();    if (strcasecmp(action, "useradd") == 0) {        ret = do_useradd(file, &pwinfo);        if (with_mkdb != 0) {            ret |= do_mkdb(dbfile, file);        }    } else if (strcasecmp(action, "usermod") == 0) {        ret = do_usermod(file, &pwinfo);        if (with_mkdb != 0) {            ret |= do_mkdb(dbfile, file);        }            } else if (strcasecmp(action, "userdel") == 0) {        ret = do_userdel(file, &pwinfo);        if (with_mkdb != 0) {            ret |= do_mkdb(dbfile, file);        }            } else if (strcasecmp(action, "passwd") == 0) {        do_passwd(file, &pwinfo);        if (with_mkdb != 0) {            do_mkdb(dbfile, file);        }            } else if (strcasecmp(action, "show") == 0) {        ret = do_show(file, &pwinfo);    } else if (strcasecmp(action, "mkdb") == 0) {        ret = do_mkdb(pwinfo.login, file);    } else if (strcasecmp(action, "list") == 0) {        ret = do_list(file);    } else {        ret = PW_ERROR_UNEXPECTED_ERROR;        help();    }                   return ret;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?