summaryrefslogtreecommitdiff
path: root/src/commission_func.h
blob: 6bddba805643bd24be5291ffeb7f0fe2926b4f83 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/************************************************
 * 
 * 
 * 
 * 
 * **********************************************/



#ifndef __FCGICOMMISSIONING_H
#define __FCGICOMMISSIONING_H

#include "fcgio.h"
#include <shadow.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <jsoncpp/json/json.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>

/* Constant declarations */
const int STDIN_MAX = 1000000;
const int NUM_ATTEMPTS = 3;
const int AASID_LENGTH = 30;
const int MAX_GET = 4096;

/* header declarations */
const std::string HEADER = "Content-type:application/json\r\n\r\n";

/* message declarations */
const std::string ERR_MALDATA = "Submitted data is malformed";
const std::string ERR_BADAASID = "aasID is incorrect or missing";
const std::string ERR_PWMISMATCH = "Password verification failed. Password mismatch";
const std::string ERR_USRMISMATCH = "username does not match request entry.";
const std::string ERR_NOPASS = "No password given";
const std::string ERR_ILLEGALUSRNAME = "Username is not permitted. Attempt new username";

//TEST COOOODE
//const std::string DEBUG ="Content-type:text/plain\r\n";
const std::string ERROR_PARSE = "Content-type: application/json\r\n\r\n{\r\n\"error\" : \"Json input failed to parse\",\r\n\"status\" : \"failed\"\r\n}\n";
//END TEST COOOOODE

const std::string MSG_NEWPASSWD = "New password: ";
const std::string MSG_RETYPEPASS = "Retype new password: ";
const std::string MSG_SUCCESS = "Change password success!";

/* aasType declarations */

const std::string AASTYPE_QSTN = "question; input hide";
const std::string AASTYPE_INFO = "info";
const std::string AASTYPE_ERR = "error";

/* popen cmd declarations */

const std::string POPEN_CLOSEALL = "/usr/sbin/start-stop-daemon -S -p /var/run/commissionoff.pid -b -a /bin/bash -- -c /usr/libexec/commission/off.sh";
const std::string POPEN_MTS_UBPW = "/sbin/mts-ubpasswd -up";

/* popen cmd generators */
std::string passwd_cmd_gen(std::string pw);
std::string useradd_cmd_gen(std::string usr);

/* function headers */


/******************************************************************************
 * begin_fcgi
 * 
 * Begin_fcgi holds the main code loop for the commissioning.fcgi binary. The
 * basic flow is as follow:
 * 
 * open log and initialize the fcgx request environment
 * 
 * set the streambuffers to utilize fcgi stream buffers
 * 
 * receive a request
 * 
 * handle the requests.
 * 
 * If all requests are correct, create a user
 * 
 * end fcgi commissioning
 * 
 * ***************************************************************************/
int begin_fcgi();


/* helper functions */

/******************************************************************************
 * properInput
 * 
 * properInput receives a Json::Value and determines if its contents is a 
 * message properly formatted for fcgicommissioning input. Proper input is:
 * 
 * username : <value of username>
 * aasID : <value of aasID>
 * aasAnswer : <value of aaAnswer>
 * 
 * returns true if properly formed, false if improper
 * ***************************************************************************/
bool properInput(Json::Value jobj);

/******************************************************************************
 * legalName
 * 
 * legalName checks if the requested username already exists or is implemented
 * by the system. Returns true if the name is available, false if used or 
 * unavailable
 * ***************************************************************************/
bool legalName(std::string name);

/******************************************************************************
 * confirmCommissioning
 * 
 * confirmCommissioning returns a simple, standardized confirmation message that
 * fcgicommissioning is in fact up and running. Used when no POST data is given,
 * but instead a GET is requested by the user.
 * ***************************************************************************/
std::string confirmCommissioning();


/******************************************************************************
 * printMsg
 * 
 * printMsg formulates the response data fcgicommissioning returns after the
 * user POSTs a properly formed JSON request. Values it requires are:
 * 
 * is_commission: true if a pending commissioning attempt is occuring, false
 *                if there is no current commissioning requests yet logged.
 * 
 * atmpt_cntr: The amount of attempts the user has left to return proper data.
 *             printMsg automatically iterates the attempts left whenever the
 *             is_err flag is set, and resets the atmpt_cntr and is_commission
 *             states when all attempts are consumed, resetting the commission
 *             attempt to the beginning.
 * 
 * pw:         the password the user has requested.
 * 
 * is_err:      true if the message being sent is an error message, false if
 *              it is a successful message
 * 
 * aasid:       the aasid the system has stored to check against user requests.
 * 
 * errorMsg:    receives a string that represents the standardized error messages
 *              to return to the user. These error messages are declared as 'const'
 *              values within commission_func.h
 * 
 * aasType:     a string that represents the aas message type the user is currently
 *              receiving. These aasType messages are declares as 'const' values
 *              within commission_func.h
 * 
 * ***************************************************************************/
std::string printMsg(bool &is_commission, int &atmpt_cntr, std::string &pw, bool is_err, std::string aasid, std::string errorMsg, std::string aastype);

/*****************************************************************************
 * get_request_content
 * 
 * Parses the FCGX request sent by the user and returns its value as string data
 * ***************************************************************************/
std::string get_request_content(const FCGX_Request &request);


/********************************************************************************
 * gen_aasid
 * 
 * generates an aasid to represent the current commissioning session. This value is
 * stored by fcgicommissioning throughout a successful commissioning attempt to 
 * check against valid messages, and is reset when a user exhausts their commissioning
 * attempts and needs to start over.
 * ******************************************************************************/
std::string gen_aasid();

#endif /* ~__FCGICOMMISSIONING_H */