pure-pw.c
来自「功能强大的ftp服务器源代码」· C语言 代码 · 共 1,575 行 · 第 1/4 页
C
1,575 行
if (*line != '/') { return -1; } pwinfo->home = line; if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_ul */ return 0; } if (*line != 0) { pwinfo->has_bw_ul = 1; pwinfo->bw_ul = strtoul(line, NULL, 10); } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_dl */ return 0; } if (*line != 0) { pwinfo->has_bw_dl = 1; pwinfo->bw_dl = strtoul(line, NULL, 10); } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio up */ return 0; } if (*line != 0) { pwinfo->ul_ratio = (unsigned int) strtoul(line, NULL, 10); if (pwinfo->ul_ratio > 0U) { pwinfo->has_ul_ratio = 1; } } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio down */ return 0; } if (*line != 0) { pwinfo->dl_ratio = (unsigned int) strtoul(line, NULL, 10); if (pwinfo->dl_ratio > 0U) { pwinfo->has_dl_ratio = 1; } } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* max cnx */ return 0; } if (*line != 0) { pwinfo->per_user_max = (unsigned int) strtoul(line, NULL, 10); if (pwinfo->per_user_max > 0U) { pwinfo->has_per_user_max = 1; } } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* files quota */ return 0; } if (*line != 0) { pwinfo->has_quota_files = 1; pwinfo->quota_files = strtoull(line, NULL, 10); } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* size quota */ return 0; } if (*line != 0) { pwinfo->has_quota_size = 1; pwinfo->quota_size = strtoull(line, NULL, 10); } if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed local ip */ return 0; } pwinfo->allow_local_ip = line; if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied local ip */ return 0; } pwinfo->deny_local_ip = line; if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed client ip */ return 0; } pwinfo->allow_client_ip = line; if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied client ip */ return 0; } pwinfo->deny_client_ip = line; if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* time */ return 0; } if (sscanf(line, "%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; } return 0;}static int fetch_pw_account(const char * const file, PWInfo * const pwinfo, char * const line, const size_t sizeof_line, const char * const login){ FILE *fp; int ret = -1; if (file == NULL || pwinfo == NULL || line == NULL || sizeof_line < (size_t) 2U || login == NULL || *login == 0) { fprintf(stderr, "bad arguments to fetch account\n"); return -1; } if ((fp = fopen(file, "r")) == NULL) { perror("Unable to open the passwd file"); return -1; } while (fgets(line, (int) sizeof_line - 1U, fp) != NULL) { strip_lf(line); if (*line == 0 || *line == PW_LINE_COMMENT) { continue; } if (parse_pw_line(line, pwinfo) != 0) { fprintf(stderr, "Warning: invalid line [%s]\n", line); continue; } if (strcmp(login, pwinfo->login) != 0) { continue; } ret = 0; break; } fclose(fp); return ret;}static FILE *create_newpasswd(const char * const file, const char * const file2, const char * const skip_login, int error_if_user_exists, int error_if_not_found){ FILE *fp; FILE *fp2; int fd2; int found = 0; size_t skip_login_len; char line[LINE_MAX]; fp = fopen(file, "r"); if ((fd2 = open(file2, O_EXCL | O_NOFOLLOW | O_CREAT | O_WRONLY, (mode_t) 0700)) == -1) { if (fp != NULL) { fclose(fp); } return NULL; } if ((fp2 = fdopen(fd2, "w")) == NULL) { if (fp != NULL) { fclose(fp); } close(fd2); return NULL; } if (fp != NULL) { if (skip_login != NULL) { skip_login_len = strlen(skip_login); } else { skip_login_len = (size_t) 0U; } while (fgets(line, (int) sizeof line - 1U, fp) != NULL) { if (skip_login_len > (size_t) 0U) { if (strncmp(line, skip_login, skip_login_len) == 0 && line[skip_login_len] == *PW_LINE_SEP) { if (error_if_user_exists != 0) { goto err; } found = 1; continue; } } if (fputs(line, fp2) < 0) { err: fclose(fp2); unlink(file2); if (fp != NULL) { fclose(fp); } return NULL; } } fflush(fp2); fsync(fd2); } if (error_if_not_found != 0 && found == 0) { goto err; } if (fp != NULL) { fclose(fp); } return fp2;}static int add_new_pw_line(FILE * const fp2, const PWInfo * const pwinfo){ if (fp2 == NULL) { return -1; } if (fprintf(fp2, "%s" PW_LINE_SEP /* account */ "%s" PW_LINE_SEP /* password */ "%lu" PW_LINE_SEP /* uid */ "%lu" PW_LINE_SEP /* gid */ "%s" PW_LINE_SEP /* gecos */ "%s" PW_LINE_SEP /* home */ , pwinfo->login, pwinfo->pwd, (unsigned long) pwinfo->uid, (unsigned long) pwinfo->gid, pwinfo->gecos, pwinfo->home) < 0) { return -1; } if (pwinfo->has_bw_ul != 0) { if (fprintf(fp2, "%lu", /* bw_ul */ (unsigned long) pwinfo->bw_ul) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_bw_dl != 0) { if (fprintf(fp2, "%lu", /* bw_dl */ (unsigned long) pwinfo->bw_dl) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_ul_ratio != 0) { if (fprintf(fp2, "%u", /* ratio up */ pwinfo->ul_ratio) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_dl_ratio != 0) { if (fprintf(fp2, "%u", /* ratio down */ pwinfo->dl_ratio) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_per_user_max != 0) { if (fprintf(fp2, "%u", pwinfo->per_user_max) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_quota_files != 0) { if (fprintf(fp2, "%llu", /* files quota */ pwinfo->quota_files) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_quota_size != 0) { if (fprintf(fp2, "%llu", /* size quota */ pwinfo->quota_size) < 0) { return -1; } } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->allow_local_ip != NULL) { fprintf(fp2, "%s", pwinfo->allow_local_ip); /* allowed local ip */ } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->deny_local_ip != NULL) { fprintf(fp2, "%s", pwinfo->deny_local_ip); /* denied local ip */ } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->allow_client_ip != NULL) { fprintf(fp2, "%s", pwinfo->allow_client_ip); /* allowed client ip */ } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->deny_client_ip != NULL) { fprintf(fp2, "%s", pwinfo->deny_client_ip); /* denied local ip */ } if (fprintf(fp2, PW_LINE_SEP) < 0) { return -1; } if (pwinfo->has_time != 0) { if (fprintf(fp2, "%u-%u", /* time restrictions */ pwinfo->time_begin, pwinfo->time_end) < 0) { return -1; } } if (fprintf(fp2, "\n") < 0) { return -1; } return 0;}static char *do_get_passwd(void){ static char pwd[LINE_MAX]; char pwd2[LINE_MAX]; int tries = MAX_PASSWD_CHANGE_TRIES; *pwd = 0; *pwd2 = 0; again: printf("Password: "); fflush(stdout); disable_echo(); if (fgets(pwd, (int) (sizeof pwd - 1U), stdin) == NULL) { enable_echo(); return NULL; } strip_lf(pwd); printf("\nEnter it again: "); fflush(stdout); disable_echo(); if (fgets(pwd2, (int) (sizeof pwd2 - 1U), stdin) == NULL) { enable_echo(); return NULL; } strip_lf(pwd2); puts(""); if (strcmp(pwd, pwd2) != 0) { if (*pwd2 != 0) { memset(pwd2, 0, strlen(pwd2)); } if (*pwd != 0) { memset(pwd, 0, strlen(pwd)); } puts("You didn't enter the same password"); if (--tries > 0) { goto again; } enable_echo(); return NULL; } if (*pwd2 != 0) { memset(pwd2, 0, strlen(pwd2)); } enable_echo(); return pwd;}static int do_list(const char * const file){ FILE *fp; PWInfo pwinfo; char line[LINE_MAX]; if (file == NULL) { fprintf(stderr, "missing file to list accounts\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; } while (fgets(line, (int) sizeof line - 1U, fp) != NULL) { strip_lf(line); if (*line == 0 || *line == PW_LINE_COMMENT) { continue; } if (parse_pw_line(line, &pwinfo) != 0) { fprintf(stderr, "Warning: invalid line [%s]\n", line); continue; } if (isatty(1)) { printf("%-19s %-39s %-19s\n", pwinfo.login, pwinfo.home, pwinfo.gecos); } else { printf("%s\t%s\t%s\n", pwinfo.login, pwinfo.home, pwinfo.gecos); } } fclose(fp); return 0;}static int do_useradd(const char * const file,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?