//
// Permission.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#if __MonoCS__
using Mono.Unix;
using Mono.Unix.Native;
#endif


#if __MonoCS__
namespace Ntp.Interop
{
    public static class Permission
    {
        public static uint? GetUserId(string userName) {
            Passwd pw = Syscall.getpwnam(userName);
            if (pw != null) {
                return pw.pw_uid;
            }

            return null;
        }
            
        public static uint? GetGroupId(string groupName) {
            Group gr = Syscall.getgrnam(groupName);
            if (gr != null) {
                return gr.gr_gid;
            }

            return null;
        }

        public static bool SetUserId(uint userId) {
            try {
                Syscall.seteuid (userId);
            } catch {
                return false;
            }

            return true;
        }

        public static bool ChangeFileMode(string file, uint mode)
        {
            FilePermissions permissions = NativeConvert.FromOctalPermissionString(mode.ToString());
            return (Syscall.chmod(file, permissions) == 0);
        }

        public static bool ChangeFileOwner(string file, uint user, uint? group)
        {
            return (Syscall.chown(file, user, group ?? unchecked((uint)-1)) == 0);
        }
    }
}
#else

namespace Ntp.Interop
{
    public static class Permission
    {
        public static uint? GetUserId(string userName)
        {
            return null;
        }

        public static uint? GetGroupId(string groupName)
        {
            return null;
        }

        public static bool SetUserId(uint userId)
        {
            return true;
        }

        public static bool ChangeFileMode(string file, uint mode)
        {
            return true;
        }

        public static bool ChangeFileOwner(string file, uint user, uint? group)
        {
            return true;
        }
    }
}

#endif