Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed May 20, 2014
0 parents commit 53389d7
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
215 changes: 215 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#################
## Eclipse
#################

*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath


#################
## Visual Studio
#################

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf

#############
## Windows detritus
#############

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
.DS_Store


#############
## Python
#############

*.py[co]

# Packages
*.egg
*.egg-info
dist/
build/
eggs/
parts/
var/
sdist/
develop-eggs/
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg
16 changes: 16 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Copyright (c) 2014 David Mzareulyan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Package pageant is a small package for sending queries and receiving answers
to/from PyTTY pageant.exe utility.

This package is windows-only.

See documentation on [GoDoc](http://godoc.org/github.com/davidmz/go-pageant)

License: MIT
113 changes: 113 additions & 0 deletions pageant_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Package pageant is a small package for sending queries and receiving answers
to/from PyTTY pageant.exe utility.
This package is windows-only.
*/
package pageant

// see https://github.com/Yasushi/putty/blob/master/windows/winpgntc.c#L155
// see https://github.com/paramiko/paramiko/blob/master/paramiko/win_pageant.py

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
. "syscall"
. "unsafe"
)

var (
ErrPageantNotFound = errors.New("Pageant process not found")
ErrMessageTooLong = errors.New("Message too long")
ErrSendMessage = errors.New("Error sending message")
)

const (
// Maximum size of the message can be sent to pageant
MaxMessageLen = 8192 - 4
)

// IsActive returns true if Pageant process is active and can be queried.
func IsActive() bool {
return 0 == getPageantWindow()
}

// Query sends message msg to Pageant and returns response or error.
func Query(msg []byte) ([]byte, error) {
if len(msg) > MaxMessageLen {
return nil, ErrMessageTooLong
}

paWin := getPageantWindow()
if paWin == 0 {
return nil, ErrPageantNotFound
}

thId, _, _ := MustLoadDLL("kernel32.dll").MustFindProc("GetCurrentThreadId").Call()
mapName := fmt.Sprintf("PageantRequest%08x", thId)
pMapName, _ := UTF16PtrFromString(mapName)

mmap, err := CreateFileMapping(InvalidHandle, nil, PAGE_READWRITE, 0, MaxMessageLen+4, pMapName)
if err != nil {
return nil, err
}
defer CloseHandle(mmap)

ptr, err := MapViewOfFile(mmap, FILE_MAP_WRITE, 0, 0, 0)
if err != nil {
return nil, err
}

mmSlice := (*(*[MaxMessageLen]byte)(Pointer(ptr)))[:]

buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint32(len(msg)))
binary.Write(buf, binary.BigEndian, msg)

copy(mmSlice, buf.Bytes())

cds := copyData{
dwData: agentCopydataID,
cbData: uint32(len(mapName) + 1),
lpData: Pointer(&([]byte(mapName))[0]),
}

resp, _, _ := MustLoadDLL("user32.dll").MustFindProc("SendMessageW").Call(
paWin,
wmCopydata,
0,
uintptr(Pointer(&cds)),
)
if resp == 0 {
return nil, ErrSendMessage
}

respLen := binary.BigEndian.Uint32(mmSlice[:4])
respData := mmSlice[4 : respLen+4]

return respData, nil
}

/////// Internals ////////

const (
agentCopydataID = 0x804e50ba
wmCopydata = 74
)

type copyData struct {
dwData uintptr
cbData uint32
lpData Pointer
}

func getPageantWindow() uintptr {
nameP, _ := UTF16PtrFromString("Pageant")
win, _, _ := MustLoadDLL("user32.dll").MustFindProc("FindWindowW").Call(
uintptr(Pointer(nameP)),
uintptr(Pointer(nameP)),
)
return win
}

0 comments on commit 53389d7

Please sign in to comment.