-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchpasswd.go
104 lines (90 loc) · 2.06 KB
/
chpasswd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import "bufio"
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import "fmt"
import "io/ioutil"
import "os"
import "strings"
const configdir = "/etc/qbox"
// Implements `chpasswd` functionality to be used by
// Roundcube's password plugin
// Exit codes
// 0 = success
// 1 = Problem reading from STDIN
// 2 = Database problem
func main() {
// Read config files
var dbserver string = "127.0.0.1"
if fileExists(configdir + "/dbserver") {
buf, err := ioutil.ReadFile(configdir + "/dbserver")
if err == nil {
dbserver = string(buf)
}
}
var dbuser string = "qbox"
if fileExists(configdir + "/dbuser") {
buf, err := ioutil.ReadFile(configdir + "/dbuser")
if err == nil {
dbuser = string(buf)
}
}
var dbpass string
if fileExists(configdir + "/dbpass") {
buf, err := ioutil.ReadFile(configdir + "/dbpass")
if err == nil {
dbpass = string(buf)
}
}
// Initialize DB
db, err := sql.Open("mysql", dbuser+":"+dbpass+"@tcp("+dbserver+")/qbox")
if err == nil {
err = db.Ping()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
} else {
fmt.Println(err)
os.Exit(2)
}
defer db.Close()
// Read username:password sets from stdin
var changes = make(map[string]string)
scanner := bufio.NewScanner(os.Stdin)
if scanner.Err() != nil {
fmt.Println("Could not read STDIN: "+err.Error())
os.Exit(1)
}
for scanner.Scan() {
split := strings.SplitN(scanner.Text(), ":", 2)
if len(split) == 2 {
changes[split[0]] = split[1]
} else {
fmt.Println("Failed to parse: "+scanner.Text())
os.Exit(1)
}
}
// Execute SQL updates
for username, password := range changes {
stmt, err := db.Prepare("UPDATE passwd SET password = ? WHERE username = ? LIMIT 1")
if err != nil {
fmt.Println(err)
os.Exit(2)
}
defer stmt.Close()
_, err = stmt.Exec(password, username)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
}
// END
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}