passwd_common.c revision dbf5f584c62fe6030d81121fdddeb7588b78b867
8a77240a809197c92c0736c431b4b88947a7bac1Christian Maeder/* Licensed to the Apache Software Foundation (ASF) under one or more
8a77240a809197c92c0736c431b4b88947a7bac1Christian Maeder * contributor license agreements. See the NOTICE file distributed with
8a77240a809197c92c0736c431b4b88947a7bac1Christian Maeder * this work for additional information regarding copyright ownership.
adea2e45fa61f1097aadc490a0aeaf4831b729ccChristian Maeder * The ASF licenses this file to You under the Apache License, Version 2.0
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski * (the "License"); you may not use this file except in compliance with
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder * the License. You may obtain a copy of the License at
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * http://www.apache.org/licenses/LICENSE-2.0
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski * Unless required by applicable law or agreed to in writing, software
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski * distributed under the License is distributed on an "AS IS" BASIS,
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder * See the License for the specific language governing permissions and
0095c7efbddd0ffeed6aaf8ec015346be161d819Till Mossakowski * limitations under the License.
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder const char *buf = "Error: out of memory\n";
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski written = write(STDERR_FILENO, buf, count);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder /* NOTREACHED */
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowskistatic int generate_salt(char *s, size_t size, const char **errstr,
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder static const char itoa64[] =
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski if (n > sizeof(rnd)) {
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder apr_file_printf(errfile, "generate_salt(): BUG: Buffer too small");
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder *errstr = apr_psprintf(pool, "Unable to generate random bytes: %pm",
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski while (size > 0) {
a98fd29a06e80e447af26d898044c23497adbc73Mihai Codescu apr_file_printf(errfile, "Error writing temp file: %pm", &rv);
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski if (apr_file_open_stdin(&file_stdin, ctx->pool) != APR_SUCCESS) {
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ctx->errstr = "Unable to read from stdin.";
df11e5eab86d8247f58e301d8f0a2c6ecf4c9541Till Mossakowski if (apr_file_read_full(file_stdin, buf, sizeof(buf) - 1,
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder else if (ctx->passwd_src == PW_PROMPT_VERIFY) {
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder if (apr_password_get("Enter password: ", buf, &bufsize) != 0)
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder if (apr_password_get("New password: ", buf, &bufsize) != 0)
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder apr_password_get("Re-type new password: ", buf, &bufsize);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder memset(ctx->passwd, '\0', strlen(ctx->passwd));
c911a0ec80ca4a178399c68f1e28be4e2bf42fceChristian Maeder "password too long (>%" APR_SIZE_T_FMT ")",
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder * Make a password record from the given information. A zero return
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder * indicates success; on failure, ctx->errstr points to the error message.
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder if (ctx->cost != 0 && ctx->alg != ALG_BCRYPT) {
d3c9318c22fcf44d9135a3b2c64f880b9a785babChristian Maeder "Warning: Ignoring -C argument for this algorithm." NL);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder /* XXX out >= 28 + strlen(sha1) chars - fixed len SHA */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder rv = apr_md5_encode(pw, salt, ctx->out, ctx->out_len);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder /* XXX this len limitation is not in sync with any HTTPd len. */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ret = generate_salt(salt, 8, &ctx->errstr, ctx->pool);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ctx->errstr = apr_psprintf(ctx->pool, "crypt() failed: %pm", &rv);
a7be28e157e9ceeec73a8fd0e642c36ea29d4218Christian Maeder apr_cpystrn(ctx->out, cbuf, ctx->out_len - 1);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder char *truncpw = apr_pstrdup(ctx->pool, pw);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder if (!strcmp(ctx->out, crypt(truncpw, salt))) {
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder apr_file_printf(errfile, "Warning: Password truncated to 8 "
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder#endif /* CRYPT_ALGO_SUPPORTED */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder rv = apr_generate_random_bytes((unsigned char*)salt, 16);
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ctx->errstr = apr_psprintf(ctx->pool, "Unable to generate random "
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder rv = apr_bcrypt_encode(pw, ctx->cost, (unsigned char*)salt, 16,
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder ctx->errstr = apr_psprintf(ctx->pool, "Unable to encode with "
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder#endif /* BCRYPT_ALGO_SUPPORTED */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder apr_file_printf(errfile, "mkhash(): BUG: invalid algorithm %d",
c911a0ec80ca4a178399c68f1e28be4e2bf42fceChristian Maederint parse_common_options(struct passwd_ctx *ctx, char opt,
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder /* Backward compatible behavior: Just print a warning */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder "Warning: storing passwords as plain text might just "
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder /* Backward compatible behavior: Use MD5. OK since MD5 is more secure */
09b431a868c79a92ae7c9bd141565f43f9034144Christian Maeder "Warning: CRYPT algorithm not supported on this "
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder /* Don't fall back to something less secure */
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder ctx->errstr = "BCRYPT algorithm not supported on this platform";
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder ctx->errstr = "argument to -C must be a positive integer";
556f473448dfcceee22afaa89ed7a364489cdbbbChristian Maeder "parse_common_options(): BUG: invalid option %c",