Beispiel #1
0
func logout(ctx *macaron.Context) string {
	var tpl vision.New
	tpl.TemplateFile("template/login.tpl")

	user, auth := util.Auth(ctx, "any")

	if user.Sudo {
		ctx.SetCookie("sudo", "", -1)
		set_error("No longer logged in as "+user.System_username+".", ctx)
		ctx.Redirect("/dashboard", 302)
		return "success"
	}

	if auth {
		new_token := util.MkToken()
		db, _ := util.MySQL()
		defer db.Close()

		ustmt, _ := db.Prepare("update hostcontrol_users set login_token=? where system_username=?")
		ustmt.Exec(new_token, user.System_username)
		ustmt.Close()
	}

	ctx.SetCookie("hostcontrol_id", "", -1)
	ctx.SetCookie("login_token", "", -1)

	tpl.Parse("login")
	tpl.Parse("login/logged_out")
	return tpl.Out()

}
Beispiel #2
0
func addtoken(ctx *macaron.Context) string {
	hcuser, auth := util.Auth(ctx, "any")
	if !auth {
		ctx.Redirect("/", 302)
		return ""
	}

	description := util.Query(ctx, "description")
	token := util.MkToken()

	db, _ := util.MySQL()
	defer db.Close()

	xstmt, _ := db.Prepare("INSERT INTO `hostcontrol`.`hostcontrol_user_tokens` set `token`=?, `hostcontrol_id`=?, `description`=?, token_id=null")
	_, err := xstmt.Exec(token, hcuser.Hostcontrol_id, description)
	xstmt.Close()

	if err != nil {
		set_error("Failed to create new token.", ctx)
		ctx.Redirect("/settings", 302)
		return "Failed to create new token."
	}

	set_error("Created new token.", ctx)
	ctx.Redirect("/settings", 302)

	return ""
}
Beispiel #3
0
func Adduser(ctx *macaron.Context) string {
	hcuser, auth := util.Auth(ctx, "sysusers")
	if !auth {
		return "not_authorized"
	}

	username := util.Query(ctx, "username")
	password := util.Query(ctx, "password")

	if username == "" || username == "root" {
		return "username_required"
	}

	if password == "" {
		return "password_required"
	}

	db, _ := util.MySQL()
	defer db.Close()

	// check if username is available
	_, lookup_err1 := user.Lookup(username)
	if lookup_err1 == nil {
		return "username_taken"
	}

	// add the user
	util.Cmd("useradd", []string{username, "-d", "/home/" + username})

	// make sure user was added
	_, lookup_err2 := user.Lookup(username)
	if lookup_err2 != nil {
		return "unable_to_create"
	}

	// set the password
	util.Bash("echo " + util.SHSanitize(password) + " | passwd " + util.SHSanitize(username) + " --stdin")

	new_token := util.MkToken()

	// add the user
	istmt, _ := db.Prepare("insert hostcontrol_users set hostcontrol_id=null, system_username=?, privileges=?, owned_by=?, login_token=?, email_address=?")

	privileges := ""

	perm_all := util.Query(ctx, "allperms")
	if strings.Contains(hcuser.Privileges, "all") && perm_all != "" {
		privileges += "all "
	}
	perm_websites := util.Query(ctx, "websites")
	if (strings.Contains(hcuser.Privileges, "websites") || strings.Contains(hcuser.Privileges, "all")) && perm_websites != "" {
		privileges += "websites "
	}
	perm_mail := util.Query(ctx, "mail")
	if (strings.Contains(hcuser.Privileges, "mail") || strings.Contains(hcuser.Privileges, "all")) && perm_mail != "" {
		privileges += "mail "
	}
	perm_databases := util.Query(ctx, "databases")
	if (strings.Contains(hcuser.Privileges, "databases") || strings.Contains(hcuser.Privileges, "all")) && perm_databases != "" {
		privileges += "databases "
	}
	perm_ftpusers := util.Query(ctx, "ftpusers")
	if (strings.Contains(hcuser.Privileges, "ftpusers") || strings.Contains(hcuser.Privileges, "all")) && perm_ftpusers != "" {
		privileges += "ftpusers "
	}
	perm_dns := util.Query(ctx, "dns")
	if (strings.Contains(hcuser.Privileges, "dns") || strings.Contains(hcuser.Privileges, "all")) && perm_dns != "" {
		privileges += "dns "
	}
	perm_sysusers := util.Query(ctx, "sysusers")
	if (strings.Contains(hcuser.Privileges, "sysusers") || strings.Contains(hcuser.Privileges, "all")) && perm_sysusers != "" {
		privileges += "sysusers "
	}

	istmt.Exec(username, privileges, hcuser.System_username, new_token, "")
	istmt.Close()

	return "success"
}
Beispiel #4
0
func login_post(ctx *macaron.Context) string {
	db, err := util.MySQL()
	defer db.Close()
	if err != nil {
		return "Problem opening MySQL"
	}

	new_token := util.MkToken()

	username := util.Query(ctx, "username")
	password := util.Query(ctx, "password")
	rememberme := util.Query(ctx, "rememberme")

	login_failed := false

	if chklogin(username, password) {
		stmt, _ := db.Prepare("SELECT * from hostcontrol_users WHERE system_username = ?")
		rows, _ := stmt.Query(username)
		stmt.Close()

		var hostcontrol_id int
		var system_username string
		var privileges string
		var owned_by string
		var login_token string
		var email_address string

		// check if we have a row returned...
		if rows.Next() {
			rows.Scan(&hostcontrol_id, &system_username, &privileges, &owned_by, &login_token, &email_address)
			ustmt, _ := db.Prepare("update hostcontrol_users set login_token=? where system_username=?")
			ustmt.Exec(new_token, username)
			ustmt.Close()

			// insert root if login worked and he doesn't exist!
		} else if username == "root" {
			istmt, _ := db.Prepare("insert hostcontrol_users set hostcontrol_id=null, system_username=?, privileges=?, owned_by=?, login_token=?, email_address=?")
			istmt.Exec("root", "all", "root", new_token, "")
			istmt.Close()

			// fallback to failure.
		} else {
			login_failed = true
		}

		if !login_failed {
			// set cookies
			if rememberme == "checked" {
				ctx.SetCookie("hostcontrol_id", strconv.Itoa(hostcontrol_id), 864000)
				ctx.SetCookie("login_token", new_token, 864000)
				ctx.SetCookie("sudo", "", 864000)
			} else {
				ctx.SetCookie("hostcontrol_id", strconv.Itoa(hostcontrol_id), 0)
				ctx.SetCookie("login_token", new_token, 0)
				ctx.SetCookie("sudo", "", 0)
			}

			// send to dashboard
			ctx.Redirect("/dashboard", 302)
			return "Redirecting to the dashboard. Click <a href=\"/dashboard\">here</a> if you are not redirected."
		}
	} else {
		login_failed = true
	}

	var tpl vision.New
	tpl.TemplateFile("template/login.tpl")

	tpl.Parse("login")

	if login_failed {
		tpl.Parse("login/fail")
	}

	return tpl.Out()

}