Compare commits
3 Commits
cbc868ae35
...
418e4a4a14
Author | SHA1 | Date |
---|---|---|
vilmibm | 418e4a4a14 | |
vilmibm | 79dc987c61 | |
vilmibm | ba1a1319e3 |
|
@ -18,6 +18,7 @@ import (
|
||||||
const keyfileName = "authorized_keys2"
|
const keyfileName = "authorized_keys2"
|
||||||
|
|
||||||
func quit(msg string, code int) {
|
func quit(msg string, code int) {
|
||||||
|
// TODO print to stderr
|
||||||
fmt.Println(msg)
|
fmt.Println(msg)
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.tilde.town/tildetown/town/email"
|
"git.tilde.town/tildetown/town/email"
|
||||||
)
|
)
|
||||||
|
@ -23,8 +25,27 @@ Follow the instructions there to add your new key and restore access to your acc
|
||||||
best,
|
best,
|
||||||
~vilmibm`
|
~vilmibm`
|
||||||
|
|
||||||
|
func loadPassword() (string, error) {
|
||||||
|
f, err := os.Open("/town/docs/smtp_help.pw")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not open smtp password file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pw := make([]byte, 100)
|
||||||
|
|
||||||
|
n, err := f.Read(pw)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not read smtp password file: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return "", errors.New("smtp password file was empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(pw[0:n]), nil
|
||||||
|
}
|
||||||
|
|
||||||
func sendAuthCodeEmail(ac AuthCode) error {
|
func sendAuthCodeEmail(ac AuthCode) error {
|
||||||
pw, err := email.LoadPassword()
|
pw, err := loadPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -117,6 +118,10 @@ func (p *Prompter) Select(prompt string, opts []string) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main(cs colorScheme) error {
|
func _main(cs colorScheme) error {
|
||||||
|
logFilename := fmt.Sprintf("/town/var/log/help/%d", time.Now().Unix)
|
||||||
|
logFile, err := os.OpenFile(logFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||||
|
l := log.New(logFile, "", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile)
|
||||||
|
|
||||||
db, err := connectDB()
|
db, err := connectDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
|
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
|
||||||
|
@ -148,9 +153,9 @@ func _main(cs colorScheme) error {
|
||||||
|
|
||||||
switch c {
|
switch c {
|
||||||
case 0:
|
case 0:
|
||||||
return collectEmail(db, cs, p)
|
return collectEmail(l, db, cs, p)
|
||||||
case 1:
|
case 1:
|
||||||
return redeemCode(db, cs, p)
|
return redeemCode(l, db, cs, p)
|
||||||
case 2:
|
case 2:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -171,7 +176,7 @@ func emailToUsername(email string) (string, error) {
|
||||||
return stdoutBuff.String(), nil
|
return stdoutBuff.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
func collectEmail(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account."))
|
fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account."))
|
||||||
email, err := p.String("email to send reset code to?")
|
email, err := p.String("email to send reset code to?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -191,12 +196,12 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found != len(mustHave) {
|
if found != len(mustHave) {
|
||||||
// TODO log
|
l.Printf("corrupt email '%s'", email)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = emailToUsername(email); err != nil {
|
if _, err = emailToUsername(email); err != nil {
|
||||||
// TODO log
|
l.Printf("no user for '%s'", email)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,31 +213,30 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = ac.Insert(db); err != nil {
|
if err = ac.Insert(db); err != nil {
|
||||||
// TODO log
|
l.Printf("database error: %s", err.Error())
|
||||||
return err
|
return errors.New("the database was sad")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = sendAuthCodeEmail(*ac); err != nil {
|
if err = sendAuthCodeEmail(*ac); err != nil {
|
||||||
// TODO log
|
l.Printf("mail send error: %s", err.Error())
|
||||||
return err
|
return errors.New("email sending failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
fmt.Println(cs.Header("redeem an auth code and add a new public key"))
|
fmt.Println(cs.Header("redeem an auth code and add a new public key"))
|
||||||
fmt.Println()
|
|
||||||
c, err := p.String("paste your auth code and hit enter to submit:")
|
c, err := p.String("paste your auth code and hit enter to submit:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
l.Printf("failed to prompt: %s", err.Error())
|
||||||
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
parts, err := codes.Decode(c)
|
parts, err := codes.Decode(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
l.Printf("failed to decode auth code: %s", err.Error())
|
||||||
fmt.Println(cs.Error("sorry, that doesn't look like an auth code..."))
|
fmt.Println(cs.Error("sorry, that doesn't look like an auth code..."))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -244,7 +248,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
|
|
||||||
err = code.Hydrate(db)
|
err = code.Hydrate(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
l.Printf("hydrate failed: %s", err.Error())
|
||||||
return errors.New("the database is sad")
|
return errors.New("the database is sad")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,14 +259,14 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
|
|
||||||
username, err := emailToUsername(code.Email)
|
username, err := emailToUsername(code.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
l.Printf("could not find user: %s", err.Error())
|
||||||
fmt.Println(cs.Error("That code doesn't seem to match an account."))
|
fmt.Println(cs.Error("That code doesn't seem to match an account."))
|
||||||
// TODO log
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := p.String("paste your new public key and hit enter to submit:")
|
key, err := p.String("paste your new public key and hit enter to submit:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
l.Printf("failed to prompt: %s", err.Error())
|
||||||
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -283,27 +287,19 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
stdoutBuff := bytes.NewBuffer([]byte{})
|
stdoutBuff := bytes.NewBuffer([]byte{})
|
||||||
cmd.Stdout = stdoutBuff
|
cmd.Stdout = stdoutBuff
|
||||||
if err = cmd.Run(); err != nil {
|
if err = cmd.Run(); err != nil {
|
||||||
// TODO log error
|
l.Printf("appendkeyfile failed with '%s': %s", stdoutBuff.String(), err.Error())
|
||||||
//return fmt.Errorf("appendkeyfile failed with '%s': %w", string(stdoutBuff.Bytes()), err)
|
|
||||||
return errors.New("adding to keys file failed")
|
return errors.New("adding to keys file failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = code.MarkUsed(db)
|
err = code.MarkUsed(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log err
|
l.Printf("failed to mark used: %s", err.Error())
|
||||||
return errors.New("database was sad")
|
return errors.New("database was sad")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO db plan:
|
|
||||||
|
|
||||||
// add new db, codes (modeled after invites)
|
|
||||||
// add new helper, emailtouser, that can access town.db and report on what user matches a given email
|
|
||||||
// drop table from town.db
|
|
||||||
// update sshapps.md
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cs := newColorScheme()
|
cs := newColorScheme()
|
||||||
err := _main(cs)
|
err := _main(cs)
|
||||||
|
@ -326,7 +322,7 @@ type AuthCode struct {
|
||||||
func (c *AuthCode) Insert(db *sql.DB) error {
|
func (c *AuthCode) Insert(db *sql.DB) error {
|
||||||
stmt, err := db.Prepare(`
|
stmt, err := db.Prepare(`
|
||||||
INSERT INTO auth_codes (code, email)
|
INSERT INTO auth_codes (code, email)
|
||||||
VALUES ?, ?`)
|
VALUES (?, ?)`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.tilde.town/tildetown/town/email"
|
"git.tilde.town/tildetown/town/email"
|
||||||
"git.tilde.town/tildetown/town/invites"
|
"git.tilde.town/tildetown/town/invites"
|
||||||
|
@ -26,6 +28,25 @@ If you end up stuck, e-mail root@tilde.town with any questions.
|
||||||
See you on the server,
|
See you on the server,
|
||||||
~vilmibm`
|
~vilmibm`
|
||||||
|
|
||||||
|
func loadPassword() (string, error) {
|
||||||
|
f, err := os.Open("/town/docs/smtp.pw")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not open smtp password file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pw := make([]byte, 100)
|
||||||
|
|
||||||
|
n, err := f.Read(pw)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not read smtp password file: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return "", errors.New("smtp password file was empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(pw[0:n]), nil
|
||||||
|
}
|
||||||
|
|
||||||
func sendInviteEmail(invite invites.Invite) error {
|
func sendInviteEmail(invite invites.Invite) error {
|
||||||
pw, err := email.LoadPassword()
|
pw, err := email.LoadPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,10 +3,8 @@ package email
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,25 +14,6 @@ const (
|
||||||
SMTPPort = 465
|
SMTPPort = 465
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadPassword() (string, error) {
|
|
||||||
f, err := os.Open("/town/docs/smtp.pw")
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("could not open smtp password file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pw := make([]byte, 100)
|
|
||||||
|
|
||||||
n, err := f.Read(pw)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("could not read smtp password file: %w", err)
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
return "", errors.New("smtp password file was empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(pw[0:n]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendLocalEmail(username, subject, body string) error {
|
func SendLocalEmail(username, subject, body string) error {
|
||||||
cmd := exec.Command("/usr/sbin/sendmail", username)
|
cmd := exec.Command("/usr/sbin/sendmail", username)
|
||||||
cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))
|
cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))
|
||||||
|
|
Loading…
Reference in New Issue