summaryrefslogtreecommitdiff
path: root/scripts/.local/bin/dmnt
blob: 7966d073eff9424bb8110ebc78e7ab8399bc8d55 (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
183
184
185
186
187
188
189
190
191
#!/bin/sh

#shopt -s expand_aliases
dmenu="$(command -v cdmenu)"

getmount() { \
    [ -z "$chosen" ] && exit 1
    # shellcheck disable=SC2086
    mp="$(find $1 2>/dev/null | "$dmenu" -l 16 -i -p "Type in mount point.")" || exit 1
    [ "$mp" = "" ] && exit 1
    if [ ! -d "$mp" ]; then
        mkdiryn=$(printf "No\nYes" | "$dmenu" -i -p "$mp does not exist. Create it?") || exit 1
        [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
    fi
}

mountusb() { \
    # try to mount based on fstab, hoping "user" option is there
    mount "$1" 2>/dev/null && notify-send "$1 mounted" "" && exit 0
    # if no "user" option - retry with sudo
    sudo -A mount "$1" 2>/dev/null && notify-send "$1 mounted" "" && exit 0
    # if failed - ask for a dir to mount to
    # this creates argument for "find"
    alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not ( -path *%s -prune ) ",$3}')

    getmount "/mnt /media -maxdepth 1 -type d $alreadymounted"
    partitiontype="$(lsblk -no "fstype" "$1")"
    case "$partitiontype" in
        "vfat") sudo -A mount -t vfat "$1" "$mp" -o rw,umask=0000;;
        "exfat") sudo -A mount "$1" "$mp" -o uid="$(id -u)",gid="$(id -g)";;
        *crypto_LUKS*)
            name="$(LC_ALL=C tr -dc A-Za-z0-9 </dev/urandom | head -c 16)"
            mkfifo fifo
            dpass > fifo &
            sudo -A cryptsetup open --type luks "$1" "$name" --key-file fifo
            rm fifo
            device="/dev/mapper/$name"
            sudo -A mount "$device" "$mp"
            ;;
        *) sudo -A mount "$1" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo chown "$user":"$ug" "$mp";;
    esac
    notify-send "$1 mounted to $mp" ""
}

mountandroid() { \
    getmount "/mnt -maxdepth 1 -type d"
    jmtpfs -device="$1" "$mp"
    #echo "OK" | "$dmenu" -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1
    #jmtpfs -device="$1" "$mp"
    notify-send "Android mounted to $mp" ""
}


unmountdev() { \
    result="$(sudo umount "$1" 2>&1)"
    if [ -z "$result" ]; then
        notify-send "$1 unmounted" ""
        return
    else
        processes=$(lsof "$1" | tail -n +2 | awk '{print $1 " " $2}' | uniq)
        pids="$(echo "$processes" | awk '{print $2}')"
        #notify-send -u critical "Unmount device" "$result\nPreparing list of processes blocking $1..."
        notify-send "Unmount $1" "$result\nBlocking processes:\n$processes"
        action=$(printf 'nothing\nterminate\nkill' | "$dmenu" -i -p "What to do to processes blocking $1" -l 3)
        case $action in
            terminate) echo "$pids" | while IFS="$(printf '\n')" read -r pid ; do kill "$pid"; done ;;
            kill) echo "$pids" | while IFS="$(printf '\n')" read -r pid ; do kill -9 "$pid"; done ;;
            nothing)
                notify-send "Unmount of $1 aborted" ""
                return ;;
        esac
        unmountdev "$1"
    fi
}

selectusb() { \
    chosen=$(lsblk -lpo "vendor,model,name,label,size,mountpoint" | "$dmenu" -i -l 16 | xargs | cut -d' ' -f 1)
    # if mounted
    if grep -q "$chosen" "/etc/mtab" ; then
        choice=$(printf 'No\nYes' | "$dmenu" -i -l 2 -p "Partition is mounted. Unmount?")
        case $choice in
            Yes) unmountdev "$chosen" ;;
            No) exit ;;
        esac
    else
        mountusb "$chosen"
    fi
}


mountsamba() { \

    mounted=$(mount -t cifs)
    if [ -z "$mounted" ]; then
        actions="mount\nunmount"
        action=$(printf '%s' "$actions" | "$dmenu" -i -p "Samba action:")

        if [ "$action" = "unmount" ]; then
            mount=$(printf '%s' "$mounted" | "$dmenu" -i -p "Share to unmount:")
            printf 'Selected: %s\n' "$mount"
            mount_dir=$(printf '%s' "$mount" | cut -d' ' -f 3)
            printf '%s\n' "$mount_dir"
            sudo -A umount "$mount_dir"
            exit 0
        fi
        # proceed to mounting
    fi

    # get IP of this device
    IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')
    echo "IP: $IP"
    # prepare subnet choices: choose IP or run a scan if a wildcard specified
    subnets="$IP\n$(echo "$IP" | cut -d. -f1-3).1\n$(echo "$IP" | cut -d. -f1-3).*\n$(echo "$IP" | cut -d. -f1-2).*.*"
    printf 'Subnets available:\n%s\n' "$subnets"
    subnet=$(printf '%s' "$subnets" | "$dmenu" -i -l 16 -p "Input samba scan range")
    printf 'Subnet chosen: %s' "$subnet"

    # if there's a * in subnet
    if [ "${subnet#*\*}" != "$subnet" ]; then
        notify-send "Scanning network for samba shares..." ""
        servers="$(nmap "$subnet" -p 445 --open -oG -)"
        echo "$servers"

        IPs=$(echo "$servers" | grep Scan | cut -d' ' -f2-)
        printf 'Server IPs:\n%s\n' "$IPs"

        IP=$(printf '%s' "$IPs" | "$dmenu" -i -l 16 -p "Choose samba server IP")
        echo "Selected IP: $IP"
    else
        IP="$subnet"
    fi

    user=$(printf '%s\nanonymous' "$(whoami)" | "$dmenu" -p "Samba username:")
    echo "$user"
    if [ "$user" != "anonymous" ]; then
        password=$(pass-askpass "$user's password ")
        shares="""$(smbclient -L "$IP" --user "$user" --password "$password")"""
    else
        notify-send "Attempting anonymous login..." ""
        shares="""$(smbclient -L "$IP" -N)"""
    fi
    printf 'Raw shares:\n%s\n' "$shares"

    #shares=$(printf "$shares" | sed -e 's/^[[:space:]]*//')
    #printf "Cleaned shares:\n$shares\n"

    share=$(printf '%s' "$shares" | "$dmenu" -i -l 16 -p "Which samba share to mount?")
    echo "$share"

    # leave only the share name, assuming no whitespace in share name
    chosen=$(printf '%s' "$share" | cut -d' ' -f 1)
    echo "Chosen: $chosen"

    share="//$IP/$chosen"

    getmount "/mnt -maxdepth 1 -type d"

    if ! sudo -A mount -t cifs -o user="$user",password="$password",iocharset=utf8,uid="$(id -u)",gid="$(id -g)",file_mode=0644,dir_mode=0755 "$share" "$mp"; then
        notify-send "Samba mount failed" "$subnet to $mp as $user"
        exit 1
    fi
    notify-send "Samba share succeeded" "$subnet mounted to $mp as $user"
}

selectandroid() { \
    unmounted=$(jmtpfs -l | sed -e '1,2d')
    mounted=$(grep "jmtpfs" "/etc/mtab")

    list="Mounted:\n"
    if [ -n "$mounted" ]; then
        list="$list$mounted"
    fi
    list="$list\nUnmounted:\n"
    if [ -n "$unmounted" ]; then
        list="$list$unmounted"
    fi

    chosen=$(echo "$list" "$mounted" | "$dmenu" -i -l 16)
    #echo "CHOSEN" $chosen
    case $chosen in
        *jmtpfs*) unmountdev "$(echo "$chosen" | cut -d' ' -f 2)" ;;
        *) mountandroid "$(echo "$chosen" | awk '{print $1 $2}' | cut -d, -f1-2)" ;;
    esac
}

choice="$(printf 'drive\nSamba\nAndroid' | "$dmenu" -i -p "(Un-)mount a")" || exit 1
case $choice in
    drive) selectusb ;;
    Samba) mountsamba ;;
    Android) selectandroid ;;
esac