Compare commits

..

No commits in common. "90808c1ce0aa098a15e27d3a80ee5043435d69e3" and "418e4a4a149fed1a40021eb7f3a4f6c5c05492c7" have entirely different histories.

3 changed files with 28 additions and 29 deletions

View File

@ -24,11 +24,20 @@ func quit(msg string, code int) {
}
func main() {
username := os.Args[1]
if username == "" {
quit("expected username as argument", 1)
}
u, err := user.Current()
if err != nil {
quit(err.Error(), 2)
}
if u.Username != username {
quit("that's my purse; I don't know you", 3)
}
sshPath := path.Join("/home", u.Username, ".ssh")
keyfilePath := path.Join(sshPath, keyfileName)

View File

@ -27,7 +27,7 @@ func _main(args []string) error {
return errors.New("email does not correspond to user")
}
fmt.Print(user.Username)
fmt.Println(user.Username)
return nil
}

View File

@ -74,7 +74,7 @@ func NewPrompter(tty *tty.TTY, cs colorScheme) *Prompter {
func (p *Prompter) String(prompt string) (string, error) {
fmt.Println("")
fmt.Println(p.cs.Prompt(prompt))
fmt.Println(p.cs.Subtitle("(press enter to submit)"))
fmt.Println(p.cs.Subtitle("(type your answer below and press enter to submit)"))
s, err := p.tty.ReadString()
if err != nil {
return "", fmt.Errorf("couldn't collect input: %w", err)
@ -118,10 +118,9 @@ 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())
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)
defer logFile.Close()
db, err := connectDB()
if err != nil {
@ -148,6 +147,8 @@ func _main(cs colorScheme) error {
c, err := p.Select("What do you need help with?", options)
defer func() {
fmt.Println()
fmt.Println(cs.Header("bye~"))
}()
switch c {
@ -172,7 +173,7 @@ func emailToUsername(email string) (string, error) {
return "", fmt.Errorf("emailtouser failed with '%s': %w", stderrBuff.String(), err)
}
return strings.TrimSpace(stdoutBuff.String()), nil
return stdoutBuff.String(), nil
}
func collectEmail(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
@ -226,7 +227,7 @@ func collectEmail(l *log.Logger, 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"))
c, err := p.String("paste your auth code:")
c, err := p.String("paste your auth code and hit enter to submit:")
if err != nil {
l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that."))
@ -241,7 +242,7 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
}
code := &AuthCode{
Code: c,
Code: parts[0],
Email: parts[1],
}
@ -263,10 +264,7 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
fmt.Println()
fmt.Printf("hi, ~%s", username)
key, err := p.String("paste your new public key:")
key, err := p.String("paste your new public key and hit enter to submit:")
if err != nil {
l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that."))
@ -284,17 +282,12 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
// TODO
// this works: sudo --user help sudo --user wren /town/bin/appendkeyfile < /tmp/foo.pub
// but it's failing with nothing on STDOUT and an exit code of 1 when invoked this way
cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile")
cmd.Stdin = bytes.NewBufferString(key + "\n")
cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username)
cmd.Stdin = bytes.NewBufferString(key)
stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stdout = stdoutBuff
stderrBuff := bytes.NewBuffer([]byte{})
cmd.Stderr = stderrBuff
if err = cmd.Run(); err != nil {
l.Printf("appendkeyfile failed with '%s', '%s': %s", stderrBuff.String(), stdoutBuff.String(), err.Error())
l.Printf("appendkeyfile failed with '%s': %s", stdoutBuff.String(), err.Error())
return errors.New("adding to keys file failed")
}
@ -310,10 +303,6 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
func main() {
cs := newColorScheme()
err := _main(cs)
defer func() {
fmt.Println()
fmt.Println(cs.Header("bye~"))
}()
if err != nil {
fmt.Println(
cs.Error(fmt.Sprintf("sorry, something went wrong: %s", err.Error())))
@ -323,10 +312,11 @@ func main() {
}
type AuthCode struct {
ID int64
Code string
Email string
Used bool
ID int64
Code string
Email string
Used bool
Created time.Time
}
func (c *AuthCode) Insert(db *sql.DB) error {
@ -356,7 +346,7 @@ func (c *AuthCode) Insert(db *sql.DB) error {
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, used
SELECT id, used, created
FROM auth_codes
WHERE code = ? AND email = ?`)
if err != nil {
@ -364,7 +354,7 @@ func (c *AuthCode) Hydrate(db *sql.DB) error {
}
defer stmt.Close()
return stmt.QueryRow(c.Code, c.Email).Scan(&c.ID, &c.Used)
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created)
}
func (c *AuthCode) MarkUsed(db *sql.DB) error {