├── .gitignore ├── mpwc.nimble ├── README.md ├── LICENSE └── src └── mpwc.nim /.gitignore: -------------------------------------------------------------------------------- 1 | mpwc 2 | -------------------------------------------------------------------------------- /mpwc.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.3" 4 | author = "SolitudeSF" 5 | description = "Master Password command line utility" 6 | license = "MIT" 7 | srcDir = "src" 8 | bin = @["mpwc"] 9 | 10 | 11 | # Dependencies 12 | 13 | requires "nim >= 1.0.0", "masterpassword >= 0.2.0", "cligen >= 1.0.0" 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mpwc 2 | 3 | Stateless password manager using [Master Password algorithm](https://masterpassword.app/masterpassword-algorithm.pdf). 4 | 5 | ## Installation 6 | 7 | `nimble install mpwc` 8 | 9 | Requires `libsodium` installed. 10 | 11 | ## Example usage 12 | 13 | `mpwc -p "probably not the best way to supply a password" -n $(whoami) -s github` 14 | 15 | If not provided as command line arguments, password, name and site are prompted in the terminal: 16 | 17 | ``` 18 | $ mpwc 19 | password: 20 | name: SolitudeSF 21 | site: github 22 | [ ═☻═⚔ ]: thisisit 23 | ``` 24 | 25 | If enviroment variable `MPW_FULLNAME` is set then the name is read from it. 26 | 27 | You can use `--stdin` flag to read the password from `stdin`: 28 | 29 | ``` 30 | gpg2 -d -q "password.gpg" | mpwc --stdin -n me -s ssh 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2019 SolitudeSF 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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 | -------------------------------------------------------------------------------- /src/mpwc.nim: -------------------------------------------------------------------------------- 1 | import os, terminal, strutils 2 | import masterpassword, cligen 3 | 4 | type 5 | PassType = enum 6 | ptLong = "long", ptMedium = "medium", ptShort = "short", 7 | ptMaximum = "maximum", ptPin = "pin", ptBasic = "basic", ptLength = "length" 8 | 9 | proc abort(s: string, c = 1) = 10 | stderr.writeLine s 11 | quit c 12 | 13 | when defined(windows): 14 | from unicode import toUTF8, Rune, runeLenAt 15 | proc readPasswordFromStdin(pass: var string) = 16 | pass.setLen(0) 17 | while true: 18 | let c = getch() 19 | case c.char 20 | of '\r', chr(0xA): 21 | break 22 | of '\b': 23 | var i = 0 24 | var x = 1 25 | while i < pass.len: 26 | x = runeLenAt(pass, i) 27 | inc i, x 28 | pass.setLen(max(pass.len - x, 0)) 29 | of chr(0x0): 30 | continue 31 | else: 32 | pass.add(toUTF8(c.Rune)) 33 | 34 | else: 35 | import termios 36 | proc readPasswordFromStdin(pass: var string) = 37 | pass.setLen(0) 38 | let fd = stdin.getFileHandle() 39 | var cur, old: Termios 40 | discard fd.tcgetattr(cur.addr) 41 | old = cur 42 | cur.c_lflag = cur.c_lflag and not Cflag(ECHO) 43 | discard fd.tcsetattr(TCSADRAIN, cur.addr) 44 | pass = stdin.readLine 45 | discard fd.tcsetattr(TCSADRAIN, old.addr) 46 | 47 | proc mpwc( 48 | pass = "", 49 | name = "", 50 | site = "", 51 | kind = ptLong, 52 | counter = 1, 53 | length = 31, 54 | stdin = false 55 | ): int = 56 | 57 | if length <= 0 or length >= 32: 58 | abort "Password length must be between 1 and 31" 59 | 60 | let tty = system.stdin.isatty 61 | 62 | if stdin and tty: 63 | abort "stdin is connected to terminal" 64 | 65 | var 66 | pass = pass 67 | name = name 68 | site = site 69 | 70 | if pass.len == 0: 71 | if stdin: 72 | pass = system.stdin.readLine 73 | elif tty: 74 | stderr.write "password: " 75 | readPasswordFromStdin pass 76 | stderr.write "\n" 77 | if pass.len == 0: 78 | abort "Didn't specify the password" 79 | 80 | if name.len == 0: 81 | if existsEnv "MPW_FULLNAME": 82 | name = getEnv "MPW_FULLNAME" 83 | elif tty: 84 | stderr.write "name: " 85 | name = system.stdin.readLine 86 | if name.len == 0: 87 | abort "Didn't specify the name" 88 | 89 | if site.len == 0: 90 | if tty: 91 | stderr.write "site: " 92 | site = system.stdin.readLine 93 | if site.len == 0: 94 | abort "Didn't specify the site" 95 | 96 | let 97 | tmpl = (case kind 98 | of ptLong: @templateLong 99 | of ptMedium: @templateMedium 100 | of ptShort: @templateShort 101 | of ptMaximum: @templateMaximum 102 | of ptPin: @templatePin 103 | of ptBasic: @templateBasic 104 | of ptLength: @[ 105 | if length >= 3: 106 | "ano" & 'x'.repeat(length - 3) 107 | else: 108 | "ano"[0..