| 1 |
// Copyright Martin Dosch. |
| 2 |
// Use of this source code is governed by the BSD-2-clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"context" |
| 10 |
"crypto/tls" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"log" |
| 14 |
"log/slog" |
| 15 |
"net" |
| 16 |
"os" |
| 17 |
"os/signal" |
| 18 |
osUser "os/user" |
| 19 |
"runtime" |
| 20 |
"strings" |
| 21 |
"syscall" |
| 22 |
"time" |
| 23 |
|
| 24 |
gopenpgpConst "github.com/ProtonMail/gopenpgp/v3/constants" // MIT License |
| 25 |
"github.com/ProtonMail/gopenpgp/v3/crypto" // MIT License |
| 26 |
"github.com/pborman/getopt/v2" // BSD-3-Clause |
| 27 |
"github.com/xmppo/go-xmpp" // BSD-3-Clause |
| 28 |
"golang.org/x/net/idna" // BSD-3-Clause |
| 29 |
"salsa.debian.org/mdosch/xmppsrv" // BSD-2-Clause |
| 30 |
) |
| 31 |
|
| 32 |
type configuration struct { |
| 33 |
username string |
| 34 |
jserver string |
| 35 |
port string |
| 36 |
password string |
| 37 |
alias string |
| 38 |
oxTrustMode string |
| 39 |
allowPLAIN bool |
| 40 |
noRootWarn bool |
| 41 |
noLPGPWarn bool |
| 42 |
} |
| 43 |
|
| 44 |
func closeAndExit(client *xmpp.Client, err error) { |
| 45 |
slog.Info("closing connection and exiting") |
| 46 |
_ = client.Close() |
| 47 |
if err != nil { |
| 48 |
log.Fatal(err) |
| 49 |
} |
| 50 |
os.Exit(0) |
| 51 |
} |
| 52 |
|
| 53 |
func readFileByLine(filePath string) (string, error) { |
| 54 |
var ( |
| 55 |
output string |
| 56 |
err error |
| 57 |
) |
| 58 |
|
| 59 |
// Check that the file is existing. |
| 60 |
_, err = os.Stat(filePath) |
| 61 |
if err != nil { |
| 62 |
return output, fmt.Errorf("read file by line: %w", err) |
| 63 |
} |
| 64 |
|
| 65 |
// Open message file. |
| 66 |
slog.Info("opening file", "file", filePath) |
| 67 |
// #nosec G304 -- file path is supplied by the user, it's no untrusted input from the network |
| 68 |
file, err := os.Open(filePath) |
| 69 |
if err != nil { |
| 70 |
return output, fmt.Errorf("read file by line: %w", err) |
| 71 |
} |
| 72 |
defer func() { |
| 73 |
derr := file.Close() |
| 74 |
if derr != nil { |
| 75 |
slog.Warn("closing file failed", "error", derr) |
| 76 |
} |
| 77 |
}() |
| 78 |
scanner := bufio.NewScanner(file) |
| 79 |
scanner.Split(bufio.ScanLines) |
| 80 |
for scanner.Scan() { |
| 81 |
if output == "" { |
| 82 |
output = scanner.Text() |
| 83 |
} else { |
| 84 |
output = output + "\n" + scanner.Text() |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if err = scanner.Err(); err != nil { |
| 89 |
if err != io.EOF { |
| 90 |
return "", fmt.Errorf("read file by line: %w", err) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return output, nil |
| 95 |
} |
| 96 |
|
| 97 |
func main() { |
| 98 |
type recipientsType struct { |
| 99 |
Jid string |
| 100 |
OxKeyRing *crypto.KeyRing |
| 101 |
} |
| 102 |
|
| 103 |
var ( |
| 104 |
err error |
| 105 |
message, user, server, password, alias, oxTrustMode string |
| 106 |
allowPLAIN, pinNoPLAIN, isRoot, noRootWarn, noLPGPWarn bool |
| 107 |
oxPrivKey *crypto.Key |
| 108 |
recipients []recipientsType |
| 109 |
fast xmpp.Fast |
| 110 |
// There are some errors that we ignore as we do not want to |
| 111 |
// stop the execution. Failure is used to track those to exit |
| 112 |
// with a non-success return value. |
| 113 |
failure error |
| 114 |
) |
| 115 |
|
| 116 |
log.SetFlags(0) |
| 117 |
log.SetOutput(new(prettyLogger)) |
| 118 |
slog.SetLogLoggerLevel(slog.LevelWarn) |
| 119 |
// Define command line flags. |
| 120 |
flagHelp := getopt.BoolLong("help", 0, "Show help.") |
| 121 |
flagHTTPUpload := getopt.ListLong("http-upload", 'h', "Send a file via http-upload. Can be invoked several times to upload multiple files.") |
| 122 |
flagDebug := getopt.BoolLong("debug", 'd', "Show XMPP stanzas.") |
| 123 |
flagVerbose := getopt.BoolLong("verbose", 'v', "Show debug information.") |
| 124 |
flagServer := getopt.StringLong("jserver", 'j', "", "XMPP server address.") |
| 125 |
flagUser := getopt.StringLong("username", 'u', "", "Username for XMPP account.") |
| 126 |
flagPassword := getopt.StringLong("password", 'p', "", "Password for XMPP account.") |
| 127 |
flagChatroom := getopt.BoolLong("chatroom", 'c', "Send message to a chatroom.") |
| 128 |
flagDirectTLS := getopt.BoolLong("tls", 't', "Use direct TLS.") |
| 129 |
flagAlias := getopt.StringLong("alias", 'a', "", "Set alias/nickname"+ |
| 130 |
"for chatrooms.") |
| 131 |
flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: "+ |
| 132 |
"~/.config/go-sendxmpp/sendxmpprc)") |
| 133 |
flagMessageFile := getopt.StringLong("message", 'm', "", "Set file including the message.") |
| 134 |
flagInteractive := getopt.BoolLong("interactive", 'i', "Interactive mode (for use with e.g. 'tail -f').") |
| 135 |
flagSkipVerify := getopt.BoolLong("no-tls-verify", 'n', |
| 136 |
"Skip verification of TLS certificates (not recommended).") |
| 137 |
flagRaw := getopt.BoolLong("raw", 0, "Send raw XML.") |
| 138 |
flagListen := getopt.BoolLong("listen", 'l', "Listen for messages and print them to stdout.") |
| 139 |
flagTimeout := getopt.IntLong("timeout", 0, defaultTimeout, "Connection timeout in seconds.") |
| 140 |
flagTLSMinVersion := getopt.IntLong("tls-version", 0, defaultTLSMinVersion, |
| 141 |
"Minimal TLS version. 10 (TLSv1.0), 11 (TLSv1.1), 12 (TLSv1.2) or 13 (TLSv1.3).") |
| 142 |
flagVersion := getopt.BoolLong("version", 0, "Show version information.") |
| 143 |
flagMUCPassword := getopt.StringLong("muc-password", 0, "", "Password for password protected MUCs.") |
| 144 |
flagOx := getopt.BoolLong("ox", 0, "Use \"OpenPGP for XMPP\" encryption (experimental).") |
| 145 |
flagOxGenPrivKeyRSA := getopt.BoolLong("ox-genprivkey-rsa", 0, |
| 146 |
"Generate a private OpenPGP key (RSA 4096 bit) for the given JID and publish the "+ |
| 147 |
"corresponding public key.") |
| 148 |
flagOxGenPrivKeyX25519 := getopt.BoolLong("ox-genprivkey-x25519", 0, |
| 149 |
"Generate a private OpenPGP key (x25519) for the given JID and publish the "+ |
| 150 |
"corresponding public key.") |
| 151 |
flagOxPassphrase := getopt.StringLong("ox-passphrase", 0, "", |
| 152 |
"Passphrase for locking and unlocking the private OpenPGP key.") |
| 153 |
flagOxImportPrivKey := getopt.StringLong("ox-import-privkey", 0, "", |
| 154 |
"Import an existing private OpenPGP key.") |
| 155 |
flagOxDeleteNodes := getopt.BoolLong("ox-delete-nodes", 0, "Delete existing OpenPGP nodes on the server.") |
| 156 |
flagOxTransPrivKey := getopt.BoolLong("ox-transfer-private-key", 0, "Upload your encrypted private Ox key to the server for key transfer.") |
| 157 |
flagOxRecvPrivKey := getopt.StringLong("ox-receive-private-key", 0, "", "Import your encrypted private Ox key from the server. Requires the transfer code.") |
| 158 |
flagOOBFile := getopt.StringLong("oob-file", 0, "", "URL to send a file as out of band data.") |
| 159 |
flagHeadline := getopt.BoolLong("headline", 0, "Send message as type headline.") |
| 160 |
flagSCRAMPinning := getopt.StringLong("scram-mech-pinning", 0, "", "Enforce the use of a certain SCRAM authentication mechanism.") |
| 161 |
flagSSDPOff := getopt.BoolLong("ssdp-off", 0, "Disable XEP-0474: SASL SCRAM Downgrade Protection.") |
| 162 |
flagSubject := getopt.StringLong("subject", 's', "", "Set message subject.") |
| 163 |
flagFastOff := getopt.BoolLong("fast-off", 0, "Disable XEP-0484: Fast Authentication Streamlining Tokens.") |
| 164 |
flagFastInvalidate := getopt.BoolLong("fast-invalidate", 0, "Invalidate XEP-0484: Fast Authentication Streamlining Tokens.") |
| 165 |
flagPLAINAllow := getopt.BoolLong("allow-plain", 0, "Allow PLAIN authentication.") |
| 166 |
flagNoRootWarning := getopt.BoolLong("suppress-root-warning", 0, "Suppress warning when run as root.") |
| 167 |
flagAnon := getopt.BoolLong("anonymous", 0, "Use anonymous authentication (specify the target anon service as username.") |
| 168 |
flagNoSASLUpgrade := getopt.BoolLong("no-sasl-upgrade", 0, "Disable XEP-0480: SASL Upgrade Tasks.") |
| 169 |
flagRecipientsFile := getopt.StringLong("recipients", 'r', "", "Read recipients from file.") |
| 170 |
flagRetryConnect := getopt.IntLong("retry-connect", 0, 0, "Time to retry to connect after failed connection in seconds. (0 = disabled)") |
| 171 |
flagRetryConnectMax := getopt.IntLong("retry-connect-max", 0, 0, "Number of maximum retries to perform for '--retry-connect'. (0 = unlimited)") |
| 172 |
flagLegacyPGP := getopt.BoolLong("legacy-pgp", 0, "Use \"Legacy PGP\" encryption using the Ox key infrastructure (deprecated and not encouraged).") |
| 173 |
|
| 174 |
// Parse command line flags. |
| 175 |
getopt.Parse() |
| 176 |
|
| 177 |
switch { |
| 178 |
case *flagHelp: |
| 179 |
// If requested, show help and quit. |
| 180 |
getopt.PrintUsage(os.Stdout) |
| 181 |
os.Exit(0) |
| 182 |
case *flagVersion: |
| 183 |
// If requested, show version and quit. |
| 184 |
fmt.Println("Go-sendxmpp", version) |
| 185 |
fmt.Println("Go-xmpp library version:", xmpp.Version) |
| 186 |
fmt.Println("Xmppsrv library version:", xmppsrv.Version) |
| 187 |
fmt.Println("Gopenpgp library version:", gopenpgpConst.Version) |
| 188 |
system := runtime.GOOS + "/" + runtime.GOARCH |
| 189 |
fmt.Println("System:", system, runtime.Version()) |
| 190 |
fmt.Println("License: BSD-2-clause") |
| 191 |
os.Exit(0) |
| 192 |
// Quit if Ox (OpenPGP for XMPP) is requested for unsupported operations like |
| 193 |
// groupchat, http-upload or listening. |
| 194 |
case *flagLegacyPGP && *flagChatroom: |
| 195 |
log.Fatal("No legacy PGP support for chat rooms available.") |
| 196 |
case len(*flagHTTPUpload) != 0 && *flagInteractive: |
| 197 |
log.Fatal("Interactive mode and http upload can't" + |
| 198 |
" be used at the same time.") |
| 199 |
case len(*flagHTTPUpload) != 0 && *flagMessageFile != "": |
| 200 |
log.Fatal("You can't send a message while using" + |
| 201 |
" http upload.") |
| 202 |
case *flagOx && *flagLegacyPGP: |
| 203 |
log.Fatal("Ox and legacy PGP can't be used simultaneously.") |
| 204 |
case (*flagOx || *flagLegacyPGP) && *flagOOBFile != "": |
| 205 |
log.Fatal("No encryption possible for OOB data.") |
| 206 |
case (*flagOx || *flagLegacyPGP) && *flagHeadline: |
| 207 |
log.Fatal("No Ox or legacy PGP support for headline messages.") |
| 208 |
case *flagLegacyPGP && *flagSubject != "": |
| 209 |
log.Fatal("No legacy PGP support for subject.") |
| 210 |
case *flagHeadline && *flagChatroom: |
| 211 |
log.Fatal("Can't use message type headline for groupchat messages.") |
| 212 |
case *flagAnon && *flagUser == "": |
| 213 |
log.Fatal("Specifying a username is required when using anonymous authentication.") |
| 214 |
} |
| 215 |
|
| 216 |
if *flagVerbose { |
| 217 |
slog.SetLogLoggerLevel(slog.LevelDebug) |
| 218 |
} |
| 219 |
|
| 220 |
switch *flagSCRAMPinning { |
| 221 |
case "", "SCRAM-SHA-1", "SCRAM-SHA-1-PLUS", "SCRAM-SHA-256", "SCRAM-SHA-256-PLUS", |
| 222 |
"SCRAM-SHA-512", "SCRAM-SHA-512-PLUS": |
| 223 |
slog.Info("SCRAM pinning", "mechanism", *flagSCRAMPinning) |
| 224 |
default: |
| 225 |
log.Fatal("Unknown SCRAM mechanism: ", *flagSCRAMPinning) |
| 226 |
} |
| 227 |
|
| 228 |
if *flagOxRecvPrivKey != "" && len(*flagOxRecvPrivKey) != 29 { |
| 229 |
log.Fatal("OX: passphrase to decrypt private key has wrong length: ", len(*flagOxRecvPrivKey), " instead of 29") |
| 230 |
} |
| 231 |
|
| 232 |
// Read recipients from command line, if no recipients list is specified, and quit |
| 233 |
// if none are specified. For listening or sending raw XML it's not required to |
| 234 |
// specify a recipient except when sending raw messages to MUCs (go-sendxmpp will |
| 235 |
// join the MUC automatically). |
| 236 |
var recipientsList []string |
| 237 |
if *flagRecipientsFile == "" { |
| 238 |
slog.Info("getting recipients from command line") |
| 239 |
recipientsList = getopt.Args() |
| 240 |
} else { |
| 241 |
slog.Info("getting recipients from recipients file") |
| 242 |
rl, err := readFileByLine(*flagRecipientsFile) |
| 243 |
if err != nil { |
| 244 |
log.Fatal(err) |
| 245 |
} |
| 246 |
if strings.Contains(rl, " ") { |
| 247 |
recipientsList = strings.Split(rl, " ") |
| 248 |
} else { |
| 249 |
recipientsList = strings.Split(rl, "\n") |
| 250 |
} |
| 251 |
} |
| 252 |
slog.Info("determining", "recipients", recipientsList) |
| 253 |
|
| 254 |
if (len(recipientsList) == 0 && !*flagRaw && !*flagListen && !*flagOxGenPrivKeyX25519 && |
| 255 |
!*flagOxGenPrivKeyRSA && *flagOxImportPrivKey == "" && !*flagFastInvalidate) && |
| 256 |
!*flagOxDeleteNodes && *flagOxRecvPrivKey == "" && !*flagOxTransPrivKey || |
| 257 |
(len(recipientsList) == 0 && *flagChatroom) { |
| 258 |
log.Fatal("No recipient specified.") |
| 259 |
} |
| 260 |
|
| 261 |
// Read configuration file if no user or password is specified and anonymous |
| 262 |
// authentication is not used. |
| 263 |
if (*flagUser == "" || *flagPassword == "") && !*flagAnon { |
| 264 |
// Read configuration from file. |
| 265 |
slog.Info("reading config from command line", "flag", *flagFile) |
| 266 |
config, err := parseConfig(*flagFile) |
| 267 |
if err != nil { |
| 268 |
log.Fatal("Error parsing ", *flagFile, ": ", err) |
| 269 |
} |
| 270 |
// Set connection options according to config. |
| 271 |
user = config.username |
| 272 |
slog.Info("config", "user", config.username) |
| 273 |
server = config.jserver |
| 274 |
slog.Info("config", "jserver", config.jserver) |
| 275 |
password = config.password |
| 276 |
slog.Info("config", "password", "REDACTED") |
| 277 |
alias = config.alias |
| 278 |
slog.Info("config", "alias", config.alias) |
| 279 |
if config.port != "" { |
| 280 |
slog.Info("config", "port", config.port) |
| 281 |
server = net.JoinHostPort(server, fmt.Sprint(config.port)) |
| 282 |
slog.Info("setting", "server", server) |
| 283 |
} |
| 284 |
allowPLAIN = config.allowPLAIN |
| 285 |
noRootWarn = config.noRootWarn |
| 286 |
noLPGPWarn = config.noLPGPWarn |
| 287 |
oxTrustMode = config.oxTrustMode |
| 288 |
slog.Info("config", "allow-plain", config.allowPLAIN) |
| 289 |
} |
| 290 |
|
| 291 |
// Print a warning if go-sendxmpp is run by the user root on non-windows systems. |
| 292 |
if runtime.GOOS != "windows" && !*flagNoRootWarning && !noRootWarn { |
| 293 |
// Get the current user. |
| 294 |
slog.Info("checking for current OS user") |
| 295 |
currUser, err := osUser.Current() |
| 296 |
if err != nil { |
| 297 |
log.Fatal("Failed to get current user: ", err) |
| 298 |
} |
| 299 |
slog.Info("checking if OS user is root") |
| 300 |
slog.Info("user identification", "UID", syscall.Getuid()) |
| 301 |
slog.Info("user identification", "GID", syscall.Getgid()) |
| 302 |
if currUser.Username == "root" { |
| 303 |
fmt.Println("WARNING: It seems you are running go-sendxmpp as root user.\n" + |
| 304 |
"This is not recommended as go-sendxmpp does not require root " + |
| 305 |
"privileges. Please consider using a less privileged user. For an " + |
| 306 |
"example how to do this with sudo please consult the manpage chapter " + |
| 307 |
"TIPS.") |
| 308 |
// Logging that we're running as root |
| 309 |
isRoot = true |
| 310 |
// Disabling FAST, legacy PGP and OX when running as root as both features |
| 311 |
// need to read/write local date in the users context and access the network |
| 312 |
// but we drop root privileges prior to network activity. |
| 313 |
*flagFastOff = true |
| 314 |
slog.Info("disabling fast as we're running as root") |
| 315 |
*flagOx = false |
| 316 |
*flagLegacyPGP = false |
| 317 |
slog.Info("disabling OX as we're running as root") |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if *flagLegacyPGP && !noLPGPWarn { |
| 322 |
slog.Warn("Legacy PGP is deprecated and will be removed in a future version, " + |
| 323 |
"please use 'OpenPGP for XMPP' (OX) instead.") |
| 324 |
} |
| 325 |
|
| 326 |
// Overwrite user if specified via command line flag. |
| 327 |
if *flagUser != "" { |
| 328 |
user = *flagUser |
| 329 |
slog.Info("overwriting config value by command line flag", "user", *flagUser) |
| 330 |
} |
| 331 |
|
| 332 |
// Overwrite server if specified via command line flag. |
| 333 |
if *flagServer != "" { |
| 334 |
server = *flagServer |
| 335 |
slog.Info("overwriting config value by command line flag", "server", *flagServer) |
| 336 |
} |
| 337 |
|
| 338 |
// Overwrite password if specified via command line flag. |
| 339 |
if *flagPassword != "" && !*flagAnon { |
| 340 |
password = *flagPassword |
| 341 |
slog.Info("overwriting config value by command line flag", "password", "REDACTED") |
| 342 |
} |
| 343 |
// If no server part is specified in the username but a server is specified |
| 344 |
// just assume the server is identical to the server part and hope for the |
| 345 |
// best. This is for compatibility with the old perl sendxmpp config files. |
| 346 |
var serverpart string |
| 347 |
if !strings.Contains(user, "@") && server != "" && !*flagAnon { |
| 348 |
slog.Info("user seems to be no full JID") |
| 349 |
// Remove port if server contains it. |
| 350 |
if strings.Contains(server, ":") { |
| 351 |
serverpart, _, err = net.SplitHostPort(server) |
| 352 |
if err != nil { |
| 353 |
log.Fatal(err) |
| 354 |
} |
| 355 |
} else { |
| 356 |
serverpart = server |
| 357 |
} |
| 358 |
slog.Info("creating JID with", "userpart", user, "serverpart", serverpart) |
| 359 |
user = user + "@" + serverpart |
| 360 |
} |
| 361 |
|
| 362 |
switch { |
| 363 |
// Use "go-sendxmpp" if no nick is specified via config or command line flag. |
| 364 |
case *flagChatroom && alias == "" && *flagAlias == "": |
| 365 |
slog.Info("no alias defined, using go-sendxmpp") |
| 366 |
alias = "go-sendxmpp." + getShortID() |
| 367 |
// Overwrite configured alias if a nick is specified via command line flag. |
| 368 |
case *flagAlias != "": |
| 369 |
alias = *flagAlias |
| 370 |
slog.Info("overwriting config value by command line flag", "alias", *flagAlias) |
| 371 |
} |
| 372 |
|
| 373 |
// Timeout |
| 374 |
timeout := time.Duration(*flagTimeout) * time.Second |
| 375 |
slog.Info("setting", "timeout", timeout) |
| 376 |
|
| 377 |
slog.Info("creating client ID") |
| 378 |
clientID, err := getClientID(user) |
| 379 |
if err != nil { |
| 380 |
fmt.Println(err) |
| 381 |
} |
| 382 |
|
| 383 |
if !*flagFastOff { |
| 384 |
slog.Info("getting FAST data") |
| 385 |
fast, err = getFastData(user, password, clientID) |
| 386 |
if err != nil { |
| 387 |
slog.Info("no FAST data found") |
| 388 |
} |
| 389 |
// Reset FAST token and mechanism if expired or |
| 390 |
// FastInvalidate is set. |
| 391 |
if time.Now().After(fast.Expiry) || *flagFastInvalidate { |
| 392 |
slog.Info("invalidating fast data:", "token expired", time.Now().After(fast.Expiry), |
| 393 |
"flag FAST invalidate", *flagFastInvalidate) |
| 394 |
fast.Token = "" |
| 395 |
fast.Mechanism = "" |
| 396 |
} |
| 397 |
} |
| 398 |
if fast.Mechanism != "" && *flagSCRAMPinning != "" { |
| 399 |
log.Fatalf("FAST: %s is requested, but we have a token for %s.", *flagSCRAMPinning, fast.Mechanism) |
| 400 |
} |
| 401 |
|
| 402 |
var tlsConfig tls.Config |
| 403 |
jserver := user[strings.Index(user, "@")+1:] |
| 404 |
if !*flagAnon { |
| 405 |
tlsConfig.ServerName, err = idna.ToASCII(jserver) |
| 406 |
if err != nil { |
| 407 |
log.Fatal(err) |
| 408 |
} |
| 409 |
slog.Info("setting TLS config:", "ServerName", tlsConfig.ServerName) |
| 410 |
} else { |
| 411 |
tlsConfig.ServerName, err = idna.ToASCII(*flagUser) |
| 412 |
if err != nil { |
| 413 |
log.Fatal(err) |
| 414 |
} |
| 415 |
slog.Info("setting TLS config:", "ServerName", tlsConfig.ServerName) |
| 416 |
*flagSCRAMPinning = "ANONYMOUS" |
| 417 |
slog.Info("setting SCRAM", "mechanism", *flagSCRAMPinning) |
| 418 |
} |
| 419 |
// Use ALPN |
| 420 |
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "xmpp-client") |
| 421 |
slog.Info("setting TLS config:", "NextProtos", tlsConfig.NextProtos) |
| 422 |
if *flagSkipVerify { |
| 423 |
// #nosec G402 -- user must explicitly enable it and a warning is logged |
| 424 |
tlsConfig.InsecureSkipVerify = *flagSkipVerify |
| 425 |
slog.Warn("TLS config INSECURE:", "InsecureSkipVerify", tlsConfig.InsecureSkipVerify) |
| 426 |
} else { |
| 427 |
slog.Info("setting TLS config:", "InsecureSkipVerify", tlsConfig.InsecureSkipVerify) |
| 428 |
} |
| 429 |
tlsConfig.Renegotiation = tls.RenegotiateNever |
| 430 |
slog.Info("setting TLS config:", "Renegotiation", tlsConfig.Renegotiation) |
| 431 |
switch *flagTLSMinVersion { |
| 432 |
case defaultTLS10: |
| 433 |
slog.Info("setting TLS config:", "MinVersion", defaultTLS10) |
| 434 |
tlsConfig.MinVersion = tls.VersionTLS10 |
| 435 |
case defaultTLS11: |
| 436 |
slog.Info("setting TLS config:", "MinVersion", defaultTLS11) |
| 437 |
tlsConfig.MinVersion = tls.VersionTLS11 |
| 438 |
case defaultTLS12: |
| 439 |
slog.Info("setting TLS config:", "MinVersion", defaultTLS12) |
| 440 |
tlsConfig.MinVersion = tls.VersionTLS12 |
| 441 |
case defaultTLS13: |
| 442 |
slog.Info("setting TLS config:", "MinVersion", defaultTLS13) |
| 443 |
tlsConfig.MinVersion = tls.VersionTLS13 |
| 444 |
default: |
| 445 |
fmt.Println("Unknown TLS version.") |
| 446 |
os.Exit(0) |
| 447 |
} |
| 448 |
|
| 449 |
resource := "go-sendxmpp." + getShortID() |
| 450 |
|
| 451 |
// Check whether PLAIN authentication is disabled. |
| 452 |
if !isRoot { |
| 453 |
pinNoPLAIN, _ = parseAuthPinFile(user) |
| 454 |
} |
| 455 |
noPLAIN := pinNoPLAIN || (!*flagPLAINAllow && !allowPLAIN) |
| 456 |
|
| 457 |
// Periodic server ping settings |
| 458 |
psp := true |
| 459 |
pspPeriod := 30000 |
| 460 |
|
| 461 |
// Set XMPP connection options. |
| 462 |
options := xmpp.Options{ |
| 463 |
Host: server, |
| 464 |
User: user, |
| 465 |
DialTimeout: timeout, |
| 466 |
Resource: resource, |
| 467 |
Password: password, |
| 468 |
// NoTLS doesn't mean that no TLS is used at all but that instead |
| 469 |
// of using an encrypted connection to the server (direct TLS) |
| 470 |
// an unencrypted connection is established. As StartTLS is |
| 471 |
// set when NoTLS is set go-sendxmpp won't use unencrypted |
| 472 |
// client-to-server connections. |
| 473 |
// See https://pkg.go.dev/github.com/xmppo/go-xmpp#Options |
| 474 |
NoTLS: !*flagDirectTLS, |
| 475 |
StartTLS: !*flagDirectTLS, |
| 476 |
Debug: *flagDebug, |
| 477 |
DebugWriter: os.Stdout, |
| 478 |
TLSConfig: &tlsConfig, |
| 479 |
Mechanism: *flagSCRAMPinning, |
| 480 |
SSDP: !*flagSSDPOff, |
| 481 |
UserAgentSW: resource, |
| 482 |
UserAgentID: clientID, |
| 483 |
Fast: !*flagFastOff, |
| 484 |
FastToken: fast.Token, |
| 485 |
FastMechanism: fast.Mechanism, |
| 486 |
FastInvalidate: *flagFastInvalidate, |
| 487 |
NoPLAIN: noPLAIN, |
| 488 |
NoSASLUpgrade: *flagNoSASLUpgrade, |
| 489 |
PeriodicServerPings: psp, |
| 490 |
PeriodicServerPingsPeriod: pspPeriod, |
| 491 |
ReportSoftwareVersion: true, |
| 492 |
SoftwareName: "go-sendxmpp", |
| 493 |
SoftwareVersion: version, |
| 494 |
ReportSoftwareOS: true, |
| 495 |
} |
| 496 |
slog.Info("setting xmpp connection option:", "Host", server) |
| 497 |
slog.Info("setting xmpp connection option:", "User", user) |
| 498 |
slog.Info("setting xmpp connection option:", "DialTimeout", timeout) |
| 499 |
slog.Info("setting xmpp connection option:", "Resource", resource) |
| 500 |
slog.Info("setting xmpp connection option:", "Password", "REDACTED") |
| 501 |
slog.Info("setting xmpp connection option:", "NoTLS", !*flagDirectTLS) |
| 502 |
slog.Info("setting xmpp connection option:", "StartTLS", !*flagDirectTLS) |
| 503 |
slog.Info("setting xmpp connection option:", "Debug", *flagDebug) |
| 504 |
slog.Info("setting xmpp connection option:", "Mechanism", *flagSCRAMPinning) |
| 505 |
slog.Info("setting xmpp connection option:", "SSDP", !*flagSSDPOff) |
| 506 |
slog.Info("setting xmpp connection option:", "UserAgentSW", resource) |
| 507 |
slog.Info("setting xmpp connection option:", "UserAgentID", clientID) |
| 508 |
slog.Info("setting xmpp connection option:", "Fast", !*flagFastOff) |
| 509 |
slog.Info("setting xmpp connection option:", "FastToken", "REDACTED") |
| 510 |
slog.Info("setting xmpp connection option:", "FastMechanism", fast.Mechanism) |
| 511 |
slog.Info("setting xmpp connection option:", "FastInvalidate", *flagFastInvalidate) |
| 512 |
slog.Info("setting xmpp connection option:", "NoSASLUpgrade", *flagNoSASLUpgrade) |
| 513 |
slog.Info("setting xmpp connection option:", "NoPLAIN", noPLAIN) |
| 514 |
slog.Info("setting xmpp connection option:", "PeriodicServerPings", psp) |
| 515 |
slog.Info("setting xmpp connection option:", "PeriodicServerPingsPeriod", pspPeriod) |
| 516 |
|
| 517 |
// Read message from file. |
| 518 |
if *flagMessageFile != "" { |
| 519 |
slog.Info("reading message from message file") |
| 520 |
message, err = readFileByLine(*flagMessageFile) |
| 521 |
if err != nil { |
| 522 |
log.Fatal(err) |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
// Skip reading message if '-i' or '--interactive' is set to work with e.g. 'tail -f'. |
| 527 |
// Also for listening mode and Ox key handling. |
| 528 |
if !*flagInteractive && !*flagListen && len(*flagHTTPUpload) == 0 && |
| 529 |
!*flagOxDeleteNodes && *flagOxImportPrivKey == "" && |
| 530 |
!*flagOxGenPrivKeyX25519 && !*flagOxGenPrivKeyRSA && *flagOOBFile == "" && |
| 531 |
!*flagFastInvalidate && *flagOxRecvPrivKey == "" && !*flagOxTransPrivKey && |
| 532 |
message == "" { |
| 533 |
slog.Info("reading message from stdin") |
| 534 |
scanner := bufio.NewScanner(os.Stdin) |
| 535 |
for scanner.Scan() { |
| 536 |
if message == "" { |
| 537 |
message = scanner.Text() |
| 538 |
slog.Info("reading message", "line", scanner.Text()) |
| 539 |
} else { |
| 540 |
message = message + "\n" + scanner.Text() |
| 541 |
slog.Info("reading message", "line", scanner.Text()) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
if err := scanner.Err(); err != nil { |
| 546 |
if err != io.EOF { |
| 547 |
log.Fatal(err) |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// Remove invalid code points. |
| 553 |
slog.Info("removing invalid code points from message") |
| 554 |
message = validUTF8(message) |
| 555 |
// Exit if message is empty. |
| 556 |
if message == "" && !*flagInteractive && !*flagListen && !*flagOxGenPrivKeyRSA && |
| 557 |
!*flagOxGenPrivKeyX25519 && *flagOxImportPrivKey == "" && |
| 558 |
!*flagOxDeleteNodes && len(*flagHTTPUpload) == 0 && *flagOOBFile == "" && |
| 559 |
!*flagFastInvalidate && !*flagOxTransPrivKey && *flagOxRecvPrivKey == "" { |
| 560 |
slog.Info("exiting due to empty message") |
| 561 |
os.Exit(0) |
| 562 |
} |
| 563 |
|
| 564 |
if isRoot { |
| 565 |
_ = dropPrivs() |
| 566 |
} |
| 567 |
|
| 568 |
// Connect to server. |
| 569 |
client, err := connect(options, *flagDirectTLS, *flagAnon, *flagRetryConnect, *flagRetryConnectMax) |
| 570 |
if err != nil { |
| 571 |
if fast.Token != "" { |
| 572 |
slog.Info("failed connecting using fast", "mechanism", options.FastMechanism, "token", options.FastToken) |
| 573 |
options.FastToken = "" |
| 574 |
options.FastMechanism = "" |
| 575 |
client, err = connect(options, *flagDirectTLS, *flagAnon, *flagRetryConnect, *flagRetryConnectMax) |
| 576 |
if err != nil { |
| 577 |
log.Fatal(err) |
| 578 |
} |
| 579 |
} else { |
| 580 |
log.Fatal(err) |
| 581 |
} |
| 582 |
} |
| 583 |
slog.Info("connection successful") |
| 584 |
|
| 585 |
// Update fast token if a new one is received or expiry time is reduced. |
| 586 |
if (client.Fast.Token != "" && client.Fast.Token != fast.Token) || |
| 587 |
(client.Fast.Expiry.Before(fast.Expiry) && !client.Fast.Expiry.IsZero()) { |
| 588 |
fast.Token = client.Fast.Token |
| 589 |
slog.Info("updating FAST:", "token", "REDACTED") |
| 590 |
fast.Mechanism = client.Fast.Mechanism |
| 591 |
slog.Info("updating FAST:", "mechanism", fast.Mechanism) |
| 592 |
fast.Expiry = client.Fast.Expiry |
| 593 |
slog.Info("updating FAST:", "expiry", fast.Expiry) |
| 594 |
slog.Info("writing FAST data to disk") |
| 595 |
err := writeFastData(user, password, clientID, fast) |
| 596 |
if err != nil { |
| 597 |
fmt.Println(err) |
| 598 |
} |
| 599 |
} |
| 600 |
// Delete stored fast data when FastInvalidate is set. |
| 601 |
if *flagFastInvalidate && *flagFastOff { |
| 602 |
slog.Info("deleting FAST data") |
| 603 |
err := deleteFastData(user) |
| 604 |
if err != nil { |
| 605 |
fmt.Println(err) |
| 606 |
} |
| 607 |
} |
| 608 |
// If authentication is not yet pinned to not use PLAIN and a SCRAM mechanism is |
| 609 |
// used, write the auth pin file. |
| 610 |
if !pinNoPLAIN && !isRoot && (strings.HasPrefix(client.Mechanism, "SCRAM") || |
| 611 |
strings.HasPrefix(client.Mechanism, "HT")) { |
| 612 |
slog.Info("writing auth pin file") |
| 613 |
err = writeAuthPinFile(user) |
| 614 |
if err != nil { |
| 615 |
log.Println("could not write authentication mechanism pin file:", err) |
| 616 |
} |
| 617 |
} |
| 618 |
iqc := make(chan xmpp.IQ, defaultBufferSize) |
| 619 |
msgc := make(chan xmpp.Chat, defaultBufferSize) |
| 620 |
prsc := make(chan xmpp.Presence, defaultBufferSize) |
| 621 |
disc := make(chan xmpp.DiscoItems, defaultBufferSize) |
| 622 |
drc := make(chan xmpp.DiscoResult, defaultBufferSize) |
| 623 |
slc := make(chan xmpp.Slot, defaultBufferSize) |
| 624 |
ctx, cancel := context.WithCancel(context.Background()) |
| 625 |
slog.Info("starting to receive stanzas") |
| 626 |
go rcvStanzas(client, ctx, iqc, msgc, prsc, disc, drc, slc) |
| 627 |
for _, r := range recipientsList { |
| 628 |
var re recipientsType |
| 629 |
re.Jid = strings.TrimPrefix(strings.ToLower(r), "xmpp:") |
| 630 |
if *flagChatroom { |
| 631 |
re.Jid = strings.TrimSuffix(re.Jid, "?join") |
| 632 |
} |
| 633 |
switch { |
| 634 |
case (*flagOx || *flagLegacyPGP) && !*flagChatroom: |
| 635 |
slog.Info("requesting OX key for", "JID", re.Jid) |
| 636 |
re.OxKeyRing, err = oxGetPublicKeyRing(client, iqc, re.Jid, oxTrustMode) |
| 637 |
if err != nil { |
| 638 |
re.OxKeyRing = nil |
| 639 |
fmt.Printf("ox: error fetching key for %s: %v\n", re.Jid, err) |
| 640 |
failure = err |
| 641 |
} |
| 642 |
case *flagOx && *flagChatroom: |
| 643 |
re.OxKeyRing, err = oxGetPublicKeyRingChatroom(client, iqc, drc, re.Jid, oxTrustMode) |
| 644 |
if err != nil { |
| 645 |
re.OxKeyRing = nil |
| 646 |
fmt.Println("Couldn't receive keys for:", re.Jid) |
| 647 |
} |
| 648 |
} |
| 649 |
recipients = append(recipients, re) |
| 650 |
} |
| 651 |
|
| 652 |
// Check that all recipient JIDs are valid. |
| 653 |
for i, recipient := range recipients { |
| 654 |
slog.Info("checking validity", "JID", recipient.Jid) |
| 655 |
validatedJid, err := MarshalJID(recipient.Jid) |
| 656 |
if err != nil { |
| 657 |
cancel() |
| 658 |
closeAndExit(client, err) |
| 659 |
} |
| 660 |
recipients[i].Jid = validatedJid |
| 661 |
} |
| 662 |
|
| 663 |
slog.Info("checking validity", "JID", user) |
| 664 |
validatedOwnJid, err := MarshalJID(user) |
| 665 |
if err != nil { |
| 666 |
cancel() |
| 667 |
closeAndExit(client, err) |
| 668 |
} |
| 669 |
|
| 670 |
switch { |
| 671 |
case *flagOxGenPrivKeyX25519: |
| 672 |
slog.Info("generating X25519 private OX key") |
| 673 |
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, |
| 674 |
crypto.KeyGenerationCurve25519Legacy) |
| 675 |
// crypto.KeyGenerationCurve25519) |
| 676 |
// TODO: Clarify whether RFC9580 can also be used instead of RFC4880bis |
| 677 |
// for OX keys. |
| 678 |
if err != nil { |
| 679 |
cancel() |
| 680 |
closeAndExit(client, err) |
| 681 |
} |
| 682 |
os.Exit(0) |
| 683 |
case *flagOxGenPrivKeyRSA: |
| 684 |
slog.Info("generating RSA4096 private OX key") |
| 685 |
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, crypto.KeyGenerationRSA4096) |
| 686 |
if err != nil { |
| 687 |
cancel() |
| 688 |
closeAndExit(client, err) |
| 689 |
} |
| 690 |
os.Exit(0) |
| 691 |
case *flagOxImportPrivKey != "": |
| 692 |
slog.Info("importing private OX key:", "file", *flagOxImportPrivKey) |
| 693 |
err = oxImportPrivKey(validatedOwnJid, *flagOxImportPrivKey, |
| 694 |
client, iqc, oxTrustMode) |
| 695 |
if err != nil { |
| 696 |
cancel() |
| 697 |
closeAndExit(client, err) |
| 698 |
} |
| 699 |
os.Exit(0) |
| 700 |
case *flagOxDeleteNodes: |
| 701 |
slog.Info("delete OX pep nodes") |
| 702 |
err = oxDeleteNodes(validatedOwnJid, client, iqc, disc) |
| 703 |
if err != nil { |
| 704 |
cancel() |
| 705 |
closeAndExit(client, err) |
| 706 |
} |
| 707 |
os.Exit(0) |
| 708 |
case *flagOxTransPrivKey: |
| 709 |
slog.Info("transferring private OX key to the server") |
| 710 |
oxPrivKey, err = oxGetPrivKey(validatedOwnJid, *flagOxPassphrase) |
| 711 |
if err != nil { |
| 712 |
cancel() |
| 713 |
closeAndExit(client, err) |
| 714 |
} |
| 715 |
err = oxTransPrivKey(validatedOwnJid, oxPrivKey, client, iqc, drc) |
| 716 |
if err != nil { |
| 717 |
cancel() |
| 718 |
closeAndExit(client, err) |
| 719 |
} |
| 720 |
os.Exit(0) |
| 721 |
case *flagOxRecvPrivKey != "": |
| 722 |
slog.Info("requesting private OX key from the server") |
| 723 |
err = oxRecvPrivKey(validatedOwnJid, *flagOxRecvPrivKey, client, iqc, drc, oxTrustMode) |
| 724 |
if err != nil { |
| 725 |
cancel() |
| 726 |
closeAndExit(client, err) |
| 727 |
} |
| 728 |
os.Exit(0) |
| 729 |
case *flagOx, *flagLegacyPGP: |
| 730 |
slog.Info("reading private OX key") |
| 731 |
oxPrivKey, err = oxGetPrivKey(validatedOwnJid, *flagOxPassphrase) |
| 732 |
if err != nil { |
| 733 |
cancel() |
| 734 |
closeAndExit(client, err) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
var uploadMessages []string |
| 739 |
if len(*flagHTTPUpload) != 0 && !*flagLegacyPGP && !*flagOx { |
| 740 |
slog.Info("http-upload:", "file", *flagHTTPUpload) |
| 741 |
uploadMessages, err = httpUpload(client, iqc, disc, drc, slc, |
| 742 |
jserver, *flagHTTPUpload, timeout, nil, nil, false) |
| 743 |
if err != nil { |
| 744 |
cancel() |
| 745 |
closeAndExit(client, err) |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
if *flagOOBFile != "" { |
| 750 |
// Remove invalid UTF8 code points. |
| 751 |
message = validUTF8(*flagOOBFile) |
| 752 |
// Check if the URI is valid. |
| 753 |
uri, err := validURI(message) |
| 754 |
if err != nil { |
| 755 |
cancel() |
| 756 |
closeAndExit(client, err) |
| 757 |
} |
| 758 |
message = uri.String() |
| 759 |
slog.Info("send OOB:", "uri", message) |
| 760 |
} |
| 761 |
|
| 762 |
var msgType string |
| 763 |
switch { |
| 764 |
case *flagHeadline: |
| 765 |
msgType = strHeadline |
| 766 |
case *flagChatroom: |
| 767 |
msgType = strGroupchat |
| 768 |
default: |
| 769 |
msgType = strChat |
| 770 |
} |
| 771 |
slog.Info("setting message", "type", msgType) |
| 772 |
if *flagChatroom { |
| 773 |
// Join the MUCs. |
| 774 |
for _, recipient := range recipients { |
| 775 |
var presence xmpp.Presence |
| 776 |
pc := make(chan xmpp.Presence, defaultBufferSize) |
| 777 |
go getPresence(recipient.Jid, pc, prsc) |
| 778 |
if *flagMUCPassword != "" { |
| 779 |
slog.Info("joining MUC using password", "MUC", recipient.Jid, "password", "REDACTED") |
| 780 |
dummyTime := time.Now() |
| 781 |
_, err = client.JoinProtectedMUC(recipient.Jid, alias, |
| 782 |
*flagMUCPassword, xmpp.NoHistory, 0, &dummyTime) |
| 783 |
} else { |
| 784 |
slog.Info("joining", "MUC", recipient.Jid) |
| 785 |
_, err = client.JoinMUCNoHistory(recipient.Jid, alias) |
| 786 |
} |
| 787 |
if err != nil { |
| 788 |
fmt.Printf("Could not join MUC %s: %v\n", recipient.Jid, err) |
| 789 |
failure = err |
| 790 |
} |
| 791 |
select { |
| 792 |
case presence = <-pc: |
| 793 |
if presence.Type == "error" { |
| 794 |
fmt.Printf("Could not join MUC %s: %s\n", recipient.Jid, presence.Error) |
| 795 |
failure = fmt.Errorf("failed to join one or more MUCs: last error: %s: %s", recipient.Jid, presence.Error) |
| 796 |
} |
| 797 |
case <-time.After(60 * time.Second): |
| 798 |
fmt.Printf("Could not join muc: didn't receive a presence from %s\n", recipient.Jid) |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
switch { |
| 803 |
case *flagRaw: |
| 804 |
if message == "" { |
| 805 |
slog.Info("not sending empty message") |
| 806 |
break |
| 807 |
} |
| 808 |
// Send raw XML |
| 809 |
slog.Info("sending raw message") |
| 810 |
_, err = client.SendOrg(message) |
| 811 |
if err != nil { |
| 812 |
cancel() |
| 813 |
closeAndExit(client, err) |
| 814 |
} |
| 815 |
case *flagInteractive: |
| 816 |
// Send in endless loop (for usage with e.g. "tail -f"). |
| 817 |
slog.Info("reading in interactive mode from stdin") |
| 818 |
reader := bufio.NewReader(os.Stdin) |
| 819 |
// Quit if ^C is pressed. |
| 820 |
c := make(chan os.Signal, 1) |
| 821 |
signal.Notify(c, os.Interrupt) |
| 822 |
go func() { |
| 823 |
for range c { |
| 824 |
slog.Info("stopping interactive mode") |
| 825 |
cancel() |
| 826 |
if failure != nil { |
| 827 |
closeAndExit(client, failure) |
| 828 |
} |
| 829 |
closeAndExit(client, nil) |
| 830 |
} |
| 831 |
}() |
| 832 |
for { |
| 833 |
message, err = reader.ReadString('\n') |
| 834 |
if err != nil { |
| 835 |
select { |
| 836 |
case <-ctx.Done(): |
| 837 |
return |
| 838 |
default: |
| 839 |
if err != nil { |
| 840 |
cancel() |
| 841 |
closeAndExit(client, fmt.Errorf("failed to read from stdin")) |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
message = strings.TrimSuffix(message, "\n") |
| 846 |
slog.Info("reading message", "line", message) |
| 847 |
|
| 848 |
// Remove invalid code points. |
| 849 |
slog.Info("removing invalid code points from message") |
| 850 |
message = validUTF8(message) |
| 851 |
if message == "" { |
| 852 |
continue |
| 853 |
} |
| 854 |
for _, recipient := range recipients { |
| 855 |
switch { |
| 856 |
case *flagOx, *flagLegacyPGP: |
| 857 |
var cryptMessage string |
| 858 |
if recipient.OxKeyRing == nil { |
| 859 |
slog.Info("skipping sending an OX encrypted message due to missing public key for", "JID", recipient.Jid) |
| 860 |
continue |
| 861 |
} |
| 862 |
switch { |
| 863 |
case *flagOx: |
| 864 |
slog.Info("encrypting message with OX for", "JID", recipient.Jid) |
| 865 |
cryptMessage, err = oxEncrypt(client, oxPrivKey, |
| 866 |
recipient.Jid, *recipient.OxKeyRing, message, *flagSubject, *flagChatroom) |
| 867 |
if err != nil { |
| 868 |
fmt.Printf("ox: couldn't encrypt to %s: %v\n", |
| 869 |
recipient.Jid, err) |
| 870 |
failure = err |
| 871 |
continue |
| 872 |
} |
| 873 |
case *flagLegacyPGP: |
| 874 |
slog.Info("encrypting message with legacy PGP for", "JID", recipient.Jid) |
| 875 |
cryptMessage, err = legacyPGPEncrypt(client, oxPrivKey, |
| 876 |
recipient.Jid, *recipient.OxKeyRing, message) |
| 877 |
if err != nil { |
| 878 |
fmt.Printf("legacy PGP: couldn't encrypt to %s: %v\n", |
| 879 |
recipient.Jid, err) |
| 880 |
failure = err |
| 881 |
continue |
| 882 |
} |
| 883 |
} |
| 884 |
slog.Info("sending OX encrypted message to", "JID", recipient.Jid) |
| 885 |
_, err = client.SendOrg(cryptMessage) |
| 886 |
if err != nil { |
| 887 |
cancel() |
| 888 |
closeAndExit(client, err) |
| 889 |
} |
| 890 |
default: |
| 891 |
slog.Info("sending message to", "JID", recipient.Jid) |
| 892 |
_, err = client.Send(xmpp.Chat{ |
| 893 |
Remote: recipient.Jid, |
| 894 |
Type: msgType, Text: message, |
| 895 |
Subject: *flagSubject, |
| 896 |
}) |
| 897 |
if err != nil { |
| 898 |
cancel() |
| 899 |
closeAndExit(client, err) |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
case *flagListen: |
| 905 |
slog.Info("listening for incoming messages") |
| 906 |
tz := time.Now().Location() |
| 907 |
// Quit if ^C is pressed. |
| 908 |
c := make(chan os.Signal, 1) |
| 909 |
signal.Notify(c, os.Interrupt) |
| 910 |
go func() { |
| 911 |
for range c { |
| 912 |
slog.Info("stopping listening for incoming messages") |
| 913 |
cancel() |
| 914 |
if failure != nil { |
| 915 |
closeAndExit(client, failure) |
| 916 |
} |
| 917 |
closeAndExit(client, nil) |
| 918 |
} |
| 919 |
}() |
| 920 |
for { |
| 921 |
v := <-msgc |
| 922 |
slog.Info("received message") |
| 923 |
isOx := isOxMsg(v) |
| 924 |
isLegacyPGP := isLegacyPGPMsg(v) |
| 925 |
switch { |
| 926 |
case isOx, isLegacyPGP: |
| 927 |
var msg, prefix string |
| 928 |
var t time.Time |
| 929 |
if isOx { |
| 930 |
slog.Info("message is OX encrypted") |
| 931 |
msg, t, err = oxDecrypt(v, client, iqc, drc, user, oxPrivKey, oxTrustMode) |
| 932 |
if err != nil { |
| 933 |
log.Println(err) |
| 934 |
continue |
| 935 |
} |
| 936 |
if msg == "" { |
| 937 |
slog.Info("message is empty") |
| 938 |
continue |
| 939 |
} |
| 940 |
prefix = "[OX]" |
| 941 |
} else { |
| 942 |
msg, t, err = legacyPGPDecrypt(v, client, iqc, user, oxPrivKey, oxTrustMode) |
| 943 |
if err != nil { |
| 944 |
log.Println(err) |
| 945 |
continue |
| 946 |
} |
| 947 |
if msg == "" { |
| 948 |
slog.Info("message is empty") |
| 949 |
continue |
| 950 |
} |
| 951 |
prefix = "[LPGP]" |
| 952 |
} |
| 953 |
var bareFrom string |
| 954 |
switch v.Type { |
| 955 |
case strChat: |
| 956 |
bareFrom = strings.Split(v.Remote, "/")[0] |
| 957 |
case strGroupchat: |
| 958 |
bareFrom = v.Remote |
| 959 |
default: |
| 960 |
bareFrom = strings.Split(v.Remote, "/")[0] |
| 961 |
} |
| 962 |
// Print any messages if no recipients are specified and if we are not |
| 963 |
// joined to a MUC. MUC messages are only received when joining them, |
| 964 |
// therefore a recipient (MUC) is specified even if we did not intend |
| 965 |
// to block receiving of other messages. |
| 966 |
if len(recipients) == 0 || *flagChatroom { |
| 967 |
fmt.Println(t.In(tz).Format(time.RFC3339), prefix, |
| 968 |
bareFrom+":", msg) |
| 969 |
} else { |
| 970 |
for _, recipient := range recipients { |
| 971 |
if strings.Split(v.Remote, "/")[0] == |
| 972 |
strings.ToLower(recipient.Jid) { |
| 973 |
fmt.Println(t.In(tz).Format(time.RFC3339), |
| 974 |
prefix, bareFrom+":", msg) |
| 975 |
} |
| 976 |
} |
| 977 |
} |
| 978 |
default: |
| 979 |
var t time.Time |
| 980 |
if v.Text == "" { |
| 981 |
continue |
| 982 |
} |
| 983 |
if v.Stamp.IsZero() { |
| 984 |
t = time.Now() |
| 985 |
} else { |
| 986 |
t = v.Stamp |
| 987 |
} |
| 988 |
var bareFrom string |
| 989 |
switch v.Type { |
| 990 |
case strChat: |
| 991 |
bareFrom = strings.Split(v.Remote, "/")[0] |
| 992 |
case strGroupchat: |
| 993 |
bareFrom = v.Remote |
| 994 |
default: |
| 995 |
bareFrom = strings.Split(v.Remote, "/")[0] |
| 996 |
} |
| 997 |
// Print any messages if no recipients are specified |
| 998 |
if len(recipients) == 0 { |
| 999 |
fmt.Println(t.In(tz).Format(time.RFC3339), bareFrom+":", v.Text) |
| 1000 |
} else { |
| 1001 |
for _, recipient := range recipients { |
| 1002 |
if strings.Split(v.Remote, "/")[0] == |
| 1003 |
strings.ToLower(recipient.Jid) { |
| 1004 |
fmt.Println(t.In(tz).Format(time.RFC3339), |
| 1005 |
bareFrom+":", v.Text) |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
default: |
| 1012 |
for _, recipient := range recipients { |
| 1013 |
if message == "" && len(*flagHTTPUpload) == 0 { |
| 1014 |
slog.Info("skipping sending of empty message to", "JID", recipient.Jid) |
| 1015 |
break |
| 1016 |
} |
| 1017 |
switch { |
| 1018 |
case len(*flagHTTPUpload) != 0 && !*flagLegacyPGP && !*flagOx: |
| 1019 |
for _, message = range uploadMessages { |
| 1020 |
slog.Info("sending http-upload message:", "file", message, "JID", recipient.Jid) |
| 1021 |
_, err = client.SendOOB(xmpp.Chat{ |
| 1022 |
Remote: recipient.Jid, |
| 1023 |
Type: msgType, Oob: xmpp.Oob{Url: message}, Subject: *flagSubject, |
| 1024 |
}) |
| 1025 |
if err != nil { |
| 1026 |
fmt.Println("Couldn't send message to", |
| 1027 |
recipient.Jid) |
| 1028 |
} |
| 1029 |
} |
| 1030 |
case len(*flagHTTPUpload) != 0 && *flagOx: |
| 1031 |
uploadMessages, err = httpUpload(client, iqc, disc, drc, slc, |
| 1032 |
jserver, *flagHTTPUpload, timeout, nil, nil, true) |
| 1033 |
if err != nil { |
| 1034 |
cancel() |
| 1035 |
closeAndExit(client, err) |
| 1036 |
} |
| 1037 |
if recipient.OxKeyRing == nil { |
| 1038 |
continue |
| 1039 |
} |
| 1040 |
for _, message = range uploadMessages { |
| 1041 |
slog.Info("sending ox encrypted http-upload message:", "file", message, "JID", recipient.Jid) |
| 1042 |
cryptMessage, err := oxEncrypt(client, oxPrivKey, |
| 1043 |
recipient.Jid, *recipient.OxKeyRing, message, *flagSubject, *flagChatroom) |
| 1044 |
if err != nil { |
| 1045 |
fmt.Printf("ox: couldn't encrypt to %s: %v\n", |
| 1046 |
recipient.Jid, err) |
| 1047 |
failure = err |
| 1048 |
continue |
| 1049 |
} |
| 1050 |
_, err = client.SendOrg(cryptMessage) |
| 1051 |
if err != nil { |
| 1052 |
cancel() |
| 1053 |
closeAndExit(client, err) |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
case len(*flagHTTPUpload) != 0 && *flagLegacyPGP: |
| 1058 |
uploadMessages, err = httpUpload(client, iqc, disc, drc, slc, |
| 1059 |
jserver, *flagHTTPUpload, timeout, oxPrivKey, recipient.OxKeyRing, false) |
| 1060 |
if err != nil { |
| 1061 |
cancel() |
| 1062 |
closeAndExit(client, err) |
| 1063 |
} |
| 1064 |
for _, message = range uploadMessages { |
| 1065 |
slog.Info("sending legacy pgp http-upload message:", "file", message, "JID", recipient.Jid) |
| 1066 |
_, err = client.SendOOB(xmpp.Chat{ |
| 1067 |
Remote: recipient.Jid, |
| 1068 |
Type: msgType, Oob: xmpp.Oob{Url: message}, Subject: *flagSubject, |
| 1069 |
}) |
| 1070 |
if err != nil { |
| 1071 |
fmt.Println("Couldn't send message to", |
| 1072 |
recipient.Jid) |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
// (Hopefully) temporary workaround due to go-xmpp choking on URL encoding. |
| 1077 |
// Once this is fixed in the lib the http-upload case above can be reused. |
| 1078 |
case *flagOOBFile != "": |
| 1079 |
_, err = client.SendOOB(xmpp.Chat{ |
| 1080 |
Remote: recipient.Jid, |
| 1081 |
Type: msgType, Oob: xmpp.Oob{Url: message}, Subject: *flagSubject, |
| 1082 |
}) |
| 1083 |
if err != nil { |
| 1084 |
fmt.Println("Couldn't send message to", |
| 1085 |
recipient.Jid) |
| 1086 |
} |
| 1087 |
case *flagOx, *flagLegacyPGP: |
| 1088 |
if recipient.OxKeyRing == nil { |
| 1089 |
slog.Info("skipping sending an OX encrypted message due to missing public key for", "JID", recipient.Jid) |
| 1090 |
continue |
| 1091 |
} |
| 1092 |
var cryptMessage string |
| 1093 |
switch { |
| 1094 |
case *flagOx: |
| 1095 |
slog.Info("encrypting message with OX for", "JID", recipient.Jid) |
| 1096 |
cryptMessage, err = oxEncrypt(client, oxPrivKey, |
| 1097 |
recipient.Jid, *recipient.OxKeyRing, message, *flagSubject, *flagChatroom) |
| 1098 |
if err != nil { |
| 1099 |
fmt.Printf("ox: couldn't encrypt to %s: %v\n", |
| 1100 |
recipient.Jid, err) |
| 1101 |
failure = err |
| 1102 |
continue |
| 1103 |
} |
| 1104 |
case *flagLegacyPGP: |
| 1105 |
slog.Info("encrypting message with legacy PGP for", "JID", recipient.Jid) |
| 1106 |
cryptMessage, err = legacyPGPEncrypt(client, oxPrivKey, |
| 1107 |
recipient.Jid, *recipient.OxKeyRing, message) |
| 1108 |
if err != nil { |
| 1109 |
fmt.Printf("legacy PGP: couldn't encrypt to %s: %v\n", |
| 1110 |
recipient.Jid, err) |
| 1111 |
failure = err |
| 1112 |
continue |
| 1113 |
} |
| 1114 |
} |
| 1115 |
_, err = client.SendOrg(cryptMessage) |
| 1116 |
if err != nil { |
| 1117 |
cancel() |
| 1118 |
closeAndExit(client, err) |
| 1119 |
} |
| 1120 |
default: |
| 1121 |
slog.Info("sending message to", "JID", recipient.Jid) |
| 1122 |
_, err = client.Send(xmpp.Chat{ |
| 1123 |
Remote: recipient.Jid, |
| 1124 |
Type: msgType, Text: message, |
| 1125 |
Subject: *flagSubject, |
| 1126 |
}) |
| 1127 |
if err != nil { |
| 1128 |
cancel() |
| 1129 |
closeAndExit(client, err) |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
cancel() |
| 1135 |
if failure != nil { |
| 1136 |
closeAndExit(client, failure) |
| 1137 |
} |
| 1138 |
closeAndExit(client, nil) |
| 1139 |
} |