Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.40 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <mysql.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8.  
  9. #define MYSQL_HOST "127.0.0.1" //Host for MySQL
  10. #define MYSQL_USER "root" //Username for MySQL
  11. #define MYSQL_PASS "Dr. John A. Zoidberg" //Password for MySQL
  12. #define MYSQL_DB "turnoutc" //MySQL database to use
  13. #define PASSWORD "Slurm"
  14.  
  15. typedef struct _auser AUser;
  16.  
  17. struct _auser {
  18. char *username;
  19. AUser *next;
  20. };
  21.  
  22. AUser *readConfig(const char *); // Reads the given config file
  23. char *getUsername(void); // Gets the current username
  24. int superUser(const char *, const AUser *); // Is a superuser?
  25. char *getGrade(const MYSQL *, const char *); // Returns the grade for user
  26. void showGrade(const MYSQL *, const char *); // Shows the grade for a user
  27. int showMenu(int superuser); // Show menu and get choice
  28. void studentList(const MYSQL *); // Give student list w/grades
  29. void changeGrade(const MYSQL *); // Change a grade
  30. int superLogin();
  31.  
  32. char *config = "/usr/local/etc/gradebook.cfg"; // Config file (default)
  33. char *misc = "cylons!";
  34. int debug=0; // Are we running in debug mode?
  35. int isSU;
  36.  
  37. int main(int argc, char **argv) {
  38. AUser *admins;
  39. MYSQL *connect;
  40. char *username=getUsername();
  41. int choice;
  42.  
  43. if (argc > 0) {
  44. int i;
  45. for(i = 1; i < argc; i++) {
  46. if(!strcasecmp(argv[i], "-debug")) {
  47. debug = 1;
  48. }
  49. else if(!strcasecmp(argv[i], "-config")) {
  50. if(i < argc) {
  51. config = argv[i+1];
  52. i++;
  53. printf("Using config file: %s\n\n", config);
  54. }
  55. }
  56. else if(!strcasecmp(argv[i], "--help")) {
  57. printf("Usage: turnout [OPTION]\n");
  58. return 0;
  59. } else {
  60. printf("turnout: invalid option -- '%s'\n",argv[i]);
  61. printf("Try `turnout --help for more information.\n");
  62. return 1;
  63. }
  64. }
  65. }
  66.  
  67. if (debug)
  68. printf("DEBUG: Current username is %s\n\n", username);
  69.  
  70.  
  71. // Read Config
  72. admins=readConfig(config);
  73.  
  74. // Initialize MySQL connection variable
  75. connect = mysql_init(NULL);
  76.  
  77. // Connect to the MySQL database, altering the user and dieing if we fail
  78. if (mysql_real_connect(connect,MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB,3306,NULL,0)==NULL) {
  79. printf("FATAL: Unable to connect to database!\n\n");
  80. return -1;
  81. }
  82.  
  83. // If they ain't a superuser ..
  84. if (!superUser(username,admins)) {
  85. showGrade(connect,username); // Give them THEIR grade
  86.  
  87. return 1; // And bail
  88. }
  89.  
  90. // If we get here, they are a superuser
  91. do {
  92. choice=showMenu(isSU);
  93. switch(choice) {
  94. case 1: // They want a list of students
  95. studentList(connect);
  96. break;
  97. case 2: // They want to change a grade
  98. changeGrade(connect);
  99. break;
  100. case 3: // They want their grade
  101. showGrade(connect,username);
  102. break;
  103. case 4:
  104. superLogin();
  105. break;
  106. case 9: // They want to exit
  107. printf("\nExiting...\n\n");
  108. break;
  109. default:
  110. printf("\nPlease select an option from the menu\n\n");
  111. }
  112. } while (choice != 9); // Continue until they want to exit
  113.  
  114. mysql_close(connect); // Close the database
  115.  
  116. return 0;
  117. }
  118.  
  119. int superLogin() {
  120. char buff[10];
  121. int su=1;
  122.  
  123. printf("Password: ");
  124. gets(buff);
  125.  
  126. if(!strcmp(buff, PASSWORD)) {
  127. su = 1;
  128. }
  129. isSU = su;
  130.  
  131. return 0;
  132. }
  133.  
  134. // Reads the given config file and imports the admins
  135. // Pre: config contains a valid config file location
  136. // Post: returns a linked list of admin user names
  137. AUser *readConfig(const char *config) {
  138. FILE *inputFile;
  139. char buff[50];
  140. AUser *admins=NULL, *head=NULL;
  141.  
  142. // If it's NULL, better bail
  143. if (!config) {
  144. printf("FATAL: Error in configuration file!\n");
  145. exit(-1);
  146. }
  147.  
  148. // Can't read the config? BAIL
  149. if ((inputFile=fopen(config,"r"))==NULL) {
  150. printf("FATAL: Unable to open config file %s\n", config);
  151. exit(-1);
  152. }
  153. while (fgets(&buff[0],50,inputFile)) { // Step through the file
  154. if (buff[0]!='#') {
  155. if (!admins) { // Is this the first one?
  156. if ((admins=malloc(sizeof(AUser)))==NULL) { // Allocate memory
  157. printf("FATAL: Unable to allocate memory!\n"); // Die if we can't
  158. exit(-1);
  159. }
  160. head=admins; // Save the head of the list
  161. } else {
  162. if ((admins->next=malloc(sizeof(AUser)))==NULL) { // Allocate memory
  163. printf("FATAL: Unable to allocate memory!\n"); // Die if we can't
  164. exit(-1);
  165. }
  166. admins=admins->next; // We want to work with the new one
  167. }
  168.  
  169. admins->username=strdup(&buff[0]); // Copy the user name into the list
  170. admins->next=NULL;
  171. if (admins->username[strlen(admins->username)-1]=='\n')
  172. admins->username[strlen(admins->username)-1]='\0';
  173. }
  174. }
  175.  
  176. fclose(inputFile); // Close the file
  177.  
  178. return head;
  179. }
  180.  
  181. // Returns the user name of the user running this process
  182. // Pre: The program is NOT run setuid()
  183. // Post: The username of the logged in user is returned
  184. char *getUsername() {
  185. return getenv("USERNAME");
  186. }
  187.  
  188. // Returns non-zero iff user is a member of the admins linked list
  189. // Pre: username contains a valid username; admins is a valid linked list
  190. // Post: returns true iff user is in admins
  191. int superUser(const char *username, const AUser *admins) {
  192. AUser *ptr;
  193.  
  194. for (ptr=admins; ptr; ptr=ptr->next) {
  195. if (debug)
  196. printf("DEBUG: Processing admin %s against %s\n",ptr->username,username);
  197. if (ptr && ptr->username && !strcasecmp(ptr->username, username))
  198. return 1; // We have a match
  199. }
  200.  
  201. return 0; // Nope, no match
  202. }
  203.  
  204. // Get the grade for the given username from the MySQL database
  205. // Pre: conect is a valid, active MySQL connection
  206. // username contains a valid username
  207. // Post: the grade is returned. Returns NULL if there is an error
  208. char *getGrade(const MYSQL *connect, const char *username) {
  209. int rows;
  210. char buff[1024], *grade;
  211. MYSQL_RES *result;
  212. MYSQL_ROW row;
  213.  
  214. if ((!connect) || (!username)) {
  215. // Somethings messed up... let's consider this FATAL
  216. // Tell the user and BAIL
  217. printf("FATAL: NULL values in parameters to getGrade()!\n");
  218. return NULL;
  219. }
  220.  
  221. snprintf(&buff[0],1023,"SELECT grade FROM grades WHERE username='%s'",username);
  222. if (mysql_query(connect,&buff[0]) != 0) {
  223. // If it failed, tell the user
  224. printf("Error: %s!\n", mysql_error(connect));
  225. return NULL; // Return NULL tells the caller the data isn't valid
  226. }
  227.  
  228. result = mysql_store_result(connect);
  229. if (result==NULL) {
  230. // If it failed, tell the user
  231. printf("Error: %s!\n", mysql_error(connect));
  232. return NULL; // Return NULL tells the caller the data isn't valid
  233. }
  234.  
  235. // See how many rows we have
  236. rows=mysql_num_rows(result);
  237. if (rows==0) {
  238. return NULL; // Return NULL tells the caller the data isn't valid
  239. }
  240.  
  241. // Get the data out of the result
  242. row=mysql_fetch_row(result);
  243. grade=strdup(row[0]);
  244.  
  245. mysql_free_result(result); // Free the result
  246.  
  247. return grade; // Send the grade back to the caller
  248. }
  249.  
  250. // Show the grade for the given user from the MySQL database given
  251. // Pre: conect is a valid, active MySQL connection
  252. // username contains a valid username
  253. // Post: the grade has been shown -- variables remain unchanged
  254. void showGrade(const MYSQL *connect, const char *username) {
  255. if ((!connect) || (!username)) {
  256. // Something's messed up ... tell the user and BAIL
  257. printf("FATAL: NULL values in parameters to showGrade()!\n");
  258. exit(-1);
  259. }
  260.  
  261. char *grade=getGrade(connect,username); // Get the grade
  262.  
  263. if (grade) { // If it exists show them
  264. printf("Grade for %s is: %s\n",username,grade);
  265. free(grade); // The free the memory used
  266. } else {
  267. // We don't have a grade for this user... tell them
  268. printf("Error: This user does not have a grade!\n");
  269. }
  270.  
  271. return;
  272. }
  273.  
  274. // Show the user a menu and get their answer
  275. // Pre: None
  276. // Post: Returns the number of the menu option the user selected
  277. int showMenu(int superuser) {
  278. char myChar;
  279. int choice;
  280.  
  281. if (!superuser) {
  282. printf("1. Get My Grade\n");
  283. printf("2. Login As Superuser\n");
  284. } else {
  285. printf("1. List Students & Grades\n");
  286. printf("2. Change A Grade\n");
  287. }
  288. printf("9. Exit\n");
  289.  
  290. do {
  291. myChar=getchar();
  292. } while (myChar=='\n');
  293.  
  294. getchar();
  295. choice = (myChar-48);
  296.  
  297. if (!superuser && (choice != 9))
  298. return 2+choice;
  299. else
  300. return choice;
  301. }
  302.  
  303. // Lists the sudents in the database, alphabetical by username, with grades
  304. // Pre: Database is connected
  305. // Post: List is shown
  306. void studentList(const MYSQL *connect) {
  307. int rows, i;
  308. char buff[1024];
  309. MYSQL_RES *result;
  310. MYSQL_ROW row;
  311.  
  312. if (!connect) {
  313. // Somethings messed up... let's consider this FATAL
  314. // Tell the user and BAIL
  315. printf("FATAL: NULL values in parameters to studentList()!\n");
  316. return;
  317. }
  318.  
  319. snprintf(&buff[0],1023,"SELECT * FROM grades ORDER BY username");
  320. if (mysql_query(connect,&buff[0]) != 0) {
  321. // If it failed, tell the user
  322. printf("Error: %s!\n", mysql_error(connect));
  323. return;
  324. }
  325.  
  326. result = mysql_store_result(connect);
  327. if (result==NULL) {
  328. // If it failed, tell the user
  329. printf("Error: %s!\n", mysql_error(connect));
  330. return;
  331. }
  332.  
  333. // See how many rows we have
  334. rows=mysql_num_rows(result);
  335. if (rows==0) {
  336. // We don't have any grades... tell them
  337. printf("Error: There are no users in the system!\n");
  338. return;
  339. }
  340.  
  341. // Get the data out of the result
  342.  
  343. printf("\nUsername\tGrade\n");
  344. for (i=0; i<rows; i++) {
  345. row=mysql_fetch_row(result);
  346. printf("%s \t%s\n",row[0],row[1]);
  347. }
  348. printf("End of list.\n\n");
  349.  
  350. mysql_free_result(result); // Free used memory
  351.  
  352. return;
  353. }
  354.  
  355. // Changes the grade for a student in the database
  356. // Pre: Database is connected
  357. // Post: Grade is updated, or no change
  358. void changeGrade(const MYSQL *connect) {
  359. char buff[1024], choice;
  360. char uname[30];
  361.  
  362. if (!connect) {
  363. // Somethings messed up... let's consider this FATAL
  364. // Tell the user and BAIL
  365. printf("FATAL: NULL values in parameters to changeGrade()!\n");
  366. return;
  367. }
  368.  
  369. // Get the username
  370. printf("Enter the username: ");
  371. fgets(&uname[0],30,stdin);
  372. uname[strlen(&uname[0])-1]='\0';
  373.  
  374. if (debug)
  375. printf("DEBUG: Calling getGrade() for %s\n",&uname[0]);
  376.  
  377. if (!getGrade(connect,&uname[0])) { // Check if they exist
  378. printf("Error: The username you entered is not in the system!\n\n");
  379. return;
  380. }
  381.  
  382. // If they exist, get the new grade
  383. printf("New Grade (A=A,B=B,C=C,N=NR): ");
  384. choice=toupper(getchar());
  385.  
  386. // Make sure its valid
  387. if (((choice<'A') || (choice>'C')) && (choice != 'N')) {
  388. printf("Error: Please enter A,B,C or N for grade\n\n");
  389. return;
  390. }
  391.  
  392. // Build our query
  393. if (choice=='N')
  394. snprintf(&buff[0],1023,"UPDATE grades SET grade='NR' WHERE username='%s'",&uname[0]);
  395. else
  396. snprintf(&buff[0],1023,"UPDATE grades SET grade='%c' WHERE username='%s'",choice,&uname[0]);
  397.  
  398. if (debug)
  399. printf("DEBUG: Query is %s\n",&buff[0]);
  400.  
  401. // And run it
  402. if (mysql_query(connect,&buff[0]) != 0) {
  403. // If it failed, tell the user
  404. printf("Error: %s!\n", mysql_error(connect));
  405. return;
  406. }
  407.  
  408. return;
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement