From 3b792117d974aa2ca042d49c7857b5553840dab2 Mon Sep 17 00:00:00 2001 From: Joel Falknau Date: Fri, 6 Dec 2024 19:59:47 +1000 Subject: [PATCH 01/14] Django Context Mumble Authenticator --- .pre-commit-config.yaml | 3 +- .../services/modules/mumble/MumbleServer.ice | 941 ++++++++++++++++++ .../services/modules/mumble/Murmur.ice | 927 +++++++++++++++++ allianceauth/services/modules/mumble/admin.py | 20 +- .../services/modules/mumble/authenticator.py | 580 +++++++++++ .../services/modules/mumble/managers.py | 61 ++ ...remove_mumbleuser_display_name_and_more.py | 66 ++ ...015_alter_idlerhandler_options_and_more.py | 121 +++ .../services/modules/mumble/models.py | 196 ++-- 9 files changed, 2823 insertions(+), 92 deletions(-) create mode 100644 allianceauth/services/modules/mumble/MumbleServer.ice create mode 100644 allianceauth/services/modules/mumble/Murmur.ice create mode 100644 allianceauth/services/modules/mumble/authenticator.py create mode 100644 allianceauth/services/modules/mumble/managers.py create mode 100644 allianceauth/services/modules/mumble/migrations/0014_idlerhandler_remove_mumbleuser_display_name_and_more.py create mode 100644 allianceauth/services/modules/mumble/migrations/0015_alter_idlerhandler_options_and_more.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04c74b56..42400cba 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -72,7 +72,8 @@ repos: allianceauth\/static\/allianceauth\/css\/themes\/bootstrap-locals.less| \.po| \.mo| - swagger\.json + swagger\.json| + \.ice ) - repo: https://github.com/igorshubovych/markdownlint-cli rev: v0.41.0 diff --git a/allianceauth/services/modules/mumble/MumbleServer.ice b/allianceauth/services/modules/mumble/MumbleServer.ice new file mode 100644 index 00000000..0d1d5cbb --- /dev/null +++ b/allianceauth/services/modules/mumble/MumbleServer.ice @@ -0,0 +1,941 @@ +// Copyright 2022-2023 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +/** + * + * Information and control of the murmur server. Each server has + * one {@link Meta} interface that controls global information, and + * each virtual server has a {@link Server} interface. + * + **/ + +#include + +module MumbleServer +{ + + /** A network address in IPv6 format. + **/ + ["python:seq:tuple"] sequence NetAddress; + + /** A connected user. + **/ + struct User { + /** Session ID. This identifies the connection to the server. */ + int session; + /** User ID. -1 if the user is anonymous. */ + int userid; + /** Is user muted by the server? */ + bool mute; + /** Is user deafened by the server? If true, this implies mute. */ + bool deaf; + /** Is the user suppressed by the server? This means the user is not muted, but does not have speech privileges in the current channel. */ + bool suppress; + /** Is the user a priority speaker? */ + bool prioritySpeaker; + /** Is the user self-muted? */ + bool selfMute; + /** Is the user self-deafened? If true, this implies mute. */ + bool selfDeaf; + /** Is the User recording? (This flag is read-only and cannot be changed using setState().) **/ + bool recording; + /** Channel ID the user is in. Matches {@link Channel.id}. */ + int channel; + /** The name of the user. */ + string name; + /** Seconds user has been online. */ + int onlinesecs; + /** Average transmission rate in bytes per second over the last few seconds. */ + int bytespersec; + /** Legacy client version. */ + int version; + /** New client version. (See https://github.com/mumble-voip/mumble/issues/5827) */ + long version2; + /** Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else. */ + string release; + /** Client OS. */ + string os; + /** Client OS Version. */ + string osversion; + /** Plugin Identity. This will be the user's unique ID inside the current game. */ + string identity; + /** + Base64-encoded Plugin context. This is a binary blob identifying the game and team the user is on. + + The used Base64 alphabet is the one specified in RFC 2045. + + Before Mumble 1.3.0, this string was not Base64-encoded. This could cause problems for some Ice + implementations, such as the .NET implementation. + + If you need the exact string that is used by Mumble, you can get it by Base64-decoding this string. + + If you simply need to detect whether two users are in the same game world, string comparisons will + continue to work as before. + */ + string context; + /** User comment. Shown as tooltip for this user. */ + string comment; + /** Client address. */ + NetAddress address; + /** TCP only. True until UDP connectivity is established. */ + bool tcponly; + /** Idle time. This is how many seconds it is since the user last spoke. Other activity is not counted. */ + int idlesecs; + /** UDP Ping Average. This is the average ping for the user via UDP over the duration of the connection. */ + float udpPing; + /** TCP Ping Average. This is the average ping for the user via TCP over the duration of the connection. */ + float tcpPing; + }; + + sequence IntList; + + /** A text message between users. + **/ + struct TextMessage { + /** Sessions (connected users) who were sent this message. */ + IntList sessions; + /** Channels who were sent this message. */ + IntList channels; + /** Trees of channels who were sent this message. */ + IntList trees; + /** The contents of the message. */ + string text; + }; + + /** A channel. + **/ + struct Channel { + /** Channel ID. This is unique per channel, and the root channel is always id 0. */ + int id; + /** Name of the channel. There can not be two channels with the same parent that has the same name. */ + string name; + /** ID of parent channel, or -1 if this is the root channel. */ + int parent; + /** List of id of linked channels. */ + IntList links; + /** Description of channel. Shown as tooltip for this channel. */ + string description; + /** Channel is temporary, and will be removed when the last user leaves it. */ + bool temporary; + /** Position of the channel which is used in Client for sorting. */ + int position; + }; + + /** A group. Groups are defined per channel, and can inherit members from parent channels. + **/ + struct Group { + /** Group name */ + string name; + /** Is this group inherited from a parent channel? Read-only. */ + bool inherited; + /** Does this group inherit members from parent channels? */ + bool inherit; + /** Can subchannels inherit members from this group? */ + bool inheritable; + /** List of users to add to the group. */ + IntList add; + /** List of inherited users to remove from the group. */ + IntList remove; + /** Current members of the group, including inherited members. Read-only. */ + IntList members; + }; + + /** Write access to channel control. Implies all other permissions (except Speak). */ + const int PermissionWrite = 0x01; + /** Traverse channel. Without this, a client cannot reach subchannels, no matter which privileges he has there. */ + const int PermissionTraverse = 0x02; + /** Enter channel. */ + const int PermissionEnter = 0x04; + /** Speak in channel. */ + const int PermissionSpeak = 0x08; + /** Whisper to channel. This is different from Speak, so you can set up different permissions. */ + const int PermissionWhisper = 0x100; + /** Mute and deafen other users in this channel. */ + const int PermissionMuteDeafen = 0x10; + /** Move users from channel. You need this permission in both the source and destination channel to move another user. */ + const int PermissionMove = 0x20; + /** Make new channel as a subchannel of this channel. */ + const int PermissionMakeChannel = 0x40; + /** Make new temporary channel as a subchannel of this channel. */ + const int PermissionMakeTempChannel = 0x400; + /** Link this channel. You need this permission in both the source and destination channel to link channels, or in either channel to unlink them. */ + const int PermissionLinkChannel = 0x80; + /** Send text message to channel. */ + const int PermissionTextMessage = 0x200; + /** Kick user from server. Only valid on root channel. */ + const int PermissionKick = 0x10000; + /** Ban user from server. Only valid on root channel. */ + const int PermissionBan = 0x20000; + /** Register and unregister users. Only valid on root channel. */ + const int PermissionRegister = 0x40000; + /** Register and unregister users. Only valid on root channel. */ + const int PermissionRegisterSelf = 0x80000; + /** Reset the comment or avatar of a user. Only valid on root channel. */ + const int ResetUserContent = 0x100000; + + + /** Access Control List for a channel. ACLs are defined per channel, and can be inherited from parent channels. + **/ + struct ACL { + /** Does the ACL apply to this channel? */ + bool applyHere; + /** Does the ACL apply to subchannels? */ + bool applySubs; + /** Is this ACL inherited from a parent channel? Read-only. */ + bool inherited; + /** ID of user this ACL applies to. -1 if using a group name. */ + int userid; + /** Group this ACL applies to. Blank if using userid. */ + string group; + /** Binary mask of privileges to allow. */ + int allow; + /** Binary mask of privileges to deny. */ + int deny; + }; + + /** A single ip mask for a ban. + **/ + struct Ban { + /** Address to ban. */ + NetAddress address; + /** Number of bits in ban to apply. */ + int bits; + /** Username associated with ban. */ + string name; + /** Hash of banned user. */ + string hash; + /** Reason for ban. */ + string reason; + /** Date ban was applied in unix time format. */ + int start; + /** Duration of ban. */ + int duration; + }; + + /** A entry in the log. + **/ + struct LogEntry { + /** Timestamp in UNIX time_t */ + int timestamp; + /** The log message. */ + string txt; + }; + + class Tree; + sequence TreeList; + + enum ChannelInfo { ChannelDescription, ChannelPosition }; + enum UserInfo { UserName, UserEmail, UserComment, UserHash, UserPassword, UserLastActive, UserKDFIterations }; + + dictionary UserMap; + dictionary ChannelMap; + sequence ChannelList; + sequence UserList; + sequence GroupList; + sequence ACLList; + sequence LogList; + sequence BanList; + sequence IdList; + sequence NameList; + dictionary NameMap; + dictionary IdMap; + sequence Texture; + dictionary ConfigMap; + sequence GroupNameList; + sequence CertificateDer; + sequence CertificateList; + + /** User information map. + * Older versions of ice-php can't handle enums as keys. If you are using one of these, replace 'UserInfo' with 'byte'. + */ + + dictionary UserInfoMap; + + /** User and subchannel state. Read-only. + **/ + class Tree { + /** Channel definition of current channel. */ + Channel c; + /** List of subchannels. */ + TreeList children; + /** Users in this channel. */ + UserList users; + }; + + exception MurmurException {}; + /** This is thrown when you specify an invalid session. This may happen if the user has disconnected since your last call to {@link Server.getUsers}. See {@link User.session} */ + exception InvalidSessionException extends MurmurException {}; + /** This is thrown when you specify an invalid channel id. This may happen if the channel was removed by another provess. It can also be thrown if you try to add an invalid channel. */ + exception InvalidChannelException extends MurmurException {}; + /** This is thrown when you try to do an operation on a server that does not exist. This may happen if someone has removed the server. */ + exception InvalidServerException extends MurmurException {}; + /** This happens if you try to fetch user or channel state on a stopped server, if you try to stop an already stopped server or start an already started server. */ + exception ServerBootedException extends MurmurException {}; + /** This is thrown if {@link Server.start} fails, and should generally be the cause for some concern. */ + exception ServerFailureException extends MurmurException {}; + /** This is thrown when you specify an invalid userid. */ + exception InvalidUserException extends MurmurException {}; + /** This is thrown when you try to set an invalid texture. */ + exception InvalidTextureException extends MurmurException {}; + /** This is thrown when you supply an invalid callback. */ + exception InvalidCallbackException extends MurmurException {}; + /** This is thrown when you supply the wrong secret in the calling context. */ + exception InvalidSecretException extends MurmurException {}; + /** This is thrown when the channel operation would exceed the channel nesting limit */ + exception NestingLimitException extends MurmurException {}; + /** This is thrown when you ask the server to disclose something that should be secret. */ + exception WriteOnlyException extends MurmurException {}; + /** This is thrown when invalid input data was specified. */ + exception InvalidInputDataException extends MurmurException {}; + + /** Callback interface for servers. You can supply an implementation of this to receive notification + * messages from the server. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + * Note that callbacks are removed when a server is stopped, so you should have a callback for + * {@link MetaCallback.started} which calls {@link Server.addCallback}. + * @see MetaCallback + * @see Server.addCallback + */ + interface ServerCallback { + /** Called when a user connects to the server. + * @param state State of connected user. + */ + idempotent void userConnected(User state); + /** Called when a user disconnects from the server. The user has already been removed, so you can no longer use methods like {@link Server.getState} + * to retrieve the user's state. + * @param state State of disconnected user. + */ + idempotent void userDisconnected(User state); + /** Called when a user state changes. This is called if the user moves, is renamed, is muted, deafened etc. + * @param state New state of user. + */ + idempotent void userStateChanged(User state); + /** Called when user writes a text message + * @param state the User sending the message + * @param message the TextMessage the user has sent + */ + idempotent void userTextMessage(User state, TextMessage message); + /** Called when a new channel is created. + * @param state State of new channel. + */ + idempotent void channelCreated(Channel state); + /** Called when a channel is removed. The channel has already been removed, you can no longer use methods like {@link Server.getChannelState} + * @param state State of removed channel. + */ + idempotent void channelRemoved(Channel state); + /** Called when a new channel state changes. This is called if the channel is moved, renamed or if new links are added. + * @param state New state of channel. + */ + idempotent void channelStateChanged(Channel state); + }; + + /** Context for actions in the Server menu. */ + const int ContextServer = 0x01; + /** Context for actions in the Channel menu. */ + const int ContextChannel = 0x02; + /** Context for actions in the User menu. */ + const int ContextUser = 0x04; + + /** Callback interface for context actions. You need to supply one of these for {@link Server.addContext}. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + */ + interface ServerContextCallback { + /** Called when a context action is performed. + * @param action Action to be performed. + * @param usr User which initiated the action. + * @param session If nonzero, session of target user. + * @param channelid If not -1, id of target channel. + */ + idempotent void contextAction(string action, User usr, int session, int channelid); + }; + + /** Callback interface for server authentication. You need to supply one of these for {@link Server.setAuthenticator}. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that unlike {@link ServerCallback} and {@link ServerContextCallback}, these methods are called + * synchronously. If the response lags, the entire murmur server will lag. + * Also note that, as the method calls are synchronous, making a call to {@link Server} or {@link Meta} will + * deadlock the server. + */ + interface ServerAuthenticator { + /** Called to authenticate a user. If you do not know the username in question, always return -2 from this + * method to fall through to normal database authentication. + * Note that if authentication succeeds, murmur will create a record of the user in it's database, reserving + * the username and id so it cannot be used for normal database authentication. + * The data in the certificate (name, email addresses etc), as well as the list of signing certificates, + * should only be trusted if certstrong is true. + * + * Internally, Murmur treats usernames as case-insensitive. It is recommended + * that authenticators do the same. Murmur checks if a username is in use when + * a user connects. If the connecting user is registered, the other username is + * kicked. If the connecting user is not registered, the connecting user is not + * allowed to join the server. + * + * @param name Username to authenticate. + * @param pw Password to authenticate with. + * @param certificates List of der encoded certificates the user connected with. + * @param certhash Hash of user certificate, as used by murmur internally when matching. + * @param certstrong True if certificate was valid and signed by a trusted CA. + * @param newname Set this to change the username from the supplied one. + * @param groups List of groups on the root channel that the user will be added to for the duration of the connection. + * @return UserID of authenticated user, -1 for authentication failures, -2 for unknown user (fallthrough), + * -3 for authentication failures where the data could (temporarily) not be verified. + */ + idempotent int authenticate(string name, string pw, CertificateList certificates, string certhash, bool certstrong, out string newname, out GroupNameList groups); + + /** Fetch information about a user. This is used to retrieve information like email address, keyhash etc. If you + * want murmur to take care of this information itself, simply return false to fall through. + * @param id User id. + * @param info Information about user. This needs to include at least "name". + * @return true if information is present, false to fall through. + */ + idempotent bool getInfo(int id, out UserInfoMap info); + + /** Map a name to a user id. + * @param name Username to map. + * @return User id or -2 for unknown name. + */ + idempotent int nameToId(string name); + + /** Map a user id to a username. + * @param id User id to map. + * @return Name of user or empty string for unknown id. + */ + idempotent string idToName(int id); + + /** Map a user to a custom Texture. + * @param id User id to map. + * @return User texture or an empty texture for unknown users or users without textures. + */ + idempotent Texture idToTexture(int id); + }; + + /** Callback interface for server authentication and registration. This allows you to support both authentication + * and account updating. + * You do not need to implement this if all you want is authentication, you only need this if other scripts + * connected to the same server calls e.g. {@link Server.setTexture}. + * Almost all of these methods support fall through, meaning murmur should continue the operation against its + * own database. + */ + interface ServerUpdatingAuthenticator extends ServerAuthenticator { + /** Register a new user. + * @param info Information about user to register. + * @return User id of new user, -1 for registration failure, or -2 to fall through. + */ + int registerUser(UserInfoMap info); + + /** Unregister a user. + * @param id Userid to unregister. + * @return 1 for successful unregistration, 0 for unsuccessful unregistration, -1 to fall through. + */ + int unregisterUser(int id); + + /** Get a list of registered users matching filter. + * @param filter Substring usernames must contain. If empty, return all registered users. + * @return List of matching registered users. + */ + idempotent NameMap getRegisteredUsers(string filter); + + /** Set additional information for user registration. + * @param id Userid of registered user. + * @param info Information to set about user. This should be merged with existing information. + * @return 1 for successful update, 0 for unsuccessful update, -1 to fall through. + */ + idempotent int setInfo(int id, UserInfoMap info); + + /** Set texture (now called avatar) of user registration. + * @param id registrationId of registered user. + * @param tex New texture. + * @return 1 for successful update, 0 for unsuccessful update, -1 to fall through. + */ + idempotent int setTexture(int id, Texture tex); + }; + + /** Per-server interface. This includes all methods for configuring and altering + * the state of a single virtual server. You can retrieve a pointer to this interface + * from one of the methods in {@link Meta}. + **/ + ["amd"] interface Server { + /** Shows if the server currently running (accepting users). + * + * @return Run-state of server. + */ + idempotent bool isRunning() throws InvalidSecretException; + + /** Start server. */ + void start() throws ServerBootedException, ServerFailureException, InvalidSecretException; + + /** Stop server. + * Note: Server will be restarted on Murmur restart unless explicitly disabled + * with setConf("boot", false) + */ + void stop() throws ServerBootedException, InvalidSecretException; + + /** Delete server and all it's configuration. */ + void delete() throws ServerBootedException, InvalidSecretException; + + /** Fetch the server id. + * + * @return Unique server id. + */ + idempotent int id() throws InvalidSecretException; + + /** Add a callback. The callback will receive notifications about changes to users and channels. + * + * @param cb Callback interface which will receive notifications. + * @see removeCallback + */ + void addCallback(ServerCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. + * @see addCallback + */ + void removeCallback(ServerCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Set external authenticator. If set, all authentications from clients are forwarded to this + * proxy. + * + * @param auth Authenticator object to perform subsequent authentications. + */ + void setAuthenticator(ServerAuthenticator *auth) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Retrieve configuration item. + * @param key Configuration key. + * @return Configuration value. If this is empty, see {@link Meta.getDefaultConf} + */ + idempotent string getConf(string key) throws InvalidSecretException, WriteOnlyException; + + /** Retrieve all configuration items. + * @return All configured values. If a value isn't set here, the value from {@link Meta.getDefaultConf} is used. + */ + idempotent ConfigMap getAllConf() throws InvalidSecretException; + + /** Set a configuration item. + * @param key Configuration key. + * @param value Configuration value. + */ + idempotent void setConf(string key, string value) throws InvalidSecretException; + + /** Set superuser password. This is just a convenience for using {@link updateRegistration} on user id 0. + * @param pw Password. + */ + idempotent void setSuperuserPassword(string pw) throws InvalidSecretException; + + /** Fetch log entries. + * @param first Lowest numbered entry to fetch. 0 is the most recent item. + * @param last Last entry to fetch. + * @return List of log entries. + */ + idempotent LogList getLog(int first, int last) throws InvalidSecretException; + + /** Fetch length of log + * @return Number of entries in log + */ + idempotent int getLogLen() throws InvalidSecretException; + + /** Fetch all users. This returns all currently connected users on the server. + * @return List of connected users. + * @see getState + */ + idempotent UserMap getUsers() throws ServerBootedException, InvalidSecretException; + + /** Fetch all channels. This returns all defined channels on the server. The root channel is always channel 0. + * @return List of defined channels. + * @see getChannelState + */ + idempotent ChannelMap getChannels() throws ServerBootedException, InvalidSecretException; + + /** Fetch certificate of user. This returns the complete certificate chain of a user. + * @param session Connection ID of user. See {@link User.session}. + * @return Certificate list of user. + */ + idempotent CertificateList getCertificateList(int session) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Fetch all channels and connected users as a tree. This retrieves an easy-to-use representation of the server + * as a tree. This is primarily used for viewing the state of the server on a webpage. + * @return Recursive tree of all channels and connected users. + */ + idempotent Tree getTree() throws ServerBootedException, InvalidSecretException; + + /** Fetch all current IP bans on the server. + * @return List of bans. + */ + idempotent BanList getBans() throws ServerBootedException, InvalidSecretException; + + /** Set all current IP bans on the server. This will replace any bans already present, so if you want to add a ban, be sure to call {@link getBans} and then + * append to the returned list before calling this method. + * @param bans List of bans. + */ + idempotent void setBans(BanList bans) throws ServerBootedException, InvalidSecretException; + + /** Kick a user. The user is not banned, and is free to rejoin the server. + * @param session Connection ID of user. See {@link User.session}. + * @param reason Text message to show when user is kicked. + */ + void kickUser(int session, string reason) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Get state of a single connected user. + * @param session Connection ID of user. See {@link User.session}. + * @return State of connected user. + * @see setState + * @see getUsers + */ + idempotent User getState(int session) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Set user state. You can use this to move, mute and deafen users. + * @param state User state to set. + * @see getState + */ + idempotent void setState(User state) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Send text message to a single user. + * @param session Connection ID of user. See {@link User.session}. + * @param text Message to send. + * @see sendMessageChannel + */ + void sendMessage(int session, string text) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Check if user is permitted to perform action. + * @param session Connection ID of user. See {@link User.session}. + * @param channelid ID of Channel. See {@link Channel.id}. + * @param perm Permission bits to check. + * @return true if any of the permissions in perm were set for the user. + */ + bool hasPermission(int session, int channelid, int perm) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Return users effective permissions + * @param session Connection ID of user. See {@link User.session}. + * @param channelid ID of Channel. See {@link Channel.id}. + * @return bitfield of allowed actions + */ + idempotent int effectivePermissions(int session, int channelid) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Add a context callback. This is done per user, and will add a context menu action for the user. + * + * @param session Session of user which should receive context entry. + * @param action Action string, a unique name to associate with the action. + * @param text Name of action shown to user. + * @param cb Callback interface which will receive notifications. + * @param ctx Context this should be used in. Needs to be one or a combination of {@link ContextServer}, {@link ContextChannel} and {@link ContextUser}. + * @see removeContextCallback + */ + void addContextCallback(int session, string action, string text, ServerContextCallback *cb, int ctx) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. This callback will be removed from all from all users. + * @see addContextCallback + */ + void removeContextCallback(ServerContextCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Get state of single channel. + * @param channelid ID of Channel. See {@link Channel.id}. + * @return State of channel. + * @see setChannelState + * @see getChannels + */ + idempotent Channel getChannelState(int channelid) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Set state of a single channel. You can use this to move or relink channels. + * @param state Channel state to set. + * @see getChannelState + */ + idempotent void setChannelState(Channel state) throws ServerBootedException, InvalidChannelException, InvalidSecretException, NestingLimitException; + + /** Remove a channel and all its subchannels. + * @param channelid ID of Channel. See {@link Channel.id}. + */ + void removeChannel(int channelid) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Add a new channel. + * @param name Name of new channel. + * @param parent Channel ID of parent channel. See {@link Channel.id}. + * @return ID of newly created channel. + */ + int addChannel(string name, int parent) throws ServerBootedException, InvalidChannelException, InvalidSecretException, NestingLimitException; + + /** Send text message to channel or a tree of channels. + * @param channelid Channel ID of channel to send to. See {@link Channel.id}. + * @param tree If true, the message will be sent to the channel and all its subchannels. + * @param text Message to send. + * @see sendMessage + */ + void sendMessageChannel(int channelid, bool tree, string text) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Retrieve ACLs and Groups on a channel. + * @param channelid Channel ID of channel to fetch from. See {@link Channel.id}. + * @param acls List of ACLs on the channel. This will include inherited ACLs. + * @param groups List of groups on the channel. This will include inherited groups. + * @param inherit Does this channel inherit ACLs from the parent channel? + */ + idempotent void getACL(int channelid, out ACLList acls, out GroupList groups, out bool inherit) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Set ACLs and Groups on a channel. Note that this will replace all existing ACLs and groups on the channel. + * @param channelid Channel ID of channel to fetch from. See {@link Channel.id}. + * @param acls List of ACLs on the channel. + * @param groups List of groups on the channel. + * @param inherit Should this channel inherit ACLs from the parent channel? + */ + idempotent void setACL(int channelid, ACLList acls, GroupList groups, bool inherit) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Temporarily add a user to a group on a channel. This state is not saved, and is intended for temporary memberships. + * @param channelid Channel ID of channel to add to. See {@link Channel.id}. + * @param session Connection ID of user. See {@link User.session}. + * @param group Group name to add to. + */ + idempotent void addUserToGroup(int channelid, int session, string group) throws ServerBootedException, InvalidChannelException, InvalidSessionException, InvalidSecretException; + + /** Remove a user from a temporary group membership on a channel. This state is not saved, and is intended for temporary memberships. + * @param channelid Channel ID of channel to add to. See {@link Channel.id}. + * @param session Connection ID of user. See {@link User.session}. + * @param group Group name to remove from. + */ + idempotent void removeUserFromGroup(int channelid, int session, string group) throws ServerBootedException, InvalidChannelException, InvalidSessionException, InvalidSecretException; + + /** Redirect whisper targets for user. If set, whenever a user tries to whisper to group "source", the whisper will be redirected to group "target". + * To remove a redirect pass an empty target string. This is intended for context groups. + * @param session Connection ID of user. See {@link User.session}. + * @param source Group name to redirect from. + * @param target Group name to redirect to. + */ + idempotent void redirectWhisperGroup(int session, string source, string target) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Map a list of {@link User.userid} to a matching name. + * @param List of ids. + * @return Matching list of names, with an empty string representing invalid or unknown ids. + */ + idempotent NameMap getUserNames(IdList ids) throws ServerBootedException, InvalidSecretException; + + /** Map a list of user names to a matching id. + * @param List of names. + * @reuturn List of matching ids, with -1 representing invalid or unknown user names. + */ + idempotent IdMap getUserIds(NameList names) throws ServerBootedException, InvalidSecretException; + + /** Register a new user. + * @param info Information about new user. Must include at least "name". + * @return The ID of the user. See {@link RegisteredUser.userid}. + */ + int registerUser(UserInfoMap info) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Remove a user registration. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + */ + void unregisterUser(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Update the registration for a user. You can use this to set the email or password of a user, + * and can also use it to change the user's name. + * @param registration Updated registration record. + */ + idempotent void updateRegistration(int userid, UserInfoMap info) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Fetch registration for a single user. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @return Registration record. + */ + idempotent UserInfoMap getRegistration(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Fetch a group of registered users. + * @param filter Substring of user name. If blank, will retrieve all registered users. + * @return List of registration records. + */ + idempotent NameMap getRegisteredUsers(string filter) throws ServerBootedException, InvalidSecretException; + + /** Verify the password of a user. You can use this to verify a user's credentials. + * @param name User name. See {@link RegisteredUser.name}. + * @param pw User password. + * @return User ID of registered user (See {@link RegisteredUser.userid}), -1 for failed authentication or -2 for unknown usernames. + */ + idempotent int verifyPassword(string name, string pw) throws ServerBootedException, InvalidSecretException; + + /** Fetch user texture. Textures are stored as zlib compress()ed 600x60 32-bit BGRA data. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @return Custom texture associated with user or an empty texture. + */ + idempotent Texture getTexture(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Set a user texture (now called avatar). + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @param tex Texture (as a Byte-Array) to set for the user, or an empty texture to remove the existing texture. + */ + idempotent void setTexture(int userid, Texture tex) throws ServerBootedException, InvalidUserException, InvalidTextureException, InvalidSecretException; + + /** Get virtual server uptime. + * @return Uptime of the virtual server in seconds + */ + idempotent int getUptime() throws ServerBootedException, InvalidSecretException; + + /** + * Update the server's certificate information. + * + * Reconfigure the running server's TLS socket with the given + * certificate and private key. + * + * The certificate and and private key must be PEM formatted. + * + * New clients will see the new certificate. + * Existing clients will continue to see the certificate the server + * was using when they connected to it. + * + * This method throws InvalidInputDataException if any of the + * following errors happen: + * - Unable to decode the PEM certificate and/or private key. + * - Unable to decrypt the private key with the given passphrase. + * - The certificate and/or private key do not contain RSA keys. + * - The certificate is not usable with the given private key. + */ + idempotent void updateCertificate(string certificate, string privateKey, string passphrase) throws ServerBootedException, InvalidSecretException, InvalidInputDataException; + + /** + * Makes the given user start listening to the given channel. + * @param userid The ID of the user + * @param channelid The ID of the channel + */ + idempotent void startListening(int userid, int channelid); + + /** + * Makes the given user stop listening to the given channel. + * @param userid The ID of the user + * @param channelid The ID of the channel + */ + idempotent void stopListening(int userid, int channelid); + + /** + * @param userid The ID of the user + * @param channelid The ID of the channel + * @returns Whether the given user is currently listening to the given channel + */ + idempotent bool isListening(int userid, int channelid); + + /** + * @param userid The ID of the user + * @returns An ID-list of channels the given user is listening to + */ + idempotent IntList getListeningChannels(int userid); + + /** + * @param channelid The ID of the channel + * @returns An ID-list of users listening to the given channel + */ + idempotent IntList getListeningUsers(int channelid); + + /** + * @param channelid The ID of the channel + * @param userid The ID of the user + * @returns The volume adjustment set for a listener of the given user in the given channel + */ + idempotent float getListenerVolumeAdjustment(int channelid, int userid); + + /** + * Sets the volume adjustment set for a listener of the given user in the given channel + * @param channelid The ID of the channel + * @param userid The ID of the user + */ + idempotent void setListenerVolumeAdjustment(int channelid, int userid, float volumeAdjustment); + + /** + * @param receiverUserIDs list of IDs of the users the message shall be sent to + */ + idempotent void sendWelcomeMessage(IdList receiverUserIDs); + }; + + /** Callback interface for Meta. You can supply an implementation of this to receive notifications + * when servers are stopped or started. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + * @see ServerCallback + * @see Meta.addCallback + */ + interface MetaCallback { + /** Called when a server is started. The server is up and running when this event is sent, so all methods that + * need a running server will work. + * @param srv Interface for started server. + */ + void started(Server *srv); + + /** Called when a server is stopped. The server is already stopped when this event is sent, so no methods that + * need a running server will work. + * @param srv Interface for started server. + */ + void stopped(Server *srv); + }; + + sequence ServerList; + + /** This is the meta interface. It is primarily used for retrieving the {@link Server} interfaces for each individual server. + **/ + ["amd"] interface Meta { + /** Fetch interface to specific server. + * @param id Server ID. See {@link Server.getId}. + * @return Interface for specified server, or a null proxy if id is invalid. + */ + idempotent Server *getServer(int id) throws InvalidSecretException; + + /** Create a new server. Call {@link Server.getId} on the returned interface to find it's ID. + * @return Interface for new server. + */ + Server *newServer() throws InvalidSecretException; + + /** Fetch list of all currently running servers. + * @return List of interfaces for running servers. + */ + idempotent ServerList getBootedServers() throws InvalidSecretException; + + /** Fetch list of all defined servers. + * @return List of interfaces for all servers. + */ + idempotent ServerList getAllServers() throws InvalidSecretException; + + /** Fetch default configuration. This returns the configuration items that were set in the configuration file, or + * the built-in default. The individual servers will use these values unless they have been overridden in the + * server specific configuration. The only special case is the port, which defaults to the value defined here + + * the servers ID - 1 (so that virtual server #1 uses the defined port, server #2 uses port+1 etc). + * @return Default configuration of the servers. + */ + idempotent ConfigMap getDefaultConf() throws InvalidSecretException; + + /** Fetch version of Murmur. + * @param major Major version. + * @param minor Minor version. + * @param patch Patchlevel. + * @param text Textual representation of version. Note that this may not match the {@link major}, {@link minor} and {@link patch} levels, as it + * may be simply the compile date or the SVN revision. This is usually the text you want to present to users. + */ + idempotent void getVersion(out int major, out int minor, out int patch, out string text); + + /** Add a callback. The callback will receive notifications when servers are started or stopped. + * + * @param cb Callback interface which will receive notifications. + */ + void addCallback(MetaCallback *cb) throws InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. + */ + void removeCallback(MetaCallback *cb) throws InvalidCallbackException, InvalidSecretException; + + /** Get murmur uptime. + * @return Uptime of murmur in seconds + */ + idempotent int getUptime(); + + /** Get slice file. + * @return Contents of the slice file server compiled with. + */ + idempotent string getSlice(); + + /** Returns a checksum dict for the slice file. + * @return Checksum dict + */ + idempotent Ice::SliceChecksumDict getSliceChecksums(); + }; +}; diff --git a/allianceauth/services/modules/mumble/Murmur.ice b/allianceauth/services/modules/mumble/Murmur.ice new file mode 100644 index 00000000..f404a2af --- /dev/null +++ b/allianceauth/services/modules/mumble/Murmur.ice @@ -0,0 +1,927 @@ +// Copyright 2008-2021 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +/** + * + * Information and control of the murmur server. Each server has + * one {@link Meta} interface that controls global information, and + * each virtual server has a {@link Server} interface. + * + **/ + +#include + +module Murmur +{ + + /** A network address in IPv6 format. + **/ + ["python:seq:tuple"] sequence NetAddress; + + /** A connected user. + **/ + struct User { + /** Session ID. This identifies the connection to the server. */ + int session; + /** User ID. -1 if the user is anonymous. */ + int userid; + /** Is user muted by the server? */ + bool mute; + /** Is user deafened by the server? If true, this implies mute. */ + bool deaf; + /** Is the user suppressed by the server? This means the user is not muted, but does not have speech privileges in the current channel. */ + bool suppress; + /** Is the user a priority speaker? */ + bool prioritySpeaker; + /** Is the user self-muted? */ + bool selfMute; + /** Is the user self-deafened? If true, this implies mute. */ + bool selfDeaf; + /** Is the User recording? (This flag is read-only and cannot be changed using setState().) **/ + bool recording; + /** Channel ID the user is in. Matches {@link Channel.id}. */ + int channel; + /** The name of the user. */ + string name; + /** Seconds user has been online. */ + int onlinesecs; + /** Average transmission rate in bytes per second over the last few seconds. */ + int bytespersec; + /** Legacy client version. */ + int version; + /** New client version. (See https://github.com/mumble-voip/mumble/issues/5827) */ + long version2; + /** Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else. */ + string release; + /** Client OS. */ + string os; + /** Client OS Version. */ + string osversion; + /** Plugin Identity. This will be the user's unique ID inside the current game. */ + string identity; + /** + Base64-encoded Plugin context. This is a binary blob identifying the game and team the user is on. + + The used Base64 alphabet is the one specified in RFC 2045. + + Before Mumble 1.3.0, this string was not Base64-encoded. This could cause problems for some Ice + implementations, such as the .NET implementation. + + If you need the exact string that is used by Mumble, you can get it by Base64-decoding this string. + + If you simply need to detect whether two users are in the same game world, string comparisons will + continue to work as before. + */ + string context; + /** User comment. Shown as tooltip for this user. */ + string comment; + /** Client address. */ + NetAddress address; + /** TCP only. True until UDP connectivity is established. */ + bool tcponly; + /** Idle time. This is how many seconds it is since the user last spoke. Other activity is not counted. */ + int idlesecs; + /** UDP Ping Average. This is the average ping for the user via UDP over the duration of the connection. */ + float udpPing; + /** TCP Ping Average. This is the average ping for the user via TCP over the duration of the connection. */ + float tcpPing; + }; + + sequence IntList; + + /** A text message between users. + **/ + struct TextMessage { + /** Sessions (connected users) who were sent this message. */ + IntList sessions; + /** Channels who were sent this message. */ + IntList channels; + /** Trees of channels who were sent this message. */ + IntList trees; + /** The contents of the message. */ + string text; + }; + + /** A channel. + **/ + struct Channel { + /** Channel ID. This is unique per channel, and the root channel is always id 0. */ + int id; + /** Name of the channel. There can not be two channels with the same parent that has the same name. */ + string name; + /** ID of parent channel, or -1 if this is the root channel. */ + int parent; + /** List of id of linked channels. */ + IntList links; + /** Description of channel. Shown as tooltip for this channel. */ + string description; + /** Channel is temporary, and will be removed when the last user leaves it. */ + bool temporary; + /** Position of the channel which is used in Client for sorting. */ + int position; + }; + + /** A group. Groups are defined per channel, and can inherit members from parent channels. + **/ + struct Group { + /** Group name */ + string name; + /** Is this group inherited from a parent channel? Read-only. */ + bool inherited; + /** Does this group inherit members from parent channels? */ + bool inherit; + /** Can subchannels inherit members from this group? */ + bool inheritable; + /** List of users to add to the group. */ + IntList add; + /** List of inherited users to remove from the group. */ + IntList remove; + /** Current members of the group, including inherited members. Read-only. */ + IntList members; + }; + + /** Write access to channel control. Implies all other permissions (except Speak). */ + const int PermissionWrite = 0x01; + /** Traverse channel. Without this, a client cannot reach subchannels, no matter which privileges he has there. */ + const int PermissionTraverse = 0x02; + /** Enter channel. */ + const int PermissionEnter = 0x04; + /** Speak in channel. */ + const int PermissionSpeak = 0x08; + /** Whisper to channel. This is different from Speak, so you can set up different permissions. */ + const int PermissionWhisper = 0x100; + /** Mute and deafen other users in this channel. */ + const int PermissionMuteDeafen = 0x10; + /** Move users from channel. You need this permission in both the source and destination channel to move another user. */ + const int PermissionMove = 0x20; + /** Make new channel as a subchannel of this channel. */ + const int PermissionMakeChannel = 0x40; + /** Make new temporary channel as a subchannel of this channel. */ + const int PermissionMakeTempChannel = 0x400; + /** Link this channel. You need this permission in both the source and destination channel to link channels, or in either channel to unlink them. */ + const int PermissionLinkChannel = 0x80; + /** Send text message to channel. */ + const int PermissionTextMessage = 0x200; + /** Kick user from server. Only valid on root channel. */ + const int PermissionKick = 0x10000; + /** Ban user from server. Only valid on root channel. */ + const int PermissionBan = 0x20000; + /** Register and unregister users. Only valid on root channel. */ + const int PermissionRegister = 0x40000; + /** Register and unregister users. Only valid on root channel. */ + const int PermissionRegisterSelf = 0x80000; + /** Reset the comment or avatar of a user. Only valid on root channel. */ + const int ResetUserContent = 0x100000; + + + /** Access Control List for a channel. ACLs are defined per channel, and can be inherited from parent channels. + **/ + struct ACL { + /** Does the ACL apply to this channel? */ + bool applyHere; + /** Does the ACL apply to subchannels? */ + bool applySubs; + /** Is this ACL inherited from a parent channel? Read-only. */ + bool inherited; + /** ID of user this ACL applies to. -1 if using a group name. */ + int userid; + /** Group this ACL applies to. Blank if using userid. */ + string group; + /** Binary mask of privileges to allow. */ + int allow; + /** Binary mask of privileges to deny. */ + int deny; + }; + + /** A single ip mask for a ban. + **/ + struct Ban { + /** Address to ban. */ + NetAddress address; + /** Number of bits in ban to apply. */ + int bits; + /** Username associated with ban. */ + string name; + /** Hash of banned user. */ + string hash; + /** Reason for ban. */ + string reason; + /** Date ban was applied in unix time format. */ + int start; + /** Duration of ban. */ + int duration; + }; + + /** A entry in the log. + **/ + struct LogEntry { + /** Timestamp in UNIX time_t */ + int timestamp; + /** The log message. */ + string txt; + }; + + class Tree; + sequence TreeList; + + enum ChannelInfo { ChannelDescription, ChannelPosition }; + enum UserInfo { UserName, UserEmail, UserComment, UserHash, UserPassword, UserLastActive, UserKDFIterations }; + + dictionary UserMap; + dictionary ChannelMap; + sequence ChannelList; + sequence UserList; + sequence GroupList; + sequence ACLList; + sequence LogList; + sequence BanList; + sequence IdList; + sequence NameList; + dictionary NameMap; + dictionary IdMap; + sequence Texture; + dictionary ConfigMap; + sequence GroupNameList; + sequence CertificateDer; + sequence CertificateList; + + /** User information map. + * Older versions of ice-php can't handle enums as keys. If you are using one of these, replace 'UserInfo' with 'byte'. + */ + + dictionary UserInfoMap; + + /** User and subchannel state. Read-only. + **/ + class Tree { + /** Channel definition of current channel. */ + Channel c; + /** List of subchannels. */ + TreeList children; + /** Users in this channel. */ + UserList users; + }; + + exception MurmurException {}; + /** This is thrown when you specify an invalid session. This may happen if the user has disconnected since your last call to {@link Server.getUsers}. See {@link User.session} */ + exception InvalidSessionException extends MurmurException {}; + /** This is thrown when you specify an invalid channel id. This may happen if the channel was removed by another provess. It can also be thrown if you try to add an invalid channel. */ + exception InvalidChannelException extends MurmurException {}; + /** This is thrown when you try to do an operation on a server that does not exist. This may happen if someone has removed the server. */ + exception InvalidServerException extends MurmurException {}; + /** This happens if you try to fetch user or channel state on a stopped server, if you try to stop an already stopped server or start an already started server. */ + exception ServerBootedException extends MurmurException {}; + /** This is thrown if {@link Server.start} fails, and should generally be the cause for some concern. */ + exception ServerFailureException extends MurmurException {}; + /** This is thrown when you specify an invalid userid. */ + exception InvalidUserException extends MurmurException {}; + /** This is thrown when you try to set an invalid texture. */ + exception InvalidTextureException extends MurmurException {}; + /** This is thrown when you supply an invalid callback. */ + exception InvalidCallbackException extends MurmurException {}; + /** This is thrown when you supply the wrong secret in the calling context. */ + exception InvalidSecretException extends MurmurException {}; + /** This is thrown when the channel operation would exceed the channel nesting limit */ + exception NestingLimitException extends MurmurException {}; + /** This is thrown when you ask the server to disclose something that should be secret. */ + exception WriteOnlyException extends MurmurException {}; + /** This is thrown when invalid input data was specified. */ + exception InvalidInputDataException extends MurmurException {}; + + /** Callback interface for servers. You can supply an implementation of this to receive notification + * messages from the server. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + * Note that callbacks are removed when a server is stopped, so you should have a callback for + * {@link MetaCallback.started} which calls {@link Server.addCallback}. + * @see MetaCallback + * @see Server.addCallback + */ + interface ServerCallback { + /** Called when a user connects to the server. + * @param state State of connected user. + */ + idempotent void userConnected(User state); + /** Called when a user disconnects from the server. The user has already been removed, so you can no longer use methods like {@link Server.getState} + * to retrieve the user's state. + * @param state State of disconnected user. + */ + idempotent void userDisconnected(User state); + /** Called when a user state changes. This is called if the user moves, is renamed, is muted, deafened etc. + * @param state New state of user. + */ + idempotent void userStateChanged(User state); + /** Called when user writes a text message + * @param state the User sending the message + * @param message the TextMessage the user has sent + */ + idempotent void userTextMessage(User state, TextMessage message); + /** Called when a new channel is created. + * @param state State of new channel. + */ + idempotent void channelCreated(Channel state); + /** Called when a channel is removed. The channel has already been removed, you can no longer use methods like {@link Server.getChannelState} + * @param state State of removed channel. + */ + idempotent void channelRemoved(Channel state); + /** Called when a new channel state changes. This is called if the channel is moved, renamed or if new links are added. + * @param state New state of channel. + */ + idempotent void channelStateChanged(Channel state); + }; + + /** Context for actions in the Server menu. */ + const int ContextServer = 0x01; + /** Context for actions in the Channel menu. */ + const int ContextChannel = 0x02; + /** Context for actions in the User menu. */ + const int ContextUser = 0x04; + + /** Callback interface for context actions. You need to supply one of these for {@link Server.addContext}. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + */ + interface ServerContextCallback { + /** Called when a context action is performed. + * @param action Action to be performed. + * @param usr User which initiated the action. + * @param session If nonzero, session of target user. + * @param channelid If not -1, id of target channel. + */ + idempotent void contextAction(string action, User usr, int session, int channelid); + }; + + /** Callback interface for server authentication. You need to supply one of these for {@link Server.setAuthenticator}. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that unlike {@link ServerCallback} and {@link ServerContextCallback}, these methods are called + * synchronously. If the response lags, the entire murmur server will lag. + * Also note that, as the method calls are synchronous, making a call to {@link Server} or {@link Meta} will + * deadlock the server. + */ + interface ServerAuthenticator { + /** Called to authenticate a user. If you do not know the username in question, always return -2 from this + * method to fall through to normal database authentication. + * Note that if authentication succeeds, murmur will create a record of the user in it's database, reserving + * the username and id so it cannot be used for normal database authentication. + * The data in the certificate (name, email addresses etc), as well as the list of signing certificates, + * should only be trusted if certstrong is true. + * + * Internally, Murmur treats usernames as case-insensitive. It is recommended + * that authenticators do the same. Murmur checks if a username is in use when + * a user connects. If the connecting user is registered, the other username is + * kicked. If the connecting user is not registered, the connecting user is not + * allowed to join the server. + * + * @param name Username to authenticate. + * @param pw Password to authenticate with. + * @param certificates List of der encoded certificates the user connected with. + * @param certhash Hash of user certificate, as used by murmur internally when matching. + * @param certstrong True if certificate was valid and signed by a trusted CA. + * @param newname Set this to change the username from the supplied one. + * @param groups List of groups on the root channel that the user will be added to for the duration of the connection. + * @return UserID of authenticated user, -1 for authentication failures, -2 for unknown user (fallthrough), + * -3 for authentication failures where the data could (temporarily) not be verified. + */ + idempotent int authenticate(string name, string pw, CertificateList certificates, string certhash, bool certstrong, out string newname, out GroupNameList groups); + + /** Fetch information about a user. This is used to retrieve information like email address, keyhash etc. If you + * want murmur to take care of this information itself, simply return false to fall through. + * @param id User id. + * @param info Information about user. This needs to include at least "name". + * @return true if information is present, false to fall through. + */ + idempotent bool getInfo(int id, out UserInfoMap info); + + /** Map a name to a user id. + * @param name Username to map. + * @return User id or -2 for unknown name. + */ + idempotent int nameToId(string name); + + /** Map a user id to a username. + * @param id User id to map. + * @return Name of user or empty string for unknown id. + */ + idempotent string idToName(int id); + + /** Map a user to a custom Texture. + * @param id User id to map. + * @return User texture or an empty texture for unknown users or users without textures. + */ + idempotent Texture idToTexture(int id); + }; + + /** Callback interface for server authentication and registration. This allows you to support both authentication + * and account updating. + * You do not need to implement this if all you want is authentication, you only need this if other scripts + * connected to the same server calls e.g. {@link Server.setTexture}. + * Almost all of these methods support fall through, meaning murmur should continue the operation against its + * own database. + */ + interface ServerUpdatingAuthenticator extends ServerAuthenticator { + /** Register a new user. + * @param info Information about user to register. + * @return User id of new user, -1 for registration failure, or -2 to fall through. + */ + int registerUser(UserInfoMap info); + + /** Unregister a user. + * @param id Userid to unregister. + * @return 1 for successful unregistration, 0 for unsuccessful unregistration, -1 to fall through. + */ + int unregisterUser(int id); + + /** Get a list of registered users matching filter. + * @param filter Substring usernames must contain. If empty, return all registered users. + * @return List of matching registered users. + */ + idempotent NameMap getRegisteredUsers(string filter); + + /** Set additional information for user registration. + * @param id Userid of registered user. + * @param info Information to set about user. This should be merged with existing information. + * @return 1 for successful update, 0 for unsuccessful update, -1 to fall through. + */ + idempotent int setInfo(int id, UserInfoMap info); + + /** Set texture (now called avatar) of user registration. + * @param id registrationId of registered user. + * @param tex New texture. + * @return 1 for successful update, 0 for unsuccessful update, -1 to fall through. + */ + idempotent int setTexture(int id, Texture tex); + }; + + /** Per-server interface. This includes all methods for configuring and altering + * the state of a single virtual server. You can retrieve a pointer to this interface + * from one of the methods in {@link Meta}. + **/ + ["amd"] interface Server { + /** Shows if the server currently running (accepting users). + * + * @return Run-state of server. + */ + idempotent bool isRunning() throws InvalidSecretException; + + /** Start server. */ + void start() throws ServerBootedException, ServerFailureException, InvalidSecretException; + + /** Stop server. + * Note: Server will be restarted on Murmur restart unless explicitly disabled + * with setConf("boot", false) + */ + void stop() throws ServerBootedException, InvalidSecretException; + + /** Delete server and all it's configuration. */ + void delete() throws ServerBootedException, InvalidSecretException; + + /** Fetch the server id. + * + * @return Unique server id. + */ + idempotent int id() throws InvalidSecretException; + + /** Add a callback. The callback will receive notifications about changes to users and channels. + * + * @param cb Callback interface which will receive notifications. + * @see removeCallback + */ + void addCallback(ServerCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. + * @see addCallback + */ + void removeCallback(ServerCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Set external authenticator. If set, all authentications from clients are forwarded to this + * proxy. + * + * @param auth Authenticator object to perform subsequent authentications. + */ + void setAuthenticator(ServerAuthenticator *auth) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Retrieve configuration item. + * @param key Configuration key. + * @return Configuration value. If this is empty, see {@link Meta.getDefaultConf} + */ + idempotent string getConf(string key) throws InvalidSecretException, WriteOnlyException; + + /** Retrieve all configuration items. + * @return All configured values. If a value isn't set here, the value from {@link Meta.getDefaultConf} is used. + */ + idempotent ConfigMap getAllConf() throws InvalidSecretException; + + /** Set a configuration item. + * @param key Configuration key. + * @param value Configuration value. + */ + idempotent void setConf(string key, string value) throws InvalidSecretException; + + /** Set superuser password. This is just a convenience for using {@link updateRegistration} on user id 0. + * @param pw Password. + */ + idempotent void setSuperuserPassword(string pw) throws InvalidSecretException; + + /** Fetch log entries. + * @param first Lowest numbered entry to fetch. 0 is the most recent item. + * @param last Last entry to fetch. + * @return List of log entries. + */ + idempotent LogList getLog(int first, int last) throws InvalidSecretException; + + /** Fetch length of log + * @return Number of entries in log + */ + idempotent int getLogLen() throws InvalidSecretException; + + /** Fetch all users. This returns all currently connected users on the server. + * @return List of connected users. + * @see getState + */ + idempotent UserMap getUsers() throws ServerBootedException, InvalidSecretException; + + /** Fetch all channels. This returns all defined channels on the server. The root channel is always channel 0. + * @return List of defined channels. + * @see getChannelState + */ + idempotent ChannelMap getChannels() throws ServerBootedException, InvalidSecretException; + + /** Fetch certificate of user. This returns the complete certificate chain of a user. + * @param session Connection ID of user. See {@link User.session}. + * @return Certificate list of user. + */ + idempotent CertificateList getCertificateList(int session) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Fetch all channels and connected users as a tree. This retrieves an easy-to-use representation of the server + * as a tree. This is primarily used for viewing the state of the server on a webpage. + * @return Recursive tree of all channels and connected users. + */ + idempotent Tree getTree() throws ServerBootedException, InvalidSecretException; + + /** Fetch all current IP bans on the server. + * @return List of bans. + */ + idempotent BanList getBans() throws ServerBootedException, InvalidSecretException; + + /** Set all current IP bans on the server. This will replace any bans already present, so if you want to add a ban, be sure to call {@link getBans} and then + * append to the returned list before calling this method. + * @param bans List of bans. + */ + idempotent void setBans(BanList bans) throws ServerBootedException, InvalidSecretException; + + /** Kick a user. The user is not banned, and is free to rejoin the server. + * @param session Connection ID of user. See {@link User.session}. + * @param reason Text message to show when user is kicked. + */ + void kickUser(int session, string reason) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Get state of a single connected user. + * @param session Connection ID of user. See {@link User.session}. + * @return State of connected user. + * @see setState + * @see getUsers + */ + idempotent User getState(int session) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Set user state. You can use this to move, mute and deafen users. + * @param state User state to set. + * @see getState + */ + idempotent void setState(User state) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Send text message to a single user. + * @param session Connection ID of user. See {@link User.session}. + * @param text Message to send. + * @see sendMessageChannel + */ + void sendMessage(int session, string text) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Check if user is permitted to perform action. + * @param session Connection ID of user. See {@link User.session}. + * @param channelid ID of Channel. See {@link Channel.id}. + * @param perm Permission bits to check. + * @return true if any of the permissions in perm were set for the user. + */ + bool hasPermission(int session, int channelid, int perm) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Return users effective permissions + * @param session Connection ID of user. See {@link User.session}. + * @param channelid ID of Channel. See {@link Channel.id}. + * @return bitfield of allowed actions + */ + idempotent int effectivePermissions(int session, int channelid) throws ServerBootedException, InvalidSessionException, InvalidChannelException, InvalidSecretException; + + /** Add a context callback. This is done per user, and will add a context menu action for the user. + * + * @param session Session of user which should receive context entry. + * @param action Action string, a unique name to associate with the action. + * @param text Name of action shown to user. + * @param cb Callback interface which will receive notifications. + * @param ctx Context this should be used in. Needs to be one or a combination of {@link ContextServer}, {@link ContextChannel} and {@link ContextUser}. + * @see removeContextCallback + */ + void addContextCallback(int session, string action, string text, ServerContextCallback *cb, int ctx) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. This callback will be removed from all from all users. + * @see addContextCallback + */ + void removeContextCallback(ServerContextCallback *cb) throws ServerBootedException, InvalidCallbackException, InvalidSecretException; + + /** Get state of single channel. + * @param channelid ID of Channel. See {@link Channel.id}. + * @return State of channel. + * @see setChannelState + * @see getChannels + */ + idempotent Channel getChannelState(int channelid) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Set state of a single channel. You can use this to move or relink channels. + * @param state Channel state to set. + * @see getChannelState + */ + idempotent void setChannelState(Channel state) throws ServerBootedException, InvalidChannelException, InvalidSecretException, NestingLimitException; + + /** Remove a channel and all its subchannels. + * @param channelid ID of Channel. See {@link Channel.id}. + */ + void removeChannel(int channelid) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Add a new channel. + * @param name Name of new channel. + * @param parent Channel ID of parent channel. See {@link Channel.id}. + * @return ID of newly created channel. + */ + int addChannel(string name, int parent) throws ServerBootedException, InvalidChannelException, InvalidSecretException, NestingLimitException; + + /** Send text message to channel or a tree of channels. + * @param channelid Channel ID of channel to send to. See {@link Channel.id}. + * @param tree If true, the message will be sent to the channel and all its subchannels. + * @param text Message to send. + * @see sendMessage + */ + void sendMessageChannel(int channelid, bool tree, string text) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Retrieve ACLs and Groups on a channel. + * @param channelid Channel ID of channel to fetch from. See {@link Channel.id}. + * @param acls List of ACLs on the channel. This will include inherited ACLs. + * @param groups List of groups on the channel. This will include inherited groups. + * @param inherit Does this channel inherit ACLs from the parent channel? + */ + idempotent void getACL(int channelid, out ACLList acls, out GroupList groups, out bool inherit) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Set ACLs and Groups on a channel. Note that this will replace all existing ACLs and groups on the channel. + * @param channelid Channel ID of channel to fetch from. See {@link Channel.id}. + * @param acls List of ACLs on the channel. + * @param groups List of groups on the channel. + * @param inherit Should this channel inherit ACLs from the parent channel? + */ + idempotent void setACL(int channelid, ACLList acls, GroupList groups, bool inherit) throws ServerBootedException, InvalidChannelException, InvalidSecretException; + + /** Temporarily add a user to a group on a channel. This state is not saved, and is intended for temporary memberships. + * @param channelid Channel ID of channel to add to. See {@link Channel.id}. + * @param session Connection ID of user. See {@link User.session}. + * @param group Group name to add to. + */ + idempotent void addUserToGroup(int channelid, int session, string group) throws ServerBootedException, InvalidChannelException, InvalidSessionException, InvalidSecretException; + + /** Remove a user from a temporary group membership on a channel. This state is not saved, and is intended for temporary memberships. + * @param channelid Channel ID of channel to add to. See {@link Channel.id}. + * @param session Connection ID of user. See {@link User.session}. + * @param group Group name to remove from. + */ + idempotent void removeUserFromGroup(int channelid, int session, string group) throws ServerBootedException, InvalidChannelException, InvalidSessionException, InvalidSecretException; + + /** Redirect whisper targets for user. If set, whenever a user tries to whisper to group "source", the whisper will be redirected to group "target". + * To remove a redirect pass an empty target string. This is intended for context groups. + * @param session Connection ID of user. See {@link User.session}. + * @param source Group name to redirect from. + * @param target Group name to redirect to. + */ + idempotent void redirectWhisperGroup(int session, string source, string target) throws ServerBootedException, InvalidSessionException, InvalidSecretException; + + /** Map a list of {@link User.userid} to a matching name. + * @param List of ids. + * @return Matching list of names, with an empty string representing invalid or unknown ids. + */ + idempotent NameMap getUserNames(IdList ids) throws ServerBootedException, InvalidSecretException; + + /** Map a list of user names to a matching id. + * @param List of names. + * @reuturn List of matching ids, with -1 representing invalid or unknown user names. + */ + idempotent IdMap getUserIds(NameList names) throws ServerBootedException, InvalidSecretException; + + /** Register a new user. + * @param info Information about new user. Must include at least "name". + * @return The ID of the user. See {@link RegisteredUser.userid}. + */ + int registerUser(UserInfoMap info) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Remove a user registration. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + */ + void unregisterUser(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Update the registration for a user. You can use this to set the email or password of a user, + * and can also use it to change the user's name. + * @param registration Updated registration record. + */ + idempotent void updateRegistration(int userid, UserInfoMap info) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Fetch registration for a single user. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @return Registration record. + */ + idempotent UserInfoMap getRegistration(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Fetch a group of registered users. + * @param filter Substring of user name. If blank, will retrieve all registered users. + * @return List of registration records. + */ + idempotent NameMap getRegisteredUsers(string filter) throws ServerBootedException, InvalidSecretException; + + /** Verify the password of a user. You can use this to verify a user's credentials. + * @param name User name. See {@link RegisteredUser.name}. + * @param pw User password. + * @return User ID of registered user (See {@link RegisteredUser.userid}), -1 for failed authentication or -2 for unknown usernames. + */ + idempotent int verifyPassword(string name, string pw) throws ServerBootedException, InvalidSecretException; + + /** Fetch user texture. Textures are stored as zlib compress()ed 600x60 32-bit BGRA data. + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @return Custom texture associated with user or an empty texture. + */ + idempotent Texture getTexture(int userid) throws ServerBootedException, InvalidUserException, InvalidSecretException; + + /** Set a user texture (now called avatar). + * @param userid ID of registered user. See {@link RegisteredUser.userid}. + * @param tex Texture (as a Byte-Array) to set for the user, or an empty texture to remove the existing texture. + */ + idempotent void setTexture(int userid, Texture tex) throws ServerBootedException, InvalidUserException, InvalidTextureException, InvalidSecretException; + + /** Get virtual server uptime. + * @return Uptime of the virtual server in seconds + */ + idempotent int getUptime() throws ServerBootedException, InvalidSecretException; + + /** + * Update the server's certificate information. + * + * Reconfigure the running server's TLS socket with the given + * certificate and private key. + * + * The certificate and and private key must be PEM formatted. + * + * New clients will see the new certificate. + * Existing clients will continue to see the certificate the server + * was using when they connected to it. + * + * This method throws InvalidInputDataException if any of the + * following errors happen: + * - Unable to decode the PEM certificate and/or private key. + * - Unable to decrypt the private key with the given passphrase. + * - The certificate and/or private key do not contain RSA keys. + * - The certificate is not usable with the given private key. + */ + idempotent void updateCertificate(string certificate, string privateKey, string passphrase) throws ServerBootedException, InvalidSecretException, InvalidInputDataException; + + /** + * Makes the given user start listening to the given channel. + * @param userid The ID of the user + * @param channelid The ID of the channel + */ + idempotent void startListening(int userid, int channelid); + + /** + * Makes the given user stop listening to the given channel. + * @param userid The ID of the user + * @param channelid The ID of the channel + */ + idempotent void stopListening(int userid, int channelid); + + /** + * @param userid The ID of the user + * @param channelid The ID of the channel + * @returns Whether the given user is currently listening to the given channel + */ + idempotent bool isListening(int userid, int channelid); + + /** + * @param userid The ID of the user + * @returns An ID-list of channels the given user is listening to + */ + idempotent IntList getListeningChannels(int userid); + + /** + * @param channelid The ID of the channel + * @returns An ID-list of users listening to the given channel + */ + idempotent IntList getListeningUsers(int channelid); + + /** + * @param receiverUserIDs list of IDs of the users the message shall be sent to + */ + idempotent void sendWelcomeMessage(IdList receiverUserIDs); + }; + + /** Callback interface for Meta. You can supply an implementation of this to receive notifications + * when servers are stopped or started. + * If an added callback ever throws an exception or goes away, it will be automatically removed. + * Please note that all callbacks are done asynchronously; murmur does not wait for the callback to + * complete before continuing processing. + * @see ServerCallback + * @see Meta.addCallback + */ + interface MetaCallback { + /** Called when a server is started. The server is up and running when this event is sent, so all methods that + * need a running server will work. + * @param srv Interface for started server. + */ + void started(Server *srv); + + /** Called when a server is stopped. The server is already stopped when this event is sent, so no methods that + * need a running server will work. + * @param srv Interface for started server. + */ + void stopped(Server *srv); + }; + + sequence ServerList; + + /** This is the meta interface. It is primarily used for retrieving the {@link Server} interfaces for each individual server. + **/ + ["amd"] interface Meta { + /** Fetch interface to specific server. + * @param id Server ID. See {@link Server.getId}. + * @return Interface for specified server, or a null proxy if id is invalid. + */ + idempotent Server *getServer(int id) throws InvalidSecretException; + + /** Create a new server. Call {@link Server.getId} on the returned interface to find it's ID. + * @return Interface for new server. + */ + Server *newServer() throws InvalidSecretException; + + /** Fetch list of all currently running servers. + * @return List of interfaces for running servers. + */ + idempotent ServerList getBootedServers() throws InvalidSecretException; + + /** Fetch list of all defined servers. + * @return List of interfaces for all servers. + */ + idempotent ServerList getAllServers() throws InvalidSecretException; + + /** Fetch default configuration. This returns the configuration items that were set in the configuration file, or + * the built-in default. The individual servers will use these values unless they have been overridden in the + * server specific configuration. The only special case is the port, which defaults to the value defined here + + * the servers ID - 1 (so that virtual server #1 uses the defined port, server #2 uses port+1 etc). + * @return Default configuration of the servers. + */ + idempotent ConfigMap getDefaultConf() throws InvalidSecretException; + + /** Fetch version of Murmur. + * @param major Major version. + * @param minor Minor version. + * @param patch Patchlevel. + * @param text Textual representation of version. Note that this may not match the {@link major}, {@link minor} and {@link patch} levels, as it + * may be simply the compile date or the SVN revision. This is usually the text you want to present to users. + */ + idempotent void getVersion(out int major, out int minor, out int patch, out string text); + + /** Add a callback. The callback will receive notifications when servers are started or stopped. + * + * @param cb Callback interface which will receive notifications. + */ + void addCallback(MetaCallback *cb) throws InvalidCallbackException, InvalidSecretException; + + /** Remove a callback. + * + * @param cb Callback interface to be removed. + */ + void removeCallback(MetaCallback *cb) throws InvalidCallbackException, InvalidSecretException; + + /** Get murmur uptime. + * @return Uptime of murmur in seconds + */ + idempotent int getUptime(); + + /** Get slice file. + * @return Contents of the slice file server compiled with. + */ + idempotent string getSlice(); + + /** Returns a checksum dict for the slice file. + * @return Checksum dict + */ + idempotent Ice::SliceChecksumDict getSliceChecksums(); + }; +}; diff --git a/allianceauth/services/modules/mumble/admin.py b/allianceauth/services/modules/mumble/admin.py index ecc8328d..b6cc7e3a 100644 --- a/allianceauth/services/modules/mumble/admin.py +++ b/allianceauth/services/modules/mumble/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import MumbleUser +from .models import IdlerHandler, MumbleServerServer, MumbleUser from ...admin import ServicesUserAdmin @@ -8,11 +8,23 @@ from ...admin import ServicesUserAdmin class MumbleUserAdmin(ServicesUserAdmin): list_display = ServicesUserAdmin.list_display + ( 'username', - 'groups', + 'release', + 'version' ) search_fields = ServicesUserAdmin.search_fields + ( 'username', - 'groups' ) - fields = ('user', 'username', 'groups') # pwhash is hidden from admin panel + +@admin.register(MumbleServerServer) +class MumbleServerServerAdmin(admin.ModelAdmin): + list_display = ["name", "ip", "port", "slice", "virtual_servers", "avatar_enable", "reject_on_error", "offset"] + list_filter = ["slice", "avatar_enable", "reject_on_error", "offset"] + search_fields = ["name"] + + +@admin.register(IdlerHandler) +class IdlerhandlerAdmin(admin.ModelAdmin): + list_display = ["name", "enabled", "seconds", "interval", "channel", "denylist"] + list_filter = ["enabled", "denylist"] + search_fields = ["name"] diff --git a/allianceauth/services/modules/mumble/authenticator.py b/allianceauth/services/modules/mumble/authenticator.py new file mode 100644 index 00000000..ad8b2af8 --- /dev/null +++ b/allianceauth/services/modules/mumble/authenticator.py @@ -0,0 +1,580 @@ +#!/usr/bin/env python + +# Copyright (C) 2010 Stefan Hacker +# All rights reserved. +# Adapted by Adarnof for AllianceAuth +# Further modified by the Alliance Auth team and contributers +# Rewritten for Django Context by the Alliance Auth Team +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: + +# - Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# - Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# - Neither the name of the Mumble Developers nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. + +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +import importlib.util +import logging +import Ice + +from urllib.request import urlopen +from threading import Timer +from passlib.hash import bcrypt_sha256 +from hashlib import sha1 + +import django # noqa +import os # noqa +import sys +logger = logging.getLogger(__name__) + +sys.path.append(os.getcwd()) +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myauth.settings.local") # noqa +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" # noqa +django.setup() # noqa +from django.utils.datetime_safe import datetime + +from allianceauth import __version__ +from allianceauth.services.modules.mumble.models import MumbleServerServer, MumbleUser + + +def main() -> None: + slicedir = Ice.getSliceDir() + if not slicedir: + slicedir = ["-I/usr/share/Ice/slice", "-I/usr/share/slice"] + else: + slicedir = ['-I' + slicedir] + + package_dir = next(iter(importlib.util.find_spec('allianceauth.services.modules.mumble').submodule_search_locations)) + package_ice = os.path.join(package_dir, MumbleServerServer.objects.get(id=1).slice) + + logger.info(f"Using slice file: {package_ice}") + slicedir.append("-I" + os.path.dirname(package_ice)) + + Ice.loadSlice("", slicedir + [package_ice]) + try: + import MumbleServer as Murmur # Mumble >=1.5.17 + except ImportError: + import Murmur # Mumble <=1.5.17 + + class AllianceAuthAuthenticatorApp(Ice.Application): + def run(self, args) -> int: + self.shutdownOnInterrupt() + + if not self.initializeIceConnection(): + return 1 + + if MumbleServerServer.objects.get(id=1).watchdog > 0: + self.failedWatch = True + self.checkConnection() + + # Serve till we are stopped + self.communicator().waitForShutdown() + self.watchdog.cancel() + + if self.interrupted(): + logger.warning("Caught interrupt, shutting down") + + return 0 + + def initializeIceConnection(self) -> bool: + """ + Establishes the two-way Ice connection and adds the authenticator to the + configured servers + """ + ice = self.communicator() + logger.debug("Using shared ice secret") + ice.getImplicitContext().put("secret", MumbleServerServer.objects.get(id=1).secret) + + logger.info(f"Connecting to Ice server ({MumbleServerServer.objects.get(id=1).ip}:{MumbleServerServer.objects.get(id=1).port})") + base = ice.stringToProxy(f"Meta:tcp -h {MumbleServerServer.objects.get(id=1).ip} -p {MumbleServerServer.objects.get(id=1).port}") + self.meta = Murmur.MetaPrx.uncheckedCast(base) + + adapter = ice.createObjectAdapterWithEndpoints("Callback.Client", f"tcp -h {MumbleServerServer.objects.get(id=1).endpoint}") + adapter.activate() + + metacbprx = adapter.addWithUUID(metaCallback(self)) + self.metacb = Murmur.MetaCallbackPrx.uncheckedCast(metacbprx) + + servercbprx = adapter.addWithUUID(serverCallback(self)) + self.servercb = Murmur.ServerCallbackPrx.uncheckedCast(servercbprx) + + authprx = adapter.addWithUUID(AllianceAuthAuthenticator()) + self.auth = Murmur.ServerUpdatingAuthenticatorPrx.uncheckedCast(authprx) + + return self.attachCallbacks() + + def attachCallbacks(self, quiet=False) -> bool: + """ + Attaches all callbacks for meta and authenticators + """ + + # Ice.ConnectionRefusedException + # logger.debug('Attaching callbacks') + try: + if not quiet: + logger.info("Attaching meta callback") + + self.meta.addCallback(self.metacb) + + for server in self.meta.getBootedServers(): + if server.id() in MumbleServerServer.objects.get(id=1).virtual_servers_list(): + logger.info("Setting authenticator for virtual server %d", server.id()) + server.setAuthenticator(self.auth) + server.addCallback(self.servercb) + if MumbleServerServer.objects.get(id=1).idler_handler: + idler_handler(server) + + except (Murmur.InvalidSecretException, Ice.UnknownUserException, Ice.ConnectionRefusedException) as e: + logger.exception(e) + if isinstance(e, Ice.ConnectionRefusedException): + logger.error("Server refused connection") + elif (isinstance(e, Murmur.InvalidSecretException) or isinstance(e, Ice.UnknownUserException) and (e.unknown == "Murmur::InvalidSecretException")): + logger.error("Invalid ice secret") + else: + # We do not actually want to handle this one, re-raise it + raise e + + self.connected = False + return False + + self.connected = True + return True + + def checkConnection(self) -> None: + """ + Tries reapplies all callbacks to make sure the authenticator + survives server restarts and disconnects. + """ + try: + if not self.attachCallbacks(quiet=not self.failedWatch): + self.failedWatch = True + else: + self.failedWatch = False + except Ice.Exception as e: + logger.error(f"Failed connection check, will retry in next watchdog run ({MumbleServerServer.objects.get(id=1).watchdog}s)") + logger.exception(e) + self.failedWatch = True + + # Renew the timer + self.watchdog = Timer(MumbleServerServer.objects.get(id=1).watchdog, self.checkConnection) + self.watchdog.start() + + def checkSecret(func): + """ + Decorator that checks whether the server transmitted the right secret + if a secret is supposed to be used. + """ + if not MumbleServerServer.objects.get(id=1).secret: + return func + + def newfunc(*args, **kws): + if "current" in kws: + current = kws["current"] + else: + current = args[-1] + + if not current or "secret" not in current.ctx or current.ctx["secret"] != MumbleServerServer.objects.get(id=1).secret: + logger.error("Server transmitted invalid secret. Possible injection attempt.") + raise Murmur.InvalidSecretException() + + return func(*args, **kws) + + return newfunc + + def fortifyIceFu(retval=None, exceptions=(Ice.Exception,)): + """ + Decorator that catches exceptions,logs them and returns a safe retval + value. This helps preventing the authenticator getting stuck in + critical code paths. Only exceptions that are instances of classes + given in the exceptions list are not caught. + + The default is to catch all non-Ice exceptions. + """ + + def newdec(func): + def newfunc(*args, **kws): + try: + return func(*args, **kws) + except Exception as e: + catch = True + for ex in exceptions: + if isinstance(e, ex): + catch = False + break + if catch: + logger.critical("Unexpected exception caught") + logger.exception(e) + return retval + raise + + return newfunc + + return newdec + + class metaCallback(Murmur.MetaCallback): + def __init__(self, app) -> None: + Murmur.MetaCallback.__init__(self) + self.app = app + + @fortifyIceFu() + @checkSecret + def started(self, server, current=None) -> None: + """ + This function is called when a virtual server is started + and makes sure an authenticator gets attached if needed. + """ + if server.id() in MumbleServerServer.objects.get(id=1).virtual_servers_list(): + logger.info("Setting authenticator for virtual server %d", server.id()) + try: + server.setAuthenticator(self.app.auth) + # Apparently this server was restarted without us noticing + except (Murmur.InvalidSecretException, Ice.UnknownUserException) as e: + if (hasattr(e, "unknown") and e.unknown != "Murmur::InvalidSecretException"): + # Special handling for Murmur 1.2.2 servers with invalid slice files + raise e + logger.error("Invalid ice secret") + return + else: + logger.debug("Virtual server %d got started", server.id()) + + @fortifyIceFu() + @checkSecret + def stopped(self, server, current=None) -> None: + """ + This function is called when a virtual server is stopped + """ + if self.app.connected: + # Only try to output the server id if we think we are still connected to prevent + # flooding of our thread pool + try: + if server.id() in MumbleServerServer.objects.get(id=1).virtual_servers_list(): + logger.info(f"Authenticated virtual server {server.id()} got stopped") + else: + logger.debug(f"Virtual server {server.id()} got stopped") + return + except Ice.ConnectionRefusedException: + self.app.connected = False + + logger.debug("Server shutdown stopped a virtual server") + + if MumbleServerServer.objects.get(id=1).reject_on_error: # Python 2.4 compat + authenticateFortifyResult = (-1, None, None) + else: + authenticateFortifyResult = (-2, None, None) + + class serverCallback(Murmur.ServerCallback): + def __init__(self, app) -> None: + Murmur.ServerCallback.__init__(self) + self.app = app + + def userConnected(self, user, current=None) -> None: + try: + mumble_user = MumbleUser.objects.get(username=user) + mumble_user.release = user.release + mumble_user.version = user.version + mumble_user.last_connect = datetime.now() + mumble_user.save() + except Exception as e: + logger.error(e) + + def userDisconnected(self, user, current=None) -> None: + try: + mumble_user = MumbleUser.objects.get(username=user) + mumble_user.last_disconnect = datetime.now() + mumble_user.save() + except Exception as e: + logger.error(e) + + def userStateChanged(self, user, current=None) -> None: + pass + + def userTextMessage(self, user, text_message=None) -> None: + pass + + def channelCreated(self, channel, current=None) -> None: + pass + + def channelRemoved(self, channel, current=None) -> None: + pass + + def channelStateChanged(self, channel, current=None) -> None: + pass + + class AllianceAuthAuthenticator(Murmur.ServerUpdatingAuthenticator): + texture_cache = {} + + def __init__(self): + Murmur.ServerUpdatingAuthenticator.__init__(self) + + @fortifyIceFu(authenticateFortifyResult) + @checkSecret + def authenticate(self, name, pw, certlist, certhash, strong, current=None) -> tuple[int, str | None, str | None]: + """ + This function is called to authenticate a user + """ + FALL_THROUGH = -2 + AUTH_REFUSED = -1 + if name == "SuperUser": + logger.debug("Forced fall through for SuperUser") + return (FALL_THROUGH, None, None) + + try: + mumble_user = MumbleUser.objects.get(username=name) + except MumbleUser.DoesNotExist: + return (-2, None, None) + + logger.debug("checking password with hash function: %s" % mumble_user.hashfn) + if allianceauth_check_hash(pw, mumble_user.pwhash, mumble_user.hashfn): + logger.info(f'User authenticated: {mumble_user.get_display_name()} {mumble_user.user_id + MumbleServerServer.objects.get(id=1).offset}') + logger.debug("Group memberships: %s", mumble_user.group_string()) + return (mumble_user.user_id + MumbleServerServer.objects.get(id=1).offset, mumble_user.get_display_name(), mumble_user.group_string()) + logger.info( + f'Failed authentication attempt for user: {name} {mumble_user.user_id + MumbleServerServer.objects.get(id=1).offset}') + return (AUTH_REFUSED, None, None) + + @fortifyIceFu((False, None)) + @checkSecret + def getInfo(self, id, current=None) -> tuple[bool, None]: + """ + Gets called to fetch user specific information + """ + # We do not expose any additional information so always fall through + logger.debug("getInfo for %d -> denied", id) + return (False, None) + + @fortifyIceFu(-2) + @checkSecret + def nameToId(self, name, current=None) -> int: + """ + Gets called to get the id for a given username + """ + if name == "SuperUser": + logger.debug("nameToId SuperUser -> forced fall through") + return -2 # FALL_THROUGH + + try: + return (MumbleUser.objects.get(username=name).pk + MumbleServerServer.objects.get(id=1).offset) + except MumbleUser.DoesNotExist: + return -2 # FALL_THROUGH + + @fortifyIceFu("") + @checkSecret + def idToName(self, id, current=None) -> str: + """ + Gets called to get the username for a given id + """ + if id < MumbleServerServer.objects.get(id=1).offset: + return "" # FALL_THROUGH + + try: + mumble_user = MumbleUser.objects.get(user_id=id - MumbleServerServer.objects.get(id=1).offset) + except MumbleUser.DoesNotExist: + return "" # FALL_THROUGH + + # I dont quite rightly know why we have this + # SuperUser shouldnt be in our Authenticator? + # But Maybe it can be + if MumbleUser.objects.get(user_id=id - MumbleServerServer.objects.get(id=1).offset).username == "SuperUser": + logger.debug('idToName %d -> "SuperUser" caught') + return "" # FALL_THROUGH + else: + return mumble_user.username + + @fortifyIceFu("") + @checkSecret + def idToTexture(self, id, current=None): + """ + Gets called to get the corresponding texture for a user + """ + if MumbleServerServer.objects.get(id=1).avatar_enable is False: + logger.debug(f"idToTexture {id} -> avatar display disabled, fall through") + return "" # FALL_THROUGH + + if id < MumbleServerServer.objects.get(id=1).offset: + return "" # FALL_THROUGH + + try: + avatar_url = MumbleUser.objects.get(user_id=id - MumbleServerServer.objects.get(id=1).offset).user.profile.main_character.portrait_url() + except MumbleUser.DoesNotExist: + logger.debug(f"idToTexture {id} -> MumbleUser.DoesNotExist, Fall Through") + return "" # FALL_THROUGH + + if avatar_url: + if avatar_url in self.texture_cache: + logger.debug('idToTexture {id} -> cached avatar returned: {avatar_url}') + return self.texture_cache[avatar_url] + + # Not cached? Try to retrieve from CCP image server. + try: + logger.debug('idToTexture %d -> try file "%s"', id, avatar_url) + handle = urlopen(avatar_url) + + except (OSError, Exception) as e: + logger.exception(e) + logger.debug( + f'idToTexture {id} -> image download for {avatar_url} failed, fall through') + return "" # FALL_THROUGH + else: + file = handle.read() + handle.close() + + # Cache resulting avatar by file address and return image. + self.texture_cache[avatar_url] = file + logger.debug(f'idToTexture {id} -> avatar from {avatar_url} retrieved and returned') + return self.texture_cache[avatar_url] + + else: + logger.debug(f"idToTexture {id} -> empty avatar_url, final fall through") + return "" # FALL_THROUGH + + @fortifyIceFu(-2) + @checkSecret + def registerUser(self, name, current=None) -> int: + """ + Gets called when the server is asked to register a user. + """ + logger.debug(f'registerUser {name} -> fall through') + return -2 # FALL_THROUGH + + @fortifyIceFu(-1) + @checkSecret + def unregisterUser(self, id, current=None) -> int: + """ + Gets called when the server is asked to unregister a user. + """ + # Return -1 to fall through to internal server database, so as to not modify Alliance Auth + # but we can make murmur delete all additional logger.information it got this way. + logger.debug(f"unregisterUser {id} -> fall through", ) + return -1 # FALL_THROUGH + + @fortifyIceFu({}) + @checkSecret + def getRegisteredUsers(self, filter, current=None) -> dict: + """ + Returns a list of usernames in the AllianceAuth database which contain + filter as a substring. + """ + + if not filter: + mumble_users = MumbleUser.objects.all() + else: + mumble_users = MumbleUser.objects.filter(username__icontains=filter) + + if not mumble_users.exists(): + logger.debug(f'getRegisteredUsers -> empty list for filter {filter}', ) + return {} + logger.debug(f'getRegisteredUsers -> {len(mumble_users)} results for filter {filter}') + + return {mumble_user.user_id + MumbleServerServer.objects.get(id=1).offset: mumble_user.username for mumble_user in mumble_users} + + @fortifyIceFu(-1) + @checkSecret + def setInfo(self, id, info, current=None) -> int: + """ + Gets called when the server is supposed to save additional information + about a user to his database + """ + # Return -1 to fall through to the internal server handler. + # Store this in Murmur, Not Alliance Auth + logger.debug(f"setInfo {id} -> fall through") + return -1 # FALL_THROUGH + + @fortifyIceFu(-1) + @checkSecret + def setTexture(self, id, texture, current=None) -> int: + """ + Gets called when the server is asked to update the user texture of a user + """ + # Return -1 to fall through to the internal server handler. + # Store this in Murmur, Not Alliance Auth + logger.debug(f"setTexture {id} -> fall through") + return -1 # FALL_THROUGH + + # + # --- Start of authenticator + # + logger.info(f"Starting AllianceAuth Mumble Authenticator V:{__version__}") + initdata = Ice.InitializationData() + initdata.properties = Ice.createProperties([], initdata.properties) + + initdata.properties.setProperty('Ice.ImplicitContext', 'Shared') + initdata.properties.setProperty('Ice.Default.EncodingVersion', '1.0') + initdata.logger = logger + + app = AllianceAuthAuthenticatorApp() + state = app.main(sys.argv, initData=initdata) + logger.info('Shutdown complete') + + +def allianceauth_check_hash(password, hash, hash_type) -> bool: + """ + :param password: Password to be verified + :param hash: Hash for the password to be checked against + :param hash_type: Hashing function originally used to generate the hash + """ + if hash_type == "sha1": + return sha1(password).hexdigest() == hash + elif hash_type == "bcrypt-sha256": + return bcrypt_sha256.verify(password, hash) + else: + logger.warning("No valid hash function found for %s" % hash_type) + return False + + +def idler_handler(server) -> None: + logger.debug("IdlerHandler: Starting") + users = server.getUsers().values() + logger.debug("IdleHandler: Fetched All Users") + for user in users: + logger.debug(f"IdleHandler: Checking user {user.name}") + if isinstance(user, int): + logger.debug(f"IdleHandler: Skipping User {user.name}, This happens occasionally") + continue + + if user_idlesecs > MumbleServerServer.objects.get(id=1).idler_handler.seconds: + logger.debug( + f"IdleHandler: User {user.name} is AFK, for {user_idlesecs}/{MumbleServerServer.objects.get(id=1).idler_handler.seconds}" + ) + state = server.getState(user.session) + if state: + # AllowList > DenyList + if MumbleServerServer.objects.get(id=1).idler_handler.denylist is False: + if state.channel not in MumbleServerServer.objects.get(id=1).idler_handler.list: + return + elif MumbleServerServer.objects.get(id=1).idler_handler.denylist is True: + if state.channel in MumbleServerServer.objects.get(id=1).idler_handler.list: + return + + if state.channel == MumbleServerServer.objects.get(id=1).idler_handler.channel: + return + + state.channel = MumbleServerServer.objects.get(id=1).idler_handler.channel + state.selfMute = True + state.selfDeaf = True + server.setState(state) + logger.debug(f"IdleHandler: Moved AFK User {user.name}") + + Timer(MumbleServerServer.objects.get(id=1).idler_handler.interval, idler_handler, (server,)).start() + + +if __name__ == "__main__": + main() diff --git a/allianceauth/services/modules/mumble/managers.py b/allianceauth/services/modules/mumble/managers.py new file mode 100644 index 00000000..81185a1e --- /dev/null +++ b/allianceauth/services/modules/mumble/managers.py @@ -0,0 +1,61 @@ +import random +import string +from passlib.hash import bcrypt_sha256 + +from django.db import models +from allianceauth.services.hooks import NameFormatter +import logging + +logging.getLogger(__name__) + + +class MumbleManager(models.Manager): + + @staticmethod + def get_display_name(user): + from .auth_hooks import MumbleService + return NameFormatter(MumbleService(), user).format_name() + + @staticmethod + def get_username(user): + return user.profile.main_character.character_name # main character as the user.username may be incorect + + @staticmethod + def sanitise_username(username): + return username.replace(" ", "_") + + @staticmethod + def generate_random_pass(): + return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) + + @staticmethod + def gen_pwhash(password): + return bcrypt_sha256.encrypt(password.encode('utf-8')) + + def create(self, user): + try: + username = self.get_username(user) + logger.debug(f"Creating mumble user with username {username}") + username_clean = self.sanitise_username(username) + display_name = self.get_display_name(user) + password = self.generate_random_pass() + pwhash = self.gen_pwhash(password) + logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format( + username_clean, pwhash[0:5])) + logger.info(f"Creating mumble user {username_clean}") + + result = super().create( + user=user, + username=username_clean, + pwhash=pwhash, + hashfn=self.HashFunction.SHA256, + display_name=display_name) + result.update_groups() + result.credentials.update({'username': result.username, 'password': password}) + return result + except AttributeError: # No Main or similar errors + return False + return False + + def user_exists(self, username): + return self.filter(username=username).exists() diff --git a/allianceauth/services/modules/mumble/migrations/0014_idlerhandler_remove_mumbleuser_display_name_and_more.py b/allianceauth/services/modules/mumble/migrations/0014_idlerhandler_remove_mumbleuser_display_name_and_more.py new file mode 100644 index 00000000..234535d5 --- /dev/null +++ b/allianceauth/services/modules/mumble/migrations/0014_idlerhandler_remove_mumbleuser_display_name_and_more.py @@ -0,0 +1,66 @@ +# Generated by Django 4.2.16 on 2024-12-06 06:20 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mumble', '0013_connection_history'), + ] + + operations = [ + migrations.CreateModel( + name='IdlerHandler', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50, verbose_name='')), + ('enabled', models.BooleanField(default=False, verbose_name='')), + ('seconds', models.SmallIntegerField(default=3600, verbose_name='')), + ('interval', models.SmallIntegerField(default=60, verbose_name='')), + ('channel', models.SmallIntegerField(verbose_name='')), + ('denylist', models.BooleanField(default=True, verbose_name='')), + ('list', models.CharField(default='', max_length=50, verbose_name='')), + ], + options={ + 'verbose_name': '', + 'verbose_name_plural': 's', + }, + ), + migrations.RemoveField( + model_name='mumbleuser', + name='display_name', + ), + migrations.RemoveField( + model_name='mumbleuser', + name='groups', + ), + migrations.AlterField( + model_name='mumbleuser', + name='hashfn', + field=models.CharField(choices=[('bcrypt-sha256', ''), ('sha1', '')], default='bcrypt-sha256', max_length=15), + ), + migrations.CreateModel( + name='MumbleServerServer', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50, verbose_name='')), + ('ip', models.GenericIPAddressField(verbose_name='')), + ('endpoint', models.GenericIPAddressField(verbose_name='')), + ('port', models.PositiveSmallIntegerField(default=6502, verbose_name='')), + ('secret', models.CharField(max_length=50, verbose_name='')), + ('watchdog', models.SmallIntegerField(default=30, verbose_name='')), + ('slice', models.CharField(choices=[('MumbleServer.ice', 'MumbleServer.ice eg, Mumble >=1.5.17'), ('Murmur.Ice', 'Murmur.Ice eg, Mumble <=1.5.17')], default='MumbleServer.ice', max_length=16)), + ('virtual_servers', models.CharField(default='1', max_length=50, verbose_name='')), + ('avatar_enable', models.BooleanField(verbose_name='')), + ('reject_on_error', models.BooleanField(verbose_name='')), + ('offset', models.IntegerField(default=1000000000, verbose_name='')), + ('idler_handler', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='mumble.idlerhandler', verbose_name='')), + ], + options={ + 'verbose_name': '', + 'verbose_name_plural': 's', + }, + ), + ] diff --git a/allianceauth/services/modules/mumble/migrations/0015_alter_idlerhandler_options_and_more.py b/allianceauth/services/modules/mumble/migrations/0015_alter_idlerhandler_options_and_more.py new file mode 100644 index 00000000..64fa49fa --- /dev/null +++ b/allianceauth/services/modules/mumble/migrations/0015_alter_idlerhandler_options_and_more.py @@ -0,0 +1,121 @@ +# Generated by Django 4.2.16 on 2024-12-06 09:58 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('mumble', '0014_idlerhandler_remove_mumbleuser_display_name_and_more'), + ] + + operations = [ + migrations.AlterModelOptions( + name='idlerhandler', + options={'default_permissions': (), 'verbose_name': 'Idler Handler', 'verbose_name_plural': 'Idler Handlers'}, + ), + migrations.AlterModelOptions( + name='mumbleserverserver', + options={'default_permissions': (), 'verbose_name': 'Mumble Server', 'verbose_name_plural': 'Mumble Servers'}, + ), + migrations.AlterModelOptions( + name='mumbleuser', + options={'permissions': (('access_mumble', 'Can access the Mumble service'), ('view_connection_history', 'Can access the connection history of the Mumble service')), 'verbose_name': 'User', 'verbose_name_plural': 'Users'}, + ), + migrations.AlterField( + model_name='idlerhandler', + name='channel', + field=models.SmallIntegerField(verbose_name='AFK Channel'), + ), + migrations.AlterField( + model_name='idlerhandler', + name='enabled', + field=models.BooleanField(default=False, verbose_name='Enabled'), + ), + migrations.AlterField( + model_name='idlerhandler', + name='interval', + field=models.SmallIntegerField(default=60, verbose_name='Run Interval'), + ), + migrations.AlterField( + model_name='idlerhandler', + name='name', + field=models.CharField(max_length=50, verbose_name='Name'), + ), + migrations.AlterField( + model_name='idlerhandler', + name='seconds', + field=models.SmallIntegerField(default=3600, verbose_name='Idle Seconds'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='avatar_enable', + field=models.BooleanField(default=True, verbose_name='Enable EVE Avatars'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='endpoint', + field=models.GenericIPAddressField(default='127.0.0.1', verbose_name='Endpoint IP Address'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='idler_handler', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='mumble.idlerhandler', verbose_name='Idler Handler'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='ip', + field=models.GenericIPAddressField(default='127.0.0.1', verbose_name='Host IP Address'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='name', + field=models.CharField(max_length=50), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='offset', + field=models.IntegerField(default=1000000000, verbose_name='ID Offset'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='port', + field=models.PositiveSmallIntegerField(default=6502, verbose_name='Port'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='reject_on_error', + field=models.BooleanField(default=True, verbose_name='Reject Unauthenticated'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='secret', + field=models.CharField(max_length=50, verbose_name='ICE Secret'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='slice', + field=models.CharField(choices=[('MumbleServer.ice', 'MumbleServer.ice (Mumble >=1.5.17)'), ('Murmur.Ice', 'Murmur.Ice (Mumble <=1.5.17)')], default='MumbleServer.ice', max_length=16), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='virtual_servers', + field=models.CharField(default='1', max_length=50, verbose_name='Virtual Servers'), + ), + migrations.AlterField( + model_name='mumbleserverserver', + name='watchdog', + field=models.SmallIntegerField(default=30, verbose_name='Watchdog Interval'), + ), + migrations.AlterField( + model_name='mumbleuser', + name='hashfn', + field=models.CharField(choices=[('bcrypt-sha256', 'SHA256'), ('sha1', 'SHA1')], default='bcrypt-sha256', max_length=15), + ), + migrations.AlterField( + model_name='mumbleuser', + name='last_disconnect', + field=models.DateTimeField(blank=True, editable=False, help_text='Timestamp of the users Last Disconnection from Mumble', max_length=254, null=True, verbose_name='Last Disconnection'), + ), + ] diff --git a/allianceauth/services/modules/mumble/models.py b/allianceauth/services/modules/mumble/models.py index 8c7d0ead..eeddf7f2 100644 --- a/allianceauth/services/modules/mumble/models.py +++ b/allianceauth/services/modules/mumble/models.py @@ -1,77 +1,31 @@ -import random -import string -from passlib.hash import bcrypt_sha256 - -from django.db import models -from django.contrib.auth.models import Group +from typing import LiteralString from allianceauth.services.hooks import NameFormatter +from allianceauth.services.modules.mumble.managers import MumbleManager +from django.db import models from allianceauth.services.abstract import AbstractServiceModel import logging +from django.contrib.auth.models import User +from django.utils.translation import gettext_lazy as _ logger = logging.getLogger(__name__) -class MumbleManager(models.Manager): - HASH_FN = 'bcrypt-sha256' - - @staticmethod - def get_display_name(user): - from .auth_hooks import MumbleService - return NameFormatter(MumbleService(), user).format_name() - - @staticmethod - def get_username(user): - return user.profile.main_character.character_name # main character as the user.username may be incorect - - @staticmethod - def sanitise_username(username): - return username.replace(" ", "_") - - @staticmethod - def generate_random_pass(): - return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) - - @staticmethod - def gen_pwhash(password): - return bcrypt_sha256.encrypt(password.encode('utf-8')) - - def create(self, user): - try: - username = self.get_username(user) - logger.debug(f"Creating mumble user with username {username}") - username_clean = self.sanitise_username(username) - display_name = self.get_display_name(user) - password = self.generate_random_pass() - pwhash = self.gen_pwhash(password) - logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format( - username_clean, pwhash[0:5])) - logger.info(f"Creating mumble user {username_clean}") - - result = super().create(user=user, username=username_clean, - pwhash=pwhash, hashfn=self.HASH_FN, - display_name=display_name) - result.update_groups() - result.credentials.update({'username': result.username, 'password': password}) - return result - except AttributeError: # No Main or similar errors - return False - return False - - def user_exists(self, username): - return self.filter(username=username).exists() - - class MumbleUser(AbstractServiceModel): + class HashFunction(models.TextChoices): + SHA256 = 'bcrypt-sha256', _('SHA256') + SHA1 = 'sha1', _('SHA1') user = models.OneToOneField( - 'auth.User', + User, primary_key=True, on_delete=models.CASCADE, related_name='mumble' ) username = models.CharField(max_length=254, unique=True) pwhash = models.CharField(max_length=90) - hashfn = models.CharField(max_length=20, default='sha1') - groups = models.TextField(blank=True, null=True) + hashfn = models.CharField( + max_length=15, + choices=HashFunction.choices, + default=HashFunction.SHA256) certhash = models.CharField( verbose_name="Certificate Hash", max_length=254, @@ -80,10 +34,6 @@ class MumbleUser(AbstractServiceModel): editable=False, help_text="Hash of Mumble client certificate as presented when user authenticates" ) - display_name = models.CharField( - max_length=254, - unique=True - ) release = models.TextField( verbose_name="Mumble Release", max_length=254, @@ -113,24 +63,23 @@ class MumbleUser(AbstractServiceModel): blank=True, null=True, editable=False, - help_text="Timestamp of the users Last Disconnection to Mumble" + help_text="Timestamp of the users Last Disconnection from Mumble" ) objects = MumbleManager() - def __str__(self): - return self.username + def __str__(self) -> str: + return f"{self.username}" - def update_password(self, password=None): + def update_password(self, password=None) -> None: init_password = password - logger.debug(f"Updating mumble user %s password.") + logger.debug(f"Updating mumble user {self.user} password.") if not password: password = MumbleManager.generate_random_pass() pwhash = MumbleManager.gen_pwhash(password) - logger.debug("Proceeding with mumble user {} password update - pwhash starts with {}".format( - self.user, pwhash[0:5])) + logger.debug(f"Proceeding with mumble user {self.user} password update - pwhash starts with {pwhash[0:5]}") self.pwhash = pwhash - self.hashfn = MumbleManager.HASH_FN + self.hashfn = self.HashFunction.SHA256 self.save() if init_password is None: self.credentials.update({'username': self.username, 'password': password}) @@ -138,26 +87,99 @@ class MumbleUser(AbstractServiceModel): def reset_password(self): self.update_password() - def update_groups(self, groups: Group=None): - if groups is None: - groups = self.user.groups.all() - groups_str = [self.user.profile.state.name] - for group in groups: - groups_str.append(str(group.name)) - safe_groups = ','.join({g.replace(' ', '-') for g in groups_str}) - logger.info(f"Updating mumble user {self.user} groups to {safe_groups}") - self.groups = safe_groups - self.save() - return True + def group_string(self) -> LiteralString: + """Return a Mumble Safe Formatted List of Groups + This used to be a ModelField, generated on the fly now with DjangoAuthenticatorTM - def update_display_name(self): - logger.info(f"Updating mumble user {self.user} display name") - self.display_name = MumbleManager.get_display_name(self.user) - self.save() - return True + Returns: + LiteralString: Mumble Safe Formatted List of Groups + """ + groups_str = [self.user.profile.state.name] + for group in self.user.groups.all(): + groups_str.append(str(group.name)) + return ','.join({g.replace(' ', '-') for g in groups_str}) + + def get_display_name(self): + from .auth_hooks import MumbleService + return NameFormatter(MumbleService(), self.user).format_name() class Meta: + verbose_name = _("User") + verbose_name_plural = _("Users") permissions = ( ("access_mumble", "Can access the Mumble service"), ("view_connection_history", "Can access the connection history of the Mumble service"), ) + + +class IdlerHandler(models.Model): + name = models.CharField(_("Name"), max_length=50) + enabled = models.BooleanField(_("Enabled"), default=False) + seconds = models.SmallIntegerField(_("Idle Seconds"), default=3600) + interval = models.SmallIntegerField(_("Run Interval"), default=60) + channel = models.SmallIntegerField(_("AFK Channel")) + denylist = models.BooleanField(_(""), default=True) + list = models.CharField(_(""), max_length=50, default="") + + class Meta: + verbose_name = _("Idler Handler") + verbose_name_plural = _("Idler Handlers") + default_permissions = () + + def __str__(self) -> str: + return f"{self.name}" + + +class MumbleServerServer(models.Model): # This will clash with ICE MumbleServer + class ServerVersion(models.TextChoices): + MUMBLESERVER_ICE = 'MumbleServer.ice', _('MumbleServer.ice (Mumble >=1.5.17)') + MURMUR_ICE = 'Murmur.Ice', _('Murmur.Ice (Mumble <=1.5.17)') + name = models.CharField(max_length=50) + ip = models.GenericIPAddressField( + verbose_name=_("Host IP Address"), + protocol="both", + unpack_ipv4=False, + default="127.0.0.1") + endpoint = models.GenericIPAddressField( + verbose_name=_("Endpoint IP Address"), + protocol="both", + unpack_ipv4=False, + default="127.0.0.1") + + port = models.PositiveSmallIntegerField(verbose_name=_("Port"), default=6502) + secret = models.CharField(verbose_name=_("ICE Secret"), max_length=50) + watchdog = models.SmallIntegerField(verbose_name=_("Watchdog Interval"), default=30) + slice = models.CharField( + max_length=16, + choices=ServerVersion.choices, + default=ServerVersion.MUMBLESERVER_ICE) + virtual_servers = models.CharField( + verbose_name=_("Virtual Servers"), + max_length=50, + default="1") + avatar_enable = models.BooleanField( + verbose_name=_("Enable EVE Avatars"), + default=True) + reject_on_error = models.BooleanField( + verbose_name=_("Reject Unauthenticated"), + default=True) + offset = models.IntegerField( + verbose_name=_("ID Offset"), + default=1000000000) + idler_handler = models.ForeignKey( + IdlerHandler, + verbose_name=_("Idler Handler"), + on_delete=models.SET_NULL, + null=True, + blank=True) + + class Meta: + verbose_name = _("Mumble Server") + verbose_name_plural = _("Mumble Servers") + default_permissions = () + + def __str__(self) -> str: + return f"{self.name}" + + def virtual_servers_list(self) -> list: + return [int(num) for num in self.virtual_servers.replace(",", " ").split() if num] From 3ef3098ad1ab39b81a31a760f2ca6f9a13d43ffa Mon Sep 17 00:00:00 2001 From: Joel Falknau Date: Fri, 6 Dec 2024 21:07:42 +1000 Subject: [PATCH 02/14] push templink framework --- .../services/modules/mumble/auth_hooks.py | 30 +- .../services/modules/mumble/authenticator.py | 3 +- .../services/modules/mumble/managers.py | 5 +- ..._alter_mumbleuser_last_connect_and_more.py | 60 ++++ .../services/modules/mumble/models.py | 89 ++++-- allianceauth/services/modules/mumble/tasks.py | 10 +- .../templates/services/mumble/base.html | 19 ++ .../templates/services/mumble/index.html | 274 ++++++++++++++++++ .../templates/services/mumble/link.html | 121 ++++++++ .../templates/services/mumble/login.html | 140 +++++++++ allianceauth/services/modules/mumble/urls.py | 6 +- allianceauth/services/modules/mumble/views.py | 197 ++++++++++++- 12 files changed, 912 insertions(+), 42 deletions(-) create mode 100644 allianceauth/services/modules/mumble/migrations/0016_templink_alter_mumbleuser_last_connect_and_more.py create mode 100644 allianceauth/services/modules/mumble/templates/services/mumble/base.html create mode 100644 allianceauth/services/modules/mumble/templates/services/mumble/index.html create mode 100644 allianceauth/services/modules/mumble/templates/services/mumble/link.html create mode 100644 allianceauth/services/modules/mumble/templates/services/mumble/login.html diff --git a/allianceauth/services/modules/mumble/auth_hooks.py b/allianceauth/services/modules/mumble/auth_hooks.py index efc8df33..f1522e0a 100644 --- a/allianceauth/services/modules/mumble/auth_hooks.py +++ b/allianceauth/services/modules/mumble/auth_hooks.py @@ -1,15 +1,18 @@ import logging import urllib +from allianceauth.menu.hooks import MenuItemHook from django.conf import settings from django.template.loader import render_to_string from allianceauth.notifications import notify from allianceauth import hooks -from allianceauth.services.hooks import ServicesHook +from allianceauth.services.hooks import ServicesHook, UrlHook from .tasks import MumbleTasks from .models import MumbleUser from .urls import urlpatterns +from allianceauth.services.modules.mumble import urls +from django.utils.translation import gettext_lazy as _ logger = logging.getLogger(__name__) @@ -71,7 +74,28 @@ class MumbleService(ServicesHook): 'username': request.user.mumble.username if MumbleTasks.has_account(request.user) else '', }, request=request) - @hooks.register('services_hook') -def register_mumble_service(): +def register_mumble_service() -> ServicesHook: return MumbleService() + + +class MumbleMenuItem(MenuItemHook): + def __init__(self): + # setup menu entry for sidebar + MenuItemHook.__init__( + self=self, + text=_("Mumble Temp Links"), + classes="fa-solid fa-microphone", + url_name="mumble:index", + navactive=["mumble:index"], + ) + + def render(self, request): + if request.user.has_perm("mumble.create_new_templinks"): + return MenuItemHook.render(self, request) + return "" + + +@hooks.register("menu_item_hook") +def register_menu(): + return MumbleMenuItem() diff --git a/allianceauth/services/modules/mumble/authenticator.py b/allianceauth/services/modules/mumble/authenticator.py index ad8b2af8..579c0c03 100644 --- a/allianceauth/services/modules/mumble/authenticator.py +++ b/allianceauth/services/modules/mumble/authenticator.py @@ -430,8 +430,7 @@ def main() -> None: except (OSError, Exception) as e: logger.exception(e) - logger.debug( - f'idToTexture {id} -> image download for {avatar_url} failed, fall through') + logger.debug(f'idToTexture {id} -> image download for {avatar_url} failed, fall through') return "" # FALL_THROUGH else: file = handle.read() diff --git a/allianceauth/services/modules/mumble/managers.py b/allianceauth/services/modules/mumble/managers.py index 81185a1e..5ee02357 100644 --- a/allianceauth/services/modules/mumble/managers.py +++ b/allianceauth/services/modules/mumble/managers.py @@ -6,7 +6,7 @@ from django.db import models from allianceauth.services.hooks import NameFormatter import logging -logging.getLogger(__name__) +logger = logging.getLogger(__name__) class MumbleManager(models.Manager): @@ -48,7 +48,6 @@ class MumbleManager(models.Manager): user=user, username=username_clean, pwhash=pwhash, - hashfn=self.HashFunction.SHA256, display_name=display_name) result.update_groups() result.credentials.update({'username': result.username, 'password': password}) @@ -57,5 +56,5 @@ class MumbleManager(models.Manager): return False return False - def user_exists(self, username): + def user_exists(self, username) -> bool: return self.filter(username=username).exists() diff --git a/allianceauth/services/modules/mumble/migrations/0016_templink_alter_mumbleuser_last_connect_and_more.py b/allianceauth/services/modules/mumble/migrations/0016_templink_alter_mumbleuser_last_connect_and_more.py new file mode 100644 index 00000000..c9f3f0b7 --- /dev/null +++ b/allianceauth/services/modules/mumble/migrations/0016_templink_alter_mumbleuser_last_connect_and_more.py @@ -0,0 +1,60 @@ +# Generated by Django 4.2.16 on 2024-12-06 10:47 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('eveonline', '0017_alliance_and_corp_names_are_not_unique'), + ('mumble', '0015_alter_idlerhandler_options_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='TempLink', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('expires', models.DateTimeField(verbose_name='Expiry')), + ('link_ref', models.CharField(max_length=20)), + ('creator', models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.SET_NULL, to='eveonline.evecharacter')), + ], + options={ + 'permissions': (('create_new_links', 'Can Create Temp Links'),), + }, + ), + migrations.AlterField( + model_name='mumbleuser', + name='last_connect', + field=models.DateTimeField(blank=True, editable=False, help_text='Timestamp of the users Last Connection to Mumble', null=True, verbose_name='Last Connection'), + ), + migrations.AlterField( + model_name='mumbleuser', + name='last_disconnect', + field=models.DateTimeField(blank=True, editable=False, help_text='Timestamp of the users Last Disconnection from Mumble', null=True, verbose_name='Last Disconnection'), + ), + migrations.AlterField( + model_name='mumbleuser', + name='release', + field=models.TextField(blank=True, editable=False, help_text='Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else.', null=True, verbose_name='Mumble Release'), + ), + migrations.CreateModel( + name='TempUser', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200)), + ('username', models.CharField(max_length=254, unique=True)), + ('pwhash', models.CharField(max_length=90)), + ('hashfn', models.CharField(choices=[('bcrypt-sha256', 'SHA256'), ('sha1', 'SHA1')], default='bcrypt-sha256', max_length=15)), + ('certhash', models.CharField(blank=True, editable=False, help_text='Hash of Mumble client certificate as presented when user authenticates', max_length=254, null=True, verbose_name='Certificate Hash')), + ('release', models.TextField(blank=True, editable=False, help_text='Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else.', null=True, verbose_name='Mumble Release')), + ('version', models.IntegerField(blank=True, editable=False, help_text='Client version. Major version in upper 16 bits, followed by 8 bits of minor version and 8 bits of patchlevel. Version 1.2.3 = 0x010203.', null=True, verbose_name='Mumble Version')), + ('last_connect', models.DateTimeField(blank=True, editable=False, help_text='Timestamp of the users Last Connection to Mumble', null=True, verbose_name='Last Connection')), + ('last_disconnect', models.DateTimeField(blank=True, editable=False, help_text='Timestamp of the users Last Disconnection from Mumble', null=True, verbose_name='Last Disconnection')), + ('expires', models.DateTimeField(verbose_name='Expiry')), + ('character_id', models.IntegerField(blank=True, default=None, null=True)), + ('templink', models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='mumble.templink')), + ], + ), + ] diff --git a/allianceauth/services/modules/mumble/models.py b/allianceauth/services/modules/mumble/models.py index eeddf7f2..8e04f5b2 100644 --- a/allianceauth/services/modules/mumble/models.py +++ b/allianceauth/services/modules/mumble/models.py @@ -1,4 +1,5 @@ from typing import LiteralString +from allianceauth.eveonline.models import EveCharacter from allianceauth.services.hooks import NameFormatter from allianceauth.services.modules.mumble.managers import MumbleManager from django.db import models @@ -28,43 +29,24 @@ class MumbleUser(AbstractServiceModel): default=HashFunction.SHA256) certhash = models.CharField( verbose_name="Certificate Hash", - max_length=254, - blank=True, - null=True, - editable=False, - help_text="Hash of Mumble client certificate as presented when user authenticates" - ) + max_length=254,blank=True, null=True, editable=False, + help_text="Hash of Mumble client certificate as presented when user authenticates") release = models.TextField( verbose_name="Mumble Release", - max_length=254, - blank=True, - null=True, - editable=False, - help_text="Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else." - ) + blank=True, null=True, editable=False, + help_text="Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else.") version = models.IntegerField( verbose_name="Mumble Version", - blank=True, - null=True, - editable=False, - help_text="Client version. Major version in upper 16 bits, followed by 8 bits of minor version and 8 bits of patchlevel. Version 1.2.3 = 0x010203." - ) + blank=True, null=True, editable=False, + help_text="Client version. Major version in upper 16 bits, followed by 8 bits of minor version and 8 bits of patchlevel. Version 1.2.3 = 0x010203.") last_connect = models.DateTimeField( verbose_name="Last Connection", - max_length=254, - blank=True, - null=True, - editable=False, - help_text="Timestamp of the users Last Connection to Mumble" - ) + blank=True, null=True, editable=False, + help_text="Timestamp of the users Last Connection to Mumble") last_disconnect = models.DateTimeField( verbose_name="Last Disconnection", - max_length=254, - blank=True, - null=True, - editable=False, - help_text="Timestamp of the users Last Disconnection from Mumble" - ) + blank=True, null=True, editable=False, + help_text="Timestamp of the users Last Disconnection from Mumble") objects = MumbleManager() @@ -112,6 +94,55 @@ class MumbleUser(AbstractServiceModel): ) +class TempLink(models.Model): + expires = models.DateTimeField(_("Expiry"), auto_now=False, auto_now_add=False) + link_ref = models.CharField(max_length=20) + creator = models.ForeignKey(EveCharacter, on_delete=models.SET_NULL, null=True, default=None) + + class Meta: + permissions = (("create_new_links", "Can Create Temp Links"),) + + def __str__(self): + return f"Link {self.link_ref} - {self.expires}" + + +class TempUser(models.Model): + name = models.CharField(max_length=200) # Display name to show + username = models.CharField(max_length=254, unique=True) + pwhash = models.CharField(max_length=90) + hashfn = models.CharField( + max_length=15, + choices=MumbleUser.HashFunction.choices, + default=MumbleUser.HashFunction.SHA256) + certhash = models.CharField( + verbose_name="Certificate Hash", + max_length=254,blank=True, null=True, editable=False, + help_text="Hash of Mumble client certificate as presented when user authenticates") + release = models.TextField( + verbose_name="Mumble Release", + blank=True, null=True, editable=False, + help_text="Client release. For official releases, this equals the version. For snapshots and git compiles, this will be something else.") + version = models.IntegerField( + verbose_name="Mumble Version", + blank=True, null=True, editable=False, + help_text="Client version. Major version in upper 16 bits, followed by 8 bits of minor version and 8 bits of patchlevel. Version 1.2.3 = 0x010203.") + last_connect = models.DateTimeField( + verbose_name="Last Connection", + blank=True, null=True, editable=False, + help_text="Timestamp of the users Last Connection to Mumble") + last_disconnect = models.DateTimeField( + verbose_name="Last Disconnection", + blank=True, null=True, editable=False, + help_text="Timestamp of the users Last Disconnection from Mumble") + expires = models.DateTimeField(_("Expiry"), auto_now=False, auto_now_add=False) + + templink = models.ForeignKey(TempLink, on_delete=models.CASCADE, null=True, default=None) + character_id = models.IntegerField(default=None, blank=True, null=True) + + def __str__(self) -> str: + return f"Temp User: {self.username} - {self.name}" + + class IdlerHandler(models.Model): name = models.CharField(_("Name"), max_length=50) enabled = models.BooleanField(_("Enabled"), default=False) diff --git a/allianceauth/services/modules/mumble/tasks.py b/allianceauth/services/modules/mumble/tasks.py index 5ce7c9c4..e02a1c86 100644 --- a/allianceauth/services/modules/mumble/tasks.py +++ b/allianceauth/services/modules/mumble/tasks.py @@ -1,10 +1,11 @@ +from datetime import datetime, timezone import logging from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from celery import shared_task from allianceauth.services.tasks import QueueOnce -from .models import MumbleUser +from .models import MumbleUser, TempLink, TempUser logger = logging.getLogger(__name__) @@ -78,3 +79,10 @@ class MumbleTasks: logger.debug("Updating ALL mumble display names") for mumble_user in MumbleUser.objects.exclude(username__exact=''): MumbleTasks.update_display_name.delay(mumble_user.user.pk) + + +@shared_task +def tidy_up_temp_links(): + TempLink.objects.filter(expires__lt=datetime.now(timezone.utc).replace(tzinfo=timezone.utc).timestamp()).delete() + TempUser.objects.filter(templink__isnull=True).delete() + TempUser.objects.filter(expires__lt=datetime.now(timezone.utc).replace(tzinfo=timezone.utc).timestamp()).delete() diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/base.html b/allianceauth/services/modules/mumble/templates/services/mumble/base.html new file mode 100644 index 00000000..c08c78a7 --- /dev/null +++ b/allianceauth/services/modules/mumble/templates/services/mumble/base.html @@ -0,0 +1,19 @@ +{% extends "allianceauth/base-bs5.html" %} + +{% load i18n %} + +{% block page_title %} + {% translate "Mumble Temporary Links" %} +{% endblock page_title %} + +{% block header_nav_brand %} + {% translate "Mumble Temporary Links" %} +{% endblock header_nav_brand %} + +{% block content %} +
+
+ {% block mumbletemps %}{% endblock %} +
+
+{% endblock %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/index.html b/allianceauth/services/modules/mumble/templates/services/mumble/index.html new file mode 100644 index 00000000..c7ab0444 --- /dev/null +++ b/allianceauth/services/modules/mumble/templates/services/mumble/index.html @@ -0,0 +1,274 @@ +{% extends "mumbletemps/base.html" %} + +{% load i18n %} +{% load humanize %} +{% load timetags %} + +{% block mumbletemps %} +
+
+
+
{% translate "Mumble Temporary Links" %}
+
+ +
+

+ {% translate 'Temp Links Give Access to mumble with the Guest Group for the preset time.' %} +

+ +

+ {% translate 'Connected users will not be kicked at the end of this period.' %} +

+ +

+ {% translate 'Templink users can be kicked in mumble by typing !kicktemps in mumble chat.' %} +

+ +

+ {% translate 'There are no restrictions on who or how many can use a templink, share wisely.' %} +

+
+
+
+ +
+
+
+
{% translate "Create New Link" %}
+
+ +
+

{% translate "Your link will be displayed on the next page for an easy copy and paste." %}

+ +
+ {% csrf_token %} + +
+ + +
+ + +
+
+
+
+ + {% if tl %} +
+
+
+
{% translate "New Link" %}
+
+ +
+

+ {% translate "Expires in" %}: +

+ +
{{ SITE_URL }}{% url 'mumbletemps:join' tl.link_ref %}
+ + +
+
+
+ {% endif %} + +
+
+
+
{% translate "Active Links" %}
+
+ +
+ + + + + + + + + + + + + + {% for lnk in tl_list %} + + + + + + + + + + + + + + {% endfor %} + +
{% translate "Creator" %}{% translate "Key" %}{% translate "Time Left" %}{% translate "Copy" %}{% translate "Nuke" %}
+ {{ lnk.creator.character_name }} + {{ lnk.creator.character_name }}{{ lnk.link_ref }} + + + {% translate "Nuke Link!" %} +
+
+
+
+ + {% if ex_tl_list.count > 0 %} +
+
+
+
{% translate "Expired Links" %}
+
+ +
+ + + + + + + + + + + + {% for lnk in ex_tl_list %} + + + + + + + + + + {% endfor %} + +
{% translate "Creator" %}{% translate "Key" %}{% translate "Nuke" %}
+ {{ lnk.creator.character_name }} + {{ lnk.creator.character_name }}{{ lnk.link_ref }} + {% translate "Nuke Link!" %} +
+
+
+
+ {% endif %} +{% endblock mumbletemps %} + +{% block extra_javascript %} + {% include 'bundles/clipboard-js.html' %} + + + + {% include "bundles/moment-js.html" with locale=True %} + {% include "bundles/timers-js.html" %} + + +{% endblock extra_javascript %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/link.html b/allianceauth/services/modules/mumble/templates/services/mumble/link.html new file mode 100644 index 00000000..34f41aa2 --- /dev/null +++ b/allianceauth/services/modules/mumble/templates/services/mumble/link.html @@ -0,0 +1,121 @@ +{% extends "mumbletemps/base.html" %} + +{% load static %} +{% load i18n %} +{% load timetags %} + +{% block mumbletemps %} +
+

+ {{ link.creator.character_name }} +

+ +

+ {% blocktranslate with character=link.creator.character_name %} + {{ character }} has invited you to join Mumble! + {% endblocktranslate %} +

+ +

+ {% translate "Time Remaining" %}: +

+

+ {% translate "Link Ref" %}: {{ link.link_ref }} +

+ +

+ + {% translate 'Mumble' %} + + + {% translate "Click to Join Mumble as" %}: {{ temp_user.name }} + + +

+ +
+
+
+
+
{% translate "Manually Connect" %}
+
+ +
+

+ {% translate "If you have problems with the application link above, please use the following in Mumble's connection dialog." %} +

+

+ {% translate "Mumble URL" %}: {{ mumble }} +

+

+ {% translate "Username" %}: {{ temp_user.username }} +

+

+ {% translate "Password" %}: {{ temp_user.password }} +

+
+
+
+
+
+ + {% include "bundles/moment-js.html" with locale=True %} + {% include "bundles/timers-js.html" %} + + +{% endblock mumbletemps %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/login.html b/allianceauth/services/modules/mumble/templates/services/mumble/login.html new file mode 100644 index 00000000..f7bea38a --- /dev/null +++ b/allianceauth/services/modules/mumble/templates/services/mumble/login.html @@ -0,0 +1,140 @@ +{% extends "mumbletemps/base.html" %} + +{% load static %} +{% load i18n %} +{% load timetags %} + +{% block mumbletemps %} +
+ + + +
+ + {% include "bundles/moment-js.html" with locale=True %} + {% include "bundles/timers-js.html" %} + + + {% endblock mumbletemps %} diff --git a/allianceauth/services/modules/mumble/urls.py b/allianceauth/services/modules/mumble/urls.py index d3276e0b..4d9a5340 100644 --- a/allianceauth/services/modules/mumble/urls.py +++ b/allianceauth/services/modules/mumble/urls.py @@ -1,4 +1,4 @@ -from django.urls import include, path +from django.urls import include, path, re_path from . import views @@ -14,6 +14,10 @@ module_urls = [ path('ajax/connection_history_data', views.connection_history_data, name="connection_history_data"), path('ajax/release_counts_data', views.release_counts_data, name="release_counts_data"), path('ajax/release_pie_chart_data', views.release_pie_chart_data, name="release_pie_chart_data"), + # Temp Links + path("", views.index, name="index"), + re_path(r"^join/(?P[\w\-]+)/$", views.link, name="join"), + re_path(r"^nuke/(?P[\w\-]+)/$", views.nuke, name="nuke"), ] urlpatterns = [ diff --git a/allianceauth/services/modules/mumble/views.py b/allianceauth/services/modules/mumble/views.py index 7dd07c82..f975ff86 100644 --- a/allianceauth/services/modules/mumble/views.py +++ b/allianceauth/services/modules/mumble/views.py @@ -1,15 +1,23 @@ +from datetime import datetime, timedelta, timezone import logging +from allianceauth.authentication.models import get_guest_state +from allianceauth.eveonline.models import EveCharacter from allianceauth.services.forms import ServicePasswordModelForm from allianceauth.services.abstract import BaseCreatePasswordServiceAccountView, BaseDeactivateServiceAccountView, \ BaseResetPasswordServiceAccountView, BaseSetPasswordServiceAccountView +from allianceauth.services.hooks import NameFormatter +from allianceauth.services.modules.mumble.auth_hooks import MumbleService from django.conf import settings +from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.db.models import Count -from django.http import HttpResponse, JsonResponse -from django.shortcuts import render +from django.http import Http404, HttpResponse, JsonResponse +from django.shortcuts import redirect, render +from django.utils.crypto import get_random_string -from .models import MumbleUser +from .models import MumbleUser, TempLink, TempUser logger = logging.getLogger(__name__) @@ -88,3 +96,186 @@ def release_pie_chart_data(request) -> JsonResponse: "labels": list(release_counts.values_list("release", flat=True)), "values": list(release_counts.values_list("user_count", flat=True)), }) + +class PseudoProfile: + def __init__(self, main): + self.main_character = main + self.state = get_guest_state() + + +class PseudoUser: + def __init__(self, main, username): + self.username = username + self.profile = PseudoProfile(main) + + +@login_required +@permission_required("mumbletemps.create_new_links") +def index(request): + tl = None + + if request.method == "POST": + duration = request.POST.get("time") + + if duration in ["3", "6", "12", "24"]: + expiry = datetime.now().replace(tzinfo=timezone.utc) + timedelta(hours=int(duration)) + tl = TempLink.objects.create( + creator=request.user.profile.main_character, + link_ref=get_random_string(15), + expires=expiry.timestamp(), + ) + tl.save() + + tl_list = TempLink.objects.prefetch_related("creator").filter( + expires__gte=datetime.datetime.utcnow() + .replace(tzinfo=datetime.timezone.utc) + .timestamp() + ) + ex_tl_list = TempLink.objects.prefetch_related("creator").filter( + expires__lt=datetime.datetime.utcnow() + .replace(tzinfo=datetime.timezone.utc) + .timestamp() + ) + + context = { + "tl": tl, + "text": "Make Links", + "tl_list": tl_list, + "ex_tl_list": ex_tl_list, + } + + return render( + request=request, template_name="mumbletemps/index.html", context=context + ) + + +def link(request, link_ref): + try: + templink = TempLink.objects.get(link_ref=link_ref) + except ObjectDoesNotExist: + raise Http404("Temp Link Does not Exist") + + token = _check_callback(request=request) + if token: + return link_sso(request=request, token=token, link=templink) + + if app_settings.MUMBLE_TEMPS_FORCE_SSO: # default always SSO + # prompt the user to log in for a new token + return sso_redirect(request=request, scopes=["publicData"]) + + if request.method == "POST": # ok so maybe we want to let some other people in too. + if request.POST.get("sso", False) == "False": # they picked user + name = request.POST.get("name", False) + association = request.POST.get("association", False) + + return link_username( + request=request, name=name, association=association, link=templink + ) + elif request.POST.get("sso", False) == "True": # they picked SSO + # prompt the user to log in for a new token + return sso_redirect(request=request, scopes=["publicData"]) + + context = {"link": templink} + + return render( + request=request, template_name="mumbletemps/login.html", context=context + ) + + +def link_username(request, name, association, link): + username = get_random_string(length=10) + + while TempUser.objects.filter(username=username).exists(): # force unique + username = get_random_string(length=10) + + password = get_random_string(length=15) + + display_name = "{}[{}] {}".format( + app_settings.MUMBLE_TEMPS_LOGIN_PREFIX, association, name + ) + + temp_user = TempUser.objects.create( + username=username, + password=password, + name=display_name, + expires=link.expires, + templink=link, + ) + + connect_url = f"{username}:{password}@{settings.MUMBLE_URL}" + + context = { + "temp_user": temp_user, + "link": link, + "connect_url": connect_url, + "mumble": settings.MUMBLE_URL, + } + + return render( + request=request, template_name="mumbletemps/link.html", context=context + ) + + +def link_sso(request, token, link): + try: + char = EveCharacter.objects.get(character_id=token.character_id) + except ObjectDoesNotExist: + try: # create a new character, we should not get here. + char = EveCharacter.objects.update_character( + character_id=token.character_id + ) + except: # noqa: E722 + pass # Yeah… ain't gonna happen + except MultipleObjectsReturned: + pass # authenticator won't care…, but the DB will be unhappy. + + username = get_random_string(length=10) + + while TempUser.objects.filter(username=username).exists(): # force unique + username = get_random_string(length=10) + + password = get_random_string(length=15) + + display_name = "{}{}".format( + app_settings.MUMBLE_TEMPS_SSO_PREFIX, + NameFormatter( + service=MumbleService(), user=PseudoUser(main=char, username=username) + ).format_name(), + ) + + temp_user = TempUser.objects.create( + username=username, + password=password, + name=display_name, + expires=link.expires, + templink=link, + character_id=char.character_id, + ) + + connect_url = f"{username}:{password}@{settings.MUMBLE_URL}" + + context = { + "temp_user": temp_user, + "link": link, + "connect_url": connect_url, + "mumble": settings.MUMBLE_URL, + } + + return render( + request=request, template_name="mumbletemps/link.html", context=context + ) + + +@login_required +@permission_required("mumbletemps.create_new_links") +def nuke(request, link_ref): + try: + TempLink.objects.get(link_ref=link_ref).delete() + TempUser.objects.filter(templink__isnull=True).delete() + + messages.success(request=request, message=f"Deleted Token {link_ref}") + except: # noqa: E722 + messages.error(request=request, message=f"Deleted Token {link_ref}") + pass # Crappy link + + return redirect(to="mumbletemps:index") From baf3c85ac7b8746c6051c61597fd72c722a4a198 Mon Sep 17 00:00:00 2001 From: Joel Falknau Date: Sun, 29 Dec 2024 17:36:04 +1000 Subject: [PATCH 03/14] Cleanup of templinks merge --- .../services/modules/mumble/app_settings.py | 5 ++ .../services/modules/mumble/auth_hooks.py | 16 ++--- .../services/modules/mumble/managers.py | 60 ------------------- .../services/modules/mumble/models.py | 53 +++++++++++++--- allianceauth/services/modules/mumble/tasks.py | 60 +------------------ allianceauth/services/modules/mumble/tests.py | 26 -------- allianceauth/services/modules/mumble/views.py | 24 ++------ 7 files changed, 63 insertions(+), 181 deletions(-) create mode 100644 allianceauth/services/modules/mumble/app_settings.py delete mode 100644 allianceauth/services/modules/mumble/managers.py diff --git a/allianceauth/services/modules/mumble/app_settings.py b/allianceauth/services/modules/mumble/app_settings.py new file mode 100644 index 00000000..df03155e --- /dev/null +++ b/allianceauth/services/modules/mumble/app_settings.py @@ -0,0 +1,5 @@ +from django.conf import settings + +MUMBLE_TEMPS_FORCE_SSO = getattr(settings, "MUMBLE_TEMPS_FORCE_SSO", True) +MUMBLE_TEMPS_SSO_PREFIX = getattr(settings, "MUMBLE_TEMPS_SSO_PREFIX", "[TEMP]") +MUMBLE_TEMPS_LOGIN_PREFIX = getattr(settings, "MUMBLE_TEMPS_LOGIN_PREFIX", "[*TEMP]") diff --git a/allianceauth/services/modules/mumble/auth_hooks.py b/allianceauth/services/modules/mumble/auth_hooks.py index f1522e0a..f395531d 100644 --- a/allianceauth/services/modules/mumble/auth_hooks.py +++ b/allianceauth/services/modules/mumble/auth_hooks.py @@ -7,11 +7,10 @@ from django.template.loader import render_to_string from allianceauth.notifications import notify from allianceauth import hooks -from allianceauth.services.hooks import ServicesHook, UrlHook +from allianceauth.services.hooks import ServicesHook from .tasks import MumbleTasks from .models import MumbleUser from .urls import urlpatterns -from allianceauth.services.modules.mumble import urls from django.utils.translation import gettext_lazy as _ logger = logging.getLogger(__name__) @@ -43,11 +42,6 @@ class MumbleService(ServicesHook): if MumbleTasks.has_account(user): MumbleTasks.update_groups.delay(user.pk) - def sync_nickname(self, user): - logger.debug(f"Updating {self.name} nickname for {user}") - if MumbleTasks.has_account(user): - MumbleTasks.update_display_name.apply_async(args=[user.pk], countdown=5) # cooldown on this task to ensure DB clean when syncing - def validate_user(self, user): if MumbleTasks.has_account(user) and not self.service_active_for_user(user): self.delete_user(user, notify_user=True) @@ -74,14 +68,14 @@ class MumbleService(ServicesHook): 'username': request.user.mumble.username if MumbleTasks.has_account(request.user) else '', }, request=request) + @hooks.register('services_hook') def register_mumble_service() -> ServicesHook: return MumbleService() class MumbleMenuItem(MenuItemHook): - def __init__(self): - # setup menu entry for sidebar + def __init__(self) -> None: MenuItemHook.__init__( self=self, text=_("Mumble Temp Links"), @@ -90,12 +84,12 @@ class MumbleMenuItem(MenuItemHook): navactive=["mumble:index"], ) - def render(self, request): + def render(self, request) -> str: if request.user.has_perm("mumble.create_new_templinks"): return MenuItemHook.render(self, request) return "" @hooks.register("menu_item_hook") -def register_menu(): +def register_menu() -> MumbleMenuItem: return MumbleMenuItem() diff --git a/allianceauth/services/modules/mumble/managers.py b/allianceauth/services/modules/mumble/managers.py deleted file mode 100644 index 5ee02357..00000000 --- a/allianceauth/services/modules/mumble/managers.py +++ /dev/null @@ -1,60 +0,0 @@ -import random -import string -from passlib.hash import bcrypt_sha256 - -from django.db import models -from allianceauth.services.hooks import NameFormatter -import logging - -logger = logging.getLogger(__name__) - - -class MumbleManager(models.Manager): - - @staticmethod - def get_display_name(user): - from .auth_hooks import MumbleService - return NameFormatter(MumbleService(), user).format_name() - - @staticmethod - def get_username(user): - return user.profile.main_character.character_name # main character as the user.username may be incorect - - @staticmethod - def sanitise_username(username): - return username.replace(" ", "_") - - @staticmethod - def generate_random_pass(): - return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) - - @staticmethod - def gen_pwhash(password): - return bcrypt_sha256.encrypt(password.encode('utf-8')) - - def create(self, user): - try: - username = self.get_username(user) - logger.debug(f"Creating mumble user with username {username}") - username_clean = self.sanitise_username(username) - display_name = self.get_display_name(user) - password = self.generate_random_pass() - pwhash = self.gen_pwhash(password) - logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format( - username_clean, pwhash[0:5])) - logger.info(f"Creating mumble user {username_clean}") - - result = super().create( - user=user, - username=username_clean, - pwhash=pwhash, - display_name=display_name) - result.update_groups() - result.credentials.update({'username': result.username, 'password': password}) - return result - except AttributeError: # No Main or similar errors - return False - return False - - def user_exists(self, username) -> bool: - return self.filter(username=username).exists() diff --git a/allianceauth/services/modules/mumble/models.py b/allianceauth/services/modules/mumble/models.py index 8e04f5b2..28217c8a 100644 --- a/allianceauth/services/modules/mumble/models.py +++ b/allianceauth/services/modules/mumble/models.py @@ -1,7 +1,9 @@ +import random +import string from typing import LiteralString from allianceauth.eveonline.models import EveCharacter from allianceauth.services.hooks import NameFormatter -from allianceauth.services.modules.mumble.managers import MumbleManager +from passlib.hash import bcrypt_sha256 from django.db import models from allianceauth.services.abstract import AbstractServiceModel import logging @@ -48,7 +50,21 @@ class MumbleUser(AbstractServiceModel): blank=True, null=True, editable=False, help_text="Timestamp of the users Last Disconnection from Mumble") - objects = MumbleManager() + @staticmethod + def get_username(user) -> str: + return user.profile.main_character.character_name # main character as the user.username may be incorrect + + @staticmethod + def sanitise_username(username) -> str: + return username.replace(" ", "_") + + @staticmethod + def generate_random_pass() -> str: + return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) + + @staticmethod + def gen_pwhash(password) -> str: + return bcrypt_sha256.encrypt(password.encode('utf-8')) def __str__(self) -> str: return f"{self.username}" @@ -57,8 +73,8 @@ class MumbleUser(AbstractServiceModel): init_password = password logger.debug(f"Updating mumble user {self.user} password.") if not password: - password = MumbleManager.generate_random_pass() - pwhash = MumbleManager.gen_pwhash(password) + password = self.generate_random_pass() + pwhash = self.gen_pwhash(password) logger.debug(f"Proceeding with mumble user {self.user} password update - pwhash starts with {pwhash[0:5]}") self.pwhash = pwhash self.hashfn = self.HashFunction.SHA256 @@ -66,10 +82,10 @@ class MumbleUser(AbstractServiceModel): if init_password is None: self.credentials.update({'username': self.username, 'password': password}) - def reset_password(self): + def reset_password(self) -> None: self.update_password() - def group_string(self) -> LiteralString: + def group_string(self) -> str: """Return a Mumble Safe Formatted List of Groups This used to be a ModelField, generated on the fly now with DjangoAuthenticatorTM @@ -81,10 +97,33 @@ class MumbleUser(AbstractServiceModel): groups_str.append(str(group.name)) return ','.join({g.replace(' ', '-') for g in groups_str}) - def get_display_name(self): + def get_display_name(self) -> str: from .auth_hooks import MumbleService return NameFormatter(MumbleService(), self.user).format_name() + def display_name(self) -> str: + return self.get_display_name() + + def create(self, user): + try: + username = self.get_username(user) + logger.debug(f"Creating mumble user with username {username}") + username_clean = self.sanitise_username(username) + password = self.generate_random_pass() + pwhash = self.gen_pwhash(password) + logger.debug("Proceeding with mumble user creation: clean username {}, pwhash starts with {}".format( + username_clean, pwhash[0:5])) + logger.info(f"Creating mumble user {username_clean}") + + result = self.objects.create(user=user, username=username_clean, pwhash=pwhash) + result.credentials.update({'username': result.username, 'password': password}) + return result + except AttributeError: # No Main or similar errors + return False + + def user_exists(self, username) -> bool: + return self.objects.filter(username=username).exists() + class Meta: verbose_name = _("User") verbose_name_plural = _("Users") diff --git a/allianceauth/services/modules/mumble/tasks.py b/allianceauth/services/modules/mumble/tasks.py index e02a1c86..c1cffedb 100644 --- a/allianceauth/services/modules/mumble/tasks.py +++ b/allianceauth/services/modules/mumble/tasks.py @@ -26,63 +26,9 @@ class MumbleTasks: logger.info("Deleting all MumbleUser models") MumbleUser.objects.all().delete() - @staticmethod - @shared_task(bind=True, name="mumble.update_groups", base=QueueOnce) - def update_groups(self, pk): - user = User.objects.get(pk=pk) - logger.debug("Updating mumble groups for user %s" % user) - if MumbleTasks.has_account(user): - try: - if not user.mumble.update_groups(): - raise Exception("Group sync failed") - logger.debug("Updated user %s mumble groups." % user) - return True - except MumbleUser.DoesNotExist: - logger.info(f"Mumble group sync failed for {user}, user does not have a mumble account") - except: - logger.exception("Mumble group sync failed for %s, retrying in 10 mins" % user) - raise self.retry(countdown=60 * 10) - else: - logger.debug("User %s does not have a mumble account, skipping" % user) - return False - - @staticmethod - @shared_task(bind=True, name="mumble.update_display_name", base=QueueOnce) - def update_display_name(self, pk): - user = User.objects.get(pk=pk) - logger.debug("Updating mumble groups for user %s" % user) - if MumbleTasks.has_account(user): - try: - if not user.mumble.update_display_name(): - raise Exception("Display Name Sync failed") - logger.debug("Updated user %s mumble display name." % user) - return True - except MumbleUser.DoesNotExist: - logger.info(f"Mumble display name sync failed for {user}, user does not have a mumble account") - except: - logger.exception("Mumble display name sync failed for %s, retrying in 10 mins" % user) - raise self.retry(countdown=60 * 10) - else: - logger.debug("User %s does not have a mumble account, skipping" % user) - return False - - @staticmethod - @shared_task(name="mumble.update_all_groups") - def update_all_groups(): - logger.debug("Updating ALL mumble groups") - for mumble_user in MumbleUser.objects.exclude(username__exact=''): - MumbleTasks.update_groups.delay(mumble_user.user.pk) - - @staticmethod - @shared_task(name="mumble.update_all_display_names") - def update_all_display_names(): - logger.debug("Updating ALL mumble display names") - for mumble_user in MumbleUser.objects.exclude(username__exact=''): - MumbleTasks.update_display_name.delay(mumble_user.user.pk) - @shared_task -def tidy_up_temp_links(): - TempLink.objects.filter(expires__lt=datetime.now(timezone.utc).replace(tzinfo=timezone.utc).timestamp()).delete() +def tidy_up_temp_links() -> None: + TempLink.objects.filter(expires__lt=datetime.now(timezone.utc).timestamp()).delete() TempUser.objects.filter(templink__isnull=True).delete() - TempUser.objects.filter(expires__lt=datetime.now(timezone.utc).replace(tzinfo=timezone.utc).timestamp()).delete() + TempUser.objects.filter(expires__lt=datetime.now(timezone.utc).timestamp()).delete() diff --git a/allianceauth/services/modules/mumble/tests.py b/allianceauth/services/modules/mumble/tests.py index e17d0267..5c4f3acb 100644 --- a/allianceauth/services/modules/mumble/tests.py +++ b/allianceauth/services/modules/mumble/tests.py @@ -47,32 +47,6 @@ class MumbleHooksTestCase(TestCase): self.assertTrue(service.service_active_for_user(member)) self.assertFalse(service.service_active_for_user(none_user)) - @mock.patch(MODULE_PATH + '.tasks.User.mumble') - def test_update_all_groups(self, mumble): - service = self.service() - service.update_all_groups() - # Check member and blue user have groups updated - self.assertTrue(mumble.update_groups.called) - self.assertEqual(mumble.update_groups.call_count, 1) - - def test_update_groups(self): - # Check member has Member group updated - service = self.service() - member = User.objects.get(username=self.member) - member.mumble.groups = '' # Remove the group set in setUp - member.mumble.save() - - service.update_groups(member) - - mumble_user = MumbleUser.objects.get(user=member) - self.assertIn(DEFAULT_AUTH_GROUP, mumble_user.groups) - - # Check none user does not have groups updated - service = self.service() - none_user = User.objects.get(username=self.none_user) - result = service.update_groups(none_user) - self.assertFalse(result) - def test_validate_user(self): service = self.service() # Test member is not deleted diff --git a/allianceauth/services/modules/mumble/views.py b/allianceauth/services/modules/mumble/views.py index f975ff86..337c0dbe 100644 --- a/allianceauth/services/modules/mumble/views.py +++ b/allianceauth/services/modules/mumble/views.py @@ -6,8 +6,7 @@ from allianceauth.eveonline.models import EveCharacter from allianceauth.services.forms import ServicePasswordModelForm from allianceauth.services.abstract import BaseCreatePasswordServiceAccountView, BaseDeactivateServiceAccountView, \ BaseResetPasswordServiceAccountView, BaseSetPasswordServiceAccountView -from allianceauth.services.hooks import NameFormatter -from allianceauth.services.modules.mumble.auth_hooks import MumbleService +from allianceauth.services.modules.mumble import app_settings from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required @@ -16,6 +15,7 @@ from django.db.models import Count from django.http import Http404, HttpResponse, JsonResponse from django.shortcuts import redirect, render from django.utils.crypto import get_random_string +from esi.views import sso_redirect from .models import MumbleUser, TempLink, TempUser @@ -126,16 +126,8 @@ def index(request): ) tl.save() - tl_list = TempLink.objects.prefetch_related("creator").filter( - expires__gte=datetime.datetime.utcnow() - .replace(tzinfo=datetime.timezone.utc) - .timestamp() - ) - ex_tl_list = TempLink.objects.prefetch_related("creator").filter( - expires__lt=datetime.datetime.utcnow() - .replace(tzinfo=datetime.timezone.utc) - .timestamp() - ) + tl_list = TempLink.objects.prefetch_related("creator").filter(expires__gte=datetime.now(timezone.utc)) + ex_tl_list = TempLink.objects.prefetch_related("creator").filter(expires__lt=datetime.now(timezone.utc)) context = { "tl": tl, @@ -236,17 +228,9 @@ def link_sso(request, token, link): password = get_random_string(length=15) - display_name = "{}{}".format( - app_settings.MUMBLE_TEMPS_SSO_PREFIX, - NameFormatter( - service=MumbleService(), user=PseudoUser(main=char, username=username) - ).format_name(), - ) - temp_user = TempUser.objects.create( username=username, password=password, - name=display_name, expires=link.expires, templink=link, character_id=char.character_id, From 168e6cc29072fbdab72356404db11d24c1240fa8 Mon Sep 17 00:00:00 2001 From: Joel Falknau Date: Sun, 29 Dec 2024 21:14:40 +1000 Subject: [PATCH 04/14] remove temps from more templates --- .../templates/services/mumble/base.html | 4 +- .../templates/services/mumble/index.html | 131 ++++-------------- .../templates/services/mumble/link.html | 15 +- .../templates/services/mumble/login.html | 21 ++- 4 files changed, 46 insertions(+), 125 deletions(-) diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/base.html b/allianceauth/services/modules/mumble/templates/services/mumble/base.html index c08c78a7..590da6fa 100644 --- a/allianceauth/services/modules/mumble/templates/services/mumble/base.html +++ b/allianceauth/services/modules/mumble/templates/services/mumble/base.html @@ -11,9 +11,9 @@ {% endblock header_nav_brand %} {% block content %} -
+
- {% block mumbletemps %}{% endblock %} + {% block mumble %}{% endblock mumble %}
{% endblock %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/index.html b/allianceauth/services/modules/mumble/templates/services/mumble/index.html index c7ab0444..0da8824e 100644 --- a/allianceauth/services/modules/mumble/templates/services/mumble/index.html +++ b/allianceauth/services/modules/mumble/templates/services/mumble/index.html @@ -1,10 +1,9 @@ -{% extends "mumbletemps/base.html" %} +{% extends "services/mumble/base.html" %} {% load i18n %} {% load humanize %} -{% load timetags %} -{% block mumbletemps %} +{% block mumble %}
@@ -13,20 +12,29 @@

- {% translate 'Temp Links Give Access to mumble with the Guest Group for the preset time.' %} + {% blocktranslate trimmed with bold_start="" bold_end="" %} + Temp Links Give Access to mumble with the {{ bold_start }}Guest{{ bold_end }} Group for the preset time + {% endblocktranslate %}

- {% translate 'Connected users will not be kicked at the end of this period.' %} + {% blocktranslate trimmed with bold_start="" bold_end="" %} + Connected users {{ bold_start }}will not{{ bold_end }} be kicked at the end of this period. + {% endblocktranslate %}

- {% translate 'Templink users can be kicked in mumble by typing !kicktemps in mumble chat.' %} + {% blocktranslate trimmed with badge_start="" badge_end="" %} + Templink users can be kicked in mumble by typing {{ badge_start }}!kicktemps{{ badge_end }} in mumble chat. + {% endblocktranslate %}

- {% translate 'There are no restrictions on who or how many can use a templink, share wisely.' %} + {% blocktranslate trimmed with bold_start="" bold_end="" %} + There are {{ bold_start }}no restrictions{{ bold_end }} on who or how many can use a templink, share wisely. + {% endblocktranslate %}

+
@@ -40,7 +48,7 @@

{% translate "Your link will be displayed on the next page for an easy copy and paste." %}

-
+ {% csrf_token %}
@@ -71,9 +79,9 @@ {% translate "Expires in" %}:

-
{{ SITE_URL }}{% url 'mumbletemps:join' tl.link_ref %}
+
{{ SITE_URL }}{% url 'mumble:join' tl.link_ref %}
-
@@ -116,13 +124,13 @@ - {% translate "Nuke Link!" %} + {% translate "Nuke Link!" %} {% endfor %} @@ -162,7 +170,7 @@ {{ lnk.link_ref }} - {% translate "Nuke Link!" %} + {% translate "Nuke Link!" %} {% endfor %} @@ -172,103 +180,18 @@
{% endif %} -{% endblock mumbletemps %} +{% endblock mumble %} {% block extra_javascript %} - {% include 'bundles/clipboard-js.html' %} + {% include "bundles/clipboard-js.html" %} - {% include "bundles/moment-js.html" with locale=True %} - {% include "bundles/timers-js.html" %} - - {% endblock extra_javascript %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/link.html b/allianceauth/services/modules/mumble/templates/services/mumble/link.html index 34f41aa2..55db3fe5 100644 --- a/allianceauth/services/modules/mumble/templates/services/mumble/link.html +++ b/allianceauth/services/modules/mumble/templates/services/mumble/link.html @@ -1,19 +1,18 @@ -{% extends "mumbletemps/base.html" %} +{% extends "services/mumble/base.html" %} {% load static %} {% load i18n %} -{% load timetags %} -{% block mumbletemps %} +{% block mumble %}

{{ link.creator.character_name }}

- {% blocktranslate with character=link.creator.character_name %} + {% blocktranslatelate with character=link.creator.character_name %} {{ character }} has invited you to join Mumble! - {% endblocktranslate %} + {% endblocktranslatelate %}

@@ -25,7 +24,7 @@

- {% translate 'Mumble' %} + {% translate 'Mumble' %} {% translate "Click to Join Mumble as" %}: {{ temp_user.name }} @@ -67,7 +66,7 @@ const locale = '{{ LANGUAGE_CODE }}'; const timers = [{ 'id': '{{ link.link_ref }}', - 'targetDate': moment('{{ link.expires|print_timestamp| date:"c" }}'), + 'targetDate': moment('{{ link.expires| date:"c" }}'), 'expired': false }]; @@ -118,4 +117,4 @@ // Start timed updates setInterval(timedUpdate, 1000); -{% endblock mumbletemps %} +{% endblock mumble %} diff --git a/allianceauth/services/modules/mumble/templates/services/mumble/login.html b/allianceauth/services/modules/mumble/templates/services/mumble/login.html index f7bea38a..a327ced9 100644 --- a/allianceauth/services/modules/mumble/templates/services/mumble/login.html +++ b/allianceauth/services/modules/mumble/templates/services/mumble/login.html @@ -1,20 +1,19 @@ -{% extends "mumbletemps/base.html" %} +{% extends "services/mumble/base.html" %} {% load static %} {% load i18n %} -{% load timetags %} -{% block mumbletemps %} +{% block mumble %}

-