git.fiddlerwoaroof.com
Browse code

convert to c++

Greg Wiley authored on 11/04/2017 17:55:57
Showing 9 changed files
1 1
similarity index 100%
2 2
rename from conversation.c
3 3
rename to conversation.cc
4 4
similarity index 95%
5 5
rename from dual_control.c
6 6
rename to dual_control.cc
... ...
@@ -1,7 +1,7 @@
1 1
 #include <security/pam_appl.h>
2 2
 #include <security/pam_modules.h>
3
-#include <string.h>
4
-#include <stdlib.h>
3
+#include <cstring>
4
+#include <cstdlib>
5 5
 
6 6
 #include "logging.h"
7 7
 #include "token.h"
8 8
similarity index 99%
9 9
rename from dual_control_test.c
10 10
rename to dual_control_test.cc
... ...
@@ -1,6 +1,6 @@
1 1
 #include <security/pam_appl.h>
2 2
 #include <security/pam_modules.h>
3
-#include <stdio.h>
3
+#include <cstdio>
4 4
 
5 5
 #include "conversation.h"
6 6
 #include "token.h"
7 7
similarity index 100%
8 8
rename from logging.c
9 9
rename to logging.cc
10 10
similarity index 97%
11 11
rename from logging_test.c
12 12
rename to logging_test.cc
... ...
@@ -1,5 +1,5 @@
1
-#include <stdio.h>
2
-#include <string.h>
1
+#include <cstdio>
2
+#include <cstring>
3 3
 #include <syslog.h>
4 4
 
5 5
 #include "logging.h"
... ...
@@ -4,8 +4,8 @@
4 4
 
5 5
 #ifndef _TEST_SUPPORT_H
6 6
 #define _TEST_SUPPORT_H
7
-#include <stdlib.h>
8
-#include <stdio.h>
7
+#include <cstdlib>
8
+#include <cstdio>
9 9
 #include <pwd.h>
10 10
 #include <sys/stat.h>
11 11
 // SYSLOG
... ...
@@ -1,8 +1,8 @@
1 1
 #ifndef _TESTUTIL_H
2 2
 #define _TESTUTIL_H
3 3
 
4
-#include <string.h>
5
-#include <stdio.h>
4
+#include <cstring>
5
+#include <cstdio>
6 6
 #define check(assertion, msg) \
7 7
     if (!(assertion)) { \
8 8
       fprintf(stderr, "assertion failed: %s\n", msg); \
9 9
similarity index 97%
10 10
rename from token.c
11 11
rename to token.cc
... ...
@@ -1,9 +1,9 @@
1
-#include <stdlib.h>
2
-#include <string.h>
1
+#include <cstdlib>
2
+#include <cstring>
3 3
 #include <security/pam_modules.h>
4 4
 #include <pwd.h>
5 5
 #include <unistd.h>
6
-#include <stdio.h>
6
+#include <cstdio>
7 7
 #include <sys/stat.h>
8 8
 
9 9
 #include "token.h"
... ...
@@ -45,9 +45,11 @@ int get_passwd(const char *user, struct passwd *passwd, buffer_t buffer) {
45 45
     return (found_passwd != 0);
46 46
 }
47 47
 
48
-int validate_token(const char *user, const char *token) {
49 48
 
49
+int validate_token(const char *user, const char *token) {
50 50
 
51
+    char fetched_token[7];
52
+    FILE *fp = 0;
51 53
     char *filepath = 0;
52 54
     char *working_token = 0;
53 55
     buffer_t buffer = allocate_buffer();
... ...
@@ -55,31 +57,27 @@ int validate_token(const char *user, const char *token) {
55 57
     int ok = 0;
56 58
     struct passwd passwd;
57 59
     int user_found = get_passwd(user, &passwd, buffer);
58
-
59
-    // check if user is known
60
-    if(!user_found) {
61
-       goto finally;
62
-    }
63
-
64 60
     const char *directory = passwd.pw_dir;
65
-
66 61
     int dir_len = strlen(directory);
67 62
     int fname_len = strlen(".dual_control");
63
+    struct stat file_stat;
68 64
     filepath = (char *)malloc((dir_len + 1 + fname_len + 1) * sizeof(char));
69 65
 
70 66
     strcpy(filepath, directory);
71 67
     strcat(filepath, "/");
72 68
     strcat(filepath, ".dual_control");
73
-
74
-    struct stat file_stat;
75 69
     int check_file = stat(filepath, &file_stat);
70
+    // check if user is known
71
+    if(!user_found) {
72
+       goto finally;
73
+    }
74
+
75
+
76 76
     if (check_file) {
77 77
         goto finally;
78 78
     }
79 79
 
80 80
     // read the file and grab token
81
-    char fetched_token[7];
82
-    FILE *fp = 0;
83 81
     fp = fopen(filepath, "r");
84 82
     fgets(fetched_token, 7, fp);
85 83
     fclose(fp);
86 84
similarity index 92%
87 85
rename from token_test.c
88 86
rename to token_test.cc
... ...
@@ -1,6 +1,6 @@
1
-#include <string.h>
1
+#include <cstring>
2 2
 #include <pwd.h>
3
-#include <stdio.h>
3
+#include <cstdio>
4 4
 #include <sys/stat.h>
5 5
 
6 6
 #include "token.h"
... ...
@@ -10,7 +10,7 @@ const char *fake_user = "";
10 10
 const char *fake_user_token = "";
11 11
 
12 12
 // all the fake system calls
13
-char *fake_home_dir = "";
13
+const char *fake_home_dir = "";
14 14
 int fake_getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result) {
15 15
   strcpy(buffer, fake_home_dir);
16 16
   pwd->pw_dir = buffer;
... ...
@@ -20,13 +20,13 @@ int fake_getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bu
20 20
 }
21 21
 
22 22
 
23
-char *fake_stat_path = "";
23
+const char *fake_stat_path = "";
24 24
 int fake_stat(const char *path, struct stat *stat) {
25 25
     return (strcmp(fake_stat_path, path));
26 26
 }
27 27
 
28
-char *fake_fopen_path = "";
29
-char *fake_fopen_mode = "";
28
+const char *fake_fopen_path = "";
29
+const char *fake_fopen_mode = "";
30 30
 FILE *_fhandle = 0;
31 31
 FILE *fake_fopen(const char *path, const char *mode) {
32 32
     static FILE handle;