From 73a17f46e6f9ec141e0ed2be1910b75853647dca Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Sun, 19 Mar 2017 19:20:25 +0000 Subject: [PATCH] add hardcoded enrollment list --- connect/connect.go | 21 ++++- connect/queue.go | 38 +++++---- connect/transport_http.go | 6 +- dep_hardcode.go | 170 ++++++++++++++++++++++++++++++++++++++ serve.go | 17 +++- 5 files changed, 231 insertions(+), 21 deletions(-) create mode 100644 dep_hardcode.go diff --git a/connect/connect.go b/connect/connect.go index 72191214..26019e6a 100644 --- a/connect/connect.go +++ b/connect/connect.go @@ -2,6 +2,7 @@ package connect import ( "fmt" + "log" "golang.org/x/net/context" @@ -29,5 +30,23 @@ func New(queue *Queue) (ConnectService, error) { func (svc *connectSvc) Acknowledge(ctx context.Context, req mdm.Response) (payload []byte, err error) { fmt.Printf("connected udid=%s type=%s, status=%s\n", req.UDID, req.RequestType, req.Status) - return nil, nil + dc, err := svc.queue.DeviceCommand(req.UDID) + if err != nil { + log.Println(err) + return nil, nil + } + + if len(dc.Commands) == 0 { + return nil, nil + } + payload = dc.Commands[0].Payload + + // delete first element + dc.Commands = append(dc.Commands[:0], dc.Commands[0+1:]...) + + if err := svc.queue.Save(dc); err != nil { + return nil, err + } + + return payload, nil } diff --git a/connect/queue.go b/connect/queue.go index 2377225f..5123513a 100644 --- a/connect/queue.go +++ b/connect/queue.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/boltdb/bolt" + "github.com/groob/plist" "github.com/micromdm/nano/command" "github.com/micromdm/nano/pubsub" "github.com/pkg/errors" @@ -49,7 +50,7 @@ func (db *Queue) Save(cmd *DeviceCommand) error { } key := []byte(cmd.DeviceUDID) if err := bkt.Put(key, devproto); err != nil { - return errors.Wrap(err, "put DeviceCOmmand to boltdb") + return errors.Wrap(err, "put DeviceCommand to boltdb") } return tx.Commit() } @@ -94,22 +95,27 @@ func (db *Queue) pollCommands(sub pubsub.Subscriber) error { fmt.Println(err) continue } - cmd, _ := db.DeviceCommand(ev.DeviceUDID) - if cmd == nil { - cmd = &DeviceCommand{ - DeviceUDID: ev.DeviceUDID, - Commands: []Command{{ - UUID: ev.Payload.CommandUUID, - Payload: nil, // TODO - }}, - } - } else { - cmd.Commands = append(cmd.Commands, Command{ - UUID: ev.Payload.CommandUUID, - Payload: nil, // TODO - }) - } + cmd := new(DeviceCommand) + cmd.DeviceUDID = ev.DeviceUDID + byUDID, err := db.DeviceCommand(ev.DeviceUDID) + if err == nil && byUDID != nil { + cmd = byUDID + } + newPayload, err := plist.Marshal(&ev.Payload) + if err != nil { + fmt.Println(err) + continue + } + newCmd := Command{ + UUID: ev.Payload.CommandUUID, + Payload: newPayload, + } + cmd.Commands = append(cmd.Commands, newCmd) + if err := db.Save(cmd); err != nil { + fmt.Println(err) + continue + } fmt.Printf("queued event for device: %s\n", ev.DeviceUDID) } } diff --git a/connect/transport_http.go b/connect/transport_http.go index 3c4fa396..29820405 100644 --- a/connect/transport_http.go +++ b/connect/transport_http.go @@ -3,7 +3,6 @@ package connect import ( "context" "fmt" - "io" "net/http" httptransport "github.com/go-kit/kit/transport/http" @@ -36,7 +35,7 @@ type errorWrapper struct { func decodeRequest(ctx context.Context, r *http.Request) (interface{}, error) { var req mdmConnectRequest - err := plist.NewDecoder(io.LimitReader(r.Body, 10000)).Decode(&req) + err := plist.NewDecoder(r.Body).Decode(&req) return req, err } @@ -48,7 +47,10 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa return nil } + resp := response.(mdmConnectResponse) + w.WriteHeader(http.StatusOK) + w.Write(resp.payload) return nil } diff --git a/dep_hardcode.go b/dep_hardcode.go new file mode 100644 index 00000000..f9fc8a34 --- /dev/null +++ b/dep_hardcode.go @@ -0,0 +1,170 @@ +package main + +import ( + "context" + "fmt" + "log" + "time" + + "github.com/micromdm/mdm" + "github.com/micromdm/nano/checkin" + "github.com/micromdm/nano/command" + "github.com/pkg/errors" +) + +func hardcodeCommands(sm *config) error { + sub := sm.pubclient + cmdsvc := sm.commandService + pushsvc := sm.pushService + authEvents, err := sub.Subscribe("hardcode-dep", checkin.AuthenticateTopic) + if err != nil { + return errors.Wrapf(err, + "subscribing devices to %s topic", checkin.AuthenticateTopic) + } + + go func() { + for { + select { + case event := <-authEvents: + var ev checkin.Event + if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil { + fmt.Println(err) + continue + } + if err := hardcodeList(cmdsvc, ev.Command.UDID); err != nil { + log.Println(err) + continue + } + go func() { + time.Sleep(10 * time.Second) + pushsvc.Push(context.Background(), ev.Command.UDID) + }() + + } + } + }() + + return nil +} + +func hardcodeList(svc command.Service, udid string) error { + ctx := context.Background() + devInfo := &mdm.CommandRequest{ + RequestType: "DeviceInformation", + UDID: udid, + Queries: []string{"UDID"}, + } + + devConfigured := &mdm.CommandRequest{ + RequestType: "DeviceConfigured", + UDID: udid, + } + + installProfile := &mdm.CommandRequest{ + RequestType: "InstallProfile", + UDID: udid, + InstallProfile: mdm.InstallProfile{ + Payload: debugProfile, + }, + } + + var requests = []*mdm.CommandRequest{ + devInfo, + installProfile, + devConfigured, + } + + for _, r := range requests { + _, err := svc.NewCommand(ctx, r) + if err != nil { + return err + } + } + return nil +} + +var debugProfile = []byte(` + + + + PayloadContent + + + PayloadDisplayName + ManagedClient logging + PayloadEnabled + + PayloadIdentifier + com.apple.logging.ManagedClient.1 + PayloadType + com.apple.system.logging + PayloadUUID + ED5DE307-A5FC-434F-AD88-187677F02222 + PayloadVersion + 1 + Subsystems + + com.apple.ManagedClient + + DEFAULT-OPTIONS + + Default-Privacy-Setting + Public + Level + + Enable + debug + Persist + debug + + + + + + + PayloadDisplayName + MDM debug mode + PayloadType + com.apple.mdmclient + EnableDebug + + PayloadIdentifier + com.apple.logging.ManagedClient.3 + PayloadUUID + 3EFF8784-7AE1-43E0-A2BA-6B77BBA54341 + PayloadVersion + 1 + + + PayloadDisplayName + ALR debug mode + PayloadType + com.apple.mcx.alr + EnableDebug + + PayloadIdentifier + com.apple.logging.ManagedClient.4 + PayloadUUID + 126C9C6B-AE28-4EA6-9BDB-FBB058A291B8 + PayloadVersion + 1 + + + PayloadDescription + Enables ManagedClient debug mode and logging + PayloadDisplayName + MCX debug mode and logging + PayloadIdentifier + com.apple.logging.ManagedClient + PayloadRemovalDisallowed + + PayloadScope + System + PayloadType + Configuration + PayloadUUID + D30C25BD-E0C1-44C8-830A-964F27DAD4BA + PayloadVersion + 1 + +`) diff --git a/serve.go b/serve.go index 263f7e10..433d71a7 100644 --- a/serve.go +++ b/serve.go @@ -63,6 +63,7 @@ func serve(args []string) error { flHTTPAddr = flagset.String("http-addr", ":https", "http(s) listen address of mdm server. defaults to :8080 if tls is false") flRedirAddr = flagset.String("redir-addr", ":http", "http redirect to https listen address") flHTTPDebug = flagset.Bool("http-debug", false, "enable debug for http(dumps full request)") + flRepoPath = flagset.String("filerepo", "", "path to http file repo") ) flagset.Usage = usageFor(flagset, "micromdm serve [flags]") if err := flagset.Parse(args); err != nil { @@ -97,6 +98,10 @@ func serve(args []string) error { stdlog.Fatal(sm.err) } + if err := hardcodeCommands(sm); err != nil { + stdlog.Fatal(err) + } + _, err := device.NewDB(sm.db, sm.pubclient) if err != nil { stdlog.Fatal(err) @@ -127,7 +132,11 @@ func serve(args []string) error { NewCommandEndpoint: command.MakeNewCommandEndpoint(sm.commandService), } - commandHandlers := command.MakeHTTPHandlers(ctx, commandEndpoints, checkinOpts...) + connectOpts := []httptransport.ServerOption{ + httptransport.ServerErrorLogger(httpLogger), + httptransport.ServerErrorEncoder(connect.EncodeError), + } + commandHandlers := command.MakeHTTPHandlers(ctx, commandEndpoints, connectOpts...) var connectEndpoint endpoint.Endpoint { @@ -137,7 +146,7 @@ func serve(args []string) error { ConnectEndpoint: connectEndpoint, } - connectHandlers := connect.MakeHTTPHandlers(ctx, connectEndpoints, checkinOpts...) + connectHandlers := connect.MakeHTTPHandlers(ctx, connectEndpoints, connectOpts...) pushHandlers := nanopush.MakeHTTPHandlers(ctx, pushEndpoints, checkinOpts...) scepHandler := scep.ServiceHandler(ctx, sm.scepService, httpLogger) @@ -150,6 +159,10 @@ func serve(args []string) error { r.Handle("/push/{udid}", pushHandlers.PushHandler) r.Handle("/v1/commands", commandHandlers.NewCommandHandler).Methods("POST") + if *flRepoPath != "" { + r.Handle("/repo/", http.StripPrefix("/repo/", http.FileServer(http.Dir(*flRepoPath)))) + } + var handler http.Handler if *flHTTPDebug { handler = debugHTTPmiddleware(r)