github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/api4/license.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package api4 5 6 import ( 7 "bytes" 8 "io" 9 "net/http" 10 11 "github.com/vnforks/kid/v5/audit" 12 "github.com/vnforks/kid/v5/model" 13 ) 14 15 func (api *API) InitLicense() { 16 api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(addLicense)).Methods("POST") 17 api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(removeLicense)).Methods("DELETE") 18 api.BaseRoutes.ApiRoot.Handle("/license/client", api.ApiHandler(getClientLicense)).Methods("GET") 19 } 20 21 func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) { 22 format := r.URL.Query().Get("format") 23 24 if format == "" { 25 c.Err = model.NewAppError("getClientLicense", "api.license.client.old_format.app_error", nil, "", http.StatusNotImplemented) 26 return 27 } 28 29 if format != "old" { 30 c.SetInvalidParam("format") 31 return 32 } 33 34 var clientLicense map[string]string 35 36 if c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { 37 clientLicense = c.App.ClientLicense() 38 } else { 39 clientLicense = c.App.GetSanitizedClientLicense() 40 } 41 42 w.Write([]byte(model.MapToJson(clientLicense))) 43 } 44 45 func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { 46 auditRec := c.MakeAuditRecord("addLicense", audit.Fail) 47 defer c.LogAuditRec(auditRec) 48 c.LogAudit("attempt") 49 50 if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { 51 c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) 52 return 53 } 54 55 if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin { 56 c.Err = model.NewAppError("addLicense", "api.restricted_system_admin", nil, "", http.StatusForbidden) 57 return 58 } 59 60 err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize) 61 if err != nil { 62 http.Error(w, err.Error(), http.StatusBadRequest) 63 return 64 } 65 66 m := r.MultipartForm 67 68 fileArray, ok := m.File["license"] 69 if !ok { 70 c.Err = model.NewAppError("addLicense", "api.license.add_license.no_file.app_error", nil, "", http.StatusBadRequest) 71 return 72 } 73 74 if len(fileArray) <= 0 { 75 c.Err = model.NewAppError("addLicense", "api.license.add_license.array.app_error", nil, "", http.StatusBadRequest) 76 return 77 } 78 79 fileData := fileArray[0] 80 auditRec.AddMeta("filename", fileData.Filename) 81 82 file, err := fileData.Open() 83 if err != nil { 84 c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusBadRequest) 85 return 86 } 87 defer file.Close() 88 89 buf := bytes.NewBuffer(nil) 90 io.Copy(buf, file) 91 92 license, appErr := c.App.SaveLicense(buf.Bytes()) 93 if appErr != nil { 94 if appErr.Id == model.EXPIRED_LICENSE_ERROR { 95 c.LogAudit("failed - expired or non-started license") 96 } else if appErr.Id == model.INVALID_LICENSE_ERROR { 97 c.LogAudit("failed - invalid license") 98 } else { 99 c.LogAudit("failed - unable to save license") 100 } 101 c.Err = appErr 102 return 103 } 104 105 if *c.App.Config().JobSettings.RunJobs { 106 c.App.Srv().Jobs.Workers = c.App.Srv().Jobs.InitWorkers() 107 c.App.Srv().Jobs.StartWorkers() 108 } 109 110 auditRec.Success() 111 c.LogAudit("success") 112 113 w.Write([]byte(license.ToJson())) 114 } 115 116 func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) { 117 auditRec := c.MakeAuditRecord("removeLicense", audit.Fail) 118 defer c.LogAuditRec(auditRec) 119 c.LogAudit("attempt") 120 121 if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { 122 c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) 123 return 124 } 125 126 if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin { 127 c.Err = model.NewAppError("removeLicense", "api.restricted_system_admin", nil, "", http.StatusForbidden) 128 return 129 } 130 131 if err := c.App.RemoveLicense(); err != nil { 132 c.Err = err 133 return 134 } 135 136 auditRec.Success() 137 c.LogAudit("success") 138 139 ReturnStatusOK(w) 140 }