Compare commits

...

2 Commits

Author SHA1 Message Date
vilmibm 90808c1ce0 bug fixes, ready for external qa 2023-10-25 19:24:01 +00:00
vilmibm 17d39483fb fix logging 2023-10-25 17:52:04 +00:00
3 changed files with 29 additions and 28 deletions

View File

@ -24,20 +24,11 @@ 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.Println(user.Username)
fmt.Print(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("(type your answer below and press enter to submit)"))
fmt.Println(p.cs.Subtitle("(press enter to submit)"))
s, err := p.tty.ReadString()
if err != nil {
return "", fmt.Errorf("couldn't collect input: %w", err)
@ -118,9 +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)
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 {
@ -147,8 +148,6 @@ 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 {
@ -173,7 +172,7 @@ func emailToUsername(email string) (string, error) {
return "", fmt.Errorf("emailtouser failed with '%s': %w", stderrBuff.String(), err)
}
return stdoutBuff.String(), nil
return strings.TrimSpace(stdoutBuff.String()), nil
}
func collectEmail(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
@ -227,7 +226,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 and hit enter to submit:")
c, err := p.String("paste your auth code:")
if err != nil {
l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that."))
@ -242,7 +241,7 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
}
code := &AuthCode{
Code: parts[0],
Code: c,
Email: parts[1],
}
@ -264,7 +263,10 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
key, err := p.String("paste your new public key and hit enter to submit:")
fmt.Println()
fmt.Printf("hi, ~%s", username)
key, err := p.String("paste your new public key:")
if err != nil {
l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that."))
@ -282,12 +284,17 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username)
cmd.Stdin = bytes.NewBufferString(key)
// 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")
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", stdoutBuff.String(), err.Error())
l.Printf("appendkeyfile failed with '%s', '%s': %s", stderrBuff.String(), stdoutBuff.String(), err.Error())
return errors.New("adding to keys file failed")
}
@ -303,6 +310,10 @@ 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())))
@ -312,11 +323,10 @@ func main() {
}
type AuthCode struct {
ID int64
Code string
Email string
Used bool
Created time.Time
ID int64
Code string
Email string
Used bool
}
func (c *AuthCode) Insert(db *sql.DB) error {
@ -346,7 +356,7 @@ func (c *AuthCode) Insert(db *sql.DB) error {
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, used, created
SELECT id, used
FROM auth_codes
WHERE code = ? AND email = ?`)
if err != nil {
@ -354,7 +364,7 @@ func (c *AuthCode) Hydrate(db *sql.DB) error {
}
defer stmt.Close()
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created)
return stmt.QueryRow(c.Code, c.Email).Scan(&c.ID, &c.Used)
}
func (c *AuthCode) MarkUsed(db *sql.DB) error {