d8c0a1fcb7b1ce213f29354f22bd75e42deba918
[tpg/opendispense2.git] / src / server / handler_door.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * handler_doror.c - Door Relay code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #define DEBUG   1
11 #define USE_POPEN       0
12
13 #include "common.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include <sys/wait.h>
22 #include <pty.h>
23
24 #define DOOR_UNLOCKED_DELAY     10      // 10 seconds before it re-locks
25
26 // === IMPORTS ===
27
28 // === PROTOTYPES ===
29  int    Door_InitHandler();
30  int    Door_CanDispense(int User, int Item);
31  int    Door_DoDispense(int User, int Item);
32
33 // === GLOBALS ===
34 tHandler        gDoor_Handler = {
35         "door",
36         Door_InitHandler,
37         Door_CanDispense,
38         Door_DoDispense
39 };
40 char    *gsDoor_Password = "";
41 volatile int    giDoor_ChildStatus;
42
43 // == CODE ===
44 void Door_SIGCHLDHandler(int signum)
45 {
46         signum = 0;
47         giDoor_ChildStatus ++;
48         printf("SIGCHLD: giDoor_ChildStatus = %i \n", giDoor_ChildStatus);
49 }
50
51 int Door_InitHandler(void)
52 {
53         signal(SIGCHLD, Door_SIGCHLDHandler);
54         return 0;
55 }
56
57 /**
58  */
59 int Door_CanDispense(int User, int Item)
60 {
61         #if DEBUG
62         printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
63         #endif
64         // Sanity please
65         if( Item != 0 ) return -1;
66         
67         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
68         {
69                 #if DEBUG
70                 printf("Door_CanDispense: User %i not in door\n", User);
71                 #endif
72                 return 1;
73         }
74         
75         #if DEBUG
76         printf("Door_CanDispense: User %i can open the door\n", User);
77         #endif
78         
79         return 0;
80 }
81
82 /**
83  * \brief Actually do a dispense from the coke machine
84  */
85 int Door_DoDispense(int User, int Item)
86 {
87         FILE    *child_stdin;
88         #if 0
89          int    stdin_pair[2];
90          int    stdout_pair[2];
91         #else
92          int    child_stdin_fd;
93         #endif
94         pid_t   childPid;
95         pid_t   parentPid;
96         
97         #if DEBUG
98         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
99         #endif
100         
101         // Sanity please
102         if( Item != 0 ) return -1;
103         
104         // Check if user is in door
105         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
106         {
107                 #if DEBUG
108                 printf("Door_CanDispense: User %i not in door\n", User);
109                 #endif
110                 return 1;
111         }
112         
113         giDoor_ChildStatus = 0; // Set child status to zero
114         parentPid = getpid();
115         #if 0
116         // Create stdin/stdout
117         if( pipe(stdin_pair) || pipe(stdout_pair) )
118         {
119                 perror("pipe");
120                 return -1;
121         }
122         
123         childPid = fork();
124         #else
125         childPid = forkpty(&child_stdin_fd, NULL, NULL, NULL);
126         #endif
127         
128         if( childPid < 0 )
129         {
130                 perror("fork");
131                 return -1;
132         }
133         
134         // Child process
135         if( childPid == 0 )
136         {
137                 #if 0
138                 // Close write end of stdin, and set it to #0
139                 close(stdin_pair[1]);   dup2(stdin_pair[0], 0);
140                 // Close read end of stdout, and set it to #1
141                 close(stdout_pair[0]);  dup2(stdout_pair[1], 1);
142                 #endif
143                 
144                 //execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
145                 execl("/usr/bin/llogin", "llogin", "door", "-w-", NULL);
146                 perror("execl");
147                 exit(-1);
148         }
149         
150         #if 0
151         child_stdin = fdopen(stdin_pair[1], "w");
152         close(stdin_pair[0]);   // child stdin read
153         close(stdout_pair[1]);  // child stdout write
154         #else
155         child_stdin = fdopen(child_stdin_fd, "w");
156         #endif
157         
158         {
159                 char    buf[1024];
160                  int    len;
161                 #if 0
162                 if( giDoor_ChildStatus || (len = read(stdout_pair[0], buf, sizeof buf)) < 0)
163                 #else
164                 if( giDoor_ChildStatus || (len = read(child_stdin_fd, buf, sizeof buf)) < 0)
165                 #endif
166                 {
167                         #if DEBUG
168                         int child_exit;
169                         waitpid(childPid, &child_exit, 0);
170                         printf("Door_DoDispense: fread fail (child status %i)\n", child_exit);
171                         #endif
172                         return -1;
173                 }
174                 buf[len] = '\0';
175                 
176                 #if DEBUG
177                 printf("Door_DoDispense: buf = %i '%s'\n", len, buf);
178                 #endif
179         }
180         
181         // Send password
182         if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) {
183                 #if DEBUG
184                 int child_exit;
185                 waitpid(childPid, &child_exit, 0);
186                 printf("Door_DoDispense: fputs password fail (child status %i)\n", child_exit);
187                 #endif
188                 return -1;
189         }
190         fputs("\n", child_stdin);
191         
192         
193         #if DEBUG
194         printf("Door_DoDispense: Door unlock\n");
195         #endif
196         
197         // ATH1 - Unlock door
198         if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) {
199                 #if DEBUG
200                 printf("Door_DoDispense: fputs unlock\n");
201                 #endif
202                 return -1;
203         }
204         
205         // Wait before re-locking
206         sleep(DOOR_UNLOCKED_DELAY);
207
208
209         #if DEBUG
210         printf("Door_DoDispense: Door re-lock\n");
211         #endif
212
213         // Re-lock the door (and quit llogin)
214         if( giDoor_ChildStatus || fputs("ATH0\n\x1D", child_stdin) == 0 ) {
215                 #if DEBUG
216                 printf("Door_DoDispense: fputs lock\n");
217                 #endif
218                 return -1;
219         }
220         
221         fclose(child_stdin);
222         #if 0
223         close(stdin_pair[1]);   // child stdin write
224         close(stdout_pair[0]);  // child stdout read
225         #else
226         close(child_stdin_fd);
227         #endif
228         
229         #if DEBUG
230         printf("Door_DoDispense: User %i opened door\n", User);
231         #endif
232
233         kill(childPid, SIGKILL);
234
235         return 0;
236 }
237

UCC git Repository :: git.ucc.asn.au