a15a433d2ed4b9aabea9d52108657113a589f0ad
[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 #if !USE_POPEN
21 # include <unistd.h>
22 #endif
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         printf("SIGCHLD\n");
48         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         char    buf[512];
89          int    stdin_pair[2];
90          int    stdout_pair[2];
91         pid_t   childPid;
92         pid_t   parentPid;
93         
94         #if DEBUG
95         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
96         #endif
97         
98         // Sanity please
99         if( Item != 0 ) return -1;
100         
101         // Check if user is in door
102         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
103         {
104                 #if DEBUG
105                 printf("Door_CanDispense: User %i not in door\n", User);
106                 #endif
107                 return 1;
108         }
109         
110         // Create stdin/stdout
111         if( pipe(stdin_pair) || pipe(stdout_pair) )
112         {
113                 perror("pipe");
114                 return -1;
115         }
116         
117         giDoor_ChildStatus = 0; // Set child status to zero
118         parentPid = getpid();
119         childPid = fork();
120         
121         if( childPid < 0 )
122         {
123                 perror("fork");
124                 return -1;
125         }
126         
127         // Child process
128         if( childPid == 0 )
129         {
130                 
131                 // Close write end of stdin, and set it to #0
132                 close(stdin_pair[1]);   dup2(stdin_pair[0], 0);
133                 // Close read end of stdout, and set it to #1
134                 close(stdout_pair[0]);  dup2(stdout_pair[1], 1);
135                 
136                 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
137                 perror("execl");
138                 exit(-1);
139         }
140         
141         child_stdin = fdopen(stdin_pair[1], "w");
142         close(stdin_pair[0]);   // child stdin read
143         close(stdout_pair[1]);  // child stdout write
144         
145         if( giDoor_ChildStatus || read(stdout_pair[0], buf, 512) < 0) {
146                 #if DEBUG
147                 printf("Door_DoDispense: fread fail\n");
148                 #endif
149                 return -1;
150         }
151         
152         // Send password
153         if( giDoor_ChildStatus || fputs(gsDoor_Password, child_stdin) <= 0 ) {
154                 #if DEBUG
155                 printf("Door_DoDispense: fputs password\n");
156                 #endif
157                 return -1;
158         }
159         fputs("\n", child_stdin);
160         
161         
162         #if DEBUG
163         printf("Door_DoDispense: Door unlock\n");
164         #endif
165         
166         // ATH1 - Unlock door
167         if( giDoor_ChildStatus || fputs("ATH1\n", child_stdin) <= 0) {
168                 #if DEBUG
169                 printf("Door_DoDispense: fputs unlock\n");
170                 #endif
171                 return -1;
172         }
173         
174         // Wait before re-locking
175         sleep(DOOR_UNLOCKED_DELAY);
176
177
178         #if DEBUG
179         printf("Door_DoDispense: Door re-lock\n");
180         #endif
181
182         // Re-lock the door
183         if( giDoor_ChildStatus || fputs("ATH0\n", child_stdin) == 0 ) {
184                 #if DEBUG
185                 printf("Door_DoDispense: fputs lock\n");
186                 #endif
187                 return -1;
188         }
189         
190         fclose(child_stdin);
191         close(stdin_pair[1]);   // child stdin write
192         close(stdout_pair[0]);  // child stdout read
193         
194         #if DEBUG
195         printf("Door_DoDispense: User %i opened door\n", User);
196         #endif
197
198         return 0;
199 }
200

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