2018-02-22 05:42:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"encoding/hex"
|
2020-03-27 13:57:14 +00:00
|
|
|
"encoding/json"
|
2018-02-22 05:42:21 +00:00
|
|
|
"errors"
|
2018-02-23 19:43:51 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-02-22 05:42:21 +00:00
|
|
|
"log"
|
2018-02-23 19:43:51 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-02-26 15:48:55 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2018-02-22 05:42:21 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2020-03-27 13:57:14 +00:00
|
|
|
|
2021-10-31 15:43:43 +00:00
|
|
|
"git.nemunai.re/srs/adlin/libadlin"
|
2018-02-22 05:42:21 +00:00
|
|
|
)
|
|
|
|
|
2019-02-28 08:41:10 +00:00
|
|
|
var AuthorizedKeysLocation = "/root/.ssh/authorized_keys"
|
2019-02-26 15:48:55 +00:00
|
|
|
var SshPiperLocation = "/var/sshpiper/"
|
2018-02-23 19:43:51 +00:00
|
|
|
|
2018-02-22 05:42:21 +00:00
|
|
|
func init() {
|
2019-03-04 08:00:22 +00:00
|
|
|
router.GET("/sshkeys", apiHandler(
|
2018-02-22 05:42:21 +00:00
|
|
|
func(httprouter.Params, []byte) (interface{}, error) {
|
2020-03-27 13:57:14 +00:00
|
|
|
return adlin.GetStudentKeys()
|
2018-02-22 05:42:21 +00:00
|
|
|
}))
|
2020-03-01 17:09:20 +00:00
|
|
|
router.POST("/sshkeys", rawHandler(responseHandler(receiveKey)))
|
2019-02-26 15:48:55 +00:00
|
|
|
router.GET("/sshkeys/authorizedkeys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2018-02-23 19:43:51 +00:00
|
|
|
dumpAuthorizedKeysFile(w)
|
|
|
|
})
|
2019-03-04 08:00:22 +00:00
|
|
|
router.GET("/api/students/:sid/hassshkeys", apiHandler(studentHandler(hasSSHKeys)))
|
2019-02-26 15:48:55 +00:00
|
|
|
router.GET("/api/students/:sid/authorizedkeys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
if sid, err := strconv.Atoi(string(ps.ByName("sid"))); err != nil {
|
2020-03-27 13:57:14 +00:00
|
|
|
if student, err := adlin.GetStudentByLogin(ps.ByName("sid")); err != nil {
|
2019-02-26 15:48:55 +00:00
|
|
|
http.Error(w, "Student doesn't exist.", http.StatusNotFound)
|
|
|
|
} else {
|
2020-03-27 13:57:14 +00:00
|
|
|
dumpStdAuthorizedKeysFile(student, w)
|
2019-02-26 15:48:55 +00:00
|
|
|
}
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if student, err := adlin.GetStudent(sid); err != nil {
|
2019-02-26 15:48:55 +00:00
|
|
|
http.Error(w, "Student doesn't exist.", http.StatusNotFound)
|
|
|
|
} else {
|
2020-03-27 13:57:14 +00:00
|
|
|
dumpStdAuthorizedKeysFile(student, w)
|
2019-02-26 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2018-02-22 05:42:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func hasSSHKeys(student *adlin.Student, body []byte) (interface{}, error) {
|
2020-03-27 13:57:14 +00:00
|
|
|
if keys, err := student.GetKeys(); err != nil {
|
2019-03-04 08:00:22 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return len(keys) > 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 05:42:21 +00:00
|
|
|
func receiveKey(r *http.Request, ps httprouter.Params, body []byte) (interface{}, error) {
|
|
|
|
var gt givenToken
|
|
|
|
if err := json.Unmarshal(body, >); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gt.token = make([]byte, hex.DecodedLen(len(gt.Token)))
|
|
|
|
if _, err := hex.Decode(gt.token, []byte(gt.Token)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
if std, err := adlin.GetStudentByLogin(gt.Login); err != nil {
|
2018-02-22 05:42:21 +00:00
|
|
|
return nil, err
|
|
|
|
} else if len(gt.Data) < 2 {
|
|
|
|
return nil, errors.New("No key found!")
|
|
|
|
} else {
|
|
|
|
pkey := std.GetPKey()
|
|
|
|
|
|
|
|
data := [][]byte{}
|
|
|
|
for _, d := range gt.Data {
|
|
|
|
data = append(data, []byte(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedToken, err := GenerateToken(pkey, 0, data...); err != nil {
|
|
|
|
return nil, err
|
2020-03-27 13:57:14 +00:00
|
|
|
} else if !hmac.Equal(expectedToken, gt.token) {
|
2018-02-22 05:42:21 +00:00
|
|
|
return nil, errors.New("This is not the expected token.")
|
|
|
|
}
|
|
|
|
|
2019-03-04 08:00:22 +00:00
|
|
|
if _, err := std.NewKey(gt.Data[0] + " " + gt.Data[1]); err != nil {
|
2018-02-22 05:42:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("%s just pushed sshkey\n", std.Login)
|
2018-02-23 19:43:51 +00:00
|
|
|
|
2019-02-26 15:48:55 +00:00
|
|
|
if len(AuthorizedKeysLocation) > 0 {
|
|
|
|
file, err := os.Create(AuthorizedKeysLocation)
|
2018-02-23 19:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Cannot create file", err)
|
2019-02-26 15:48:55 +00:00
|
|
|
goto sshpiperimport
|
2018-02-23 19:43:51 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
dumpAuthorizedKeysFile(file)
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:48:55 +00:00
|
|
|
sshpiperimport:
|
|
|
|
if len(SshPiperLocation) > 0 {
|
|
|
|
if err := os.MkdirAll(path.Join(SshPiperLocation, std.Login), 0777); err != nil {
|
|
|
|
log.Fatal("Cannot create sshpiper directory:", err)
|
|
|
|
} else {
|
|
|
|
file, err := os.Create(path.Join(SshPiperLocation, std.Login, "authorized_keys"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Cannot create sshpiperd file", err)
|
|
|
|
goto onerr
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
dumpStdAuthorizedKeysFile(std, file)
|
2020-02-27 14:26:27 +00:00
|
|
|
os.Symlink(path.Join(SshPiperLocation, "sshpiper_upstream"), path.Join(SshPiperLocation, std.Login, "sshpiper_upstream"))
|
|
|
|
os.Symlink(path.Join(SshPiperLocation, "id_rsa"), path.Join(SshPiperLocation, std.Login, "id_rsa"))
|
2019-02-26 15:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onerr:
|
2018-02-22 05:42:21 +00:00
|
|
|
return "Key imported", nil
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 19:43:51 +00:00
|
|
|
|
|
|
|
func dumpAuthorizedKeysFile(w io.Writer) {
|
|
|
|
seen := map[string]interface{}{}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
if keys, _ := adlin.GetStudentKeys(); keys != nil {
|
2018-02-23 19:43:51 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
if _, exists := seen[k.Key]; exists {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
seen[k.Key] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
s, _ := k.GetStudent()
|
2019-02-28 08:41:10 +00:00
|
|
|
w.Write([]byte("command=\"/root/adlin.sh " + fmt.Sprintf("%d", k.IdStudent) + " '" + s.Login + "'\",restrict " + k.Key + fmt.Sprintf(" Student#%d-%q\n", k.IdStudent, s.Login)))
|
2018-02-23 19:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 15:48:55 +00:00
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func dumpStdAuthorizedKeysFile(s *adlin.Student, w io.Writer) {
|
2019-02-26 15:48:55 +00:00
|
|
|
seen := map[string]interface{}{}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
if keys, _ := s.GetKeys(); keys != nil {
|
2019-02-26 15:48:55 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
if _, exists := seen[k.Key]; exists {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
seen[k.Key] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
s, _ := k.GetStudent()
|
2019-02-28 08:41:10 +00:00
|
|
|
w.Write([]byte(k.Key + fmt.Sprintf(" Student#%d-%q\n", k.IdStudent, s.Login)))
|
2019-02-26 15:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|