-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
executable file
·94 lines (86 loc) · 3.33 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using Npam.Interop;
using Npam;
namespace Npam.Example
{
public class Program
{
//const string PamServiceName = "password-auth"; //For RHEL based Linux
const string PamServiceName = "passwd";
public static void Main(string[] args){
Console.Write("Username: ");
string user = Console.ReadLine();
using (NpamSession mySession = new NpamSession(PamServiceName, user, ConvHandler, IntPtr.Zero)) {
var retval = mySession.Start();
if (retval == PamStatus.PAM_SUCCESS) {
Console.WriteLine("START - SUCCESS!");
retval = mySession.Authenticate(0);
if (retval == PamStatus.PAM_SUCCESS) {
Console.WriteLine("AUTHENTICATION - SUCCESS!");
retval = mySession.AccountManagement(0);
if (retval == PamStatus.PAM_SUCCESS) {
Console.WriteLine("ACCESS - SUCCESS!");
} else {
Console.WriteLine("ACCESS - Failure: {0}", retval);
}
}
else {
Console.WriteLine("AUTHENTICATION - Failure: {0}", retval);
}
} else {
Console.WriteLine("START - Failure: {0}", retval);
}
}
}
private static IEnumerable<PamResponse> ConvHandler (IEnumerable<PamMessage> messages, IntPtr appData) {
foreach (PamMessage message in messages)
{
string response = "";
switch (message.MsgStyle) {
case MessageStyle.PAM_PROMPT_ECHO_ON :
Console.Write(message.Message);
response = Console.ReadLine();
break;
case MessageStyle.PAM_PROMPT_ECHO_OFF :
Console.Write(message.Message);
response = AcceptInputNoEcho();
break;
case MessageStyle.PAM_ERROR_MSG :
Console.Error.WriteLine(message.Message);
break;
default:
Console.WriteLine(message.Message);
Console.WriteLine("Unsupported PAM message style \"{0}\".", message.MsgStyle);
break;
}
yield return new PamResponse(response);
}
}
private static string AcceptInputNoEcho()
{
string password = "";
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password = password.Substring(0, password.Length - 1);
}
}
else
{
password += (i.KeyChar);
}
}
return password;
}
}
}