pure-pw.c

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

C
1,575
字号
                      const PWInfo * const pwinfo_){    char *file2;    FILE *fp2;    PWInfo pwinfo = *pwinfo_;        if (pwinfo.login == NULL || *(pwinfo.login) == 0) {        fprintf(stderr, "Missing login\n");        return PW_ERROR_MISSING_LOGIN;    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }    if (pwinfo.uid <= (uid_t) 0 || pwinfo.gid <= (gid_t) 0) {        fprintf(stderr, "You must give (non-root) uid and gid\n");        return PW_ERROR_USERADD_NOT_ROOT;    }    if (pwinfo.home == NULL) {        fprintf(stderr, "Missing home directory\n");                return PW_ERROR_USERADD_MISSING_HOME_DIR;    }    if (pwinfo.gecos == NULL) {        if ((pwinfo.gecos = strdup("")) == NULL) {            no_mem();        }    }               if ((pwinfo.pwd = do_get_passwd()) == NULL) {        fprintf(stderr, "Error with entering password - aborting\n");                return PW_ERROR_ENTER_PASSWD_PW_ERROR;    }    {        char *cleartext = pwinfo.pwd;        pwinfo.pwd = best_crypt(cleartext);        if (*cleartext != 0) {            memset(cleartext, 0, strlen(cleartext));        }    }                if ((file2 = newpasswd_filename(file)) == NULL) {        no_mem();    }    if ((fp2 = create_newpasswd(file, file2, pwinfo.login, 1, 0)) == NULL) {        fprintf(stderr, "Error.\n"                "Check that [%s] doesn't already exist,\n"                "and that [%s] can be written.\n",                 pwinfo.login, file2);        free(file2);                return PW_ERROR_USER_ALREADY_EXIST;    }        if (add_new_pw_line(fp2, &pwinfo) != 0) {        fprintf(stderr, "Unable to append a line\n");        goto bye;    }    fflush(fp2);#ifdef HAVE_FILENO    fsync(fileno(fp2));#endif      if (fclose(fp2) != 0) {        perror("Unable to close the file");        goto bye2;    }    if (rename(file2, file) != 0) {        perror("Unable to rename the file");        goto bye2;    }    free(file2);    return 0;        bye:    fclose(fp2);    bye2:    unlink(file2);    free(file2);        return PW_ERROR_UNEXPECTED_ERROR;}static int do_usermod(const char * const file,                      const PWInfo *pwinfo){    char *file2;    FILE *fp2;    PWInfo fetched_info;    static char line[LINE_MAX];    if (pwinfo->login == NULL || *(pwinfo->login) == 0) {        fprintf(stderr, "Missing login\n");        return PW_ERROR_MISSING_LOGIN;    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }    if (fetch_pw_account(file, &fetched_info, line, sizeof line,                         pwinfo->login) != 0) {        fprintf(stderr, "Unable to fetch info about user [%s] in file [%s]\n",                pwinfo->login, file);        return PW_ERROR_UNABLE_TO_FETCH;    }    if (pwinfo->pwd != NULL) {        char *cleartext = pwinfo->pwd;        fetched_info.pwd = best_crypt(cleartext);        if (*cleartext != 0) {            memset(cleartext, 0, strlen(cleartext));        }            }    if (pwinfo->uid > (uid_t) 0) {        fetched_info.uid = pwinfo->uid;    }    if (pwinfo->gid > (gid_t) 0) {        fetched_info.gid = pwinfo->gid;    }    if (pwinfo->home != NULL) {        fetched_info.home = pwinfo->home;    }    if (pwinfo->gecos != NULL) {        fetched_info.gecos = pwinfo->gecos;    }    if (pwinfo->has_bw_dl != 0) {        if (pwinfo->has_bw_dl < 0) {            fetched_info.has_bw_dl = 0;        } else {            fetched_info.has_bw_dl = pwinfo->has_bw_dl;            fetched_info.bw_dl = pwinfo->bw_dl;        }    }    if (pwinfo->has_bw_ul != 0) {        if (pwinfo->has_bw_ul < 0) {            fetched_info.has_bw_ul = 0;                    } else {            fetched_info.has_bw_ul = pwinfo->has_bw_ul;            fetched_info.bw_ul = pwinfo->bw_ul;        }    }    if (pwinfo->has_quota_files != 0) {        if (pwinfo->has_quota_files < 0) {            fetched_info.has_quota_files = 0;        } else {            fetched_info.has_quota_files = pwinfo->has_quota_files;            fetched_info.quota_files = pwinfo->quota_files;        }    }    if (pwinfo->has_quota_size != 0) {        if (pwinfo->has_quota_size < 0) {            fetched_info.has_quota_size = 0;                    } else {            fetched_info.has_quota_size = pwinfo->has_quota_size;            fetched_info.quota_size = pwinfo->quota_size;        }    }    if (pwinfo->has_ul_ratio != 0) {        if (pwinfo->has_ul_ratio < 0) {            fetched_info.has_ul_ratio = 0;                    } else {            fetched_info.has_ul_ratio = pwinfo->has_ul_ratio;            fetched_info.ul_ratio = pwinfo->ul_ratio;        }    }    if (pwinfo->has_dl_ratio != 0) {        if (pwinfo->has_dl_ratio < 0) {            fetched_info.has_dl_ratio = 0;        } else {            fetched_info.has_dl_ratio = pwinfo->has_dl_ratio;            fetched_info.dl_ratio = pwinfo->dl_ratio;        }    }    if (pwinfo->allow_local_ip != NULL) {        fetched_info.allow_local_ip = pwinfo->allow_local_ip;    }    if (pwinfo->deny_local_ip != NULL) {        fetched_info.deny_local_ip = pwinfo->deny_local_ip;    }    if (pwinfo->allow_client_ip != NULL) {        fetched_info.allow_client_ip = pwinfo->allow_client_ip;    }    if (pwinfo->deny_client_ip != NULL) {        fetched_info.deny_client_ip = pwinfo->deny_client_ip;    }    if (pwinfo->has_time != 0) {        if (pwinfo->has_time < 0) {            fetched_info.has_time = 0;        } else {            fetched_info.has_time = pwinfo->has_time;        }        fetched_info.time_begin = pwinfo->time_begin;        fetched_info.time_end = pwinfo->time_end;    }    if (pwinfo->has_per_user_max != 0) {        if (pwinfo->has_per_user_max < 0) {            fetched_info.has_per_user_max = 0;                    } else {            fetched_info.has_per_user_max = pwinfo->has_per_user_max;            fetched_info.per_user_max = pwinfo->per_user_max;        }    }    if ((file2 = newpasswd_filename(file)) == NULL) {        no_mem();    }    if ((fp2 = create_newpasswd(file, file2, pwinfo->login, 0, 1)) == NULL) {        fprintf(stderr, "Error.\n"                "Check that [%s] already exists,\n"                "and that [%s] can be written.\n", pwinfo->login, file2);        free(file2);        return PW_ERROR_USER_ALREADY_EXIST;    }        if (add_new_pw_line(fp2, &fetched_info) != 0) {        fprintf(stderr, "Unable to append a line\n");        goto bye;    }    fflush(fp2);#ifdef HAVE_FILENO    fsync(fileno(fp2));#endif          if (fclose(fp2) != 0) {        perror("Unable to close the file");        goto bye2;    }    if (rename(file2, file) != 0) {        perror("Unable to rename the file");        goto bye2;    }    free(file2);    return 0;        bye:    fclose(fp2);    bye2:    unlink(file2);    free(file2);        return PW_ERROR_UNEXPECTED_ERROR;}static int do_userdel(const char * const file,                      const PWInfo * const pwinfo){    char *file2;    FILE *fp2;        if (pwinfo->login == NULL || *(pwinfo->login) == 0) {        fprintf(stderr, "Missing login\n");        return -1;    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }        if ((file2 = newpasswd_filename(file)) == NULL) {        no_mem();    }    if ((fp2 = create_newpasswd(file, file2, pwinfo->login, 0, 1)) == NULL) {        fprintf(stderr, "Error.\n"                "Check that [%s] already exists,\n"                "and that [%s] can be written.\n", pwinfo->login, file2);        free(file2);        return PW_ERROR_USER_ALREADY_EXIST;    }    fflush(fp2);#ifdef HAVE_FILENO    fsync(fileno(fp2));#endif    if (fclose(fp2) != 0) {        perror("Unable to close the file");        goto bye2;    }    if (rename(file2, file) != 0) {        perror("Unable to rename the file");        goto bye2;    }    free(file2);    return 0;        bye2:    unlink(file2);    free(file2);        return PW_ERROR_UNEXPECTED_ERROR;}static int do_show(const char * const file, const PWInfo * const pwinfo){    PWInfo fetched_info;    struct passwd *pwd;    struct group *grp;    const char *pwd_name = "-";    const char *grp_name = "-";        static char line[LINE_MAX];        if (pwinfo->login == NULL || *(pwinfo->login) == 0) {        fprintf(stderr, "Missing login\n");        return PW_ERROR_MISSING_LOGIN;    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }    if (fetch_pw_account(file, &fetched_info, line, sizeof line,                         pwinfo->login) != 0) {        fprintf(stderr, "Unable to fetch info about user [%s] in file [%s]\n",                pwinfo->login, file);        return PW_ERROR_UNABLE_TO_FETCH;    }    if ((pwd = getpwuid(fetched_info.uid)) != NULL && pwd->pw_name != NULL) {        pwd_name = pwd->pw_name;    }    if ((grp = getgrgid(fetched_info.gid)) != NULL && grp->gr_name != NULL) {        grp_name = grp->gr_name;    }    printf("\n"           "Login              : %s\n"           "Password           : %s\n"           "UID                : %lu (%s)\n"           "GID                : %lu (%s)\n"           "Directory          : %s\n"           "Full name          : %s\n"           "Download bandwidth : %lu Kb (%s)\n"           "Upload   bandwidth : %lu Kb (%s)\n"           "Max files          : %llu (%s)\n"           "Max size           : %llu Mb (%s)\n"           "Ratio              : %u:%u (%s:%s)\n"           "Allowed local  IPs : %s\n"           "Denied  local  IPs : %s\n"                      "Allowed client IPs : %s\n"           "Denied  client IPs : %s\n"                      "Time restrictions  : %04u-%04u (%s)\n"	   "Max sim sessions   : %u (%s)\n"           "\n",           fetched_info.login,           fetched_info.pwd,           (unsigned long) fetched_info.uid, pwd_name,           (unsigned long) fetched_info.gid, grp_name,           fetched_info.home,           fetched_info.gecos,           SHOW_IFEN(fetched_info.has_bw_dl, (unsigned long) fetched_info.bw_dl / 1024UL),           SHOW_STATE(fetched_info.has_bw_dl),           SHOW_IFEN(fetched_info.has_bw_ul, (unsigned long) fetched_info.bw_ul / 1024UL),           SHOW_STATE(fetched_info.has_bw_ul),           SHOW_IFEN(fetched_info.has_quota_files, (unsigned long long) fetched_info.quota_files),           SHOW_STATE(fetched_info.has_quota_files),           SHOW_IFEN(fetched_info.has_quota_size, (unsigned long long) fetched_info.quota_size / (1024ULL * 1024ULL)),           SHOW_STATE(fetched_info.has_quota_size),           SHOW_IFEN(fetched_info.has_ul_ratio, fetched_info.ul_ratio),           SHOW_IFEN(fetched_info.has_dl_ratio, fetched_info.dl_ratio),           SHOW_STATE(fetched_info.has_ul_ratio),           SHOW_STATE(fetched_info.has_dl_ratio),           SHOW_STRING(fetched_info.allow_local_ip),           SHOW_STRING(fetched_info.deny_local_ip),                      SHOW_STRING(fetched_info.allow_client_ip),           SHOW_STRING(fetched_info.deny_client_ip),           SHOW_IFEN(fetched_info.has_time, fetched_info.time_begin),           SHOW_IFEN(fetched_info.has_time, fetched_info.time_end),           SHOW_STATE(fetched_info.has_time),	   SHOW_IFEN(fetched_info.has_per_user_max, fetched_info.per_user_max),           SHOW_STATE(fetched_info.per_user_max));        return 0;}static int do_passwd(const char * const file,                     PWInfo * const pwinfo){    if (pwinfo->login == NULL || *(pwinfo->login) == 0) {        fprintf(stderr, "Missing login\n");        return PW_ERROR_MISSING_LOGIN;    }    if (file == NULL) {        fprintf(stderr, "Missing passwd file\n");        return PW_ERROR_MISSING_PASSWD_FILE;    }    if ((pwinfo->pwd = do_get_passwd()) == NULL) {        fprintf(stderr, "Error with entering password - aborting\n");                return PW_ERROR_ENTER_PASSWD_PW_ERROR;    }        do_usermod(file, pwinfo);        return 0;}static int do_mkdb(const char *dbfile, const char * const file){    FILE *fp;    char *index_dbfile;    size_t sizeof_index_dbfile;    char *data_dbfile;    size_t sizeof_data_dbfile;    char *s;    PureDBW dbw;    int ret = PW_ERROR_UNEXPECTED_ERROR;    char line[LINE_MAX];        if (dbfile == NULL || *dbfile == 0) {        char *dbfile_;

⌨️ 快捷键说明

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