📄 htpasswd.c
字号:
else if (*arg == 'm') { *alg = ALG_APMD5; } else if (*arg == 's') { *alg = ALG_APSHA; } else if (*arg == 'p') { *alg = ALG_PLAIN; } else if (*arg == 'd') { *alg = ALG_CRYPT; } else if (*arg == 'b') { *mask |= APHTP_NONINTERACTIVE; args_left++; } else if (*arg == 'D') { *mask |= APHTP_DELUSER; } else { usage(); } } } if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_NOFILE)) { apr_file_printf(errfile, "%s: -c and -n options conflict\n", argv[0]); exit(ERR_SYNTAX); } if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_DELUSER)) { apr_file_printf(errfile, "%s: -c and -D options conflict\n", argv[0]); exit(ERR_SYNTAX); } if ((*mask & APHTP_NOFILE) && (*mask & APHTP_DELUSER)) { apr_file_printf(errfile, "%s: -n and -D options conflict\n", argv[0]); exit(ERR_SYNTAX); } /* * Make sure we still have exactly the right number of arguments left * (the filename, the username, and possibly the password if -b was * specified). */ if ((argc - i) != args_left) { usage(); } if (*mask & APHTP_NOFILE) { i--; } else { if (strlen(argv[i]) > (APR_PATH_MAX - 1)) { apr_file_printf(errfile, "%s: filename too long\n", argv[0]); exit(ERR_OVERFLOW); } *pwfilename = apr_pstrdup(pool, argv[i]); if (strlen(argv[i + 1]) > (MAX_STRING_LEN - 1)) { apr_file_printf(errfile, "%s: username too long (> %d)\n", argv[0], MAX_STRING_LEN - 1); exit(ERR_OVERFLOW); } } *user = apr_pstrdup(pool, argv[i + 1]); if ((arg = strchr(*user, ':')) != NULL) { apr_file_printf(errfile, "%s: username contains illegal " "character '%c'\n", argv[0], *arg); exit(ERR_BADUSER); } if (*mask & APHTP_NONINTERACTIVE) { if (strlen(argv[i + 2]) > (MAX_STRING_LEN - 1)) { apr_file_printf(errfile, "%s: password too long (> %d)\n", argv[0], MAX_STRING_LEN); exit(ERR_OVERFLOW); } *password = apr_pstrdup(pool, argv[i + 2]); }}/* * Let's do it. We end up doing a lot of file opening and closing, * but what do we care? This application isn't run constantly. */int main(int argc, const char * const argv[]){ apr_file_t *fpw = NULL; char record[MAX_STRING_LEN]; char line[MAX_STRING_LEN]; char *password = NULL; char *pwfilename = NULL; char *user = NULL; char tn[] = "htpasswd.tmp.XXXXXX"; char *dirname; char *scratch, cp[MAX_STRING_LEN]; int found = 0; int i; int alg = ALG_CRYPT; int mask = 0; apr_pool_t *pool; int existing_file = 0;#if APR_CHARSET_EBCDIC apr_status_t rv; apr_xlate_t *to_ascii;#endif apr_app_initialize(&argc, &argv, NULL); atexit(terminate); apr_pool_create(&pool, NULL); apr_file_open_stderr(&errfile, pool);#if APR_CHARSET_EBCDIC rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, pool); if (rv) { apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d\n", rv); exit(1); } rv = apr_SHA1InitEBCDIC(to_ascii); if (rv) { apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d\n", rv); exit(1); } rv = apr_MD5InitEBCDIC(to_ascii); if (rv) { apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d\n", rv); exit(1); }#endif /*APR_CHARSET_EBCDIC*/ check_args(pool, argc, argv, &alg, &mask, &user, &pwfilename, &password);#if defined(WIN32) || defined(NETWARE) if (alg == ALG_CRYPT) { alg = ALG_APMD5; apr_file_printf(errfile, "Automatically using MD5 format.\n"); }#endif#if (!(defined(WIN32) || defined(TPF) || defined(NETWARE))) if (alg == ALG_PLAIN) { apr_file_printf(errfile,"Warning: storing passwords as plain text " "might just not work on this platform.\n"); }#endif /* * Only do the file checks if we're supposed to frob it. */ if (!(mask & APHTP_NOFILE)) { existing_file = exists(pwfilename, pool); if (existing_file) { /* * Check that this existing file is readable and writable. */ if (!accessible(pool, pwfilename, APR_READ | APR_APPEND)) { apr_file_printf(errfile, "%s: cannot open file %s for " "read/write access\n", argv[0], pwfilename); exit(ERR_FILEPERM); } } else { /* * Error out if -c was omitted for this non-existant file. */ if (!(mask & APHTP_NEWFILE)) { apr_file_printf(errfile, "%s: cannot modify file %s; use '-c' to create it\n", argv[0], pwfilename); exit(ERR_FILEPERM); } /* * As it doesn't exist yet, verify that we can create it. */ if (!accessible(pool, pwfilename, APR_CREATE | APR_WRITE)) { apr_file_printf(errfile, "%s: cannot create file %s\n", argv[0], pwfilename); exit(ERR_FILEPERM); } } } /* * All the file access checks (if any) have been made. Time to go to work; * try to create the record for the username in question. If that * fails, there's no need to waste any time on file manipulations. * Any error message text is returned in the record buffer, since * the mkrecord() routine doesn't have access to argv[]. */ if (!(mask & APHTP_DELUSER)) { i = mkrecord(user, record, sizeof(record) - 1, password, alg); if (i != 0) { apr_file_printf(errfile, "%s: %s\n", argv[0], record); exit(i); } if (mask & APHTP_NOFILE) { printf("%s\n", record); exit(0); } } /* * We can access the files the right way, and we have a record * to add or update. Let's do it.. */ if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: could not determine temp dir\n", argv[0]); exit(ERR_FILEPERM); } dirname = apr_psprintf(pool, "%s/%s", dirname, tn); if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to create temporary file %s\n", argv[0], dirname); exit(ERR_FILEPERM); } /* * If we're not creating a new file, copy records from the existing * one to the temporary file until we find the specified user. */ if (existing_file && !(mask & APHTP_NEWFILE)) { if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to read file %s\n", argv[0], pwfilename); exit(ERR_FILEPERM); } while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) { char *colon; strcpy(cp, line); scratch = cp; while (apr_isspace(*scratch)) { ++scratch; } if (!*scratch || (*scratch == '#')) { putline(ftemp, line); continue; } /* * See if this is our user. */ colon = strchr(scratch, ':'); if (colon != NULL) { *colon = '\0'; } else { /* * If we've not got a colon on the line, this could well * not be a valid htpasswd file. * We should bail at this point. */ apr_file_printf(errfile, "\n%s: The file %s does not appear " "to be a valid htpasswd file.\n", argv[0], pwfilename); apr_file_close(fpw); exit(ERR_INVALID); } if (strcmp(user, scratch) != 0) { putline(ftemp, line); continue; } else { if (!(mask & APHTP_DELUSER)) { /* We found the user we were looking for. * Add him to the file. */ apr_file_printf(errfile, "Updating "); putline(ftemp, record); found++; } else { /* We found the user we were looking for. * Delete them from the file. */ apr_file_printf(errfile, "Deleting "); found++; } } } apr_file_close(fpw); } if (!found && !(mask & APHTP_DELUSER)) { apr_file_printf(errfile, "Adding "); putline(ftemp, record); } else if (!found && (mask & APHTP_DELUSER)) { apr_file_printf(errfile, "User %s not found\n", user); exit(0); } apr_file_printf(errfile, "password for user %s\n", user); /* The temporary file has all the data, just copy it to the new location. */ if (apr_file_copy(dirname, pwfilename, APR_FILE_SOURCE_PERMS, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to update file %s\n", argv[0], pwfilename); exit(ERR_FILEPERM); } apr_file_close(ftemp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -