Let's try this again...
[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
42 // == CODE ===
43 int Door_InitHandler(void)
44 {       
45         return 0;
46 }
47
48 /**
49  */
50 int Door_CanDispense(int User, int Item)
51 {
52         #if DEBUG
53         printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
54         #endif
55         // Sanity please
56         if( Item != 0 ) return -1;
57         
58         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
59         {
60                 #if DEBUG
61                 printf("Door_CanDispense: User %i not in door\n", User);
62                 #endif
63                 return 1;
64         }
65         
66         #if DEBUG
67         printf("Door_CanDispense: User %i can open the door\n", User);
68         #endif
69         
70         return 0;
71 }
72
73 /**
74  * \brief Actually do a dispense from the coke machine
75  */
76 int Door_DoDispense(int User, int Item)
77 {
78         FILE    *child_stdin;
79         char    buf[512];
80          int    stdin_pair[2];
81          int    stdout_pair[2];
82         pid_t   childPid;
83         
84         #if DEBUG
85         printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
86         #endif
87         
88         // Sanity please
89         if( Item != 0 ) return -1;
90         
91         // Check if user is in door
92         if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
93         {
94                 #if DEBUG
95                 printf("Door_CanDispense: User %i not in door\n", User);
96                 #endif
97                 return 1;
98         }
99         
100         // Create stdin/stdout
101         if( pipe(stdin_pair) || pipe(stdout_pair) )
102         {
103                 perror("pipe");
104                 return -1;
105         }
106         
107         childPid = fork();
108         
109         if( childPid < 0 )
110         {
111                 perror("fork");
112                 return -1;
113         }
114         
115         // Child process
116         if( childPid == 0 )
117         {
118                 // Close write end of stdin, and set it to #0
119                 close(stdin_pair[1]);   dup2(stdin_pair[0], 0);
120                 // Close read end of stdout, and set it to #1
121                 close(stdout_pair[0]);  dup2(stdout_pair[1], 1);
122                 
123                 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
124                 perror("execl");
125                 exit(-1);
126         }
127         
128         child_stdin = fdopen(stdin_pair[1], "w");
129         close(stdin_pair[0]);   // child stdin read
130         close(stdout_pair[1]);  // child stdout write
131         
132         if(read(stdout_pair[1], buf, 512) < 0) {
133                 #if DEBUG
134                 printf("Door_DoDispense: fread fail\n");
135                 #endif
136                 return -1;
137         }
138         
139         // Send password
140         if( fputs(gsDoor_Password, child_stdin) <= 0 ) {
141                 #if DEBUG
142                 printf("Door_DoDispense: fputs password\n");
143                 #endif
144                 return -1;
145         }
146         fputs("\n", child_stdin);
147         
148         
149         #if DEBUG
150         printf("Door_DoDispense: Door unlock\n");
151         #endif
152         
153         // ATH1 - Unlock door
154         if( fputs("ATH1\n", child_stdin) <= 0) {
155                 #if DEBUG
156                 printf("Door_DoDispense: fputs unlock\n");
157                 #endif
158                 return -1;
159         }
160         
161         // Wait before re-locking
162         sleep(DOOR_UNLOCKED_DELAY);
163
164
165         #if DEBUG
166         printf("Door_DoDispense: Door re-lock\n");
167         #endif
168
169         // Re-lock the door
170         if( fputs("ATH0\n", child_stdin) <= 0 ) {
171                 #if DEBUG
172                 printf("Door_DoDispense: fputs lock\n");
173                 #endif
174                 return -1;
175         }
176         
177         fclose(child_stdin);
178         close(stdin_pair[1]);   // child stdin write
179         close(stdout_pair[0]);  // child stdout read
180         
181         #if DEBUG
182         printf("Door_DoDispense: User %i opened door\n", User);
183         #endif
184
185         return 0;
186 }
187

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