#Checks if $username begins by a letter and contains only letters, digits, -, _ or .
proctc2:username_isvalid{username}{
regexp{^[A-Za-z][A-Za-z0-9_\-\.]*$}$username
}
#Determines if $username exists on the system
#SECURITY: to avoid shell injection, call first tc2:username_isvalid $username
proctc2:username_exists{username}{
#TODO: Windows and other OSes (this line has been tested under FreeBSD)
if{[exec--logins-oxl$username]==""}{
return0
}{
return1
}
}
#Gets server hostname
proctc2:hostname{}{
exechostname-s
}
#Determines if $username is root
proctc2:isroot{username}{
#Validates input data
setusername[stringtolower$username]
if![tc2:username_isvalid$username]{
return0
}
#Check 1 - User has local accreditation
if![sql"SELECT count(*) FROM tc2_roots WHERE account_username = '$username' AND server_name = '[sqlescape [tc2:hostname]]'"]{
return0
}
#Check 2 - User is in the group wheel on the server
if{[lsearch[exec--id-Gn$username]wheel]=="-1"}{
return0
}{
return1
}
}
#Determines if $requester is *EXPLICITELY* allowed to allowed to manage the account $user
#When you invoke this proc, you should also check if the user is root.
# e.g. if {[tc2:isroot $requester] || [tc2:userallow $requester $user]} { ... }
proctc2:userallow{requesteruser}{
setsql"SELECT count(*) FROM tc2_users_permissions WHERE server_name = '[sqlescape [tc2:hostname]]' AND account_username = '[sqlescape $user]' AND user_id = [getuserid $user]"
putdebug$sql
sql$sql
}
#tc2:getpermissions on $username: Gets permissions on the $username account
#tc2:getpermissions from $username: Gets permissions $username have on server accounts
proctc2:getpermissions{keywordusername}{
switch$keyword{
"from"{
setsql"SELECT account_username FROM tc2_users_permissions WHERE server_name = '[sqlescape [tc2:hostname]]' AND user_id = '[getuserid $username]'"
}
"on"{
setsql"SELECT u.username FROM tc2_users_permissions p, users u WHERE p.server_name = '[sqlescape [tc2:hostname]]' AND p.account_username = '$username' AND u.user_id = p.user_id"
}
default{
error"from or on expected"
}
}
setaccounts""
foreachrow[sql$sql]{
lappendaccounts[lindex$row0]
}
}
#account permission
#account isroot
#account exists
proctc2:command:account{requesterarg}{
setcommand[lindex$arg0]
switch--$command{
"exists"{
setusername[lindex$arg1]
if![tc2:username_isvalid$username]{
return{0"this is not a valid username"}
}
if[tc2:username_exists$username]{
list1"$username is a valid account on [tc2:hostname]."
}{
list1"$username isn't a valid account on [tc2:hostname]."
}
}
"isroot"{
setusername[lindex$arg1]
if![tc2:username_isvalid$username]{
return{0"this is not a valid username"}
}
if[tc2:isroot$username]{
list1"$username has got root accreditation on [tc2:hostname]."
}{
list1"$username doesn't seem to have any root accreditation [tc2:hostname]."
}
}
"permission"{
setusername[lindex$arg1]
if![tc2:username_isvalid$username]{
return{0"this is not a valid username"}
}
switch--[lindex$arg2]{
""{
setsentences{}
setaccounts_from[tc2:getpermissionsfrom$username]
setaccounts_on[tc2:getpermissionson$username]
if{$accounts_on!=""}{
lappendsentences"has authority upon [join $accounts_on ", "]"
}
if{$accounts_from!=""}{
lappendsentences"account can be managed from IRC by [join $accounts_from ", "]"
}
if{[tc2:isroot$username]}{
lappendsentences"has root access"
}
if{$sentences==""}{
list1nada
}{
list1"$username [join $sentences " / "]."
}
}
"add"{
#e.g. .account permission espacewin add dereckson
# will give access to the espacewin account to dereckson
return"0 {$mandataire doesn't have a bot account, and so, no such permission.}"
}
#Checks if the permission exists
if![tc2:userallow$requester$mandataire]{
return"0 {$mandataire haven't had an access to $username account.}"
}
#Removess the permission
sql"DELETE FROM tc2_users_permissions WHERE server_name = '[sqlescape [tc2:hostname]]' AND account_username = '$username' AND user_id = '$mandataire_user_id'"
return"1 {$mandataire doesn't have access to $username account anymore.}"
}
"+root"{
#Checks right and need
if![tc2:isroot$requester]{
return"0 {you don't have root authority yourself.}"
}
if[tc2:isroot$username]{
return"0 {$username have already root authority.}"