Advertisement
Guest User

makedevs.c

a guest
Jun 8th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.73 KB | None | 0 0
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 as
  5. * published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. */
  17.  
  18. #define _GNU_SOURCE
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <time.h>
  25. #include <pwd.h>
  26. #include <grp.h>
  27. #include <unistd.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <libgen.h>
  31. #include <stdarg.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #ifndef __APPLE__
  35. #include <sys/sysmacros.h> /* major() and minor() */
  36. #endif
  37.  
  38. int nbGroups = 0;
  39. int nbUsers = 0;
  40. const int grow = 100;
  41. struct passwd *passwords;
  42. struct group *groupsInFile;
  43. const char *bb_applet_name;
  44.  
  45. void bb_verror_msg(const char *s, va_list p)
  46. {
  47. fflush(stdout);
  48. fprintf(stderr, "%s: ", bb_applet_name);
  49. vfprintf(stderr, s, p);
  50. }
  51.  
  52. void bb_error_msg(const char *s, ...)
  53. {
  54. va_list p;
  55.  
  56. va_start(p, s);
  57. bb_verror_msg(s, p);
  58. va_end(p);
  59. putc('\n', stderr);
  60. }
  61.  
  62. void bb_error_msg_and_die(const char *s, ...)
  63. {
  64. va_list p;
  65.  
  66. va_start(p, s);
  67. bb_verror_msg(s, p);
  68. va_end(p);
  69. putc('\n', stderr);
  70. exit(1);
  71. }
  72.  
  73. void bb_vperror_msg(const char *s, va_list p)
  74. {
  75. int err=errno;
  76. if(s == 0) s = "";
  77. bb_verror_msg(s, p);
  78. if (*s) s = ": ";
  79. fprintf(stderr, "%s%s\n", s, strerror(err));
  80. }
  81.  
  82. void bb_perror_msg(const char *s, ...)
  83. {
  84. va_list p;
  85.  
  86. va_start(p, s);
  87. bb_vperror_msg(s, p);
  88. va_end(p);
  89. }
  90.  
  91. void bb_perror_msg_and_die(const char *s, ...)
  92. {
  93. va_list p;
  94.  
  95. va_start(p, s);
  96. bb_vperror_msg(s, p);
  97. va_end(p);
  98. exit(1);
  99. }
  100.  
  101. FILE *bb_xfopen(const char *path, const char *mode)
  102. {
  103. FILE *fp;
  104. if ((fp = fopen(path, mode)) == NULL)
  105. bb_perror_msg_and_die("%s", path);
  106. return fp;
  107. }
  108.  
  109. enum {
  110. FILEUTILS_PRESERVE_STATUS = 1,
  111. FILEUTILS_DEREFERENCE = 2,
  112. FILEUTILS_RECUR = 4,
  113. FILEUTILS_FORCE = 8,
  114. FILEUTILS_INTERACTIVE = 16
  115. };
  116. int bb_make_directory (char *path, long mode, int flags)
  117. {
  118. mode_t mask;
  119. const char *fail_msg;
  120. char *s = path;
  121. char c;
  122. struct stat st;
  123.  
  124. mask = umask(0);
  125. if (mode == -1) {
  126. umask(mask);
  127. mode = (S_IXUSR | S_IXGRP | S_IXOTH |
  128. S_IWUSR | S_IWGRP | S_IWOTH |
  129. S_IRUSR | S_IRGRP | S_IROTH) & ~mask;
  130. } else {
  131. umask(mask & ~0300);
  132. }
  133.  
  134. do {
  135. c = 0;
  136.  
  137. if (flags & FILEUTILS_RECUR) { /* Get the parent. */
  138. /* Bypass leading non-'/'s and then subsequent '/'s. */
  139. while (*s) {
  140. if (*s == '/') {
  141. do {
  142. ++s;
  143. } while (*s == '/');
  144. c = *s; /* Save the current char */
  145. *s = 0; /* and replace it with nul. */
  146. break;
  147. }
  148. ++s;
  149. }
  150. }
  151.  
  152. if (mkdir(path, 0777) < 0) {
  153. /* If we failed for any other reason than the directory
  154. * already exists, output a diagnostic and return -1.*/
  155. if ((errno != EEXIST && errno != EISDIR)
  156. || !(flags & FILEUTILS_RECUR)
  157. || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
  158. fail_msg = "create";
  159. umask(mask);
  160. break;
  161. }
  162. /* Since the directory exists, don't attempt to change
  163. * permissions if it was the full target. Note that
  164. * this is not an error conditon. */
  165. if (!c) {
  166. umask(mask);
  167. return 0;
  168. }
  169. }
  170.  
  171. if (!c) {
  172. /* Done. If necessary, updated perms on the newly
  173. * created directory. Failure to update here _is_
  174. * an error.*/
  175. umask(mask);
  176. if ((mode != -1) && (chmod(path, mode) < 0)){
  177. fail_msg = "set permissions of";
  178. break;
  179. }
  180. return 0;
  181. }
  182.  
  183. /* Remove any inserted nul from the path (recursive mode). */
  184. *s = c;
  185.  
  186. } while (1);
  187.  
  188. bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
  189. return -1;
  190. }
  191.  
  192. const char * const bb_msg_memory_exhausted = "memory exhausted";
  193.  
  194. void *xmalloc(size_t size)
  195. {
  196. void *ptr = malloc(size);
  197. if (ptr == NULL && size != 0)
  198. bb_error_msg_and_die(bb_msg_memory_exhausted);
  199. return ptr;
  200. }
  201.  
  202. void *xcalloc(size_t nmemb, size_t size)
  203. {
  204. void *ptr = calloc(nmemb, size);
  205. if (ptr == NULL && nmemb != 0 && size != 0)
  206. bb_error_msg_and_die(bb_msg_memory_exhausted);
  207. return ptr;
  208. }
  209.  
  210. void *xrealloc(void *ptr, size_t size)
  211. {
  212. ptr = realloc(ptr, size);
  213. if (ptr == NULL && size != 0)
  214. bb_error_msg_and_die(bb_msg_memory_exhausted);
  215. return ptr;
  216. }
  217.  
  218. char *private_get_line_from_file(FILE *file, int c)
  219. {
  220. #define GROWBY (80) /* how large we will grow strings by */
  221.  
  222. int ch;
  223. int idx = 0;
  224. char *linebuf = NULL;
  225. int linebufsz = 0;
  226.  
  227. while ((ch = getc(file)) != EOF) {
  228. /* grow the line buffer as necessary */
  229. if (idx > linebufsz - 2) {
  230. linebuf = xrealloc(linebuf, linebufsz += GROWBY);
  231. }
  232. linebuf[idx++] = (char)ch;
  233. if (!ch) return linebuf;
  234. if (c<2 && ch == '\n') {
  235. if (c) {
  236. --idx;
  237. }
  238. break;
  239. }
  240. }
  241. if (linebuf) {
  242. if (ferror(file)) {
  243. free(linebuf);
  244. return NULL;
  245. }
  246. linebuf[idx] = 0;
  247. }
  248. return linebuf;
  249. }
  250.  
  251. char *bb_get_chomped_line_from_file(FILE *file)
  252. {
  253. return private_get_line_from_file(file, 1);
  254. }
  255.  
  256. long my_getpwnam(const char *name)
  257. {
  258. struct passwd *myuser;
  259.  
  260. int curIndex = 0;
  261. while((myuser = &passwords[curIndex])->pw_name != NULL) {
  262. if (strcmp(myuser->pw_name, name) == 0)
  263. break;
  264. curIndex++;
  265. }
  266.  
  267. if (myuser->pw_name==NULL)
  268. bb_error_msg_and_die("unknown user name: %s", name);
  269.  
  270. return myuser->pw_uid;
  271. }
  272.  
  273. long my_getgrnam(const char *name)
  274. {
  275. struct group *mygroup;
  276.  
  277. int curIndex = 0;
  278. while((mygroup = &groupsInFile[curIndex])->gr_name != NULL) {
  279. if (strcmp(mygroup->gr_name, name) == 0)
  280. break;
  281. curIndex++;
  282. }
  283.  
  284. if (mygroup->gr_name==NULL)
  285. bb_error_msg_and_die("unknown group name: %s", name);
  286.  
  287. return (mygroup->gr_gid);
  288. }
  289.  
  290. unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
  291. {
  292. unsigned long r;
  293. char *p;
  294.  
  295. r = strtoul(s, &p, 10);
  296. if (*p || (s == p)) {
  297. r = my_getxxnam(s);
  298. }
  299.  
  300. return r;
  301. }
  302.  
  303. char * last_char_is(const char *s, int c)
  304. {
  305. char *sret = (char *)s;
  306. if (sret) {
  307. sret = strrchr(sret, c);
  308. if(sret != NULL && *(sret+1) != 0)
  309. sret = NULL;
  310. }
  311. return sret;
  312. }
  313.  
  314. void bb_xasprintf(char **string_ptr, const char *format, ...)
  315. {
  316. va_list p;
  317. int r;
  318.  
  319. va_start(p, format);
  320. r = vasprintf(string_ptr, format, p);
  321. va_end(p);
  322.  
  323. if (r < 0) {
  324. bb_perror_msg_and_die("bb_xasprintf");
  325. }
  326. }
  327.  
  328. char *concat_path_file(const char *path, const char *filename)
  329. {
  330. char *outbuf;
  331. char *lc;
  332.  
  333. if (!path)
  334. path = "";
  335. lc = last_char_is(path, '/');
  336. while (*filename == '/')
  337. filename++;
  338. bb_xasprintf(&outbuf, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
  339.  
  340. return outbuf;
  341. }
  342.  
  343. void bb_show_usage(void)
  344. {
  345. fprintf(stderr, "%s: [-d device_table] rootdir\n\n", bb_applet_name);
  346. fprintf(stderr, "Creates a batch of special files as specified in a device table.\n");
  347. fprintf(stderr, "Device table entries take the form of:\n");
  348. fprintf(stderr, "name type mode user group major minor start increment count\n\n");
  349. fprintf(stderr, "Where name is the file name, type can be one of:\n");
  350. fprintf(stderr, " f A regular file\n");
  351. fprintf(stderr, " d Directory\n");
  352. fprintf(stderr, " c Character special device file\n");
  353. fprintf(stderr, " b Block special device file\n");
  354. fprintf(stderr, " p Fifo (named pipe)\n");
  355. fprintf(stderr, "uid is the user id for the target file, gid is the group id for the\n");
  356. fprintf(stderr, "target file. The rest of the entries (major, minor, etc) apply to\n");
  357. fprintf(stderr, "to device special files. A '-' may be used for blank entries.\n\n");
  358. fprintf(stderr, "For example:\n");
  359. fprintf(stderr, "<name> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>\n");
  360. fprintf(stderr, "/dev d 755 0 0 - - - - -\n");
  361. fprintf(stderr, "/dev/console c 666 0 0 5 1 - - -\n");
  362. fprintf(stderr, "/dev/null c 666 0 0 1 3 0 0 -\n");
  363. fprintf(stderr, "/dev/zero c 666 0 0 1 5 0 0 -\n");
  364. fprintf(stderr, "/dev/hda b 640 0 0 3 0 0 0 -\n");
  365. fprintf(stderr, "/dev/hda b 640 0 0 3 1 1 1 15\n");
  366. fprintf(stderr, "/dev/rtp b 640 0 0 250 0 0 1 5\n");
  367. fprintf(stderr, "/dev/gps b 640 0 0 251 0 1 1 5\n");
  368. fprintf(stderr, "/dev/uio b 640 0 0 252 0 1 2 5\n");
  369. fprintf(stderr, "/dev/uio b 640 0 0 252 1 6 2 5\n\n");
  370. fprintf(stderr, "Will Produce:\n");
  371. fprintf(stderr, "/dev\n");
  372. fprintf(stderr, "/dev/console\n");
  373. fprintf(stderr, "/dev/null\n");
  374. fprintf(stderr, "/dev/zero\n");
  375. fprintf(stderr, "/dev/hda\n");
  376. fprintf(stderr, "/dev/hda[1-15] with minor numbers [1-15]\n");
  377. fprintf(stderr, "/dev/rtp[0-4] with minor numbers [0-4]\n");
  378. fprintf(stderr, "/dev/gps[1-5] with minor numbers [0-4]\n");
  379. fprintf(stderr, "/dev/uio[1-5] with minor numbers 0,2,4,6,8\n");
  380. fprintf(stderr, "/dev/uio[6-10] with minor numbers 1,3,5,7,9\n");
  381. exit(1);
  382. }
  383.  
  384. void loadUsersAndGroups(char *rootdir) {
  385. passwords = malloc(grow * sizeof(struct passwd));
  386.  
  387. int rootdirLen = strlen(rootdir);
  388.  
  389. FILE *passwd;
  390. char *passwdPath = "/etc/passwd";
  391. int passwdLen = strlen(passwdPath);
  392. char *passwdFile = malloc(rootdirLen + passwdLen);
  393. sprintf(passwdFile, "%s%s", rootdir, passwdPath);
  394. passwd = fopen(passwdFile, "r");
  395. struct passwd *curPass;
  396. while (curPass = fgetpwent(passwd)) {
  397. char *dest = (char *) malloc(100);
  398. passwords[nbUsers].pw_name = strcpy(dest, curPass->pw_name);
  399. passwords[nbUsers].pw_uid = curPass->pw_uid;
  400. passwords[nbUsers].pw_gid = curPass->pw_gid;
  401. nbUsers++;
  402.  
  403. if (nbUsers % grow == 0)
  404. passwords = realloc(passwords, (nbUsers + grow) * sizeof(struct passwd));
  405. }
  406. fclose(passwd);
  407.  
  408. groupsInFile = malloc(grow * sizeof(struct group));
  409. FILE *groups;
  410. char *groupPath = "/etc/group";
  411. int groupLen = strlen(groupPath);
  412. char *groupsFile = malloc(rootdirLen + groupLen);
  413. sprintf(groupsFile, "%s%s", rootdir, groupPath);
  414. groups = fopen(groupsFile, "r");
  415. struct group *curGroup;
  416. while (curGroup = fgetgrent(groups)) {
  417. char *dest = (char *) malloc(100);
  418. groupsInFile[nbGroups].gr_name = strcpy(dest, curGroup->gr_name);
  419. groupsInFile[nbGroups].gr_gid = curGroup->gr_gid;
  420. nbGroups++;
  421.  
  422. if (nbGroups % grow == 0)
  423. groupsInFile = realloc(groupsInFile, (nbGroups + grow) * sizeof(struct group)); }
  424. fclose(groups);
  425. }
  426.  
  427. int main(int argc, char **argv)
  428. {
  429. int opt;
  430. FILE *table = stdin;
  431. char *rootdir = NULL;
  432. char *line = NULL;
  433. int linenum = 0;
  434. int ret = EXIT_SUCCESS;
  435.  
  436. bb_applet_name = basename(argv[0]);
  437.  
  438. while ((opt = getopt(argc, argv, "d:")) != -1) {
  439. switch(opt) {
  440. case 'd':
  441. table = bb_xfopen((line=optarg), "r");
  442. break;
  443. default:
  444. bb_show_usage();
  445. }
  446. }
  447.  
  448. if (optind >= argc || (rootdir=argv[optind])==NULL) {
  449. bb_error_msg_and_die("root directory not speficied");
  450. }
  451.  
  452. if (chdir(rootdir) != 0) {
  453. bb_perror_msg_and_die("Couldnt chdir to %s", rootdir);
  454. }
  455.  
  456. umask(0);
  457.  
  458. printf("rootdir=%s\n", rootdir);
  459. if (line) {
  460. printf("table='%s'\n", line);
  461. } else {
  462. printf("table=<stdin>\n");
  463. }
  464.  
  465. //Load users & groups
  466. loadUsersAndGroups(rootdir);
  467.  
  468. while ((line = bb_get_chomped_line_from_file(table))) {
  469. char type;
  470. unsigned int mode = 0755;
  471. unsigned int major = 0;
  472. unsigned int minor = 0;
  473. unsigned int count = 0;
  474. unsigned int increment = 0;
  475. unsigned int start = 0;
  476. char name[4096];
  477. char user[41];
  478. char group[41];
  479. char *full_name;
  480. uid_t uid;
  481. gid_t gid;
  482.  
  483. linenum++;
  484.  
  485. if ((2 > sscanf(line, "%4095s %c %o %40s %40s %u %u %u %u %u", name,
  486. &type, &mode, user, group, &major,
  487. &minor, &start, &increment, &count)) ||
  488. ((major | minor | start | count | increment) > 0xfffff))
  489. {
  490. if (*line=='\0' || *line=='#' || isspace(*line))
  491. continue;
  492. bb_error_msg("line %d invalid: '%s'\n", linenum, line);
  493. ret = EXIT_FAILURE;
  494. continue;
  495. }
  496. if (name[0] == '#') {
  497. continue;
  498. }
  499. if (*group) {
  500. gid = get_ug_id(group, my_getgrnam);
  501. } else {
  502. gid = getgid();
  503. }
  504. if (*user) {
  505. uid = get_ug_id(user, my_getpwnam);
  506. } else {
  507. uid = getuid();
  508. }
  509. full_name = concat_path_file(rootdir, name);
  510.  
  511. if (type == 'd') {
  512. bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
  513. if (chown(full_name, uid, gid) == -1) {
  514. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  515. ret = EXIT_FAILURE;
  516. goto loop;
  517. }
  518. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  519. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  520. ret = EXIT_FAILURE;
  521. goto loop;
  522. }
  523. } else if (type == 'f') {
  524. struct stat st;
  525. if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
  526. bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
  527. ret = EXIT_FAILURE;
  528. goto loop;
  529. }
  530. if (chown(full_name, uid, gid) == -1) {
  531. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  532. ret = EXIT_FAILURE;
  533. goto loop;
  534. }
  535. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  536. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  537. ret = EXIT_FAILURE;
  538. goto loop;
  539. }
  540. } else
  541. {
  542. dev_t rdev;
  543.  
  544. if (type == 'p') {
  545. mode |= S_IFIFO;
  546. }
  547. else if (type == 'c') {
  548. mode |= S_IFCHR;
  549. }
  550. else if (type == 'b') {
  551. mode |= S_IFBLK;
  552. } else {
  553. bb_error_msg("line %d: Unsupported file type %c", linenum, type);
  554. ret = EXIT_FAILURE;
  555. goto loop;
  556. }
  557.  
  558. if (count > 0) {
  559. int i;
  560. char *full_name_inc;
  561.  
  562. full_name_inc = xmalloc(strlen(full_name) + 8);
  563. for (i = 0; i < count; i++) {
  564. sprintf(full_name_inc, "%s%d", full_name, start + i);
  565. rdev = makedev(major, minor + i * increment);
  566. if (mknod(full_name_inc, mode, rdev) == -1) {
  567. bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
  568. ret = EXIT_FAILURE;
  569. }
  570. else if (chown(full_name_inc, uid, gid) == -1) {
  571. bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
  572. ret = EXIT_FAILURE;
  573. }
  574. if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
  575. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
  576. ret = EXIT_FAILURE;
  577. }
  578. }
  579. free(full_name_inc);
  580. } else {
  581. rdev = makedev(major, minor);
  582. if (mknod(full_name, mode, rdev) == -1) {
  583. bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
  584. ret = EXIT_FAILURE;
  585. }
  586. else if (chown(full_name, uid, gid) == -1) {
  587. bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
  588. ret = EXIT_FAILURE;
  589. }
  590. if ((mode != -1) && (chmod(full_name, mode) < 0)){
  591. bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
  592. ret = EXIT_FAILURE;
  593. }
  594. }
  595. }
  596. loop:
  597. free(line);
  598. free(full_name);
  599. }
  600. fclose(table);
  601.  
  602. return ret;
  603. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement