PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/3.2/src/manhat-lib/shared_super_util.c

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