passwd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* vi: set sw=4 ts=4: */
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <utime.h>
  10. #include <syslog.h>
  11. #include <time.h>
  12. #include <sys/resource.h>
  13. #include <errno.h>
  14. #include "busybox.h"
  15. static char crypt_passwd[128];
  16. static int create_backup(const char *backup, FILE * fp);
  17. static int new_password(const struct passwd *pw, int amroot, int algo);
  18. static void set_filesize_limit(int blocks);
  19. static int get_algo(char *a)
  20. {
  21. int x = 1; /* standard: MD5 */
  22. if (strcasecmp(a, "des") == 0)
  23. x = 0;
  24. return x;
  25. }
  26. static int update_passwd(const struct passwd *pw, char *crypt_pw)
  27. {
  28. char filename[1024];
  29. char buf[1025];
  30. char buffer[80];
  31. char username[32];
  32. char *pw_rest;
  33. int mask;
  34. int continued;
  35. FILE *fp;
  36. FILE *out_fp;
  37. struct stat sb;
  38. struct flock lock;
  39. #ifdef CONFIG_FEATURE_SHADOWPASSWDS
  40. if (access(bb_path_shadow_file, F_OK) == 0) {
  41. snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
  42. } else
  43. #endif
  44. {
  45. snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
  46. }
  47. if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
  48. /* return 0; */
  49. return 1;
  50. }
  51. /* Lock the password file before updating */
  52. lock.l_type = F_WRLCK;
  53. lock.l_whence = SEEK_SET;
  54. lock.l_start = 0;
  55. lock.l_len = 0;
  56. if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
  57. fprintf(stderr, "%s: %s\n", filename, strerror(errno));
  58. return 1;
  59. }
  60. lock.l_type = F_UNLCK;
  61. snprintf(buf, sizeof buf, "%s-", filename);
  62. if (create_backup(buf, fp)) {
  63. fcntl(fileno(fp), F_SETLK, &lock);
  64. fclose(fp);
  65. return 1;
  66. }
  67. snprintf(buf, sizeof buf, "%s+", filename);
  68. mask = umask(0777);
  69. out_fp = fopen(buf, "w");
  70. umask(mask);
  71. if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
  72. || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
  73. fcntl(fileno(fp), F_SETLK, &lock);
  74. fclose(fp);
  75. fclose(out_fp);
  76. return 1;
  77. }
  78. continued = 0;
  79. snprintf(username, sizeof username, "%s:", pw->pw_name);
  80. rewind(fp);
  81. while (!feof(fp)) {
  82. fgets(buffer, sizeof buffer, fp);
  83. if (!continued) { // Check to see if we're updating this line.
  84. if (strncmp(username, buffer, strlen(username)) == 0) { // we have a match.
  85. pw_rest = strchr(buffer, ':');
  86. *pw_rest++ = '\0';
  87. pw_rest = strchr(pw_rest, ':');
  88. fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
  89. } else {
  90. fputs(buffer, out_fp);
  91. }
  92. } else {
  93. fputs(buffer, out_fp);
  94. }
  95. if (buffer[strlen(buffer) - 1] == '\n') {
  96. continued = 0;
  97. } else {
  98. continued = 1;
  99. }
  100. bzero(buffer, sizeof buffer);
  101. }
  102. if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
  103. unlink(buf);
  104. fcntl(fileno(fp), F_SETLK, &lock);
  105. fclose(fp);
  106. return 1;
  107. }
  108. if (rename(buf, filename) < 0) {
  109. fcntl(fileno(fp), F_SETLK, &lock);
  110. fclose(fp);
  111. return 1;
  112. } else {
  113. fcntl(fileno(fp), F_SETLK, &lock);
  114. fclose(fp);
  115. return 0;
  116. }
  117. }
  118. extern int passwd_main(int argc, char **argv)
  119. {
  120. int amroot;
  121. char *cp;
  122. char *np;
  123. char *name;
  124. char *myname;
  125. int flag;
  126. int algo = 1; /* -a - password algorithm */
  127. int lflg = 0; /* -l - lock account */
  128. int uflg = 0; /* -u - unlock account */
  129. int dflg = 0; /* -d - delete password */
  130. const struct passwd *pw;
  131. #ifdef CONFIG_FEATURE_SHADOWPASSWDS
  132. const struct spwd *sp;
  133. #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
  134. amroot = (getuid() == 0);
  135. openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
  136. while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
  137. switch (flag) {
  138. case 'a':
  139. algo = get_algo(optarg);
  140. break;
  141. case 'd':
  142. dflg++;
  143. break;
  144. case 'l':
  145. lflg++;
  146. break;
  147. case 'u':
  148. uflg++;
  149. break;
  150. default:
  151. bb_show_usage();
  152. }
  153. }
  154. myname = (char *) bb_xstrdup(my_getpwuid(NULL, getuid(), -1));
  155. /* exits on error */
  156. if (optind < argc) {
  157. name = argv[optind];
  158. } else {
  159. name = myname;
  160. }
  161. if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
  162. bb_show_usage();
  163. }
  164. pw = getpwnam(name);
  165. if (!pw) {
  166. bb_error_msg_and_die("Unknown user %s\n", name);
  167. }
  168. if (!amroot && pw->pw_uid != getuid()) {
  169. syslog(LOG_WARNING, "can't change pwd for `%s'", name);
  170. bb_error_msg_and_die("Permission denied.\n");
  171. }
  172. #ifdef CONFIG_FEATURE_SHADOWPASSWDS
  173. sp = getspnam(name);
  174. if (!sp) {
  175. sp = (struct spwd *) pwd_to_spwd(pw);
  176. }
  177. cp = sp->sp_pwdp;
  178. np = sp->sp_namp;
  179. #else
  180. cp = pw->pw_passwd;
  181. np = name;
  182. #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
  183. safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
  184. if (!(dflg || lflg || uflg)) {
  185. if (!amroot) {
  186. if (cp[0] == '!') {
  187. syslog(LOG_WARNING, "password locked for `%s'", np);
  188. bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
  189. }
  190. }
  191. printf("Changing password for %s\n", name);
  192. if (new_password(pw, amroot, algo)) {
  193. bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
  194. }
  195. } else if (lflg) {
  196. if (crypt_passwd[0] != '!') {
  197. memmove(&crypt_passwd[1], crypt_passwd,
  198. sizeof crypt_passwd - 1);
  199. crypt_passwd[sizeof crypt_passwd - 1] = '\0';
  200. crypt_passwd[0] = '!';
  201. }
  202. } else if (uflg) {
  203. if (crypt_passwd[0] == '!') {
  204. memmove(crypt_passwd, &crypt_passwd[1],
  205. sizeof crypt_passwd - 1);
  206. }
  207. } else if (dflg) {
  208. crypt_passwd[0] = '\0';
  209. }
  210. set_filesize_limit(30000);
  211. signal(SIGHUP, SIG_IGN);
  212. signal(SIGINT, SIG_IGN);
  213. signal(SIGQUIT, SIG_IGN);
  214. umask(077);
  215. if (setuid(0)) {
  216. syslog(LOG_ERR, "can't setuid(0)");
  217. bb_error_msg_and_die( "Cannot change ID to root.\n");
  218. }
  219. if (!update_passwd(pw, crypt_passwd)) {
  220. syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
  221. myname);
  222. printf("Password changed.\n");
  223. } else {
  224. syslog(LOG_WARNING, "an error occurred updating the password file");
  225. bb_error_msg_and_die("An error occurred updating the password file.\n");
  226. }
  227. return (0);
  228. }
  229. static int create_backup(const char *backup, FILE * fp)
  230. {
  231. struct stat sb;
  232. struct utimbuf ub;
  233. FILE *bkfp;
  234. int c, mask;
  235. if (fstat(fileno(fp), &sb))
  236. /* return -1; */
  237. return 1;
  238. mask = umask(077);
  239. bkfp = fopen(backup, "w");
  240. umask(mask);
  241. if (!bkfp)
  242. /* return -1; */
  243. return 1;
  244. /* TODO: faster copy, not one-char-at-a-time. --marekm */
  245. rewind(fp);
  246. while ((c = getc(fp)) != EOF) {
  247. if (putc(c, bkfp) == EOF)
  248. break;
  249. }
  250. if (c != EOF || fflush(bkfp)) {
  251. fclose(bkfp);
  252. /* return -1; */
  253. return 1;
  254. }
  255. if (fclose(bkfp))
  256. /* return -1; */
  257. return 1;
  258. ub.actime = sb.st_atime;
  259. ub.modtime = sb.st_mtime;
  260. utime(backup, &ub);
  261. return 0;
  262. }
  263. static int i64c(int i)
  264. {
  265. if (i <= 0)
  266. return ('.');
  267. if (i == 1)
  268. return ('/');
  269. if (i >= 2 && i < 12)
  270. return ('0' - 2 + i);
  271. if (i >= 12 && i < 38)
  272. return ('A' - 12 + i);
  273. if (i >= 38 && i < 63)
  274. return ('a' - 38 + i);
  275. return ('z');
  276. }
  277. static char *crypt_make_salt(void)
  278. {
  279. time_t now;
  280. static unsigned long x;
  281. static char result[3];
  282. time(&now);
  283. x += now + getpid() + clock();
  284. result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
  285. result[1] = i64c(((x >> 12) ^ x) & 077);
  286. result[2] = '\0';
  287. return result;
  288. }
  289. static int new_password(const struct passwd *pw, int amroot, int algo)
  290. {
  291. char *clear;
  292. char *cipher;
  293. char *cp;
  294. char orig[200];
  295. char pass[200];
  296. time_t start, now;
  297. if (!amroot && crypt_passwd[0]) {
  298. if (!(clear = bb_askpass(0, "Old password:"))) {
  299. /* return -1; */
  300. return 1;
  301. }
  302. cipher = pw_encrypt(clear, crypt_passwd);
  303. if (strcmp(cipher, crypt_passwd) != 0) {
  304. syslog(LOG_WARNING, "incorrect password for `%s'",
  305. pw->pw_name);
  306. time(&start);
  307. now = start;
  308. while (difftime(now, start) < FAIL_DELAY) {
  309. sleep(FAIL_DELAY);
  310. time(&now);
  311. }
  312. fprintf(stderr, "Incorrect password.\n");
  313. /* return -1; */
  314. return 1;
  315. }
  316. safe_strncpy(orig, clear, sizeof(orig));
  317. bzero(clear, strlen(clear));
  318. bzero(cipher, strlen(cipher));
  319. } else {
  320. orig[0] = '\0';
  321. }
  322. if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
  323. "Please use a combination of upper and lower case letters and numbers.\n"
  324. "Enter new password: ")))
  325. {
  326. bzero(orig, sizeof orig);
  327. /* return -1; */
  328. return 1;
  329. }
  330. safe_strncpy(pass, cp, sizeof(pass));
  331. bzero(cp, strlen(cp));
  332. /* if (!obscure(orig, pass, pw)) { */
  333. if (obscure(orig, pass, pw)) {
  334. if (amroot) {
  335. printf("\nWarning: weak password (continuing).\n");
  336. } else {
  337. /* return -1; */
  338. return 1;
  339. }
  340. }
  341. if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
  342. bzero(orig, sizeof orig);
  343. /* return -1; */
  344. return 1;
  345. }
  346. if (strcmp(cp, pass)) {
  347. fprintf(stderr, "Passwords do not match.\n");
  348. /* return -1; */
  349. return 1;
  350. }
  351. bzero(cp, strlen(cp));
  352. bzero(orig, sizeof(orig));
  353. if (algo == 1) {
  354. cp = pw_encrypt(pass, "$1$");
  355. } else
  356. cp = pw_encrypt(pass, crypt_make_salt());
  357. bzero(pass, sizeof pass);
  358. safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
  359. return 0;
  360. }
  361. static void set_filesize_limit(int blocks)
  362. {
  363. struct rlimit rlimit_fsize;
  364. rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
  365. setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
  366. }