cfg.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
  3. Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. */
  9. #include <fstream>
  10. #include <string>
  11. #include <iostream>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <dirent.h>
  17. #include "cfg.h"
  18. using namespace std;
  19. typedef pair<string,string> option;
  20. Cfg::Cfg()
  21. : currentSession(-1)
  22. {
  23. // Configuration options
  24. options.insert(option("default_path","./:/bin:/usr/bin:/usr/local/bin"));
  25. options.insert(option("default_xserver","/usr/bin/X"));
  26. options.insert(option("xserver_arguments",""));
  27. options.insert(option("numlock",""));
  28. options.insert(option("daemon",""));
  29. options.insert(option("xauth_path","/usr/bin/xauth"));
  30. options.insert(option("login_cmd","exec /bin/bash -login ~/.xinitrc %session"));
  31. options.insert(option("halt_cmd","/sbin/shutdown -h now"));
  32. options.insert(option("reboot_cmd","/sbin/shutdown -r now"));
  33. options.insert(option("suspend_cmd",""));
  34. options.insert(option("sessionstart_cmd",""));
  35. options.insert(option("sessionstop_cmd",""));
  36. options.insert(option("console_cmd","/usr/bin/xterm -C -fg white -bg black +sb -g %dx%d+%d+%d -fn %dx%d -T ""Console login"" -e /bin/sh -c ""/bin/cat /etc/issue; exec /bin/login"""));
  37. options.insert(option("screenshot_cmd","import -window root /slim.png"));
  38. options.insert(option("welcome_msg","Welcome to %host"));
  39. options.insert(option("default_user",""));
  40. options.insert(option("focus_password","no"));
  41. options.insert(option("auto_login","no"));
  42. options.insert(option("current_theme","default"));
  43. options.insert(option("lockfile","/var/run/slim.lock"));
  44. options.insert(option("logfile","/var/log/slim.log"));
  45. options.insert(option("authfile","/var/run/slim.auth"));
  46. options.insert(option("shutdown_msg","The system is halting..."));
  47. options.insert(option("reboot_msg","The system is rebooting..."));
  48. options.insert(option("sessions","wmaker,blackbox,icewm"));
  49. options.insert(option("sessiondir",""));
  50. options.insert(option("hidecursor","false"));
  51. // Theme stuff
  52. options.insert(option("input_panel_x","50%"));
  53. options.insert(option("input_panel_y","40%"));
  54. options.insert(option("input_name_x","200"));
  55. options.insert(option("input_name_y","154"));
  56. options.insert(option("input_pass_x","-1")); // default is single inputbox
  57. options.insert(option("input_pass_y","-1"));
  58. options.insert(option("input_font","Verdana:size=11"));
  59. options.insert(option("input_color", "#000000"));
  60. options.insert(option("input_cursor_height","20"));
  61. options.insert(option("input_maxlength_name","20"));
  62. options.insert(option("input_maxlength_passwd","20"));
  63. options.insert(option("input_shadow_xoffset", "0"));
  64. options.insert(option("input_shadow_yoffset", "0"));
  65. options.insert(option("input_shadow_color","#FFFFFF"));
  66. options.insert(option("welcome_font","Verdana:size=14"));
  67. options.insert(option("welcome_color","#FFFFFF"));
  68. options.insert(option("welcome_x","-1"));
  69. options.insert(option("welcome_y","-1"));
  70. options.insert(option("welcome_shadow_xoffset", "0"));
  71. options.insert(option("welcome_shadow_yoffset", "0"));
  72. options.insert(option("welcome_shadow_color","#FFFFFF"));
  73. options.insert(option("intro_msg",""));
  74. options.insert(option("intro_font","Verdana:size=14"));
  75. options.insert(option("intro_color","#FFFFFF"));
  76. options.insert(option("intro_x","-1"));
  77. options.insert(option("intro_y","-1"));
  78. options.insert(option("background_style","stretch"));
  79. options.insert(option("background_color","#CCCCCC"));
  80. options.insert(option("username_font","Verdana:size=12"));
  81. options.insert(option("username_color","#FFFFFF"));
  82. options.insert(option("username_x","-1"));
  83. options.insert(option("username_y","-1"));
  84. options.insert(option("username_msg","Please enter your username"));
  85. options.insert(option("username_shadow_xoffset", "0"));
  86. options.insert(option("username_shadow_yoffset", "0"));
  87. options.insert(option("username_shadow_color","#FFFFFF"));
  88. options.insert(option("password_x","-1"));
  89. options.insert(option("password_y","-1"));
  90. options.insert(option("password_msg","Please enter your password"));
  91. options.insert(option("msg_color","#FFFFFF"));
  92. options.insert(option("msg_font","Verdana:size=16:bold"));
  93. options.insert(option("msg_x","40"));
  94. options.insert(option("msg_y","40"));
  95. options.insert(option("msg_shadow_xoffset", "0"));
  96. options.insert(option("msg_shadow_yoffset", "0"));
  97. options.insert(option("msg_shadow_color","#FFFFFF"));
  98. error = "";
  99. }
  100. Cfg::~Cfg() {
  101. options.clear();
  102. }
  103. /*
  104. * Creates the Cfg object and parses
  105. * known options from the given configfile / themefile
  106. */
  107. bool Cfg::readConf(string configfile) {
  108. int n = -1;
  109. string line, fn(configfile);
  110. map<string,string>::iterator it;
  111. string op;
  112. ifstream cfgfile( fn.c_str() );
  113. if (cfgfile) {
  114. while (getline( cfgfile, line )) {
  115. it = options.begin();
  116. while (it != options.end()) {
  117. op = it->first;
  118. n = line.find(op);
  119. if (n == 0)
  120. options[op] = parseOption(line, op);
  121. it++;
  122. }
  123. }
  124. cfgfile.close();
  125. fillSessionList();
  126. return true;
  127. } else {
  128. error = "Cannot read configuration file: " + configfile;
  129. return false;
  130. }
  131. }
  132. /* Returns the option value, trimmed */
  133. string Cfg::parseOption(string line, string option ) {
  134. return Trim( line.substr(option.size(), line.size() - option.size()));
  135. }
  136. const string& Cfg::getError() const {
  137. return error;
  138. }
  139. string& Cfg::getOption(string option) {
  140. return options[option];
  141. }
  142. /* return a trimmed string */
  143. string Cfg::Trim( const string& s ) {
  144. if ( s.empty() ) {
  145. return s;
  146. }
  147. int pos = 0;
  148. string line = s;
  149. int len = line.length();
  150. while ( pos < len && isspace( line[pos] ) ) {
  151. ++pos;
  152. }
  153. line.erase( 0, pos );
  154. pos = line.length()-1;
  155. while ( pos > -1 && isspace( line[pos] ) ) {
  156. --pos;
  157. }
  158. if ( pos != -1 ) {
  159. line.erase( pos+1 );
  160. }
  161. return line;
  162. }
  163. /* Return the welcome message with replaced vars */
  164. string Cfg::getWelcomeMessage(){
  165. string s = getOption("welcome_msg");
  166. int n = -1;
  167. n = s.find("%host");
  168. if (n >= 0) {
  169. string tmp = s.substr(0, n);
  170. char host[40];
  171. gethostname(host,40);
  172. tmp = tmp + host;
  173. tmp = tmp + s.substr(n+5, s.size() - n);
  174. s = tmp;
  175. }
  176. n = s.find("%domain");
  177. if (n >= 0) {
  178. string tmp = s.substr(0, n);;
  179. char domain[40];
  180. getdomainname(domain,40);
  181. tmp = tmp + domain;
  182. tmp = tmp + s.substr(n+7, s.size() - n);
  183. s = tmp;
  184. }
  185. return s;
  186. }
  187. int Cfg::string2int(const char* string, bool* ok) {
  188. char* err = 0;
  189. int l = (int)strtol(string, &err, 10);
  190. if (ok) {
  191. *ok = (*err == 0);
  192. }
  193. return (*err == 0) ? l : 0;
  194. }
  195. // Get absolute position
  196. int Cfg::absolutepos(const string& position, int max, int width) {
  197. int n = -1;
  198. n = position.find("%");
  199. if (n>0) { // X Position expressed in percentage
  200. int result = (max*string2int(position.substr(0, n).c_str())/100) - (width / 2);
  201. return result < 0 ? 0 : result ;
  202. } else { // Absolute X position
  203. return string2int(position.c_str());
  204. }
  205. }
  206. // split a comma separated string into a vector of strings
  207. void Cfg::split(vector<string>& v, const string& str, char c, bool useEmpty) {
  208. v.clear();
  209. string::const_iterator s = str.begin();
  210. string tmp;
  211. while (true) {
  212. string::const_iterator begin = s;
  213. while (*s != c && s != str.end()) { ++s; }
  214. tmp = string(begin, s);
  215. if (useEmpty || tmp.size() > 0)
  216. v.push_back(tmp);
  217. if (s == str.end()) {
  218. break;
  219. }
  220. if (++s == str.end()) {
  221. if (useEmpty)
  222. v.push_back("");
  223. break;
  224. }
  225. }
  226. }
  227. void Cfg::fillSessionList(){
  228. string strSessionList = getOption("sessions");
  229. string strSessionDir = getOption("sessiondir");
  230. sessions.clear();
  231. if( !strSessionDir.empty() ) {
  232. DIR *pDir = opendir(strSessionDir.c_str());
  233. if (pDir != NULL) {
  234. struct dirent *pDirent = NULL;
  235. while ((pDirent = readdir(pDir)) != NULL) {
  236. string strFile(strSessionDir);
  237. strFile += "/";
  238. strFile += pDirent->d_name;
  239. struct stat oFileStat;
  240. if (stat(strFile.c_str( ), &oFileStat) == 0){
  241. if (S_ISREG(oFileStat.st_mode) &&
  242. access(strFile.c_str(), R_OK | X_OK) == 0){
  243. sessions.push_back(string(pDirent->d_name));
  244. }
  245. }
  246. }
  247. closedir(pDir);
  248. }
  249. }
  250. if (sessions.empty()){
  251. split(sessions, strSessionList, ',', false);
  252. }
  253. }
  254. string Cfg::nextSession(string current) {
  255. if (sessions.size() < 1)
  256. return current;
  257. currentSession = (currentSession + 1) % sessions.size();
  258. return sessions[currentSession];
  259. }