summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/linux/installer/module-autologon.sh
blob: 8fdf792b4c47b4068a3db6ec5dd4e66b7f2f6e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Oracle VM VirtualBox
# $Id: module-autologon.sh $
## @file
# VirtualBox Linux Guest Additions installer - autologon module
#

#
# Copyright (C) 2012-2022 Oracle and/or its affiliates.
#
# This file is part of VirtualBox base platform packages, as
# available from https://www.virtualbox.org.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, in version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-3.0-only
#

# @todo Document functions and their usage!

MOD_AUTOLOGON_DEFAULT_LIGHTDM_CONFIG="/etc/lightdm/lightdm.conf"
MOD_AUTOLOGON_DEFAULT_LIGHTDM_GREETER_DIR="/usr/share/xgreeters"

mod_autologon_init()
{
    echo "Initializing auto-logon support ..."
    return 0
}

mod_autologon_install_ex()
{
    info "Installing auto-logon support ..."

    ## Parameters:
    # Greeter directory. Defaults to /usr/share/xgreeters.
    greeter_dir="$1"
    # LightDM config. Defaults to /etc/lightdm/lightdm.conf.
    lightdm_config="$2"
    # Whether to force installation if non-compatible distribution
    # is detected.
    force="$3"

    # Check for Ubuntu and derivates. @todo Debian?
    distros="Ubuntu UbuntuStudio Edubuntu Kubuntu Lubuntu Mythbuntu Xubuntu"
    ## @todo Map Linux Mint versions to Ubuntu ones.

    ## @todo Move the distro check to a routine / globals as soon as
    ##       we have other distribution-dependent stuff.
    which lsb_release &>/dev/null
    if test "$?" -ne "0"; then
        info "Error: lsb_release not found (path set?), skipping auto-logon installation"
        return 1
    fi
    distro_name=$(lsb_release -si)
    distro_ver=$(lsb_release -sr)

    for distro_cur in ${distros}; do
        if test "$distro_name" = "$distro_cur"; then
            distro_found="true"
            break
        fi
    done

    if test -z "$distro_found"; then
        if ! test "$force" = "force"; then
            info "Error: Unsupported distribution \"$distro_name\" found, skipping auto-logon installation"
            return 1
        fi
        info "Warning: Unsupported distribution \"$distro_name\" found"
    else
        # Do we have Ubuntu 11.10 or greater?
        # Use AWK for comparison since we run on plan sh.
        echo | awk 'END { exit ( !('"$distro_ver >= 11.10"') ); }'
        if test "$?" -ne "0"; then
            if ! test "$force" = "force"; then
                info "Error: Version $distro_ver of \"$distro_name\" not supported, skipping auto-logon installation"
                return 1
            fi
            info "Warning: Unsupported \"$distro_name\" version $distro_ver found"
        fi
    fi

    # Install dependencies (lightdm and FLTK 1.3+) using apt-get.
    which apt-get &>/dev/null
    if test "$?" -ne "0"; then
        info "Error: apt-get not found (path set?), skipping auto-logon installation"
        return 1
    fi
    info "Checking and installing necessary dependencies ..."
    apt-get -qqq -y install libfltk1.3 libfltk-images1.3 || return 1
    apt-get -qqq -y install lightdm || return 1

    # Check for LightDM config.
    if ! test -f "$lightdm_config"; then
        info "Error: LightDM config \"$lightdm_config\" not found (LightDM installed?), skipping auto-logon installation"
        return 1
    fi

    # Check for /usr/share/xgreeters.
    if ! test -d "$greeter_dir"; then
        if ! test "$force" = "force"; then
            info "Error: Directory \"$greeter_dir\" does not exist, skipping auto-logon installation"
            return 1
        fi
        info "Warning: Directory \"$greeter_dir\" does not exist, creating it"
        mkdir -p -m 755 "$greeter_dir" || return 1
    fi

    # Link to required greeter files into $greeter_dir.
    add_symlink "$INSTALLATION_DIR/other/vbox-greeter.desktop" "$greeter_dir/vbox-greeter.desktop"

    # Backup and activate greeter config.
    if ! test -f "$lightdm_config.vbox-backup"; then
        info "Backing up LightDM configuration file ..."
        cp "$lightdm_config" "$lightdm_config.vbox-backup" || return 1
        chmod 644 "$lightdm_config.vbox-backup" || return 1
    fi
    sed -i -e 's/^\s*greeter-session\s*=.*/greeter-session=vbox-greeter/g' "$lightdm_config" || return 1
    chmod 644 "$lightdm_config" || return 1

    info "Auto-logon installation successful"
    return 0
}

mod_autologon_install()
{
    if [ -z "$MOD_AUTOLOGON_LIGHTDM_GREETER_DIR" ]; then
        MOD_AUTOLOGON_LIGHTDM_GREETER_DIR=$MOD_AUTOLOGON_DEFAULT_LIGHTDM_GREETER_DIR
    fi
    if [ -z "$MOD_AUTOLOGON_LIGHTDM_CONFIG" ]; then
        MOD_AUTOLOGON_LIGHTDM_CONFIG=$MOD_AUTOLOGON_DEFAULT_LIGHTDM_CONFIG
    fi

    mod_autologon_install_ex "$MOD_AUTOLOGON_LIGHTDM_GREETER_DIR" "$MOD_AUTOLOGON_LIGHTDM_CONFIG" "$MOD_AUTOLOGON_FORCE"
    return $?
}

mod_autologon_pre_uninstall()
{
    echo "Preparing to uninstall auto-logon support ..."
    return 0
}

mod_autologon_uninstall()
{
    if test -z "$MOD_AUTOLOGON_LIGHTDM_CONFIG"; then
        return 0
    fi
    info "Un-installing auto-logon support ..."

    # Switch back to original greeter.
    if test -f "$MOD_AUTOLOGON_LIGHTDM_CONFIG.vbox-backup"; then
        mv "$MOD_AUTOLOGON_LIGHTDM_CONFIG.vbox-backup" "$MOD_AUTOLOGON_LIGHTDM_CONFIG"
        if test "$?" -ne "0"; then
            info "Warning: Could not restore original LightDM config \"$MOD_AUTOLOGON_LIGHTDM_CONFIG\""
        fi
    fi

    # Remove greeter directory (if not empty).
    rm "$MOD_AUTOLOGON_LIGHTDM_GREETER_DIR" 2>/dev/null

    info "Auto-logon uninstallation successful"
    return 0
}

mod_autologon_config_save()
{
    echo "
MOD_AUTOLOGON_LIGHTDM_CONFIG='$MOD_AUTOLOGON_LIGHTDM_CONFIG'
MOD_AUTOLOGON_LIGHTDM_GREETER_DIR='$MOD_AUTOLOGON_LIGHTDM_GREETER_DIR'"
}