Merge pull request #52 from jamesandariese/master

ANONYMOUS auth by default if user and password are empty.
This commit is contained in:
mattn 2015-05-13 17:33:29 +09:00
commit c8c5371616
2 changed files with 99 additions and 79 deletions

View File

@ -32,8 +32,12 @@ func main() {
} }
flag.Parse() flag.Parse()
if *username == "" || *password == "" { if *username == "" || *password == "" {
if *debug && *username == "" && *password == "" {
fmt.Fprintf(os.Stderr, "no username or password were given; attempting ANONYMOUS auth\n")
} else if *username != "" || *password != "" {
flag.Usage() flag.Usage()
} }
}
if !*notls { if !*notls {
xmpp.DefaultConfig = tls.Config{ xmpp.DefaultConfig = tls.Config{

20
xmpp.go
View File

@ -272,11 +272,14 @@ func (c *Client) init(o *Options) error {
c.p = xml.NewDecoder(c.conn) c.p = xml.NewDecoder(c.conn)
} }
var domain string
a := strings.SplitN(o.User, "@", 2) a := strings.SplitN(o.User, "@", 2)
if len(o.User) > 0 {
if len(a) != 2 { if len(a) != 2 {
return errors.New("xmpp: invalid username (want user@domain): " + o.User) return errors.New("xmpp: invalid username (want user@domain): " + o.User)
} }
domain := a[1] domain = a[1]
} // Otherwise, we'll be attempting ANONYMOUS
// Declare intent to be a jabber client and gather stream features. // Declare intent to be a jabber client and gather stream features.
f, err := c.startStream(o, domain) f, err := c.startStream(o, domain)
@ -289,6 +292,19 @@ func (c *Client) init(o *Options) error {
return err return err
} }
if o.User == "" && o.Password == "" {
foundAnonymous := false
for _, m := range f.Mechanisms.Mechanism {
if m == "ANONYMOUS" {
fmt.Fprintf(c.conn, "<auth xmlns='%s' mechanism='ANONYMOUS' />\n", nsSASL)
foundAnonymous = true
break
}
}
if !foundAnonymous {
return fmt.Errorf("ANONYMOUS authentication is not an option and username and password were not specified")
}
} else {
// Even digest forms of authentication are unsafe if we do not know that the host // Even digest forms of authentication are unsafe if we do not know that the host
// we are talking to is the actual server, and not a man in the middle playing // we are talking to is the actual server, and not a man in the middle playing
// proxy. // proxy.
@ -370,7 +386,7 @@ func (c *Client) init(o *Options) error {
if mechanism == "" { if mechanism == "" {
return fmt.Errorf("PLAIN authentication is not an option: %v", f.Mechanisms.Mechanism) return fmt.Errorf("PLAIN authentication is not an option: %v", f.Mechanisms.Mechanism)
} }
}
// Next message should be either success or failure. // Next message should be either success or failure.
name, val, err := next(c.p) name, val, err := next(c.p)
if err != nil { if err != nil {