2018-12-02 22:18:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-25 15:16:27 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2018-12-02 22:18:32 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2019-07-11 17:52:13 +00:00
|
|
|
"log"
|
2019-11-25 15:16:27 +00:00
|
|
|
"math/rand"
|
2018-12-02 22:18:32 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type wantChoices struct {
|
2021-08-30 16:33:14 +00:00
|
|
|
FlagId int `json:"id"`
|
2018-12-02 22:18:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:35:07 +00:00
|
|
|
func treatWantChoices(pathname string, team *fic.Team) {
|
2019-11-25 15:16:27 +00:00
|
|
|
// Generate a unique identifier to follow the request in logs
|
|
|
|
bid := make([]byte, 5)
|
|
|
|
binary.LittleEndian.PutUint32(bid, rand.Uint32())
|
|
|
|
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
|
|
|
|
log.Println(id, "New wantChoices receive", pathname)
|
|
|
|
|
2018-12-02 22:18:32 +00:00
|
|
|
var ask wantChoices
|
|
|
|
|
2024-03-17 11:01:09 +00:00
|
|
|
cnt_raw, err := ioutil.ReadFile(pathname)
|
|
|
|
if err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [ERR] %s\n", id, err)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(cnt_raw, &ask)
|
|
|
|
if err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [ERR] %s\n", id, err)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ask.FlagId == 0 {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [WRN] Invalid content in wantChoices file: %s\n", id, pathname)
|
2018-12-02 22:18:32 +00:00
|
|
|
os.Remove(pathname)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
flag, err := fic.GetFlagKey(ask.FlagId)
|
|
|
|
if err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [ERR] %s\n", id, err)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !team.CanSeeFlag(flag) {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [!!!] The team asks to display choices whereas it doesn't have access to the flag\n", id)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exercice, err := flag.GetExercice()
|
|
|
|
if err != nil {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [ERR] Unable to retrieve the flag's underlying exercice: %s\n", id, err)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !team.HasAccess(exercice) {
|
2019-11-25 15:16:27 +00:00
|
|
|
log.Printf("%s [!!!] The team asks to display choices whereas it doesn't have access to the exercice\n", id)
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exercice.Disabled {
|
2023-03-20 10:23:03 +00:00
|
|
|
log.Println("[!!!] The team submits something for a disabled exercice")
|
2024-03-17 11:01:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exercice.IdTheme != nil {
|
|
|
|
theme, err := fic.GetTheme(*exercice.IdTheme)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s [ERR] Unable to retrieve theme for exercice %d: %s\n", id, exercice.Id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Theme should not be locked
|
|
|
|
if theme.Locked {
|
|
|
|
log.Printf("%s [!!!] Want choice received for locked theme %d (fid=%d): %s\n", id, exercice.IdTheme, ask.FlagId, theme.Name)
|
|
|
|
return
|
2018-12-02 22:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-17 11:01:09 +00:00
|
|
|
|
|
|
|
err = team.DisplayChoices(flag)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s [ERR] %s\n", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenTeam, TeamId: team.Id})
|
|
|
|
if err = os.Remove(pathname); err != nil {
|
|
|
|
log.Printf("%s [ERR] %s\n", id, err)
|
|
|
|
}
|
2018-12-02 22:18:32 +00:00
|
|
|
}
|