#!/usr/bin/env perl # $Id: $ # win-admin.pl # # Copyright 2007 Siep Kroonenberg # This file is licensed under the GNU General Public License version 2 # or any later version. # # This file determines whether the script is run with admin rights. # # This code first sets $admin to true. The function sys-access returns # registry access permissions based on the value of $admin. The code # then tries to access the system environment with the permissions # returned by sys_access. If this fails then it modifies its # assumptions and tries again. use Win32::TieRegistry( Delimiter => "/", ArrayValues => 0, FixSzNulls => 1, qw(REG_SZ REG_EXPAND_SZ KEY_READ KEY_WRITE KEY_ALL_ACCESS KEY_ENUMERATE_SUB_KEYS)); my $admin=1; my $SystemEnv; our %vars; sub sys_access { $admin? KEY_ALL_ACCESS() : KEY_READ() | KEY_ENUMERATE_SUB_KEYS(); } $SystemEnv=$Registry -> Open( "LMachine/system/currentcontrolset/control/session manager/Environment/", {Access => sys_access()}); if (not $SystemEnv) { print "No admin permissions\n"; $admin=0; $SystemEnv=$Registry -> Open( "LMachine/system/currentcontrolset/control/session manager/Environment/", {Access => sys_access()}); } $SystemEnv->ArrayValues(1); $vars{'win-admin'}=$admin? 1:0; # needs a terminal 1 for require to succeed! 1; __END__ ### Local Variables: ### perl-indent-level: 2 ### tab-width: 2 ### indent-tabs-mode: nil ### End: # vim:set tabstop=2 expandtab: #