cfg.cpp 11 KB

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