How-to: Mail-Primary-Address - Change the mailPrimaryAddress from user objects

How to:

In this article, I will explain how to change a user’s mailPrimaryAddress using UDM. In our scenario, we select users from a specific group and update their mail addresses as needed.
Additionally, a Bash script is provided that allows you to search for users within any group and automatically update their email addresses to the format username@mail-domain.

Step 1: Search for groups

For example, if I want to know which groups my account is in, I could use the following command with the username uid=mirac.erde.

udm users/user list --filter uid=mirac.erde | grep groups

  groups: cn=Domain Users mejneschool2,cn=groups,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  groups: cn=schueler-mejneschool2,cn=groups,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  groups: cn=mejneschool2-Mejne2-Gruppe,cn=schueler,cn=groups,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  groups: cn=Freigaben-Grupen,cn=groups,dc=ucs5schoolhejne,dc=intranet
  groups: cn=Paulaner-Gruppe,cn=groups,dc=ucs5schoolhejne,dc=intranet
  groups: cn=mejneschool2-mejne2-Klasse,cn=klassen,cn=schueler,cn=groups,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  primaryGroup: cn=Domain Users mejneschool2,cn=groups,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet

Step 2: Search for all user object from the group

For our case, let’s list all users from the group schueler-mejneschool2 so that we can then change the mailPrimaryAddress.

udm groups/group list --filter cn=schueler-mejneschool2 | grep users

  users: uid=jan.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  users: uid=max.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  users: uid=ingrid.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  users: uid=mirac.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet
  users: uid=theo.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet

Step 3: Modify the user object

With the respective DN of the user object, the mailPrimaryAddress can now be changed.

udm users/user modify --dn "uid=mirac.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet" --set "mailPrimaryAddress=mirac.erde@univention.de"

Object modified: uid=mirac.erde,cn=schueler,cn=users,ou=mejneschool2,dc=ucs5schoolhejne,dc=intranet

Bash script update_mailPrimaryAddress.sh

The procedure described above can be very laborious in a large environment with a large number of users if each user is to be modified individually.
We therefore provide the following script, which can be used to do this automatically.
At the beginning 2 questions appear:

  1. Which group should be searched?
  2. Which mail domain should be used (e.g. univention.de)?

After the questions have been answered, the system searches for user objects in the respective group and then changes the mail address to username@mail-domain.

#!/bin/bash
  
# === Eingaben abfragen ===
read -p "Welche Gruppe soll durchsucht werden? " GRUPPE
read -p "Welche Mail-Domain soll verwendet werden (z.B. univention.de)? " DOMAIN

# Benutzer aus der Gruppe filtern
echo "Lese Benutzer aus Gruppe: $GRUPPE ..."
USER_DNS=$(udm groups/group list --filter "cn=$GRUPPE" | grep 'users:' -A100 | grep 'uid=' | awk '{print $2}')

if [[ -z "$USER_DNS" ]]; then
    echo "Keine Benutzer gefunden in Gruppe '$GRUPPE'."
    exit 1
fi

# Jeden Benutzer verarbeiten
echo "Bearbeite Benutzer..."

while read -r DN; do
    echo "Verarbeite: $DN"

    # UID extrahieren
    user_UID=$(echo "$DN" | grep -oP 'uid=\K[^,]+')

    if [[ -z "$user_UID" ]]; then
        echo "Fehler: UID konnte nicht extrahiert werden aus $DN"
        continue
    fi

    # Benutzerdaten mit Filter abrufen
    USER_DATA=$(udm users/user list --filter "uid=$user_UID")

    USERNAME=$(echo "$USER_DATA" | awk -F': ' '/^  username: / {print $2}' | tr '[:upper:]' '[:lower:]')

    if [[ -z "$USERNAME" ]]; then
        echo "Fehler: Name konnte nicht ermittelt werden für UID $user_UID"
        continue
    fi

    NEUE_MAIL="${USERNAME:0}@$DOMAIN"
    echo "➤ Setze neue Mailadresse: $NEUE_MAIL"

    # E-Mail setzen
    udm users/user modify --dn "$DN" --set "mailPrimaryAddress=$NEUE_MAIL"

done <<< "$USER_DNS"

echo "Fertig."

update_mailPrimaryAddress.sh (1.3 KB)

Once you’ve downloaded the script or saved it via copy and paste, you can simply run it as root. If needed, make sure to adjust the permissions beforehand.

1.Make the script executable: chmod +x update_mailPrimaryAddress.sh
2. Run the script: ./update_mailPrimaryAddress.sh


See also: