예제 #1
0
func stdin_read_password() string {
	var in = C.GetStdHandle(C.STD_INPUT_HANDLE)
	var old_flags C.DWORD
	var isTty = (C.GetFileType(in) == C.FILE_TYPE_CHAR)
	if isTty {
		if C.GetConsoleMode(in, &old_flags) != C.FALSE {
			C.SetConsoleMode(in, C.ENABLE_LINE_INPUT|C.ENABLE_PROCESSED_INPUT)
		} else {
			isTty = false
		}
	}

	var input [inputLength]byte
	var length = inputLength
	var count C.DWORD
	C.ReadFile(in, C.PVOID(&input[0]), inputLength, &count, nil)
	var countInt = int(count)
	if countInt >= 2 && input[countInt-2] == '\r' {
		length = countInt - 2
	} else {
		var buf [inputLength]byte
		for C.ReadFile(in, C.PVOID(&buf[0]), inputLength, &count, nil) > 0 {
			if count >= 2 && buf[count-2] == '\r' {
				break
			}
		}
	}

	fmt.Fprintln(os.Stderr)

	if isTty {
		C.SetConsoleMode(in, old_flags)
	}
	return string(input[:length])
}
예제 #2
0
파일: kernel.go 프로젝트: ikbear/go_code
func SetWallPaper(file_path string) error {
	path := []byte(file_path)
	result := int(C.SystemParametersInfo(C.SPI_SETDESKWALLPAPER, 0, C.PVOID(unsafe.Pointer(&path[0])), C.SPIF_UPDATEINIFILE))
	if result != C.TRUE {
		return fmt.Errorf("", C.GetLastError())
	}
	return nil
}
예제 #3
0
func (*keychainWindows) GetPassword(service, account string) (string, error) {
	// Validate input
	serviceValid := isValidNonNullUTF8(service)
	accountValid := isValidNonNullUTF8(account)
	if !(serviceValid && accountValid) {
		return "", ErrInvalidValue
	}

	// Compute target item name
	target := targetFormat(service, account)

	// Convert the target name.  See note in AddPassword.
	targetUTF16Ptr, err := syscall.UTF16PtrFromString(target)
	if err != nil {
		return "", ErrInvalidValue
	}
	// NOTE: For some reason they use LPCWSTR here, as opposed to LPWSTR in the
	// CREDENTIALW struct
	targetUTF16 := C.LPCWSTR(unsafe.Pointer(targetUTF16Ptr))

	// Query the credential
	var credential C.PCREDENTIALW
	if C.CredReadW(targetUTF16, C.CRED_TYPE_GENERIC, 0, &credential) != C.TRUE {
		return "", ErrNotFound
	}

	// Extract the password blob
	result := C.GoStringN(
		(*C.char)(unsafe.Pointer(credential.CredentialBlob)),
		C.int(credential.CredentialBlobSize),
	)

	// Free the credential memory
	C.CredFree(C.PVOID(credential))

	// All done
	return result, nil
}
예제 #4
0
func (ss *saslSession) Step(serverData []byte) (clientData []byte, done bool, err error) {
	ss.step++
	if ss.step > 10 {
		return nil, false, fmt.Errorf("too many SSPI steps without authentication")
	}
	var buffer C.PVOID
	var bufferLength C.ULONG
	var outBuffer C.PVOID
	var outBufferLength C.ULONG
	if len(serverData) > 0 {
		buffer = (C.PVOID)(unsafe.Pointer(&serverData[0]))
		bufferLength = C.ULONG(len(serverData))
	}
	var status C.int
	if ss.authComplete {
		// Step 3: last bit of magic to use the correct server credentials
		status = C.sspi_send_client_authz_id(&ss.context, &outBuffer, &outBufferLength, ss.cstr(ss.userPlusRealm))
	} else {
		// Step 1 + Step 2: set up security context with the server and TGT
		status = C.sspi_step(&ss.credHandle, ss.hasContext, &ss.context, buffer, bufferLength, &outBuffer, &outBufferLength, ss.cstr(ss.target))
	}
	if outBuffer != C.PVOID(nil) {
		defer C.free(unsafe.Pointer(outBuffer))
	}
	if status != C.SEC_E_OK && status != C.SEC_I_CONTINUE_NEEDED {
		ss.errored = true
		return nil, false, ss.handleSSPIErrorCode(status)
	}

	clientData = C.GoBytes(unsafe.Pointer(outBuffer), C.int(outBufferLength))
	if status == C.SEC_E_OK {
		ss.authComplete = true
		return clientData, true, nil
	} else {
		ss.hasContext = 1
		return clientData, false, nil
	}
}