/*
* Copyright 2004-2006 Luke Quinane and Daniel Frampton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Mono.Posix;
using log4net;
namespace MonoPrivileges{
/// <summary>
/// Drops the privileges of the current user by changing to a different
/// user Id and group Id.
/// </summary>
public class MonoPrivileges {
/// <summary>
/// Logging support for this class.
/// </summary>
protected static readonly ILog log = LogManager.GetLogger(typeof(MonoPrivileges));
/// <summary>
/// Sets the current user Id (from /etc/password).
/// </summary>
/// <param name="uid">The new user Id to use.</param>
/// <returns>True if the user Id equals the requested Id.</returns>
public static bool SetUser(int uid) {
// attempt to set the user Id
if (log.IsDebugEnabled) {
log.Debug("Changing user Id to: " + uid);
}
Syscall.setuid(uid);
if (Syscall.getuid() == uid) {
return true;
}
else {
return false;
}
}
/// <summary>
/// Returns the current user Id.
/// </summary>
/// <returns>The current user Id.</returns>
public static int GetUser() {
return Syscall.getuid();
}
/// <summary>
/// Returns the current group Id.
/// </summary>
/// <returns>The current group Id.</returns>
public static int GetGroup() {
return -1; //Syscall.getgid();
}
/// <summary>
/// Sets the current group Id (from /etc/group).
/// </summary>
/// <param name="gid">The new group Id to use.</param>
/// <returns>True if the group Id equals the requested Id.</returns>
public static bool SetGroup(int gid) {
// attempt to set the group Id
if (log.IsDebugEnabled) {
log.Debug("Changing group Id to: " + gid);
}
Syscall.setgid(gid);
// if (Syscall.getgid() == gid) {
return true;
// }
// else {
// return false;
// }
}
}
}
|