handler.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/dgrijalva/jwt-go"
  8. "github.com/labstack/echo"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. "time"
  14. )
  15. type handler struct{}
  16. type userInfo struct {
  17. Result struct {
  18. Result struct {
  19. Sshpubkeyfp []string `json:"sshpubkeyfp"`
  20. HasKeytab bool `json:"has_keytab"`
  21. Ipasshpubkey []string `json:"ipasshpubkey"`
  22. Cn []string `json:"cn"`
  23. Krbcanonicalname []string `json:"krbcanonicalname"`
  24. Krbticketflags []string `json:"krbticketflags"`
  25. MemberofGroup []string `json:"memberof_group"`
  26. HasPassword bool `json:"has_password"`
  27. Homedirectory []string `json:"homedirectory"`
  28. Nsaccountlock bool `json:"nsaccountlock"`
  29. UID []string `json:"uid"`
  30. Title []string `json:"title"`
  31. Loginshell []string `json:"loginshell"`
  32. Uidnumber []string `json:"uidnumber"`
  33. Preserved bool `json:"preserved"`
  34. Krbextradata []struct {
  35. Base64 string `json:"__base64__"`
  36. } `json:"krbextradata"`
  37. Mail []string `json:"mail"`
  38. MemberofindirectHbacrule []string `json:"memberofindirect_hbacrule"`
  39. Dn string `json:"dn"`
  40. Displayname []string `json:"displayname"`
  41. Mepmanagedentry []string `json:"mepmanagedentry"`
  42. Ipauniqueid []string `json:"ipauniqueid"`
  43. Krbloginfailedcount []string `json:"krbloginfailedcount"`
  44. Krbpwdpolicyreference []string `json:"krbpwdpolicyreference"`
  45. Krbprincipalname []string `json:"krbprincipalname"`
  46. Givenname []string `json:"givenname"`
  47. Krblastadminunlock []struct {
  48. Datetime string `json:"__datetime__"`
  49. } `json:"krblastadminunlock"`
  50. Krbpasswordexpiration []struct {
  51. Datetime string `json:"__datetime__"`
  52. } `json:"krbpasswordexpiration"`
  53. Krblastfailedauth []struct {
  54. Datetime string `json:"__datetime__"`
  55. } `json:"krblastfailedauth"`
  56. Objectclass []string `json:"objectclass"`
  57. Gidnumber []string `json:"gidnumber"`
  58. Gecos []string `json:"gecos"`
  59. Sn []string `json:"sn"`
  60. MemberofSudorule []string `json:"memberof_sudorule"`
  61. Krblastpwdchange []struct {
  62. Datetime string `json:"__datetime__"`
  63. } `json:"krblastpwdchange"`
  64. Initials []string `json:"initials"`
  65. } `json:"result"`
  66. Value string `json:"value"`
  67. Summary interface{} `json:"summary"`
  68. } `json:"result"`
  69. Version string `json:"version"`
  70. Error interface{} `json:"error"`
  71. ID int `json:"id"`
  72. Principal string `json:"principal"`
  73. }
  74. func (h *handler) login(c echo.Context) error {
  75. username := c.FormValue("username")
  76. password := c.FormValue("password")
  77. _url := URL + "/ipa/session/login_password"
  78. method := "POST"
  79. params := url.Values{}
  80. params.Add("user", username)
  81. params.Add("password", password)
  82. payload := strings.NewReader(params.Encode())
  83. tr := &http.Transport{
  84. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  85. }
  86. client := &http.Client{Transport: tr}
  87. req, err := http.NewRequest(method, _url, payload)
  88. audit("Recieved Login request from: " + RealIP)
  89. if err != nil {
  90. fmt.Println(err)
  91. }
  92. req.Header.Add("Referer", URL+"/ipa")
  93. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  94. req.Header.Add("Accept", "text/plain")
  95. res, err := client.Do(req)
  96. cockie := res.Cookies()
  97. token := cockie[0].Raw
  98. defer res.Body.Close()
  99. //fmt.Println(res.StatusCode)
  100. if res.StatusCode == 200 {
  101. user := getUserInfo(cockie, username)
  102. //fmt.Println(user.Result)
  103. tokens, err := generateTokenPair(user, token)
  104. if err != nil {
  105. return err
  106. }
  107. return c.JSON(http.StatusOK, tokens)
  108. }
  109. return echo.ErrUnauthorized
  110. }
  111. func getUserInfo(cockie []*http.Cookie, username string) userInfo {
  112. url := URL + "/ipa/session/json"
  113. method := "POST"
  114. _json := fmt.Sprintf(`
  115. {
  116. "method": "user_show",
  117. "params": [
  118. [
  119. "%s"
  120. ],
  121. {
  122. "all": true,
  123. "version": "2.215"
  124. }
  125. ],
  126. "id": 0
  127. }
  128. `, username)
  129. payload := strings.NewReader(_json)
  130. tr := &http.Transport{
  131. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  132. }
  133. client := &http.Client{Transport: tr}
  134. req, err := http.NewRequest(method, url, payload)
  135. if err != nil {
  136. fmt.Println(err)
  137. }
  138. req.Header.Add("Referer", URL+"/ipa")
  139. req.Header.Add("Content-Type", "application/json")
  140. req.Header.Add("Accept", "text/plain")
  141. req.Header.Add("Cookie", cockie[0].Raw)
  142. res, err := client.Do(req)
  143. defer res.Body.Close()
  144. body, err := ioutil.ReadAll(res.Body)
  145. user := userInfo{}
  146. json.Unmarshal(body, &user)
  147. return user
  148. }
  149. func (h *handler) private(c echo.Context) error {
  150. user := c.Get("user").(*jwt.Token)
  151. claims := user.Claims.(jwt.MapClaims)
  152. name := claims["name"].(string)
  153. return c.String(http.StatusOK, "Welcome "+name+"!")
  154. }
  155. func (h *handler) addUser(c echo.Context) error {
  156. type apiErr struct {
  157. Result interface{} `json:"result"`
  158. Error struct {
  159. Code int `json:"code"`
  160. Message string `json:"message"`
  161. Data struct {
  162. } `json:"data"`
  163. Name string `json:"name"`
  164. } `json:"error"`
  165. ID int `json:"id"`
  166. Principal string `json:"principal"`
  167. Version string `json:"version"`
  168. }
  169. type addUser struct {
  170. Result struct {
  171. Result struct {
  172. Displayname []string `json:"displayname"`
  173. UID []string `json:"uid"`
  174. Uidnumber []string `json:"uidnumber"`
  175. Objectclass []string `json:"objectclass"`
  176. Sn []string `json:"sn"`
  177. Telephonenumber []string `json:"telephonenumber"`
  178. Cn []string `json:"cn"`
  179. Krbpasswordexpiration []struct {
  180. Datetime string `json:"__datetime__"`
  181. } `json:"krbpasswordexpiration"`
  182. Mobile []string `json:"mobile"`
  183. Krbprincipalname []string `json:"krbprincipalname"`
  184. Ipauniqueid []string `json:"ipauniqueid"`
  185. Givenname []string `json:"givenname"`
  186. Gidnumber []string `json:"gidnumber"`
  187. Krbcanonicalname []string `json:"krbcanonicalname"`
  188. Mail []string `json:"mail"`
  189. Initials []string `json:"initials"`
  190. Homedirectory []string `json:"homedirectory"`
  191. Loginshell []string `json:"loginshell"`
  192. Gecos []string `json:"gecos"`
  193. Randompassword string `json:"randompassword"`
  194. HasPassword bool `json:"has_password"`
  195. HasKeytab bool `json:"has_keytab"`
  196. MemberofGroup []string `json:"memberof_group"`
  197. Dn string `json:"dn"`
  198. } `json:"result"`
  199. Value string `json:"value"`
  200. Summary string `json:"summary"`
  201. } `json:"result"`
  202. Error string `json:"error"`
  203. ID int `json:"id"`
  204. Principal string `json:"principal"`
  205. Version string `json:"version"`
  206. }
  207. user := c.Get("user").(*jwt.Token)
  208. claims := user.Claims.(jwt.MapClaims)
  209. token := claims["IPAToken"].(string)
  210. b, err := json.Marshal(claims)
  211. if err != nil {
  212. fmt.Println("err:", err)
  213. }
  214. username := c.FormValue("Username")
  215. sha256 := sha256.Sum256([]byte(username))
  216. var hashChannel = make(chan []byte, 1)
  217. hashChannel <- sha256[:]
  218. ciphertext := encrypt(<-hashChannel, string(b))
  219. //fmt.Println(b)
  220. //fmt.Println(ciphertext)
  221. sn := c.FormValue("Lname")
  222. cn := c.FormValue("FullName")
  223. givenname := c.FormValue("Fname")
  224. displayname := c.FormValue("displayname")
  225. krbpasswordexpiration := c.FormValue("krbpasswordexpiration")
  226. mail := c.FormValue("mail")
  227. telephonenumber := c.FormValue("telephonenumber")
  228. mobile := c.FormValue("mobile")
  229. _url := URL + "/ipa/session/json"
  230. method := "POST"
  231. _json := fmt.Sprintf(`
  232. {
  233. "id": 0,
  234. "method": "user_add/1",
  235. "params": [
  236. [
  237. "%s"
  238. ],
  239. {
  240. "givenname": "%s",
  241. "sn": "%s",
  242. "cn":"%s",
  243. "displayname":"%s",
  244. "loginshell":"/usr/sbin/nologin",
  245. "krbpasswordexpiration":"%s",
  246. "mail":"%s",
  247. "random":"true",
  248. "gidnumber":"599200001",
  249. "telephonenumber":"%s",
  250. "mobile":"%s",
  251. "version": "2.235"
  252. }
  253. ]
  254. }
  255. `, username, givenname, sn, cn, displayname, krbpasswordexpiration, mail, telephonenumber, mobile)
  256. __json := fmt.Sprintf(`
  257. {
  258. "id": 0,
  259. "method": "group_add_member/1",
  260. "params": [
  261. [
  262. "svcaccounts"
  263. ],
  264. {
  265. "user": [
  266. "%s"
  267. ],
  268. "version": "2.235"
  269. }
  270. ]
  271. }
  272. `, username)
  273. payload := strings.NewReader(_json)
  274. _payload := strings.NewReader(__json)
  275. tr := &http.Transport{
  276. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  277. }
  278. client := &http.Client{Transport: tr}
  279. req, err := http.NewRequest(method, _url, payload)
  280. if err != nil {
  281. fmt.Println(err)
  282. }
  283. req.Header.Add("Referer", URL+"/ipa")
  284. req.Header.Add("Content-Type", "application/json")
  285. req.Header.Add("Accept", "text/plain")
  286. req.Header.Add("Cookie", token)
  287. res, err := client.Do(req)
  288. _req, _ := http.NewRequest(method, _url, _payload)
  289. _req.Header.Add("Referer", URL+"/ipa")
  290. _req.Header.Add("Content-Type", "application/json")
  291. _req.Header.Add("Accept", "text/plain")
  292. _req.Header.Add("Cookie", token)
  293. client.Do(_req)
  294. defer res.Body.Close()
  295. body, err := ioutil.ReadAll(res.Body)
  296. result := addUser{}
  297. _err := json.Unmarshal(body, &result)
  298. if _err != nil {
  299. _apiErr := apiErr{}
  300. __err := json.Unmarshal(body, &_apiErr)
  301. if __err != nil {
  302. return c.String(http.StatusBadRequest, "Error of error!!")
  303. }
  304. res2B, _ := json.Marshal(_apiErr)
  305. return c.String(http.StatusBadRequest, "Failed with error \n"+string(res2B))
  306. }
  307. sendMail("Welcome to ZiCloud\r\n Your temporary link is :\r\n https://zicloud.com/reset/"+url.QueryEscape(ciphertext), "Welcome to ZiCloud", mail)
  308. return c.String(http.StatusOK, "Done, Pass:"+string(ciphertext))
  309. }
  310. func (h *handler) disableUser(c echo.Context) error {
  311. user := c.Get("user").(*jwt.Token)
  312. claims := user.Claims.(jwt.MapClaims)
  313. token := claims["IPAToken"].(string)
  314. username := c.FormValue("Username")
  315. url := URL + "/ipa/session/json"
  316. method := "POST"
  317. _json := fmt.Sprintf(`
  318. {
  319. "id": 0,
  320. "method": "user_disable/1",
  321. "params": [
  322. [
  323. "%s"
  324. ],
  325. {
  326. "version": "2.235"
  327. }
  328. ]
  329. }
  330. `, username)
  331. payload := strings.NewReader(_json)
  332. tr := &http.Transport{
  333. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  334. }
  335. client := &http.Client{Transport: tr}
  336. req, err := http.NewRequest(method, url, payload)
  337. if err != nil {
  338. fmt.Println(err)
  339. }
  340. req.Header.Add("Referer", URL+"/ipa")
  341. req.Header.Add("Content-Type", "application/json")
  342. req.Header.Add("Accept", "text/plain")
  343. req.Header.Add("Cookie", token)
  344. res, err := client.Do(req)
  345. if err != nil {
  346. return c.String(http.StatusBadRequest, "Error"+err.Error())
  347. }
  348. defer res.Body.Close()
  349. return c.String(http.StatusOK, "Done")
  350. }
  351. func (h *handler) resetUser(c echo.Context) error {
  352. type keyJson struct {
  353. IPAToken string `json:"IPAToken"`
  354. Admin bool `json:"admin"`
  355. Exp int `json:"exp"`
  356. Memberof []string `json:"memberof"`
  357. Name []string `json:"name"`
  358. Sub int `json:"sub"`
  359. }
  360. t := time.Now() //%Y%m%d%H%M%SZ
  361. t = t.Add(time.Hour * 24 * 60)
  362. username := c.FormValue("Username")
  363. password := c.FormValue("Password")
  364. key := c.FormValue("key")
  365. key, _ = url.QueryUnescape(key)
  366. sha256 := sha256.Sum256([]byte(username))
  367. var hashChannel = make(chan []byte, 1)
  368. hashChannel <- sha256[:]
  369. plainkey := decrypt(<-hashChannel, key)
  370. _plainkey := keyJson{}
  371. json.Unmarshal([]byte(plainkey), &_plainkey)
  372. token := _plainkey.IPAToken
  373. _url := URL + "/ipa/session/json"
  374. method := "POST"
  375. _json := fmt.Sprintf(`
  376. {
  377. "id": 0,
  378. "method": "user_mod/1",
  379. "params": [
  380. [
  381. "%s"
  382. ],
  383. {
  384. "userpassword":"%s",
  385. "version": "2.235"
  386. }
  387. ]
  388. }
  389. `, username, password)
  390. payload := strings.NewReader(_json)
  391. tr := &http.Transport{
  392. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  393. }
  394. client := &http.Client{Transport: tr}
  395. req, err := http.NewRequest(method, _url, payload)
  396. if err != nil {
  397. fmt.Println(err)
  398. }
  399. req.Header.Add("Referer", URL+"/ipa")
  400. req.Header.Add("Content-Type", "application/json")
  401. req.Header.Add("Accept", "text/plain")
  402. req.Header.Add("Cookie", token)
  403. res, err := client.Do(req)
  404. _json = fmt.Sprintf(`
  405. {
  406. "id": 0,
  407. "method": "user_mod/1",
  408. "params": [
  409. [
  410. "%s"
  411. ],
  412. {
  413. "krbpasswordexpiration":"%s",
  414. "version": "2.235"
  415. }
  416. ]
  417. }
  418. `, username, t.Format("2006-01-02")+"Z")
  419. payload = strings.NewReader(_json)
  420. req, err = http.NewRequest(method, _url, payload)
  421. if err != nil {
  422. fmt.Println(err)
  423. }
  424. req.Header.Add("Referer", URL+"/ipa")
  425. req.Header.Add("Content-Type", "application/json")
  426. req.Header.Add("Accept", "text/plain")
  427. req.Header.Add("Cookie", token)
  428. err = nil
  429. res, err = client.Do(req)
  430. //fmt.Println(payload)
  431. if err != nil {
  432. return c.String(http.StatusBadRequest, "Error"+err.Error())
  433. }
  434. defer res.Body.Close()
  435. return c.String(http.StatusOK, "Done")
  436. }
  437. func (h *handler) dnsrecordadd(c echo.Context) error {
  438. user := c.Get("user").(*jwt.Token)
  439. claims := user.Claims.(jwt.MapClaims)
  440. token := claims["IPAToken"].(string)
  441. recordName := c.FormValue("recordName")
  442. record := c.FormValue("record")
  443. url := URL + "/ipa/session/json"
  444. method := "POST"
  445. _json := fmt.Sprintf(`
  446. {
  447. "id": 0,
  448. "method": "dnsrecord_add/1",
  449. "params": [
  450. [
  451. "ZI-TEL.COM",
  452. {
  453. "__dns_name__": "%s"
  454. }
  455. ],
  456. {
  457. "a_part_ip_address": "%s",
  458. "raw": true,
  459. "version": "2.235"
  460. }
  461. ]
  462. }
  463. `, recordName, record)
  464. payload := strings.NewReader(_json)
  465. tr := &http.Transport{
  466. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  467. }
  468. client := &http.Client{Transport: tr}
  469. req, err := http.NewRequest(method, url, payload)
  470. if err != nil {
  471. fmt.Println(err)
  472. }
  473. req.Header.Add("Referer", URL+"/ipa")
  474. req.Header.Add("Content-Type", "application/json")
  475. req.Header.Add("Accept", "text/plain")
  476. req.Header.Add("Cookie", token)
  477. res, err := client.Do(req)
  478. if err != nil {
  479. return c.String(http.StatusBadRequest, "Error"+err.Error())
  480. }
  481. //body, err := ioutil.ReadAll(res.Body)
  482. //_res:=result{}
  483. //json.Unmarshal(body, &_res)
  484. //fmt.Println(_res)
  485. defer res.Body.Close()
  486. return c.String(http.StatusOK, "Done")
  487. }