Nextcloud:Nutzern einen 2FA Token zumailen

Gerade stand ich vor der Aufgabe, den Nutzern einer Nextcloud Instanz 2FA aufzuzwingen. Dies lässt sich im Nextcloud Admin per Schiebregler konfigurieren.

2FA ist zur Absicherung eines Kontos natürlich alternativlos. Erzwinge ich 2FA als Admin so, stellt sich für alle User, die diese zusätliche Sicherheitsschicht in ihrem Konto nocht nicht aktiviert haben das Problem, dass sie sich an ihrem Konto nicht mehr anmelden können.

Aus o.g Grund habe ich ein kleines Bash-Skript erstellt, dass die Userdatenbank nach aktiven Konten durchsucht und dabei diejenigen User anmailt, die 2FA noch nicht aktiviert haben.

Getestet habe ich es unter Ubuntu 22.04. Voraussetzung ist u.A dass PHP installiert ist und der Server Mails versenden kann, z.B. durch einen internen Mailserver, oder via Relay.

#! /usr/bin/env bash
#Author:todde
#Version:0.1
#This script search for users, who do not have 2fa enabled on their account.
#Then a one time code is generated and mailed to the user for initial login
##############################################################################

site="https://www.example.net"
mail_from="admin@example.net"


#Find all active users
user=`sudo -u www-data php /var/www/nextcloud/occ user:list | cut -c 5- | cut -d : -f 1`



for i in $user; do

#Extract email address from each user
   email=`sudo -u www-data php /var/www/nextcloud/occ user:info $i | egrep 'email:' | cut -c 5- | cut -d : -f 2`;
#Compare if totp is already in place
   status=`sudo -u www-data php /var/www/nextcloud/occ twofactorauth:state $i | egrep 'totp' | cut -c 3-`;

#Do nothing if totp is enabled    
     if [ "$status" = "totp" ]; then
        echo $i  >> /dev/null
#Generate code for initial login and mail code and instructions 2 user
     else
        code=`sudo -u www-data php /var/www/nextcloud/occ twofactorauth:admin:generate-code $i`
        printf "Dear $i, please enable 2fa on $site.\n\n Your inital code: \n $code \n\n\n For detailed instructions visit:\n https://docs.nextcloud.com/server/latest/user_manual/en/user_2fa.html \n\n You must complete the above steps from instructions, otherwise you will not be able to log into your account a second time!\n\n Regards - Your Site Administrator from $site" | mail -s "Urgent notice from $site" $email -r $mail_from



     fi

done

Das ganze lässt sich auch via cron in regelmässigen Abständen automatisch ausführen. Der generierte Code is 48 Stunden gültig, sodass eine Ausführung in diesem Abstand Sinn ergibt.

Ich hoffe, ihr findet dieses Skript nützlich. Bei Fragen/Problemen oder Anregungen nutzt gerne die Kommentarfunktion weiter unten.

Print Friendly, PDF & Email
Abonnieren
Benachrichtige mich bei

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x