2016-08-14 04:41:52 +05:30
// Copyright 2016 The Gogs Authors. All rights reserved.
2016-12-21 17:43:17 +05:30
// Copyright 2016 The Gitea Authors. All rights reserved.
2016-08-14 04:41:52 +05:30
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
2020-09-09 03:36:39 +05:30
"context"
2018-10-31 03:04:25 +05:30
"errors"
2016-08-14 04:41:52 +05:30
"fmt"
2018-09-12 20:16:02 +05:30
"os"
2020-12-17 13:27:32 +05:30
"strings"
2018-09-12 20:16:02 +05:30
"text/tabwriter"
2016-08-14 04:41:52 +05:30
2021-12-10 13:44:24 +05:30
asymkey_model "code.gitea.io/gitea/models/asymkey"
2022-08-25 08:01:57 +05:30
auth_model "code.gitea.io/gitea/models/auth"
2021-09-24 17:02:56 +05:30
"code.gitea.io/gitea/models/db"
2022-06-06 13:31:49 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2019-03-27 15:03:00 +05:30
"code.gitea.io/gitea/modules/git"
2020-01-21 01:31:19 +05:30
"code.gitea.io/gitea/modules/graceful"
2017-12-31 20:15:46 +05:30
"code.gitea.io/gitea/modules/log"
2019-10-14 20:54:26 +05:30
pwd "code.gitea.io/gitea/modules/password"
2020-01-21 01:31:19 +05:30
repo_module "code.gitea.io/gitea/modules/repository"
2016-11-10 21:54:48 +05:30
"code.gitea.io/gitea/modules/setting"
2021-04-22 22:18:47 +05:30
"code.gitea.io/gitea/modules/storage"
2022-04-30 01:08:11 +05:30
"code.gitea.io/gitea/modules/util"
2021-09-24 17:02:56 +05:30
auth_service "code.gitea.io/gitea/services/auth"
2021-07-24 15:46:34 +05:30
"code.gitea.io/gitea/services/auth/source/oauth2"
2022-01-13 04:24:53 +05:30
"code.gitea.io/gitea/services/auth/source/smtp"
2021-11-17 20:47:31 +05:30
repo_service "code.gitea.io/gitea/services/repository"
2021-11-18 23:12:27 +05:30
user_service "code.gitea.io/gitea/services/user"
2017-04-12 13:14:54 +05:30
"github.com/urfave/cli"
2016-08-14 04:41:52 +05:30
)
var (
2016-11-04 17:12:18 +05:30
// CmdAdmin represents the available admin sub-command.
2016-08-14 04:41:52 +05:30
CmdAdmin = cli . Command {
Name : "admin" ,
2018-01-13 03:46:49 +05:30
Usage : "Command line interface to perform common administrative operations" ,
2016-08-14 04:41:52 +05:30
Subcommands : [ ] cli . Command {
2020-10-16 08:18:38 +05:30
subcmdUser ,
2017-12-31 20:15:46 +05:30
subcmdRepoSyncReleases ,
2018-05-17 07:05:07 +05:30
subcmdRegenerate ,
2018-09-12 20:16:02 +05:30
subcmdAuth ,
2020-10-25 02:08:14 +05:30
subcmdSendMail ,
2016-08-14 04:41:52 +05:30
} ,
}
2020-10-16 08:18:38 +05:30
subcmdUser = cli . Command {
Name : "user" ,
Usage : "Modify users" ,
Subcommands : [ ] cli . Command {
microcmdUserCreate ,
microcmdUserList ,
microcmdUserChangePassword ,
microcmdUserDelete ,
2022-03-05 01:23:35 +05:30
microcmdUserGenerateAccessToken ,
2020-10-16 08:18:38 +05:30
} ,
}
microcmdUserList = cli . Command {
Name : "list" ,
Usage : "List users" ,
Action : runListUsers ,
Flags : [ ] cli . Flag {
cli . BoolFlag {
Name : "admin" ,
Usage : "List only admin users" ,
} ,
} ,
}
microcmdUserCreate = cli . Command {
Name : "create" ,
2016-08-14 04:41:52 +05:30
Usage : "Create a new user in database" ,
Action : runCreateUser ,
Flags : [ ] cli . Flag {
2016-11-10 03:48:22 +05:30
cli . StringFlag {
Name : "name" ,
2019-04-09 20:51:55 +05:30
Usage : "Username. DEPRECATED: use username instead" ,
} ,
cli . StringFlag {
Name : "username" ,
2016-11-10 03:48:22 +05:30
Usage : "Username" ,
} ,
cli . StringFlag {
Name : "password" ,
2016-11-10 04:02:24 +05:30
Usage : "User password" ,
2016-11-10 03:48:22 +05:30
} ,
cli . StringFlag {
Name : "email" ,
2016-11-10 04:02:24 +05:30
Usage : "User email address" ,
2016-11-10 03:48:22 +05:30
} ,
cli . BoolFlag {
Name : "admin" ,
Usage : "User is an admin" ,
} ,
2018-10-31 03:04:25 +05:30
cli . BoolFlag {
Name : "random-password" ,
Usage : "Generate a random password for the user" ,
} ,
2018-10-21 03:35:01 +05:30
cli . BoolFlag {
Name : "must-change-password" ,
2019-10-30 23:58:14 +05:30
Usage : "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)" ,
2018-10-21 03:35:01 +05:30
} ,
2018-10-31 03:04:25 +05:30
cli . IntFlag {
Name : "random-password-length" ,
Usage : "Length of the random password to be generated" ,
Value : 12 ,
} ,
2019-05-05 03:33:10 +05:30
cli . BoolFlag {
Name : "access-token" ,
Usage : "Generate access token for the user" ,
} ,
2022-04-30 01:08:11 +05:30
cli . BoolFlag {
Name : "restricted" ,
Usage : "Make a restricted user account" ,
} ,
2016-08-14 04:41:52 +05:30
} ,
}
2017-03-20 13:53:38 +05:30
2020-10-16 08:18:38 +05:30
microcmdUserChangePassword = cli . Command {
2017-03-20 13:53:38 +05:30
Name : "change-password" ,
Usage : "Change a user's password" ,
Action : runChangePassword ,
Flags : [ ] cli . Flag {
cli . StringFlag {
Name : "username,u" ,
Value : "" ,
Usage : "The user to change password for" ,
} ,
cli . StringFlag {
Name : "password,p" ,
Value : "" ,
Usage : "New password to set for user" ,
} ,
} ,
}
2017-12-31 20:15:46 +05:30
2020-10-16 08:18:38 +05:30
microcmdUserDelete = cli . Command {
2020-12-17 13:27:32 +05:30
Name : "delete" ,
Usage : "Delete specific user by id, name or email" ,
Flags : [ ] cli . Flag {
cli . Int64Flag {
Name : "id" ,
Usage : "ID of user of the user to delete" ,
} ,
cli . StringFlag {
Name : "username,u" ,
Usage : "Username of the user to delete" ,
} ,
cli . StringFlag {
Name : "email,e" ,
Usage : "Email of the user to delete" ,
} ,
2022-07-14 12:52:09 +05:30
cli . BoolFlag {
Name : "purge" ,
Usage : "Purge user, all their repositories, organizations and comments" ,
} ,
2020-12-17 13:27:32 +05:30
} ,
2020-10-16 08:18:38 +05:30
Action : runDeleteUser ,
}
2022-03-05 01:23:35 +05:30
microcmdUserGenerateAccessToken = cli . Command {
Name : "generate-access-token" ,
Usage : "Generate a access token for a specific user" ,
Flags : [ ] cli . Flag {
cli . StringFlag {
Name : "username,u" ,
Usage : "Username" ,
} ,
cli . StringFlag {
Name : "token-name,t" ,
Usage : "Token name" ,
Value : "gitea-admin" ,
} ,
cli . BoolFlag {
Name : "raw" ,
Usage : "Display only the token value" ,
} ,
} ,
Action : runGenerateAccessToken ,
}
2017-12-31 20:15:46 +05:30
subcmdRepoSyncReleases = cli . Command {
Name : "repo-sync-releases" ,
Usage : "Synchronize repository releases with tags" ,
Action : runRepoSyncReleases ,
}
2018-05-17 07:05:07 +05:30
subcmdRegenerate = cli . Command {
Name : "regenerate" ,
Usage : "Regenerate specific files" ,
Subcommands : [ ] cli . Command {
microcmdRegenHooks ,
microcmdRegenKeys ,
} ,
}
microcmdRegenHooks = cli . Command {
Name : "hooks" ,
Usage : "Regenerate git-hooks" ,
Action : runRegenerateHooks ,
}
microcmdRegenKeys = cli . Command {
Name : "keys" ,
Usage : "Regenerate authorized_keys file" ,
Action : runRegenerateKeys ,
}
2018-09-12 20:16:02 +05:30
subcmdAuth = cli . Command {
Name : "auth" ,
Usage : "Modify external auth providers" ,
Subcommands : [ ] cli . Command {
microcmdAuthAddOauth ,
microcmdAuthUpdateOauth ,
2019-06-18 00:02:20 +05:30
cmdAuthAddLdapBindDn ,
cmdAuthUpdateLdapBindDn ,
cmdAuthAddLdapSimpleAuth ,
cmdAuthUpdateLdapSimpleAuth ,
2022-01-13 04:24:53 +05:30
microcmdAuthAddSMTP ,
microcmdAuthUpdateSMTP ,
2018-09-12 20:16:02 +05:30
microcmdAuthList ,
microcmdAuthDelete ,
} ,
}
microcmdAuthList = cli . Command {
Name : "list" ,
Usage : "List auth sources" ,
Action : runListAuth ,
2020-03-28 02:56:43 +05:30
Flags : [ ] cli . Flag {
cli . IntFlag {
Name : "min-width" ,
Usage : "Minimal cell width including any padding for the formatted table" ,
Value : 0 ,
} ,
cli . IntFlag {
Name : "tab-width" ,
Usage : "width of tab characters in formatted table (equivalent number of spaces)" ,
Value : 8 ,
} ,
cli . IntFlag {
Name : "padding" ,
Usage : "padding added to a cell before computing its width" ,
Value : 1 ,
} ,
cli . StringFlag {
Name : "pad-char" ,
Usage : ` ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result) ` ,
Value : "\t" ,
} ,
cli . BoolFlag {
Name : "vertical-bars" ,
Usage : "Set to true to print vertical bars between columns" ,
} ,
} ,
2018-09-12 20:16:02 +05:30
}
idFlag = cli . Int64Flag {
Name : "id" ,
2019-06-18 00:02:20 +05:30
Usage : "ID of authentication source" ,
2018-09-12 20:16:02 +05:30
}
microcmdAuthDelete = cli . Command {
Name : "delete" ,
Usage : "Delete specific auth source" ,
2020-01-08 05:11:16 +05:30
Flags : [ ] cli . Flag { idFlag } ,
2018-09-12 20:16:02 +05:30
Action : runDeleteAuth ,
}
oauthCLIFlags = [ ] cli . Flag {
cli . StringFlag {
Name : "name" ,
Value : "" ,
Usage : "Application Name" ,
} ,
cli . StringFlag {
Name : "provider" ,
Value : "" ,
Usage : "OAuth2 Provider" ,
} ,
cli . StringFlag {
Name : "key" ,
Value : "" ,
Usage : "Client ID (Key)" ,
} ,
cli . StringFlag {
Name : "secret" ,
Value : "" ,
Usage : "Client Secret" ,
} ,
cli . StringFlag {
Name : "auto-discover-url" ,
Value : "" ,
Usage : "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)" ,
} ,
cli . StringFlag {
Name : "use-custom-urls" ,
Value : "false" ,
Usage : "Use custom URLs for GitLab/GitHub OAuth endpoints" ,
} ,
cli . StringFlag {
Name : "custom-auth-url" ,
Value : "" ,
Usage : "Use a custom Authorization URL (option for GitLab/GitHub)" ,
} ,
cli . StringFlag {
Name : "custom-token-url" ,
Value : "" ,
Usage : "Use a custom Token URL (option for GitLab/GitHub)" ,
} ,
cli . StringFlag {
Name : "custom-profile-url" ,
Value : "" ,
Usage : "Use a custom Profile URL (option for GitLab/GitHub)" ,
} ,
cli . StringFlag {
Name : "custom-email-url" ,
Value : "" ,
Usage : "Use a custom Email URL (option for GitHub)" ,
} ,
2020-12-28 14:09:12 +05:30
cli . StringFlag {
Name : "icon-url" ,
Value : "" ,
Usage : "Custom icon URL for OAuth2 login source" ,
} ,
2021-09-10 22:07:57 +05:30
cli . BoolFlag {
Name : "skip-local-2fa" ,
Usage : "Set to true to skip local 2fa for users authenticated by this source" ,
} ,
2021-12-14 14:07:11 +05:30
cli . StringSliceFlag {
Name : "scopes" ,
Value : nil ,
Usage : "Scopes to request when to authenticate against this OAuth2 source" ,
} ,
cli . StringFlag {
Name : "required-claim-name" ,
Value : "" ,
Usage : "Claim name that has to be set to allow users to login with this source" ,
} ,
cli . StringFlag {
Name : "required-claim-value" ,
Value : "" ,
Usage : "Claim value that has to be set to allow users to login with this source" ,
} ,
cli . StringFlag {
Name : "group-claim-name" ,
Value : "" ,
Usage : "Claim name providing group names for this source" ,
} ,
cli . StringFlag {
Name : "admin-group" ,
Value : "" ,
Usage : "Group Claim value for administrator users" ,
} ,
cli . StringFlag {
Name : "restricted-group" ,
Value : "" ,
Usage : "Group Claim value for restricted users" ,
} ,
2018-09-12 20:16:02 +05:30
}
microcmdAuthUpdateOauth = cli . Command {
Name : "update-oauth" ,
Usage : "Update existing Oauth authentication source" ,
Action : runUpdateOauth ,
Flags : append ( oauthCLIFlags [ : 1 ] , append ( [ ] cli . Flag { idFlag } , oauthCLIFlags [ 1 : ] ... ) ... ) ,
}
microcmdAuthAddOauth = cli . Command {
Name : "add-oauth" ,
Usage : "Add new Oauth authentication source" ,
Action : runAddOauth ,
Flags : oauthCLIFlags ,
}
2020-10-25 02:08:14 +05:30
subcmdSendMail = cli . Command {
Name : "sendmail" ,
Usage : "Send a message to all users" ,
Action : runSendMail ,
Flags : [ ] cli . Flag {
cli . StringFlag {
Name : "title" ,
Usage : ` a title of a message ` ,
Value : "" ,
} ,
cli . StringFlag {
Name : "content" ,
Usage : "a content of a message" ,
Value : "" ,
} ,
cli . BoolFlag {
Name : "force,f" ,
Usage : "A flag to bypass a confirmation step" ,
} ,
} ,
}
2022-01-13 04:24:53 +05:30
smtpCLIFlags = [ ] cli . Flag {
cli . StringFlag {
Name : "name" ,
Value : "" ,
Usage : "Application Name" ,
} ,
cli . StringFlag {
Name : "auth-type" ,
Value : "PLAIN" ,
Usage : "SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5) default PLAIN" ,
} ,
cli . StringFlag {
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
Name : "addr" ,
2022-01-13 04:24:53 +05:30
Value : "" ,
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
Usage : "SMTP Addr" ,
2022-01-13 04:24:53 +05:30
} ,
cli . IntFlag {
Name : "port" ,
Usage : "SMTP Port" ,
} ,
cli . BoolTFlag {
Name : "force-smtps" ,
Usage : "SMTPS is always used on port 465. Set this to force SMTPS on other ports." ,
} ,
cli . BoolTFlag {
Name : "skip-verify" ,
Usage : "Skip TLS verify." ,
} ,
cli . StringFlag {
Name : "helo-hostname" ,
Value : "" ,
Usage : "Hostname sent with HELO. Leave blank to send current hostname" ,
} ,
cli . BoolTFlag {
Name : "disable-helo" ,
Usage : "Disable SMTP helo." ,
} ,
cli . StringFlag {
Name : "allowed-domains" ,
Value : "" ,
Usage : "Leave empty to allow all domains. Separate multiple domains with a comma (',')" ,
} ,
cli . BoolTFlag {
Name : "skip-local-2fa" ,
Usage : "Skip 2FA to log on." ,
} ,
cli . BoolTFlag {
Name : "active" ,
Usage : "This Authentication Source is Activated." ,
} ,
}
microcmdAuthAddSMTP = cli . Command {
Name : "add-smtp" ,
Usage : "Add new SMTP authentication source" ,
Action : runAddSMTP ,
Flags : smtpCLIFlags ,
}
microcmdAuthUpdateSMTP = cli . Command {
Name : "update-smtp" ,
Usage : "Update existing SMTP authentication source" ,
Action : runUpdateSMTP ,
Flags : append ( smtpCLIFlags [ : 1 ] , append ( [ ] cli . Flag { idFlag } , smtpCLIFlags [ 1 : ] ... ) ... ) ,
}
2016-08-14 04:41:52 +05:30
)
2017-03-20 13:53:38 +05:30
func runChangePassword ( c * cli . Context ) error {
2018-01-13 03:46:49 +05:30
if err := argsSet ( c , "username" , "password" ) ; err != nil {
return err
2017-03-20 13:53:38 +05:30
}
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-01-13 03:46:49 +05:30
return err
2017-03-20 13:53:38 +05:30
}
2021-12-17 07:33:39 +05:30
if len ( c . String ( "password" ) ) < setting . MinPasswordLength {
return fmt . Errorf ( "Password is not long enough. Needs to be at least %d" , setting . MinPasswordLength )
}
2019-10-14 20:54:26 +05:30
if ! pwd . IsComplexEnough ( c . String ( "password" ) ) {
return errors . New ( "Password does not meet complexity requirements" )
}
2020-09-09 03:36:39 +05:30
pwned , err := pwd . IsPwned ( context . Background ( ) , c . String ( "password" ) )
if err != nil {
return err
}
if pwned {
return errors . New ( "The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords" )
}
2017-03-20 13:53:38 +05:30
uname := c . String ( "username" )
2022-05-20 19:38:52 +05:30
user , err := user_model . GetUserByName ( ctx , uname )
2017-03-20 13:53:38 +05:30
if err != nil {
2018-01-13 03:46:49 +05:30
return err
2017-03-20 13:53:38 +05:30
}
2021-01-10 23:35:18 +05:30
if err = user . SetPassword ( c . String ( "password" ) ) ; err != nil {
2018-01-13 03:46:49 +05:30
return err
2017-03-20 13:53:38 +05:30
}
2019-10-14 20:54:26 +05:30
2022-03-22 20:52:54 +05:30
if err = user_model . UpdateUserCols ( ctx , user , "passwd" , "passwd_hash_algo" , "salt" ) ; err != nil {
2018-01-13 03:46:49 +05:30
return err
2017-03-20 13:53:38 +05:30
}
2018-01-13 03:46:49 +05:30
fmt . Printf ( "%s's password has been successfully updated!\n" , user . Name )
2017-03-20 13:53:38 +05:30
return nil
}
2016-08-14 04:41:52 +05:30
func runCreateUser ( c * cli . Context ) error {
2019-04-09 20:51:55 +05:30
if err := argsSet ( c , "email" ) ; err != nil {
2018-01-13 03:46:49 +05:30
return err
2016-08-14 04:41:52 +05:30
}
2019-04-09 20:51:55 +05:30
if c . IsSet ( "name" ) && c . IsSet ( "username" ) {
return errors . New ( "Cannot set both --name and --username flags" )
}
if ! c . IsSet ( "name" ) && ! c . IsSet ( "username" ) {
return errors . New ( "One of --name or --username flags must be set" )
}
2018-10-31 03:04:25 +05:30
if c . IsSet ( "password" ) && c . IsSet ( "random-password" ) {
return errors . New ( "cannot set both -random-password and -password flags" )
}
2019-04-09 20:51:55 +05:30
var username string
if c . IsSet ( "username" ) {
username = c . String ( "username" )
} else {
username = c . String ( "name" )
fmt . Fprintf ( os . Stderr , "--name flag is deprecated. Use --username instead.\n" )
}
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2019-10-14 20:54:26 +05:30
return err
}
2018-10-31 03:04:25 +05:30
2019-10-14 20:54:26 +05:30
var password string
2018-10-31 03:04:25 +05:30
if c . IsSet ( "password" ) {
password = c . String ( "password" )
} else if c . IsSet ( "random-password" ) {
2018-11-26 20:30:38 +05:30
var err error
2019-10-14 20:54:26 +05:30
password , err = pwd . Generate ( c . Int ( "random-password-length" ) )
2018-10-31 03:04:25 +05:30
if err != nil {
return err
}
fmt . Printf ( "generated random password is '%s'\n" , password )
} else {
return errors . New ( "must set either password or random-password flag" )
}
2018-10-21 03:35:01 +05:30
// always default to true
2022-01-20 23:16:10 +05:30
changePassword := true
2018-10-21 03:35:01 +05:30
2018-11-25 13:12:24 +05:30
// If this is the first user being created.
// Take it as the admin and don't force a password update.
2022-05-02 19:05:45 +05:30
if n := user_model . CountUsers ( nil ) ; n == 0 {
2018-11-25 13:12:24 +05:30
changePassword = false
}
2018-10-21 03:35:01 +05:30
if c . IsSet ( "must-change-password" ) {
changePassword = c . Bool ( "must-change-password" )
}
2022-04-30 01:08:11 +05:30
restricted := util . OptionalBoolNone
if c . IsSet ( "restricted" ) {
restricted = util . OptionalBoolOf ( c . Bool ( "restricted" ) )
}
2021-11-24 15:19:20 +05:30
u := & user_model . User {
2019-04-09 20:51:55 +05:30
Name : username ,
2018-10-21 03:35:01 +05:30
Email : c . String ( "email" ) ,
2018-10-31 03:04:25 +05:30
Passwd : password ,
2018-10-21 03:35:01 +05:30
IsAdmin : c . Bool ( "admin" ) ,
MustChangePassword : changePassword ,
2019-05-05 03:33:10 +05:30
}
2022-04-30 01:08:11 +05:30
overwriteDefault := & user_model . CreateUserOverwriteOptions {
IsActive : util . OptionalBoolTrue ,
IsRestricted : restricted ,
}
if err := user_model . CreateUser ( u , overwriteDefault ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "CreateUser: %w" , err )
2016-08-14 04:41:52 +05:30
}
2019-05-05 03:33:10 +05:30
if c . Bool ( "access-token" ) {
2022-08-25 08:01:57 +05:30
t := & auth_model . AccessToken {
2019-05-05 03:33:10 +05:30
Name : "gitea-admin" ,
UID : u . ID ,
}
2022-08-25 08:01:57 +05:30
if err := auth_model . NewAccessToken ( t ) ; err != nil {
2019-05-05 03:33:10 +05:30
return err
}
fmt . Printf ( "Access token was successfully created... %s\n" , t . Token )
}
2019-04-09 20:51:55 +05:30
fmt . Printf ( "New user '%s' has been successfully created!\n" , username )
2016-08-14 04:41:52 +05:30
return nil
}
2017-12-31 20:15:46 +05:30
2020-10-16 08:18:38 +05:30
func runListUsers ( c * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2020-10-16 08:18:38 +05:30
return err
}
2021-11-24 15:19:20 +05:30
users , err := user_model . GetAllUsers ( )
2020-10-16 08:18:38 +05:30
if err != nil {
return err
}
w := tabwriter . NewWriter ( os . Stdout , 5 , 0 , 1 , ' ' , 0 )
if c . IsSet ( "admin" ) {
fmt . Fprintf ( w , "ID\tUsername\tEmail\tIsActive\n" )
for _ , u := range users {
if u . IsAdmin {
fmt . Fprintf ( w , "%d\t%s\t%s\t%t\n" , u . ID , u . Name , u . Email , u . IsActive )
}
}
} else {
2022-07-19 14:13:49 +05:30
twofa := user_model . UserList ( users ) . GetTwoFaStatus ( )
fmt . Fprintf ( w , "ID\tUsername\tEmail\tIsActive\tIsAdmin\t2FA\n" )
2020-10-16 08:18:38 +05:30
for _ , u := range users {
2022-07-19 14:13:49 +05:30
fmt . Fprintf ( w , "%d\t%s\t%s\t%t\t%t\t%t\n" , u . ID , u . Name , u . Email , u . IsActive , u . IsAdmin , twofa [ u . ID ] )
2020-10-16 08:18:38 +05:30
}
}
w . Flush ( )
return nil
}
func runDeleteUser ( c * cli . Context ) error {
2020-12-17 13:27:32 +05:30
if ! c . IsSet ( "id" ) && ! c . IsSet ( "username" ) && ! c . IsSet ( "email" ) {
return fmt . Errorf ( "You must provide the id, username or email of a user to delete" )
2020-10-16 08:18:38 +05:30
}
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2020-10-16 08:18:38 +05:30
return err
}
2021-04-22 22:18:47 +05:30
if err := storage . Init ( ) ; err != nil {
return err
}
2020-12-17 13:27:32 +05:30
var err error
2021-11-24 15:19:20 +05:30
var user * user_model . User
2020-12-17 13:27:32 +05:30
if c . IsSet ( "email" ) {
2021-11-24 15:19:20 +05:30
user , err = user_model . GetUserByEmail ( c . String ( "email" ) )
2020-12-17 13:27:32 +05:30
} else if c . IsSet ( "username" ) {
2022-05-20 19:38:52 +05:30
user , err = user_model . GetUserByName ( ctx , c . String ( "username" ) )
2020-12-17 13:27:32 +05:30
} else {
2021-11-24 15:19:20 +05:30
user , err = user_model . GetUserByID ( c . Int64 ( "id" ) )
2020-12-17 13:27:32 +05:30
}
2020-10-16 08:18:38 +05:30
if err != nil {
return err
}
2020-12-17 13:27:32 +05:30
if c . IsSet ( "username" ) && user . LowerName != strings . ToLower ( strings . TrimSpace ( c . String ( "username" ) ) ) {
return fmt . Errorf ( "The user %s who has email %s does not match the provided username %s" , user . Name , c . String ( "email" ) , c . String ( "username" ) )
}
if c . IsSet ( "id" ) && user . ID != c . Int64 ( "id" ) {
return fmt . Errorf ( "The user %s does not match the provided id %d" , user . Name , c . Int64 ( "id" ) )
}
2020-10-16 08:18:38 +05:30
2022-07-14 12:52:09 +05:30
return user_service . DeleteUser ( ctx , user , c . Bool ( "purge" ) )
2020-10-16 08:18:38 +05:30
}
2022-03-05 01:23:35 +05:30
func runGenerateAccessToken ( c * cli . Context ) error {
if ! c . IsSet ( "username" ) {
return fmt . Errorf ( "You must provide the username to generate a token for them" )
}
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
return err
}
2022-05-20 19:38:52 +05:30
user , err := user_model . GetUserByName ( ctx , c . String ( "username" ) )
2022-03-05 01:23:35 +05:30
if err != nil {
return err
}
2022-08-25 08:01:57 +05:30
t := & auth_model . AccessToken {
2022-03-05 01:23:35 +05:30
Name : c . String ( "token-name" ) ,
UID : user . ID ,
}
2022-08-25 08:01:57 +05:30
if err := auth_model . NewAccessToken ( t ) ; err != nil {
2022-03-05 01:23:35 +05:30
return err
}
if c . Bool ( "raw" ) {
fmt . Printf ( "%s\n" , t . Token )
} else {
fmt . Printf ( "Access token was successfully created: %s\n" , t . Token )
}
return nil
}
2021-04-09 13:10:34 +05:30
func runRepoSyncReleases ( _ * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-01-13 03:46:49 +05:30
return err
2017-12-31 20:15:46 +05:30
}
log . Trace ( "Synchronizing repository releases (this may take a while)" )
for page := 1 ; ; page ++ {
2022-06-06 13:31:49 +05:30
repos , count , err := repo_model . SearchRepositoryByName ( & repo_model . SearchRepoOptions {
2021-09-24 17:02:56 +05:30
ListOptions : db . ListOptions {
2022-06-06 13:31:49 +05:30
PageSize : repo_model . RepositoryListDefaultPageSize ,
2020-01-25 00:30:29 +05:30
Page : page ,
} ,
Private : true ,
2017-12-31 20:15:46 +05:30
} )
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "SearchRepositoryByName: %w" , err )
2017-12-31 20:15:46 +05:30
}
if len ( repos ) == 0 {
break
}
log . Trace ( "Processing next %d repos of %d" , len ( repos ) , count )
for _ , repo := range repos {
log . Trace ( "Synchronizing repo %s with path %s" , repo . FullName ( ) , repo . RepoPath ( ) )
2022-03-30 00:43:41 +05:30
gitRepo , err := git . OpenRepository ( ctx , repo . RepoPath ( ) )
2017-12-31 20:15:46 +05:30
if err != nil {
log . Warn ( "OpenRepository: %v" , err )
continue
}
2018-01-13 03:46:49 +05:30
oldnum , err := getReleaseCount ( repo . ID )
2017-12-31 20:15:46 +05:30
if err != nil {
log . Warn ( " GetReleaseCountByRepoID: %v" , err )
}
log . Trace ( " currentNumReleases is %d, running SyncReleasesWithTags" , oldnum )
2020-01-21 01:31:19 +05:30
if err = repo_module . SyncReleasesWithTags ( repo , gitRepo ) ; err != nil {
2017-12-31 20:15:46 +05:30
log . Warn ( " SyncReleasesWithTags: %v" , err )
2019-11-13 12:31:19 +05:30
gitRepo . Close ( )
2017-12-31 20:15:46 +05:30
continue
}
2018-01-13 03:46:49 +05:30
count , err = getReleaseCount ( repo . ID )
2017-12-31 20:15:46 +05:30
if err != nil {
log . Warn ( " GetReleaseCountByRepoID: %v" , err )
2019-11-13 12:31:19 +05:30
gitRepo . Close ( )
2017-12-31 20:15:46 +05:30
continue
}
log . Trace ( " repo %s releases synchronized to tags: from %d to %d" ,
repo . FullName ( ) , oldnum , count )
2019-11-13 12:31:19 +05:30
gitRepo . Close ( )
2017-12-31 20:15:46 +05:30
}
}
return nil
}
2018-01-13 03:46:49 +05:30
func getReleaseCount ( id int64 ) ( int64 , error ) {
2022-08-25 08:01:57 +05:30
return repo_model . GetReleaseCountByRepoID (
2018-01-13 03:46:49 +05:30
id ,
2022-08-25 08:01:57 +05:30
repo_model . FindReleasesOptions {
2018-01-13 03:46:49 +05:30
IncludeTags : true ,
} ,
)
}
2018-05-17 07:05:07 +05:30
2021-04-09 13:10:34 +05:30
func runRegenerateHooks ( _ * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-05-17 07:05:07 +05:30
return err
}
2021-11-17 20:47:31 +05:30
return repo_service . SyncRepositoryHooks ( graceful . GetManager ( ) . ShutdownContext ( ) )
2018-05-17 07:05:07 +05:30
}
2021-04-09 13:10:34 +05:30
func runRegenerateKeys ( _ * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-05-17 07:05:07 +05:30
return err
}
2021-12-10 13:44:24 +05:30
return asymkey_model . RewriteAllPublicKeys ( )
2018-05-17 07:05:07 +05:30
}
2018-09-12 20:16:02 +05:30
2021-07-24 15:46:34 +05:30
func parseOAuth2Config ( c * cli . Context ) * oauth2 . Source {
2018-09-12 20:16:02 +05:30
var customURLMapping * oauth2 . CustomURLMapping
if c . IsSet ( "use-custom-urls" ) {
customURLMapping = & oauth2 . CustomURLMapping {
TokenURL : c . String ( "custom-token-url" ) ,
AuthURL : c . String ( "custom-auth-url" ) ,
ProfileURL : c . String ( "custom-profile-url" ) ,
EmailURL : c . String ( "custom-email-url" ) ,
}
} else {
customURLMapping = nil
}
2021-07-24 15:46:34 +05:30
return & oauth2 . Source {
2018-09-12 20:16:02 +05:30
Provider : c . String ( "provider" ) ,
ClientID : c . String ( "key" ) ,
ClientSecret : c . String ( "secret" ) ,
OpenIDConnectAutoDiscoveryURL : c . String ( "auto-discover-url" ) ,
CustomURLMapping : customURLMapping ,
2020-12-28 14:09:12 +05:30
IconURL : c . String ( "icon-url" ) ,
2021-09-10 22:07:57 +05:30
SkipLocalTwoFA : c . Bool ( "skip-local-2fa" ) ,
2021-12-14 14:07:11 +05:30
Scopes : c . StringSlice ( "scopes" ) ,
RequiredClaimName : c . String ( "required-claim-name" ) ,
RequiredClaimValue : c . String ( "required-claim-value" ) ,
GroupClaimName : c . String ( "group-claim-name" ) ,
AdminGroup : c . String ( "admin-group" ) ,
RestrictedGroup : c . String ( "restricted-group" ) ,
2018-09-12 20:16:02 +05:30
}
}
func runAddOauth ( c * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-09-12 20:16:02 +05:30
return err
}
2022-08-25 08:01:57 +05:30
return auth_model . CreateSource ( & auth_model . Source {
Type : auth_model . OAuth2 ,
2021-07-24 15:46:34 +05:30
Name : c . String ( "name" ) ,
IsActive : true ,
Cfg : parseOAuth2Config ( c ) ,
2018-10-18 10:21:07 +05:30
} )
2018-09-12 20:16:02 +05:30
}
func runUpdateOauth ( c * cli . Context ) error {
if ! c . IsSet ( "id" ) {
return fmt . Errorf ( "--id flag is missing" )
}
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-09-12 20:16:02 +05:30
return err
}
2022-08-25 08:01:57 +05:30
source , err := auth_model . GetSourceByID ( c . Int64 ( "id" ) )
2018-09-12 20:16:02 +05:30
if err != nil {
return err
}
2021-07-24 15:46:34 +05:30
oAuth2Config := source . Cfg . ( * oauth2 . Source )
2018-09-12 20:16:02 +05:30
if c . IsSet ( "name" ) {
source . Name = c . String ( "name" )
}
if c . IsSet ( "provider" ) {
oAuth2Config . Provider = c . String ( "provider" )
}
if c . IsSet ( "key" ) {
oAuth2Config . ClientID = c . String ( "key" )
}
if c . IsSet ( "secret" ) {
oAuth2Config . ClientSecret = c . String ( "secret" )
}
if c . IsSet ( "auto-discover-url" ) {
oAuth2Config . OpenIDConnectAutoDiscoveryURL = c . String ( "auto-discover-url" )
}
2020-12-28 14:09:12 +05:30
if c . IsSet ( "icon-url" ) {
oAuth2Config . IconURL = c . String ( "icon-url" )
}
2021-12-14 14:07:11 +05:30
if c . IsSet ( "scopes" ) {
oAuth2Config . Scopes = c . StringSlice ( "scopes" )
}
if c . IsSet ( "required-claim-name" ) {
oAuth2Config . RequiredClaimName = c . String ( "required-claim-name" )
}
if c . IsSet ( "required-claim-value" ) {
oAuth2Config . RequiredClaimValue = c . String ( "required-claim-value" )
}
if c . IsSet ( "group-claim-name" ) {
oAuth2Config . GroupClaimName = c . String ( "group-claim-name" )
}
if c . IsSet ( "admin-group" ) {
oAuth2Config . AdminGroup = c . String ( "admin-group" )
}
if c . IsSet ( "restricted-group" ) {
oAuth2Config . RestrictedGroup = c . String ( "restricted-group" )
}
2018-09-12 20:16:02 +05:30
// update custom URL mapping
2022-01-20 23:16:10 +05:30
customURLMapping := & oauth2 . CustomURLMapping { }
2018-09-12 20:16:02 +05:30
if oAuth2Config . CustomURLMapping != nil {
customURLMapping . TokenURL = oAuth2Config . CustomURLMapping . TokenURL
customURLMapping . AuthURL = oAuth2Config . CustomURLMapping . AuthURL
customURLMapping . ProfileURL = oAuth2Config . CustomURLMapping . ProfileURL
customURLMapping . EmailURL = oAuth2Config . CustomURLMapping . EmailURL
}
if c . IsSet ( "use-custom-urls" ) && c . IsSet ( "custom-token-url" ) {
customURLMapping . TokenURL = c . String ( "custom-token-url" )
}
if c . IsSet ( "use-custom-urls" ) && c . IsSet ( "custom-auth-url" ) {
customURLMapping . AuthURL = c . String ( "custom-auth-url" )
}
if c . IsSet ( "use-custom-urls" ) && c . IsSet ( "custom-profile-url" ) {
customURLMapping . ProfileURL = c . String ( "custom-profile-url" )
}
if c . IsSet ( "use-custom-urls" ) && c . IsSet ( "custom-email-url" ) {
customURLMapping . EmailURL = c . String ( "custom-email-url" )
}
oAuth2Config . CustomURLMapping = customURLMapping
source . Cfg = oAuth2Config
2022-08-25 08:01:57 +05:30
return auth_model . UpdateSource ( source )
2018-09-12 20:16:02 +05:30
}
2022-01-13 04:24:53 +05:30
func parseSMTPConfig ( c * cli . Context , conf * smtp . Source ) error {
if c . IsSet ( "auth-type" ) {
conf . Auth = c . String ( "auth-type" )
validAuthTypes := [ ] string { "PLAIN" , "LOGIN" , "CRAM-MD5" }
if ! contains ( validAuthTypes , strings . ToUpper ( c . String ( "auth-type" ) ) ) {
return errors . New ( "Auth must be one of PLAIN/LOGIN/CRAM-MD5" )
}
conf . Auth = c . String ( "auth-type" )
}
Rework mailer settings (#18982)
* `PROTOCOL`: can be smtp, smtps, smtp+startls, smtp+unix, sendmail, dummy
* `SMTP_ADDR`: domain for SMTP, or path to unix socket
* `SMTP_PORT`: port for SMTP; defaults to 25 for `smtp`, 465 for `smtps`, and 587 for `smtp+startls`
* `ENABLE_HELO`, `HELO_HOSTNAME`: reverse `DISABLE_HELO` to `ENABLE_HELO`; default to false + system hostname
* `FORCE_TRUST_SERVER_CERT`: replace the unclear `SKIP_VERIFY`
* `CLIENT_CERT_FILE`, `CLIENT_KEY_FILE`, `USE_CLIENT_CERT`: clarify client certificates here
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2022-08-02 10:54:18 +05:30
if c . IsSet ( "addr" ) {
conf . Addr = c . String ( "addr" )
2022-01-13 04:24:53 +05:30
}
if c . IsSet ( "port" ) {
conf . Port = c . Int ( "port" )
}
if c . IsSet ( "allowed-domains" ) {
conf . AllowedDomains = c . String ( "allowed-domains" )
}
if c . IsSet ( "force-smtps" ) {
conf . ForceSMTPS = c . BoolT ( "force-smtps" )
}
if c . IsSet ( "skip-verify" ) {
conf . SkipVerify = c . BoolT ( "skip-verify" )
}
if c . IsSet ( "helo-hostname" ) {
conf . HeloHostname = c . String ( "helo-hostname" )
}
if c . IsSet ( "disable-helo" ) {
conf . DisableHelo = c . BoolT ( "disable-helo" )
}
if c . IsSet ( "skip-local-2fa" ) {
conf . SkipLocalTwoFA = c . BoolT ( "skip-local-2fa" )
}
return nil
}
func runAddSMTP ( c * cli . Context ) error {
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
return err
}
if ! c . IsSet ( "name" ) || len ( c . String ( "name" ) ) == 0 {
return errors . New ( "name must be set" )
}
if ! c . IsSet ( "host" ) || len ( c . String ( "host" ) ) == 0 {
return errors . New ( "host must be set" )
}
if ! c . IsSet ( "port" ) {
return errors . New ( "port must be set" )
}
2022-01-20 23:16:10 +05:30
active := true
2022-01-13 04:24:53 +05:30
if c . IsSet ( "active" ) {
active = c . BoolT ( "active" )
}
var smtpConfig smtp . Source
if err := parseSMTPConfig ( c , & smtpConfig ) ; err != nil {
return err
}
// If not set default to PLAIN
if len ( smtpConfig . Auth ) == 0 {
smtpConfig . Auth = "PLAIN"
}
2022-08-25 08:01:57 +05:30
return auth_model . CreateSource ( & auth_model . Source {
Type : auth_model . SMTP ,
2022-01-13 04:24:53 +05:30
Name : c . String ( "name" ) ,
IsActive : active ,
Cfg : & smtpConfig ,
} )
}
func runUpdateSMTP ( c * cli . Context ) error {
if ! c . IsSet ( "id" ) {
return fmt . Errorf ( "--id flag is missing" )
}
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
return err
}
2022-08-25 08:01:57 +05:30
source , err := auth_model . GetSourceByID ( c . Int64 ( "id" ) )
2022-01-13 04:24:53 +05:30
if err != nil {
return err
}
smtpConfig := source . Cfg . ( * smtp . Source )
if err := parseSMTPConfig ( c , smtpConfig ) ; err != nil {
return err
}
if c . IsSet ( "name" ) {
source . Name = c . String ( "name" )
}
if c . IsSet ( "active" ) {
source . IsActive = c . BoolT ( "active" )
}
source . Cfg = smtpConfig
2022-08-25 08:01:57 +05:30
return auth_model . UpdateSource ( source )
2022-01-13 04:24:53 +05:30
}
2018-09-12 20:16:02 +05:30
func runListAuth ( c * cli . Context ) error {
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-09-12 20:16:02 +05:30
return err
}
2022-08-25 08:01:57 +05:30
authSources , err := auth_model . Sources ( )
2018-09-12 20:16:02 +05:30
if err != nil {
return err
}
2020-03-28 02:56:43 +05:30
flags := tabwriter . AlignRight
if c . Bool ( "vertical-bars" ) {
flags |= tabwriter . Debug
}
padChar := byte ( '\t' )
if len ( c . String ( "pad-char" ) ) > 0 {
padChar = c . String ( "pad-char" ) [ 0 ]
}
2018-09-12 20:16:02 +05:30
// loop through each source and print
2020-03-28 02:56:43 +05:30
w := tabwriter . NewWriter ( os . Stdout , c . Int ( "min-width" ) , c . Int ( "tab-width" ) , c . Int ( "padding" ) , padChar , flags )
2020-01-08 05:11:16 +05:30
fmt . Fprintf ( w , "ID\tName\tType\tEnabled\n" )
2022-01-02 18:42:35 +05:30
for _ , source := range authSources {
2021-09-24 17:02:56 +05:30
fmt . Fprintf ( w , "%d\t%s\t%s\t%t\n" , source . ID , source . Name , source . Type . String ( ) , source . IsActive )
2018-09-12 20:16:02 +05:30
}
w . Flush ( )
return nil
}
func runDeleteAuth ( c * cli . Context ) error {
if ! c . IsSet ( "id" ) {
return fmt . Errorf ( "--id flag is missing" )
}
2021-11-07 08:41:27 +05:30
ctx , cancel := installSignals ( )
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
2018-09-12 20:16:02 +05:30
return err
}
2022-08-25 08:01:57 +05:30
source , err := auth_model . GetSourceByID ( c . Int64 ( "id" ) )
2018-09-12 20:16:02 +05:30
if err != nil {
return err
}
2022-01-02 18:42:35 +05:30
return auth_service . DeleteSource ( source )
2018-09-12 20:16:02 +05:30
}