Skip to content
Permalink
Browse files

Let setting admin password with script when YaCy server is not running

Still using the ConfigAccounts_p API when the server is running.
  • Loading branch information...
luccioman committed Nov 28, 2018
1 parent ede5cc0 commit 3061b58c8c36d94293d102ad1eb40f3afa1a2f3e
Showing with 33 additions and 9 deletions.
  1. +33 −9 bin/passwd.sh
@@ -5,12 +5,18 @@ cd "`dirname $0`"

echo "Setting new YaCy administrator password..."

# take the administrator user name from the config file
YACY_ADMIN_USER_NAME=$(grep ^adminAccountUserName= "$YACY_DATA_PATH/SETTINGS/yacy.conf" | cut -d= -f2 | tr -d '\r\n')
YACY_CONF_FILE="$YACY_DATA_PATH/SETTINGS/yacy.conf"

if [ -z "$YACY_ADMIN_USER_NAME" ]; then
# administrator user name should not be empty : by the way, in that case use the same default value as in YaCy application
YACY_ADMIN_USER_NAME="admin"
# take the administrator user name and realm from the config file
YACY_ADMIN_USER_NAME=$(grep ^adminAccountUserName= "$YACY_CONF_FILE" | cut -d= -f2 | tr -d '\r\n')
YACY_ADMIN_REALM=$(grep "^adminRealm=" "$YACY_CONF_FILE" | cut -d= -f2 | tr -d '\r\n')

# admin user name and realm should not be empty : by the way, in that case use the same default values as in YaCy application
if [ -z "$YACY_ADMIN_USER_NAME" ]; then
YACY_ADMIN_USER_NAME="admin"
fi
if [ -z "$YACY_ADMIN_REALM" ]; then
YACY_ADMIN_REALM="YaCy"
fi

if [ -z "$1" ]; then
@@ -28,7 +34,25 @@ if [ ${#YACY_ADMIN_PASSWORD} -le 2 ]; then
exit 1
fi

(./protectedPostApiCall.sh "ConfigAccounts_p.html" "setAdmin=&adminuser=$YACY_ADMIN_USER_NAME&adminpw1=$YACY_ADMIN_PASSWORD&adminpw2=$YACY_ADMIN_PASSWORD&access=" && \
echo "Password successfully changed for User Name '$YACY_ADMIN_USER_NAME'.") || \
(echo "Password setting failed." && \
exit 1)
if [ -f "$YACY_DATA_PATH/yacy.running" ]; then
echo "YaCy server appears to be running. Calling the ConfigAccounts_p API..."
# When the server is running we can not directly modify the yacy.conf file so we use the ConfigAccounts_p API.
# Otherwise the new password provided here could be overwritten by the server when it saves its in-memory configuration to the yacy.conf file
(./protectedPostApiCall.sh "ConfigAccounts_p.html" "setAdmin=&adminuser=$YACY_ADMIN_USER_NAME&adminpw1=$YACY_ADMIN_PASSWORD&adminpw2=$YACY_ADMIN_PASSWORD&access=" && \
echo "Password successfully changed for User Name '$YACY_ADMIN_USER_NAME'.") || \
(echo "Password setting failed." && exit 1)
else
echo "YaCy server appears to be shutdown. Modifying the configuration file at $YACY_CONF_FILE..."
B64MD5=$(java -cp "$YACY_APP_PATH/lib/yacycore.jar" net.yacy.cora.order.Digest -strfhex "$YACY_ADMIN_USER_NAME:$YACY_ADMIN_REALM:$YACY_ADMIN_PASSWORD" | head -n 1)
if [ -z "$B64MD5" ]; then
echo "Password setting failed."
exit 1
fi
echo "B64MD5 $B64MD5"
PASSWORD_HASH="MD5:$B64MD5"

(sed "/adminAccountBase64MD5=/c\adminAccountBase64MD5=$PASSWORD_HASH" "$YACY_CONF_FILE" > "$YACY_CONF_FILE".tmp && \
mv "$YACY_CONF_FILE".tmp "$YACY_CONF_FILE" && \
echo "Password successfully changed for User Name '$YACY_ADMIN_USER_NAME'.") || \
(echo "Password setting failed." && exit 1)
fi

0 comments on commit 3061b58

Please sign in to comment.
You can’t perform that action at this time.