/*
 * Check if a user has access to a domain using the domain look up (Which should be quicker)
 */
func (t *Developer) quickaccess(response http.ResponseWriter, request *http.Request) {
	log.Println("Started")

	userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(mux.Vars(request)["username"])
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	filterCollectionHelper, err := datastore.GetFilterCollectionHelper()
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	inCollections, err := filterCollectionHelper.GetFilterCollectionsWithDomain(mux.Vars(request)["domain"])
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	for _, collection := range inCollections {
		for _, userCollectionName := range userFilterCollections.Collections {
			if userCollectionName == collection.Name {
				log.Println("Blocked")
				return
			}
		}
	}

	log.Println("Not Blocked")
}
/**
 * Handles saving user access settings.
 */
func (t *UsersCollections) save(response http.ResponseWriter, request *http.Request) {
	// Try and load a user filter collection using the user name.
	username := request.FormValue("idName")
	userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(username)

	// Check for error when loading collection.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	// Set the name of the collection.
	userFilterCollections.Username = username

	// Retrieve the collections from the HTML form.
	if err := request.ParseForm(); err != nil {
		http.Error(response, err.Error(), 500)
		return
	}
	collections := request.Form["collections[]"]
	userFilterCollections.Collections = collections

	// Save the user filter collection.
	err = userFilterCollectionsHelper.SetUserFilterCollections(userFilterCollections)

	// Check for error when saving collection.
	if err != nil {
		// There was an error, so report that to the screen.
		http.Redirect(response, request, t.Base()+"?saveerror=true", http.StatusMovedPermanently)
	} else {
		// No error, so report a successful save to the screen.
		http.Redirect(response, request, t.Base()+"?savecomplete=true", http.StatusMovedPermanently)
	}
}
Example #3
0
func Hijack(response dns.ResponseWriter, message *dns.Msg) (bool, error) {

	if (message.Question[0].Qtype == dns.TypeA || message.Question[0].Qtype == dns.TypeAAAA) && message.Question[0].Qclass == dns.ClassINET {

		ipString, _, err := net.SplitHostPort(response.RemoteAddr().String())
		if err != nil {
			log.Println("Warning: Received No IP4 Address:" + response.RemoteAddr().String())
			return false, err
		}

		ip := net.ParseIP(ipString)
		deviceHelper, err := datastore.GetDeviceHelper()
		if err != nil {
			log.Println("Error: Datastore Error (Open Access):" + err.Error())
			return false, err
		}

		device, err := deviceHelper.GetDeviceByIP(ip)
		if err != nil {
			log.Println("Error: Datastore Error (Open Access):" + err.Error())
			return false, err
		}

		if bytes.Equal(device.MACAddress, net.HardwareAddr{}) {
			//Device Doesn't exists?
			log.Println("Warning: IP:" + ipString + " MAC:" + device.MACAddress.String() + " Doesn't Exists As A Local Device But is Using Our DNS??")
			return false, nil
		} else {
			if activeUser := device.GetActiveUser(); activeUser != nil {
				//User is active check they have access.
				userFiltercollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
				if err != nil {
					return false, err
				}

				usersFiltercollection, err := userFiltercollectionsHelper.GetUserFilterCollections(activeUser.Username)
				if err != nil {
					return false, err
				}

				if usersFiltercollection.Username != "" {
					//Get the collections helper.
					filterCollectionsHelper, err := datastore.GetFilterCollectionHelper()
					if err != nil {
						return false, err
					}

					//Start Moving down the domain (i.e. removing subdomains etc)
					collections := make([]datastore.FilterCollection, 0)

					domain := strings.TrimSuffix(message.Question[0].Name, ".")

					for {
						additionalCollections, err := filterCollectionsHelper.GetFilterCollectionsWithDomain(domain)
						if err != nil {
							return false, err
						}

						if len(additionalCollections) > 0 {
							collections = append(collections, additionalCollections...)
						}

						if len(strings.SplitAfterN(domain, ".", 2)) == 2 {
							domain = strings.SplitAfterN(domain, ".", 2)[1]
						} else {
							break
						}
					}

					//Does the domain appear in any collections??
					for _, collection := range collections {
						if usersFiltercollection.ContainsCollection(collection.Name) {
							//The Url Should be blocked...
							//For Now lets just not respond with a DNS record...

							//localResponse := new(dns.Msg)
							//localResponse.SetReply(message)

							//rr_header := dns.RR_Header{Name: message.Question[0].Name, Class: dns.ClassINET, Ttl: 0}

							//Main Active IP
							//mainIPNetwork, err := networktools.MainActiveIPNetwork()
							//if err != nil {
							//	return false, err
							//}

							//switch message.Question[0].Qtype {

							//case dns.TypeA:
							//	rr_header.Rrtype = dns.TypeA
							//	a := &dns.A{rr_header, mainIPNetwork.IP}
							//	localResponse.Answer = append(localResponse.Answer, a)

							//case dns.TypeAAAA:
							//	rr_header.Rrtype = dns.TypeAAAA
							//}

							//err = response.WriteMsg(localResponse)
							//if err != nil {
							//	return false, err
							//}
							return true, nil
						}
					}

					return false, nil
				}
			} else {
				//User is not active redirect to login page.
				localResponse := new(dns.Msg)
				localResponse.SetReply(message)
				rr_header := dns.RR_Header{Name: message.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
				//Main Active IP
				mainIPNetwork, err := networktools.MainActiveIPNetwork()
				if err != nil {
					return false, err
				}
				a := &dns.A{rr_header, mainIPNetwork.IP}
				localResponse.Answer = append(localResponse.Answer, a)
				err = response.WriteMsg(localResponse)
				if err != nil {
					return false, err
				}
				return true, nil
			}
		}
	}

	return false, nil
}
/**
 * Handles editing a user filter collections object.
 */
func (t *UsersCollections) edit(response http.ResponseWriter, request *http.Request) {
	// Get the current session user.
	myuser := t.Sessions.CurrentUser(response, request)

	// Get the user filter collections.
	username := mux.Vars(request)["username"]
	userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(username)

	// Check for error when loading user filter collection.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	// Get the user relating to the filter collection.
	userDataStoreHelper, err := datastore.GetUserHelper()
	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	filterCollectionsUser, err := userDataStoreHelper.GetUser(username)

	// Check for error when loading user.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	// Get all collections to display on the page.
	filterCollectionHelper, err := datastore.GetFilterCollectionHelper()
	allCollections, err := filterCollectionHelper.GetFilterCollections()

	// Check for error when loading collections.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	// Setup the data structure to pass to the page.
	data := struct {
		Action                string
		User                  datastore.User
		UserFilterCollections datastore.UserFilterCollections
		FilterCollectionsUser *datastore.User
		AllCollections        []datastore.FilterCollection
	}{
		"userAccessSettings",
		myuser,
		userFilterCollections,
		&filterCollectionsUser,
		allCollections,
	}

	// Parse the page and execute the template.
	tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/access/edit.tpl")
	tpl.Execute(response, data)
}