Compare commits
	
		
			3 Commits
		
	
	
		
			cbc868ae35
			...
			418e4a4a14
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					418e4a4a14 | ||
| 
						 | 
					79dc987c61 | ||
| 
						 | 
					ba1a1319e3 | 
@ -18,6 +18,7 @@ import (
 | 
			
		||||
const keyfileName = "authorized_keys2"
 | 
			
		||||
 | 
			
		||||
func quit(msg string, code int) {
 | 
			
		||||
	// TODO print to stderr
 | 
			
		||||
	fmt.Println(msg)
 | 
			
		||||
	os.Exit(code)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,9 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"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,
 | 
			
		||||
~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 {
 | 
			
		||||
	pw, err := email.LoadPassword()
 | 
			
		||||
	pw, err := loadPassword()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@ -117,6 +118,10 @@ func (p *Prompter) Select(prompt string, opts []string) (int, 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()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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 {
 | 
			
		||||
	case 0:
 | 
			
		||||
		return collectEmail(db, cs, p)
 | 
			
		||||
		return collectEmail(l, db, cs, p)
 | 
			
		||||
	case 1:
 | 
			
		||||
		return redeemCode(db, cs, p)
 | 
			
		||||
		return redeemCode(l, db, cs, p)
 | 
			
		||||
	case 2:
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
@ -171,7 +176,7 @@ func emailToUsername(email string) (string, error) {
 | 
			
		||||
	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."))
 | 
			
		||||
	email, err := p.String("email to send reset code to?")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@ -191,12 +196,12 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if found != len(mustHave) {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		l.Printf("corrupt email '%s'", email)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err = emailToUsername(email); err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		l.Printf("no user for '%s'", email)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -208,31 +213,30 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = ac.Insert(db); err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		return err
 | 
			
		||||
		l.Printf("database error: %s", err.Error())
 | 
			
		||||
		return errors.New("the database was sad")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = sendAuthCodeEmail(*ac); err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		return err
 | 
			
		||||
		l.Printf("mail send error: %s", err.Error())
 | 
			
		||||
		return errors.New("email sending failed")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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()
 | 
			
		||||
	c, err := p.String("paste your auth code and hit enter to submit:")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		l.Printf("failed to prompt: %s", err.Error())
 | 
			
		||||
		fmt.Println(cs.Error("sorry, I couldn't read that."))
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	parts, err := codes.Decode(c)
 | 
			
		||||
	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..."))
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
@ -244,7 +248,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
 | 
			
		||||
 | 
			
		||||
	err = code.Hydrate(db)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		l.Printf("hydrate failed: %s", err.Error())
 | 
			
		||||
		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)
 | 
			
		||||
	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."))
 | 
			
		||||
		// TODO log
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	key, err := p.String("paste your new public key and hit enter to submit:")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// TODO log
 | 
			
		||||
		l.Printf("failed to prompt: %s", err.Error())
 | 
			
		||||
		fmt.Println(cs.Error("sorry, I couldn't read that."))
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
@ -283,27 +287,19 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
 | 
			
		||||
	stdoutBuff := bytes.NewBuffer([]byte{})
 | 
			
		||||
	cmd.Stdout = stdoutBuff
 | 
			
		||||
	if err = cmd.Run(); err != nil {
 | 
			
		||||
		// TODO log error
 | 
			
		||||
		//return fmt.Errorf("appendkeyfile failed with '%s': %w", string(stdoutBuff.Bytes()), err)
 | 
			
		||||
		l.Printf("appendkeyfile failed with '%s': %s", stdoutBuff.String(), err.Error())
 | 
			
		||||
		return errors.New("adding to keys file failed")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = code.MarkUsed(db)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// TODO log err
 | 
			
		||||
		l.Printf("failed to mark used: %s", err.Error())
 | 
			
		||||
		return errors.New("database was sad")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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() {
 | 
			
		||||
	cs := newColorScheme()
 | 
			
		||||
	err := _main(cs)
 | 
			
		||||
@ -326,7 +322,7 @@ type AuthCode struct {
 | 
			
		||||
func (c *AuthCode) Insert(db *sql.DB) error {
 | 
			
		||||
	stmt, err := db.Prepare(`
 | 
			
		||||
	INSERT INTO auth_codes (code, email)
 | 
			
		||||
	VALUES ?, ?`)
 | 
			
		||||
	VALUES (?, ?)`)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,9 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.tilde.town/tildetown/town/email"
 | 
			
		||||
	"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,
 | 
			
		||||
~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 {
 | 
			
		||||
	pw, err := email.LoadPassword()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
@ -3,10 +3,8 @@ package email
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/smtp"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@ -16,25 +14,6 @@ const (
 | 
			
		||||
	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 {
 | 
			
		||||
	cmd := exec.Command("/usr/sbin/sendmail", username)
 | 
			
		||||
	cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user