2020-02-06 20:42:46 -05:00
|
|
|
package openvpn
|
|
|
|
|
|
|
|
|
|
import (
|
2020-07-12 14:55:03 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2020-02-06 20:42:46 -05:00
|
|
|
"github.com/qdm12/golibs/files"
|
|
|
|
|
)
|
|
|
|
|
|
2020-10-20 02:45:28 +00:00
|
|
|
// WriteAuthFile writes the OpenVPN auth file to disk with the right permissions.
|
2020-02-06 20:42:46 -05:00
|
|
|
func (c *configurator) WriteAuthFile(user, password string, uid, gid int) error {
|
2020-07-12 14:55:03 +00:00
|
|
|
exists, err := c.fileManager.FileExists(string(constants.OpenVPNAuthConf))
|
2020-02-06 20:42:46 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2020-07-12 14:55:03 +00:00
|
|
|
} else if exists {
|
|
|
|
|
data, err := c.fileManager.ReadFile(string(constants.OpenVPNAuthConf))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
|
if len(lines) > 1 && lines[0] == user && lines[1] == password {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
c.logger.Info("username and password changed", constants.OpenVPNAuthConf)
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|
|
|
|
|
return c.fileManager.WriteLinesToFile(
|
|
|
|
|
string(constants.OpenVPNAuthConf),
|
|
|
|
|
[]string{user, password},
|
2020-02-08 15:29:27 +00:00
|
|
|
files.Ownership(uid, gid),
|
2020-10-20 02:45:28 +00:00
|
|
|
files.Permissions(constants.UserReadPermission))
|
2020-02-06 20:42:46 -05:00
|
|
|
}
|