Files
tii/tiid-user.tcl
T

45 lines
1.5 KiB
Tcl
Raw Normal View History

2024-11-21 19:27:40 +02:00
#!/usr/bin/env tclsh
# tiid-user: user management utility for tiid (tii node daemon)
# can work both over HTTP and Gopher/Nex
# Usage: tiid-user.tcl [dbfile] user [username] auth [password] acl [acl]
# if the username doesn't exist, it will be created
# if it does exist, the corresponding fields will be updated
# Depends upon Tcllib and sqlite3
# Created by Luxferre in 2024, released into public domain
package require sqlite3
package require sha256
if {$argc > 2} {
set dbfile [lindex $argv 0]
set kvargs [lrange $argv 1 end]
puts $kvargs
set username ""
set authhash ""
set acl "*"
if {[dict exists $kvargs user]} {
set username [dict get $kvargs user]
}
if {[dict exists $kvargs auth]} {
set rawauth [dict get $kvargs auth]
set authhash [::sha2::sha256 -hex -- [string trim $rawauth]]
}
if {[dict exists $kvargs acl]} {
set acl [dict get $kvargs acl]
}
if {$username ne ""} {
if {$authhash eq ""} { # we're only changing the ACL
set query {UPDATE `auth` SET `posting_acl` = :acl WHERE `username` = :username;}
} else { # we're inserting/updating a user
set query {INSERT INTO `auth` (`username`, `authstrhash`, `posting_acl`)
VALUES (:username, :authhash, :acl)
ON CONFLICT(`username`) DO UPDATE SET `authstrhash` = excluded.`authstrhash`,
`posting_acl` = excluded.`posting_acl`;}
}
sqlite3 db $dbfile
db eval $query
db close
puts "Changes written"
} else {puts "User field is required!"}
} else {puts "DB file and at least one key is required!"}