PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/manhat-lib/shared_super_util.c

#
C | 757 lines | 523 code | 202 blank | 32 comment | 57 complexity | 0bdb789579b595caf6d215b5115b74bf MD5 | raw file
Possible License(s): 0BSD, Apache-2.0, GPL-2.0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <sys/types.h>
  5. #include <dirent.h>
  6. #include <ctype.h> /* for isdigit(), etc. */
  7. #include <unistd.h>
  8. #include "../global.h"
  9. #include "shared_util.h"
  10. #include "shared_lock.h"
  11. #include "shared_authenticate.h"
  12. #include "shared_file_util.h"
  13. #include "shared_super_util.h" /* for type definitions */
  14. #include "shared_strtrm.h"
  15. #include "shared_server_string.h"
  16. #include "shared_cs_util.h"
  17. #include "shared_system_data.h"
  18. void list_server_commands(ADMIN_CONFIG_STRUCT *conf);
  19. void list_course_commands(ADMIN_CONFIG_STRUCT *conf);
  20. void list_people_commands(ADMIN_CONFIG_STRUCT *conf);
  21. void
  22. read_param (char *key)
  23. {
  24. cs_get_required_parameter ("id", key, MAX_KEY);
  25. }
  26. static
  27. int admin_config_value(char *name)
  28. {
  29. char tmp[20];
  30. cs_get_optional_parameter(name, tmp, 19);
  31. if(strlen(tmp))
  32. return (atoi(tmp));
  33. else
  34. return 0;
  35. }
  36. void
  37. get_cgi_admin_config_values(ADMIN_CONFIG_STRUCT *conf)
  38. {
  39. cs_get_optional_parameter("page_title", conf->page_title, MAX_ADMIN_PAGE_TITLE);
  40. strtrm(conf->page_title);
  41. conf->is_super_user = 0;
  42. conf->create_new_course = admin_config_value("create_new_course");
  43. conf->delete_courses = admin_config_value("delete_courses");
  44. conf->config_courses = admin_config_value("config_courses");
  45. conf->change_course_access = admin_config_value("change_courses");
  46. conf->archive_courses = admin_config_value("archive_courses");
  47. conf->course_disk_usage = admin_config_value("course_disk_usage");
  48. conf->purge_postoffice_message = admin_config_value("purge_postoffice_message");
  49. conf->admin_surveys = admin_config_value("admin_survey");
  50. conf->add_admin = admin_config_value("add_admin");
  51. conf->del_admin = admin_config_value("del_admin");
  52. conf->grant_admin = admin_config_value("grant_admin");
  53. conf->search_profiles = admin_config_value("search_profile");
  54. conf->whos_on = admin_config_value("whos_on");
  55. conf->statistics = admin_config_value("statistics");
  56. conf->manage_preferences = admin_config_value("manage_preferences");
  57. conf->reindex = admin_config_value("reindex");
  58. conf->disk_space = admin_config_value("disk_space");
  59. conf->chat_server = admin_config_value("chat_server");
  60. conf->lock_server = admin_config_value("lock_server");
  61. conf->clean_up = admin_config_value("clean_up");
  62. conf->system_conf = admin_config_value("system_conf");
  63. conf->manage_testpilot = admin_config_value("manage_testpilot");
  64. }
  65. void
  66. check_str(char *str, const char * msg)
  67. {
  68. char *ptr;
  69. for(ptr = str; (*ptr); ptr++)
  70. {
  71. if(!(*ptr == '_') && !isdigit(*ptr) && !isalpha(*ptr))
  72. cs_critical_error(msg, "");
  73. }
  74. }
  75. void
  76. write_to_index(const char *username, const char *realname)
  77. {
  78. FILE *fp;
  79. char fname[MAX_PATH +1];
  80. snprintf(fname, MAX_PATH +1, "../%s/%s/%s", USERS_DIR, ADMIN_DIR, USER_INDEX_FNAME);
  81. fp = fopen(fname, "a+");
  82. if(!fp)
  83. cs_critical_error( ERR_FOPEN_WRITE_FAILED, "");
  84. get_exclusive_lock(fileno(fp), 1);
  85. fprintf(fp, "%s:%s\n", username, realname);
  86. release_lock(fileno(fp));
  87. fclose(fp);
  88. }
  89. void
  90. get_admin_user_passwd(const char *username, char *passwd)
  91. {
  92. FILE *fp;
  93. char fname[MAX_PATH +1];
  94. snprintf(fname, MAX_PATH +1, "../%s/%s/%c/%s/%s",
  95. USERS_DIR, ADMIN_DIR, sub_dir_name(username), username, PASSWD_FNAME);
  96. fp = fopen(fname, "r");
  97. if(!fp)
  98. cs_critical_error(ERR_FOPEN_READ_FAILED, "");
  99. if(!fgets(passwd, MAX_ENCRYPTED_PASSWORD + 1, fp) )
  100. {
  101. fclose(fp);
  102. cs_critical_error(ERR_FREAD_FAILED, "");
  103. }
  104. strtrm(passwd); /* shared_strtrm.c */
  105. fclose(fp);
  106. }
  107. void set_one_oper_value(const char *prefix, int num, char *value)
  108. {
  109. #define MAX_TMP_NAME 40
  110. char name[MAX_NAME];
  111. snprintf(name, MAX_NAME, "%s.%d", prefix, num);
  112. cs_set_value(name, value);
  113. #undef MAX_TMP_NAME
  114. }
  115. void
  116. super_conf_info_table(ADMIN_CONFIG_STRUCT *conf, char *username, char *realname)
  117. {
  118. char server_string[MAX_PATH +1];
  119. char url[MAX_PATH +1];
  120. int i = 0;
  121. cs_set_current_time();
  122. cs_set_server_name();
  123. cs_set_value("username", username);
  124. cs_set_value("realname", realname);
  125. if(conf->create_new_course)
  126. {
  127. set_one_oper_value("conf_course", i, "create_course");
  128. i++;
  129. }
  130. if(conf->delete_courses)
  131. {
  132. set_one_oper_value("conf_course", i, "delete_course");
  133. i++;
  134. }
  135. if(conf->config_courses)
  136. {
  137. set_one_oper_value("conf_course", i, "configure_course");
  138. i++;
  139. }
  140. if(conf->change_course_access)
  141. {
  142. set_one_oper_value("conf_course", i, "change_course_access");
  143. i++;
  144. }
  145. if(conf->archive_courses)
  146. {
  147. set_one_oper_value("conf_course", i, "archive_courses");
  148. i++;
  149. }
  150. if(conf->admin_surveys)
  151. set_one_oper_value("conf_course", i, "surveys");
  152. i =0;
  153. if(conf->search_profiles)
  154. {
  155. set_one_oper_value("conf_people", i, "profile");
  156. i++;
  157. }
  158. if(conf->whos_on)
  159. {
  160. set_one_oper_value("conf_people", i, "who_is_on_manhattan");
  161. i++;
  162. }
  163. if(conf->statistics)
  164. {
  165. set_one_oper_value("conf_people", i, "login_logs");
  166. i++;
  167. }
  168. if(conf->manage_preferences)
  169. {
  170. set_one_oper_value("conf_people", i, "manage_preferences");
  171. i++;
  172. }
  173. if(conf->reindex)
  174. {
  175. set_one_oper_value("conf_people", i, "reindex");
  176. i++;
  177. }
  178. i =0;
  179. if(conf->disk_space)
  180. {
  181. set_one_oper_value("conf_usageReport", i, "disk_space");
  182. i++;
  183. }
  184. if(conf->course_disk_usage)
  185. {
  186. set_one_oper_value("conf_usageReport", i, "course_disk_usage");
  187. i++;
  188. }
  189. i = 0;
  190. if(conf->chat_server)
  191. {
  192. set_one_oper_value("conf_server", i, "manage_chat");
  193. i++;
  194. }
  195. if(conf->lock_server)
  196. {
  197. set_one_oper_value("conf_server", i, "lock_server");
  198. i++;
  199. }
  200. if(conf->system_conf)
  201. {
  202. set_one_oper_value("conf_server", i, "system_conf");
  203. i++;
  204. }
  205. if(conf->manage_testpilot)
  206. {
  207. set_one_oper_value("conf_server", i, "manage_testpilot");
  208. i++;
  209. }
  210. i = 0;
  211. if(conf->clean_up)
  212. {
  213. set_one_oper_value("conf_cleanup", i, "clean_up");
  214. i++;
  215. }
  216. if(conf->purge_postoffice_message)
  217. {
  218. set_one_oper_value("conf_cleanup", i, "purge_postoffice_message");
  219. i++;
  220. }
  221. get_server_string(server_string, MAX_PATH +1);
  222. snprintf(url, MAX_PATH +1, "%s/%s/%s",server_string, system_data_str("SBIN_ALIAS"), "super_doorstep");
  223. cs_set_value("url", url);
  224. }
  225. static DEL_COURSE_NODE *
  226. get_node(char *str)
  227. {
  228. DEL_COURSE_NODE *ptr;
  229. char *str_ptr;
  230. str_ptr = strchr(str, ':');
  231. if(!str_ptr)
  232. cs_critical_error(ERR_PARAM_FORMAT, "course_list");
  233. ptr = (DEL_COURSE_NODE *)malloc(sizeof(DEL_COURSE_NODE));
  234. if(!ptr)
  235. cs_critical_error(ERR_MALLOC_FAILED, "");
  236. ptr->next = 0;
  237. *str_ptr = '\0';
  238. ptr->del_id = malloc_str(str);
  239. *str_ptr = ':';
  240. str_ptr++;
  241. ptr->del_info = malloc_str(str_ptr);
  242. return ptr;
  243. }
  244. DEL_COURSE_NODE *
  245. build_selected_course_list()
  246. {
  247. DEL_COURSE_NODE *ptr, *head =0, *current=0;
  248. char *str;
  249. HDF *obj;
  250. str = hdf_get_value(global_cgi->hdf, "Query.course_list.0", 0);
  251. if(str)
  252. {
  253. obj = hdf_get_child(global_cgi->hdf, "Query.course_list");
  254. while(obj)
  255. {
  256. str = obj->value;
  257. if(str)
  258. {
  259. ptr = get_node(str);
  260. if(!head)
  261. head = ptr;
  262. else
  263. current->next = ptr;
  264. current = ptr;
  265. }
  266. obj = obj->next;
  267. }
  268. }
  269. else
  270. {
  271. str = hdf_get_value(global_cgi->hdf, "Query.course_list", 0);
  272. if(!str)
  273. cs_critical_error(ERR_PARAM_MISSING, "course_list");
  274. head = get_node(str);
  275. }
  276. return head;
  277. }
  278. void free_del_list(DEL_COURSE_NODE *head)
  279. {
  280. DEL_COURSE_NODE *ptr;
  281. while(head)
  282. {
  283. ptr = head->next;
  284. if(head->del_id)
  285. free(head->del_id);
  286. if(head->del_info)
  287. free(head->del_info);
  288. free(head);
  289. head = ptr;
  290. }
  291. }
  292. /*** does conf contain actions that
  293. **** should be placed under 'Courses'
  294. **** on the sys admin's menu?
  295. ***/
  296. int
  297. has_course_progs(ADMIN_CONFIG_STRUCT *conf)
  298. {
  299. return (conf->create_new_course ||
  300. conf->delete_courses ||
  301. conf->config_courses ||
  302. conf->admin_surveys);
  303. }
  304. /*** does conf contain actions that
  305. **** should be placed under 'People'
  306. **** on the sys admin's menu?
  307. ***/
  308. int
  309. has_people_progs(ADMIN_CONFIG_STRUCT *conf)
  310. {
  311. return ( conf->search_profiles ||
  312. conf-> whos_on || conf->manage_preferences || conf->reindex ||
  313. conf->statistics);
  314. }
  315. /*** does conf contain actions that
  316. **** should be placed under 'Server'
  317. **** on the sys admin's menu?
  318. ***/
  319. int
  320. has_server_progs(ADMIN_CONFIG_STRUCT *conf)
  321. {
  322. return (conf->disk_space ||
  323. conf->chat_server ||
  324. conf->lock_server ||
  325. conf->course_disk_usage ||
  326. conf->purge_postoffice_message ||
  327. conf->clean_up || conf->system_conf);
  328. }
  329. int
  330. read_super_configuration_file (const char *username, ADMIN_CONFIG_STRUCT * config)
  331. {
  332. FILE *fp;
  333. int fd;
  334. char fullpath[MAX_PATH + 1];
  335. snprintf (fullpath, MAX_PATH + 1, "../%s/%s/%c/%s/%s",
  336. USERS_DIR, ADMIN_DIR, sub_dir_name(username),username, CONFIG_FNAME);
  337. fp = fopen (fullpath, "r");
  338. if (!fp)
  339. return 0;
  340. fd = fileno(fp);
  341. get_shared_lock(fd, 1); /* shared_lock.c */
  342. if (fread (config, sizeof (ADMIN_CONFIG_STRUCT), 1, fp) != 1)
  343. {
  344. release_lock(fd);
  345. fclose(fp);
  346. return 0;
  347. }
  348. release_lock(fd); /* shared_lock.c */
  349. fclose (fp);
  350. return 1; /* success */
  351. }
  352. int
  353. write_super_configuration_file (const char *username, ADMIN_CONFIG_STRUCT * config)
  354. {
  355. FILE *fp;
  356. char fullpath[MAX_PATH + 1];
  357. snprintf (fullpath, MAX_PATH + 1, "../%s/%s/%c/%s/%s",
  358. USERS_DIR, ADMIN_DIR, sub_dir_name(username),username, CONFIG_FNAME);
  359. fp = fopen (fullpath, "w");
  360. if (!fp)
  361. return 0;
  362. if (fwrite(config, sizeof (ADMIN_CONFIG_STRUCT), 1, fp) != 1)
  363. {
  364. fclose(fp);
  365. return 0;
  366. }
  367. fclose (fp);
  368. return 1; /* success */
  369. }
  370. /** is username the administrative
  371. *** super user?
  372. ***/
  373. static
  374. int is_super_user(const char *username)
  375. {
  376. ADMIN_CONFIG_STRUCT conf;
  377. if(!read_super_configuration_file(username, &conf) || !conf.is_super_user)
  378. return 0;
  379. else
  380. return 1;
  381. }
  382. /* does list of users have at least one admin
  383. * that is NOT the super user ?
  384. */
  385. int has_other_admins(ADMIN_USER *head)
  386. {
  387. ADMIN_USER *ptr;
  388. for(ptr = head; ptr; ptr = ptr->next)
  389. if(!ptr->is_super_user)
  390. return 1;
  391. return 0;
  392. }
  393. /*** builds a linked list
  394. **** of administrative users from the ../users/admin/index.txt file
  395. ****
  396. **** ADMIN_USER is #defined in shared_super_util.h
  397. ***/
  398. ADMIN_USER *
  399. build_admin_user_list()
  400. {
  401. #define MAX_LINE 200
  402. FILE *fp;
  403. char fname[MAX_PATH +1];
  404. char line[MAX_LINE +1];
  405. char *chr_ptr;
  406. ADMIN_USER *head = 0, *ptr, *current=0;
  407. snprintf(fname, MAX_PATH +1, "../%s/%s/%s",
  408. USERS_DIR, ADMIN_DIR, USER_INDEX_FNAME);
  409. fp = fopen(fname, "r");
  410. if(!fp)
  411. cs_critical_error(ERR_FOPEN_READ_FAILED, "");
  412. get_shared_lock(fileno(fp), 1);
  413. while(fgets(line, MAX_LINE, fp))
  414. {
  415. strtrm(line); /* shared_strtrm.c */
  416. ptr = (ADMIN_USER *)malloc(sizeof(ADMIN_USER));
  417. chr_ptr = strchr(line, ':');
  418. if(!chr_ptr)
  419. cs_critical_error( ERR_FILE_FORMAT_ERROR, "");
  420. *chr_ptr = '\0';
  421. strncpy(ptr->username, line, MAX_USERNAME + 1); /* copy username field */
  422. chr_ptr++;
  423. strncpy(ptr->realname, chr_ptr, MAX_NAME +1); /* copy realname field */
  424. ptr->is_super_user = is_super_user(ptr->username);
  425. ptr->next = 0;
  426. if(!head)
  427. head = ptr;
  428. else
  429. current->next = ptr;
  430. current = ptr;
  431. }
  432. release_lock(fileno(fp));
  433. fclose(fp);
  434. return head;
  435. #undef MAX_LINE
  436. }
  437. void
  438. free_admin_user_list(ADMIN_USER *list)
  439. {
  440. ADMIN_USER *ptr;
  441. while(list)
  442. {
  443. ptr = list->next;
  444. free(list);
  445. list = ptr;
  446. }
  447. }
  448. static void
  449. set_checkbox(const char *prefix, int num, int selected)
  450. {
  451. #define MAX_TMP_NAME 30
  452. char name[MAX_TMP_NAME];
  453. snprintf(name, MAX_TMP_NAME, "%s.%d.selected", prefix, num);
  454. if(selected)
  455. cs_set_int_value(name, 1);
  456. else
  457. cs_set_int_value(name, 0);
  458. #undef MAX_TMP_NAME
  459. }
  460. void
  461. list_course_commands(ADMIN_CONFIG_STRUCT *conf)
  462. {
  463. int i =0;
  464. set_checkbox("conf_course", i, conf->create_new_course);
  465. i++;
  466. set_checkbox("conf_course", i, conf->delete_courses);
  467. i++;
  468. set_checkbox("conf_course",i, conf->config_courses);
  469. i++;
  470. set_checkbox("conf_course",i, conf->change_course_access);
  471. i++;
  472. set_checkbox("conf_course",i, conf->archive_courses);
  473. i++;
  474. set_checkbox("conf_course", i, conf->admin_surveys);
  475. i++;
  476. }
  477. void
  478. list_people_commands(ADMIN_CONFIG_STRUCT *conf)
  479. {
  480. /** note the ability to add/delete/modify administrators is NEVER
  481. *** given to anyone but the super user
  482. **/
  483. int i =0;
  484. set_checkbox("conf_people", i,conf->search_profiles);
  485. i++;
  486. set_checkbox("conf_people", i, conf->whos_on);
  487. i++;
  488. set_checkbox("conf_people", i, conf->statistics);
  489. i++;
  490. set_checkbox("conf_people", i, conf->manage_preferences);
  491. i++;
  492. set_checkbox("conf_people", i, conf->reindex);
  493. }
  494. void
  495. list_server_commands(ADMIN_CONFIG_STRUCT *conf)
  496. {
  497. int i =0;
  498. set_checkbox("conf_server", i, conf->disk_space);
  499. i++;
  500. set_checkbox("conf_server",i, conf->course_disk_usage);
  501. i++;
  502. set_checkbox("conf_server", i, conf->chat_server);
  503. i++;
  504. set_checkbox("conf_server", i, conf->lock_server);
  505. i++;
  506. set_checkbox("conf_server", i, conf->clean_up);
  507. i++;
  508. set_checkbox("conf_server",i, conf->purge_postoffice_message);
  509. i++;
  510. set_checkbox("conf_server",i, conf->system_conf);
  511. i++;
  512. set_checkbox("conf_server",i, conf->manage_testpilot);
  513. }
  514. void
  515. set_admin_data( ADMIN_USER *list)
  516. {
  517. #define MAX_TMP_NAME 30
  518. ADMIN_USER *ptr;
  519. char name[MAX_TMP_NAME];
  520. int i =0;
  521. for(ptr = list; ptr; ptr = ptr->next)
  522. {
  523. if(!ptr->is_super_user) /* never show the super user */
  524. {
  525. snprintf(name, MAX_TMP_NAME, "user.%d.username",i);
  526. cs_set_value(name, ptr->username);
  527. snprintf(name, MAX_TMP_NAME, "user.%d.realname",i);
  528. cs_set_value(name, ptr->realname);
  529. i++;
  530. }
  531. }
  532. #undef MAX_TMP_NAME
  533. }
  534. /* when an admin "purges" the student to student postoffice messages
  535. ** within a classroom, the current date and time is written to a simple
  536. ** text file within the classroom.
  537. **
  538. ** this fcn copies the last date written to that file to last_purge_date
  539. */
  540. void
  541. get_last_postoffice_purge_date(const char *course_path, char *last_purge_date)
  542. {
  543. FILE *fp;
  544. char purge_log_path[MAX_PATH + 1];
  545. snprintf(purge_log_path, MAX_PATH + 1, "%s%s", course_path, POST_PURGE_LOG_FNAME); /* #defined in global.h */
  546. fp = fopen (purge_log_path, "r");
  547. if(!fp)
  548. *last_purge_date = '\0';
  549. else
  550. {
  551. while(fgets(last_purge_date, MAX_TIMESTRING + 1, fp)); /* want the last line of the file */
  552. strtrm(last_purge_date);
  553. fclose(fp);
  554. }
  555. }