forked from Plemya-x/ALR
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
// ALR - Any Linux Repository
|
|
// Copyright (C) 2025 Евгений Храмов
|
|
//
|
|
// 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, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"os/user"
|
|
"strconv"
|
|
"syscall"
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
)
|
|
|
|
func GetUidGidAlrUserString() (string, string, error) {
|
|
u, err := user.Lookup("alr")
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
return u.Uid, u.Gid, nil
|
|
}
|
|
|
|
func GetUidGidAlrUser() (int, int, error) {
|
|
strUid, strGid, err := GetUidGidAlrUserString()
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
uid, err := strconv.Atoi(strUid)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
gid, err := strconv.Atoi(strGid)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
|
|
return uid, gid, nil
|
|
}
|
|
|
|
func DropCapsToAlrUser() error {
|
|
uid, gid, err := GetUidGidAlrUser()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = syscall.Setgid(gid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = syscall.Setuid(uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ExitIfNotRoot() {
|
|
if os.Getuid() != 0 {
|
|
slog.Error(gotext.Get("You need to be root"))
|
|
os.Exit(1)
|
|
}
|
|
}
|