Browse Source

Require that server names must be hostnames, and nicks cannot be hostnames

tags/v0.1.0
Daniel Oaks 8 years ago
parent
commit
77bf7173ff
3 changed files with 32 additions and 0 deletions
  1. 3
    0
      irc/config.go
  2. 25
    0
      irc/net.go
  3. 4
    0
      irc/strings.go

+ 3
- 0
irc/config.go View File

@@ -110,6 +110,9 @@ func LoadConfig(filename string) (config *Config, err error) {
110 110
 	if config.Server.Name == "" {
111 111
 		return nil, errors.New("Server name missing")
112 112
 	}
113
+	if !IsHostname(config.Server.Name) {
114
+		return nil, errors.New("Server name must match the format of a hostname")
115
+	}
113 116
 	if config.Server.Database == "" {
114 117
 		return nil, errors.New("Server database missing")
115 118
 	}

+ 25
- 0
irc/net.go View File

@@ -27,3 +27,28 @@ func LookupHostname(addr Name) Name {
27 27
 	hostname := strings.TrimSuffix(names[0], ".")
28 28
 	return Name(hostname)
29 29
 }
30
+
31
+var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
32
+
33
+func IsHostname(name string) bool {
34
+	// IRC hostnames specifically require a period
35
+	if !strings.Contains(name, ".") || len(name) < 1 || len(name) > 253 {
36
+		return false
37
+	}
38
+
39
+	// ensure each part of hostname is valid
40
+	for _, part := range strings.Split(name, ".") {
41
+		if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
42
+			return false
43
+		}
44
+	}
45
+
46
+	// ensure all chars of hostname are valid
47
+	for _, char := range strings.Split(strings.ToLower(name), "") {
48
+		if !strings.Contains(allowedHostnameChars, char) {
49
+			return false
50
+		}
51
+	}
52
+
53
+	return true
54
+}

+ 4
- 0
irc/strings.go View File

@@ -47,6 +47,10 @@ func (name Name) IsNickname() bool {
47 47
 		strings.Contains(namestr, "!") || strings.Contains(namestr, "@") {
48 48
 		return false
49 49
 	}
50
+	// names that look like hostnames are restricted to servers, as with other ircds
51
+	if IsHostname(namestr) {
52
+		return false
53
+	}
50 54
 	return NicknameExpr.MatchString(namestr)
51 55
 }
52 56
 

Loading…
Cancel
Save