You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

serv.go 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  5. package private
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/private"
  13. "code.gitea.io/gitea/modules/setting"
  14. repo_service "code.gitea.io/gitea/services/repository"
  15. wiki_service "code.gitea.io/gitea/services/wiki"
  16. "gitea.com/macaron/macaron"
  17. )
  18. // ServNoCommand returns information about the provided keyid
  19. func ServNoCommand(ctx *macaron.Context) {
  20. keyID := ctx.ParamsInt64(":keyid")
  21. if keyID <= 0 {
  22. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  23. "err": fmt.Sprintf("Bad key id: %d", keyID),
  24. })
  25. }
  26. results := private.KeyAndOwner{}
  27. key, err := models.GetPublicKeyByID(keyID)
  28. if err != nil {
  29. if models.IsErrKeyNotExist(err) {
  30. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  31. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  32. })
  33. return
  34. }
  35. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  36. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  37. "err": err.Error(),
  38. })
  39. return
  40. }
  41. results.Key = key
  42. if key.Type == models.KeyTypeUser {
  43. user, err := models.GetUserByID(key.OwnerID)
  44. if err != nil {
  45. if models.IsErrUserNotExist(err) {
  46. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  47. "err": fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID),
  48. })
  49. return
  50. }
  51. log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err)
  52. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  53. "err": err.Error(),
  54. })
  55. return
  56. }
  57. results.Owner = user
  58. }
  59. ctx.JSON(http.StatusOK, &results)
  60. }
  61. // ServCommand returns information about the provided keyid
  62. func ServCommand(ctx *macaron.Context) {
  63. keyID := ctx.ParamsInt64(":keyid")
  64. ownerName := ctx.Params(":owner")
  65. repoName := ctx.Params(":repo")
  66. mode := models.AccessMode(ctx.QueryInt("mode"))
  67. // Set the basic parts of the results to return
  68. results := private.ServCommandResults{
  69. RepoName: repoName,
  70. OwnerName: ownerName,
  71. KeyID: keyID,
  72. }
  73. // Now because we're not translating things properly let's just default some Engish strings here
  74. modeString := "read"
  75. if mode > models.AccessModeRead {
  76. modeString = "write to"
  77. }
  78. // The default unit we're trying to look at is code
  79. unitType := models.UnitTypeCode
  80. // Unless we're a wiki...
  81. if strings.HasSuffix(repoName, ".wiki") {
  82. // in which case we need to look at the wiki
  83. unitType = models.UnitTypeWiki
  84. // And we'd better munge the reponame and tell downstream we're looking at a wiki
  85. results.IsWiki = true
  86. results.RepoName = repoName[:len(repoName)-5]
  87. }
  88. // Now get the Repository and set the results section
  89. repoExist := true
  90. repo, err := models.GetRepositoryByOwnerAndName(results.OwnerName, results.RepoName)
  91. if err != nil {
  92. if models.IsErrRepoNotExist(err) {
  93. repoExist = false
  94. for _, verb := range ctx.QueryStrings("verb") {
  95. if "git-upload-pack" == verb {
  96. // User is fetching/cloning a non-existent repository
  97. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  98. "results": results,
  99. "type": "ErrRepoNotExist",
  100. "err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  101. })
  102. return
  103. }
  104. }
  105. } else {
  106. log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  107. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  108. "results": results,
  109. "type": "InternalServerError",
  110. "err": fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
  111. })
  112. return
  113. }
  114. }
  115. log.Info("git oper to this.")
  116. for _, verb := range ctx.QueryStrings("verb") { // clone_cnt
  117. if verb == "git-upload-pack" {
  118. go repo.IncreaseCloneCnt()
  119. }
  120. }
  121. if repoExist {
  122. repo.OwnerName = ownerName
  123. results.RepoID = repo.ID
  124. if repo.IsBeingCreated() {
  125. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  126. "results": results,
  127. "type": "InternalServerError",
  128. "err": "Repository is being created, you could retry after it finished",
  129. })
  130. return
  131. }
  132. // We can shortcut at this point if the repo is a mirror
  133. if mode > models.AccessModeRead && repo.IsMirror {
  134. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  135. "results": results,
  136. "type": "ErrMirrorReadOnly",
  137. "err": fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
  138. })
  139. return
  140. }
  141. }
  142. // Get the Public Key represented by the keyID
  143. key, err := models.GetPublicKeyByID(keyID)
  144. if err != nil {
  145. if models.IsErrKeyNotExist(err) {
  146. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  147. "results": results,
  148. "type": "ErrKeyNotExist",
  149. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  150. })
  151. return
  152. }
  153. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  154. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  155. "results": results,
  156. "type": "InternalServerError",
  157. "err": fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
  158. })
  159. return
  160. }
  161. results.KeyName = key.Name
  162. results.KeyID = key.ID
  163. results.UserID = key.OwnerID
  164. // If repo doesn't exist, deploy key doesn't make sense
  165. if !repoExist && key.Type == models.KeyTypeDeploy {
  166. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  167. "results": results,
  168. "type": "ErrRepoNotExist",
  169. "err": fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
  170. })
  171. return
  172. }
  173. // Deploy Keys have ownerID set to 0 therefore we can't use the owner
  174. // So now we need to check if the key is a deploy key
  175. // We'll keep hold of the deploy key here for permissions checking
  176. var deployKey *models.DeployKey
  177. var user *models.User
  178. if key.Type == models.KeyTypeDeploy {
  179. results.IsDeployKey = true
  180. var err error
  181. deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
  182. if err != nil {
  183. if models.IsErrDeployKeyNotExist(err) {
  184. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  185. "results": results,
  186. "type": "ErrDeployKeyNotExist",
  187. "err": fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  188. })
  189. return
  190. }
  191. log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
  192. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  193. "results": results,
  194. "type": "InternalServerError",
  195. "err": fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
  196. })
  197. return
  198. }
  199. results.KeyName = deployKey.Name
  200. // FIXME: Deploy keys aren't really the owner of the repo pushing changes
  201. // however we don't have good way of representing deploy keys in hook.go
  202. // so for now use the owner of the repository
  203. results.UserName = results.OwnerName
  204. results.UserID = repo.OwnerID
  205. } else {
  206. // Get the user represented by the Key
  207. var err error
  208. user, err = models.GetUserByID(key.OwnerID)
  209. if err != nil {
  210. if models.IsErrUserNotExist(err) {
  211. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  212. "results": results,
  213. "type": "ErrUserNotExist",
  214. "err": fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
  215. })
  216. return
  217. }
  218. log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
  219. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  220. "results": results,
  221. "type": "InternalServerError",
  222. "err": fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
  223. })
  224. return
  225. }
  226. results.UserName = user.Name
  227. }
  228. // Don't allow pushing if the repo is archived
  229. if repoExist && mode > models.AccessModeRead && repo.IsArchived {
  230. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  231. "results": results,
  232. "type": "ErrRepoIsArchived",
  233. "err": fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
  234. })
  235. return
  236. }
  237. // Permissions checking:
  238. if repoExist && (mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView) {
  239. if key.Type == models.KeyTypeDeploy {
  240. if deployKey.Mode < mode {
  241. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  242. "results": results,
  243. "type": "ErrUnauthorized",
  244. "err": fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  245. })
  246. return
  247. }
  248. } else {
  249. perm, err := models.GetUserRepoPermission(repo, user)
  250. if err != nil {
  251. log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
  252. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  253. "results": results,
  254. "type": "InternalServerError",
  255. "err": fmt.Sprintf("Unable to get permissions for user %d:%s with key %d in %s/%s Error: %v", user.ID, user.Name, key.ID, results.OwnerName, results.RepoName, err),
  256. })
  257. return
  258. }
  259. userMode := perm.UnitAccessMode(unitType)
  260. if userMode < mode {
  261. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  262. "results": results,
  263. "type": "ErrUnauthorized",
  264. "err": fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),
  265. })
  266. return
  267. }
  268. }
  269. }
  270. // We already know we aren't using a deploy key
  271. if !repoExist {
  272. owner, err := models.GetUserByName(ownerName)
  273. if err != nil {
  274. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  275. "results": results,
  276. "type": "InternalServerError",
  277. "err": fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
  278. })
  279. return
  280. }
  281. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  282. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  283. "results": results,
  284. "type": "ErrForbidden",
  285. "err": "Push to create is not enabled for organizations.",
  286. })
  287. return
  288. }
  289. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  290. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  291. "results": results,
  292. "type": "ErrForbidden",
  293. "err": "Push to create is not enabled for users.",
  294. })
  295. return
  296. }
  297. repo, err = repo_service.PushCreateRepo(user, owner, results.RepoName)
  298. if err != nil {
  299. log.Error("pushCreateRepo: %v", err)
  300. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  301. "results": results,
  302. "type": "ErrRepoNotExist",
  303. "err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  304. })
  305. return
  306. }
  307. results.RepoID = repo.ID
  308. }
  309. if results.IsWiki {
  310. // Ensure the wiki is enabled before we allow access to it
  311. if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil {
  312. if models.IsErrUnitTypeNotExist(err) {
  313. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  314. "results": results,
  315. "type": "ErrForbidden",
  316. "err": "repository wiki is disabled",
  317. })
  318. return
  319. }
  320. log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
  321. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  322. "results": results,
  323. "type": "InternalServerError",
  324. "err": fmt.Sprintf("Failed to get the wiki unit in %s/%s Error: %v", ownerName, repoName, err),
  325. })
  326. return
  327. }
  328. // Finally if we're trying to touch the wiki we should init it
  329. if err = wiki_service.InitWiki(repo); err != nil {
  330. log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
  331. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  332. "results": results,
  333. "type": "InternalServerError",
  334. "err": fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
  335. })
  336. return
  337. }
  338. }
  339. log.Debug("Serv Results:\nIsWiki: %t\nIsDeployKey: %t\nKeyID: %d\tKeyName: %s\nUserName: %s\nUserID: %d\nOwnerName: %s\nRepoName: %s\nRepoID: %d",
  340. results.IsWiki,
  341. results.IsDeployKey,
  342. results.KeyID,
  343. results.KeyName,
  344. results.UserName,
  345. results.UserID,
  346. results.OwnerName,
  347. results.RepoName,
  348. results.RepoID)
  349. ctx.JSON(http.StatusOK, results)
  350. // We will update the keys in a different call.
  351. }