mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-10 19:46:07 +08:00
add hardcoded enrollment list
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
170
dep_hardcode.go
Normal file
170
dep_hardcode.go
Normal file
@@ -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(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>PayloadContent</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>PayloadDisplayName</key>
|
||||
<string>ManagedClient logging</string>
|
||||
<key>PayloadEnabled</key>
|
||||
<true/>
|
||||
<key>PayloadIdentifier</key>
|
||||
<string>com.apple.logging.ManagedClient.1</string>
|
||||
<key>PayloadType</key>
|
||||
<string>com.apple.system.logging</string>
|
||||
<key>PayloadUUID</key>
|
||||
<string>ED5DE307-A5FC-434F-AD88-187677F02222</string>
|
||||
<key>PayloadVersion</key>
|
||||
<integer>1</integer>
|
||||
<key>Subsystems</key>
|
||||
<dict>
|
||||
<key>com.apple.ManagedClient</key>
|
||||
<dict>
|
||||
<key>DEFAULT-OPTIONS</key>
|
||||
<dict>
|
||||
<key>Default-Privacy-Setting</key>
|
||||
<string>Public</string>
|
||||
<key>Level</key>
|
||||
<dict>
|
||||
<key>Enable</key>
|
||||
<string>debug</string>
|
||||
<key>Persist</key>
|
||||
<string>debug</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>PayloadDisplayName</key>
|
||||
<string>MDM debug mode</string>
|
||||
<key>PayloadType</key>
|
||||
<string>com.apple.mdmclient</string>
|
||||
<key>EnableDebug</key>
|
||||
<true/>
|
||||
<key>PayloadIdentifier</key>
|
||||
<string>com.apple.logging.ManagedClient.3</string>
|
||||
<key>PayloadUUID</key>
|
||||
<string>3EFF8784-7AE1-43E0-A2BA-6B77BBA54341</string>
|
||||
<key>PayloadVersion</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>PayloadDisplayName</key>
|
||||
<string>ALR debug mode</string>
|
||||
<key>PayloadType</key>
|
||||
<string>com.apple.mcx.alr</string>
|
||||
<key>EnableDebug</key>
|
||||
<true/>
|
||||
<key>PayloadIdentifier</key>
|
||||
<string>com.apple.logging.ManagedClient.4</string>
|
||||
<key>PayloadUUID</key>
|
||||
<string>126C9C6B-AE28-4EA6-9BDB-FBB058A291B8</string>
|
||||
<key>PayloadVersion</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
</array>
|
||||
<key>PayloadDescription</key>
|
||||
<string>Enables ManagedClient debug mode and logging</string>
|
||||
<key>PayloadDisplayName</key>
|
||||
<string>MCX debug mode and logging</string>
|
||||
<key>PayloadIdentifier</key>
|
||||
<string>com.apple.logging.ManagedClient</string>
|
||||
<key>PayloadRemovalDisallowed</key>
|
||||
<false/>
|
||||
<key>PayloadScope</key>
|
||||
<string>System</string>
|
||||
<key>PayloadType</key>
|
||||
<string>Configuration</string>
|
||||
<key>PayloadUUID</key>
|
||||
<string>D30C25BD-E0C1-44C8-830A-964F27DAD4BA</string>
|
||||
<key>PayloadVersion</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
</plist>`)
|
||||
17
serve.go
17
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)
|
||||
|
||||
Reference in New Issue
Block a user