modpib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * modpib.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <ctype.h>
  59. #include <errno.h>
  60. #include <sys/stat.h>
  61. #include <netinet/in.h>
  62. #include <arpa/inet.h>
  63. /*====================================================================*
  64. * custom header files;
  65. *--------------------------------------------------------------------*/
  66. #include "../tools/getoptv.h"
  67. #include "../tools/number.h"
  68. #include "../tools/symbol.h"
  69. #include "../tools/flags.h"
  70. #include "../tools/error.h"
  71. #include "../tools/files.h"
  72. #include "../tools/chars.h"
  73. #include "../plc/plc.h"
  74. #include "../key/HPAVKey.h"
  75. #include "../key/keys.h"
  76. #include "../pib/pib.h"
  77. /*====================================================================*
  78. * custom source files;
  79. *--------------------------------------------------------------------*/
  80. #ifndef MAKEFILE
  81. #include "../tools/getoptv.c"
  82. #include "../tools/putoptv.c"
  83. #include "../tools/version.c"
  84. #include "../tools/checksum32.c"
  85. #include "../tools/fdchecksum32.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/hexencode.c"
  88. #include "../tools/hexdecode.c"
  89. #include "../tools/hexstring.c"
  90. #include "../tools/todigit.c"
  91. #include "../tools/memincr.c"
  92. #include "../tools/synonym.c"
  93. #include "../tools/error.c"
  94. #endif
  95. #ifndef MAKEFILE
  96. #include "../pib/pibfile.c"
  97. #include "../pib/pibfile1.c"
  98. #include "../pib/pibfile2.c"
  99. #include "../pib/pibpeek1.c"
  100. #include "../pib/pibpeek2.c"
  101. #endif
  102. #ifndef MAKEFILE
  103. #include "../key/HPAVKeyNID.c"
  104. #include "../key/SHA256Reset.c"
  105. #include "../key/SHA256Write.c"
  106. #include "../key/SHA256Block.c"
  107. #include "../key/SHA256Fetch.c"
  108. #include "../key/keys.c"
  109. #endif
  110. /*====================================================================*
  111. * constants;
  112. *--------------------------------------------------------------------*/
  113. #define MEMBERSHIP_OFFSET 0x00001E8C
  114. #define MEMBERSHIP_STATUS 0x00000000
  115. /*====================================================================*
  116. *
  117. * signed pibimage1 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  118. *
  119. * modify selected PIB header values and compute a new checksum;
  120. * this function assumes that the file is open and positioned to
  121. * the start of the parameter block;
  122. *
  123. *
  124. * Contributor(s):
  125. * Nathaniel Houghton
  126. * Charles Maier
  127. *
  128. *--------------------------------------------------------------------*/
  129. static signed pibimage1 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  130. {
  131. struct simple_pib simple_pib;
  132. memset (&simple_pib, 0, sizeof (simple_pib));
  133. if (read (file->file, &simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  134. {
  135. error (1, errno, FILE_CANTREAD, file->name);
  136. }
  137. if (_anyset (flags, PIB_MAC))
  138. {
  139. memcpy (simple_pib.MAC, sample_pib->MAC, sizeof (simple_pib.MAC));
  140. }
  141. if (_anyset (flags, PIB_MACINC))
  142. {
  143. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  144. }
  145. if (_anyset (flags, PIB_DAK))
  146. {
  147. memcpy (simple_pib.DAK, sample_pib->DAK, sizeof (simple_pib.DAK));
  148. }
  149. if (_anyset (flags, PIB_NMK))
  150. {
  151. memcpy (simple_pib.NMK, sample_pib->NMK, sizeof (simple_pib.NMK));
  152. }
  153. if (_anyset (flags, PIB_NETWORK))
  154. {
  155. memcpy (simple_pib.NET, sample_pib->NET, sizeof (simple_pib.NET));
  156. }
  157. if (_anyset (flags, PIB_MFGSTRING))
  158. {
  159. memcpy (simple_pib.MFG, sample_pib->MFG, sizeof (simple_pib.MFG));
  160. }
  161. if (_anyset (flags, PIB_USER))
  162. {
  163. memcpy (simple_pib.USR, sample_pib->USR, sizeof (simple_pib.USR));
  164. }
  165. if (_anyset (flags, PIB_CCO_MODE))
  166. {
  167. simple_pib.CCoSelection = sample_pib->CCoSelection;
  168. }
  169. if (_anyset (flags, PIB_NMK | PIB_NID))
  170. {
  171. #if 0
  172. /*
  173. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  174. * step because it prevents false network association;
  175. */
  176. if (BE16TOH (simple_pib.PIBVERSION) > 0x0200)
  177. {
  178. uint32_t membership = MEMBERSHIP_STATUS;
  179. if (lseek (file->file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  180. {
  181. error (1, errno, FILE_CANTHOME, file->name);
  182. }
  183. if (write (file->file, &membership, sizeof (membership)) != sizeof (membership))
  184. {
  185. error (1, errno, FILE_CANTSAVE, file->name);
  186. }
  187. }
  188. #endif
  189. if (_allclr (flags, PIB_NID))
  190. {
  191. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN-1] >> 4;
  192. }
  193. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level & 1);
  194. }
  195. if (lseek (file->file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  196. {
  197. error (1, errno, FILE_CANTHOME, file->name);
  198. }
  199. if (write (file->file, &simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  200. {
  201. error (1, errno, FILE_CANTSAVE, file->name);
  202. }
  203. if (lseek (file->file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  204. {
  205. error (1, errno, FILE_CANTHOME, file->name);
  206. }
  207. simple_pib.CHECKSUM = fdchecksum32 (file->file, LE16TOH (simple_pib.PIBLENGTH), simple_pib.CHECKSUM);
  208. if (lseek (file->file, (off_t)(0) - LE16TOH (simple_pib.PIBLENGTH), SEEK_CUR) == -1)
  209. {
  210. error (1, errno, FILE_CANTHOME, file->name);
  211. }
  212. if (write (file->file, &simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  213. {
  214. error (1, errno, FILE_CANTSAVE, file->name);
  215. }
  216. if (_anyset (flags, PIB_VERBOSE))
  217. {
  218. pibpeek1 (&simple_pib);
  219. }
  220. return (0);
  221. }
  222. /*====================================================================*
  223. *
  224. * signed pibimage2 (struct _file_ * file, simple_pib *sample_pib, signed level, flag_t flags);
  225. *
  226. * modify selected PIB header values but do not compute a checksum;
  227. *
  228. *
  229. * Contributor(s):
  230. * Charles Maier
  231. *
  232. *--------------------------------------------------------------------*/
  233. static signed pibimage2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  234. {
  235. struct simple_pib simple_pib;
  236. memset (&simple_pib, 0, sizeof (simple_pib));
  237. if (read (file->file, &simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  238. {
  239. error (1, errno, FILE_CANTREAD, file->name);
  240. }
  241. if (_anyset (flags, PIB_MAC))
  242. {
  243. memcpy (simple_pib.MAC, sample_pib->MAC, sizeof (simple_pib.MAC));
  244. }
  245. if (_anyset (flags, PIB_MACINC))
  246. {
  247. memincr (simple_pib.MAC, sizeof (simple_pib.MAC));
  248. }
  249. if (_anyset (flags, PIB_DAK))
  250. {
  251. memcpy (simple_pib.DAK, sample_pib->DAK, sizeof (simple_pib.DAK));
  252. }
  253. if (_anyset (flags, PIB_NMK))
  254. {
  255. memcpy (simple_pib.NMK, sample_pib->NMK, sizeof (simple_pib.NMK));
  256. }
  257. if (_anyset (flags, PIB_NETWORK))
  258. {
  259. memcpy (simple_pib.NET, sample_pib->NET, sizeof (simple_pib.NET));
  260. }
  261. if (_anyset (flags, PIB_MFGSTRING))
  262. {
  263. memcpy (simple_pib.MFG, sample_pib->MFG, sizeof (simple_pib.MFG));
  264. }
  265. if (_anyset (flags, PIB_USER))
  266. {
  267. memcpy (simple_pib.USR, sample_pib->USR, sizeof (simple_pib.USR));
  268. }
  269. if (_anyset (flags, PIB_CCO_MODE))
  270. {
  271. simple_pib.CCoSelection = sample_pib->CCoSelection;
  272. }
  273. if (_anyset (flags, PIB_NMK | PIB_NID))
  274. {
  275. #if 0
  276. /*
  277. * clear the AVLNMembership bit whenever the NMK or NID change; this is an important
  278. * step because it prevents false network association;
  279. */
  280. if (BE16TOH (simple_pib.PIBVERSION) > 0x0200)
  281. {
  282. uint32_t membership = MEMBERSHIP_STATUS;
  283. if (lseek (file->file, MEMBERSHIP_OFFSET, SEEK_SET) != MEMBERSHIP_OFFSET)
  284. {
  285. error (1, errno, FILE_CANTHOME, file->name);
  286. }
  287. if (write (file->file, &membership, sizeof (membership)) != sizeof (membership))
  288. {
  289. error (1, errno, FILE_CANTSAVE, file->name);
  290. }
  291. }
  292. #endif
  293. if (_allclr (flags, PIB_NID))
  294. {
  295. level = simple_pib.PreferredNID [HPAVKEY_NID_LEN-1] >> 4;
  296. }
  297. HPAVKeyNID (simple_pib.PreferredNID, simple_pib.NMK, level & 1);
  298. }
  299. if (lseek (file->file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  300. {
  301. error (1, errno, FILE_CANTHOME, file->name);
  302. }
  303. if (write (file->file, &simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  304. {
  305. error (1, errno, FILE_CANTSAVE, file->name);
  306. }
  307. if (lseek (file->file, (off_t)(0) - sizeof (simple_pib), SEEK_CUR) == -1)
  308. {
  309. error (1, errno, FILE_CANTHOME, file->name);
  310. }
  311. if (_anyset (flags, PIB_VERBOSE))
  312. {
  313. pibpeek2 (&simple_pib);
  314. }
  315. return (0);
  316. }
  317. /*====================================================================*
  318. *
  319. * signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags);
  320. *
  321. *
  322. *
  323. * Contributor(s):
  324. * Charles Maier
  325. *
  326. *--------------------------------------------------------------------*/
  327. static signed pibchain2 (struct _file_ * file, simple_pib * sample_pib, signed level, flag_t flags)
  328. {
  329. unsigned module = 0;
  330. struct nvm_header2 nvm_header;
  331. uint32_t prev = ~0;
  332. uint32_t next = 0;
  333. if (lseek (file->file, 0, SEEK_SET))
  334. {
  335. error (1, errno, NVM_IMG_CHECKSUM, file->name, module);
  336. }
  337. do
  338. {
  339. if (read (file->file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  340. {
  341. error (1, errno, NVM_HDR_CANTREAD, file->name, module);
  342. }
  343. if (LE16TOH (nvm_header.MajorVersion) != 1)
  344. {
  345. error (1, errno, NVM_HDR_VERSION, file->name, module);
  346. }
  347. if (LE16TOH (nvm_header.MinorVersion) != 1)
  348. {
  349. error (1, errno, NVM_HDR_VERSION, file->name, module);
  350. }
  351. if (checksum32 (&nvm_header, sizeof (nvm_header), 0))
  352. {
  353. error (1, errno, NVM_HDR_CHECKSUM, file->name, module);
  354. }
  355. if (LE32TOH (nvm_header.PrevHeader) != prev)
  356. {
  357. error (1, errno, NVM_HDR_LINK, file->name, module);
  358. }
  359. if (LE32TOH (nvm_header.ImageType) == NVM_IMAGE_PIB)
  360. {
  361. pibimage2 (file, sample_pib, level, flags);
  362. nvm_header.ImageChecksum = fdchecksum32 (file->file, LE32TOH (nvm_header.ImageLength), 0);
  363. if (lseek (file->file, (off_t)(0) - LE32TOH (nvm_header.ImageLength), SEEK_CUR) == -1)
  364. {
  365. error (1, errno, FILE_CANTHOME, file->name);
  366. }
  367. nvm_header.HeaderChecksum = checksum32 (&nvm_header, sizeof (nvm_header), nvm_header.HeaderChecksum);
  368. if (lseek (file->file, (off_t)(0) - sizeof (nvm_header), SEEK_CUR) == -1)
  369. {
  370. error (1, errno, FILE_CANTHOME, file->name);
  371. }
  372. if (write (file->file, &nvm_header, sizeof (nvm_header)) != sizeof (nvm_header))
  373. {
  374. error (1, errno, FILE_CANTSAVE, file->name);
  375. }
  376. if (lseek (file->file, (off_t)(0) - sizeof (nvm_header), SEEK_CUR) == -1)
  377. {
  378. error (1, errno, FILE_CANTHOME, file->name);
  379. }
  380. break;
  381. }
  382. if (fdchecksum32 (file->file, LE32TOH (nvm_header.ImageLength), nvm_header.ImageChecksum))
  383. {
  384. error (1, errno, NVM_IMG_CHECKSUM, file->name, module);
  385. }
  386. prev = next;
  387. next = LE32TOH (nvm_header.NextHeader);
  388. module++;
  389. }
  390. while (~nvm_header.NextHeader);
  391. return (0);
  392. }
  393. /*====================================================================*
  394. *
  395. * signed function (char const * filename, struct simple_pib * simple_pib, unsigned level, flag_t flags);
  396. *
  397. *
  398. *
  399. * Contributor(s):
  400. * Charles Maier
  401. *
  402. *--------------------------------------------------------------------*/
  403. static signed function (char const * filename, struct simple_pib * sample_pib, unsigned level, flag_t flags)
  404. {
  405. uint32_t version;
  406. signed status;
  407. struct _file_ file;
  408. file.name = filename;
  409. if ((file.file = open (file.name, O_BINARY|O_RDWR, FILE_FILEMODE)) == -1)
  410. {
  411. if (_allclr (flags, PIB_SILENCE))
  412. {
  413. error (0, errno, FILE_CANTOPEN, file.name);
  414. }
  415. return (-1);
  416. }
  417. if (read (file.file, &version, sizeof (version)) != sizeof (version))
  418. {
  419. if (_allclr (flags, PIB_SILENCE))
  420. {
  421. error (0, errno, FILE_CANTREAD, file.name);
  422. }
  423. return (-1);
  424. }
  425. if (lseek (file.file, 0, SEEK_SET))
  426. {
  427. error (1, errno, FILE_CANTHOME, file.name);
  428. }
  429. if (LE32TOH (version) == 0x00010001)
  430. {
  431. status = pibchain2 (&file, sample_pib, level, flags);
  432. }
  433. else
  434. {
  435. status = pibimage1 (&file, sample_pib, level, flags);
  436. }
  437. close (file.file);
  438. return (status);
  439. }
  440. /*====================================================================*
  441. *
  442. * int main (int argc, char const * argv [])
  443. *
  444. *
  445. *--------------------------------------------------------------------*/
  446. int main (int argc, char const * argv [])
  447. {
  448. extern struct _term_ const daks [];
  449. extern struct _term_ const nmks [];
  450. static char const * optv [] =
  451. {
  452. "C:D:L:M:N:S:T:U:v",
  453. "file [file] [...]",
  454. "modify selected PIB parameters and update checksum",
  455. "C n\tCCo Selection is n",
  456. "D x\tDAK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
  457. "L n\tsecurity level is n",
  458. "M x\tMAC as xx:xx:xx:xx:xx:xx",
  459. "N x\tNMK as xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
  460. "S s\tMFG string is s",
  461. "T s\tNET string is s",
  462. "U s\tUSR string is s",
  463. "v\tverbose messages",
  464. (char const *) (0)
  465. };
  466. struct simple_pib sample_pib;
  467. signed level = 0;
  468. signed state = 0;
  469. flag_t flags = (flag_t)(0);
  470. signed c;
  471. optind = 1;
  472. memset (&sample_pib, 0, sizeof (sample_pib));
  473. while ((c = getoptv (argc, argv, optv)) != -1)
  474. {
  475. char const * sp;
  476. switch ((char) (c))
  477. {
  478. case 'C':
  479. _setbits (flags, PIB_CCO_MODE);
  480. sample_pib.CCoSelection = (uint8_t)(uintspec (optarg, 0, 4));
  481. break;
  482. case 'D':
  483. if (!hexencode (sample_pib.DAK, sizeof (sample_pib.DAK), synonym (optarg, daks, SIZEOF (daks))))
  484. {
  485. error (1, errno, PLC_BAD_DAK, optarg);
  486. }
  487. _setbits (flags, PIB_DAK);
  488. break;
  489. case 'L':
  490. level = (uint8_t)(uintspec (optarg, 0, 1));
  491. _setbits (flags, PIB_NID);
  492. break;
  493. case 'M':
  494. if (!strcmp (optarg, "auto"))
  495. {
  496. _setbits (flags, PIB_MACINC);
  497. break;
  498. }
  499. if (!strcmp (optarg, "next"))
  500. {
  501. _setbits (flags, PIB_MACINC);
  502. break;
  503. }
  504. if (!strcmp (optarg, "plus"))
  505. {
  506. _setbits (flags, PIB_MACINC);
  507. break;
  508. }
  509. if (!hexencode (sample_pib.MAC, sizeof (sample_pib.MAC), optarg))
  510. {
  511. error (1, errno, PLC_BAD_MAC, optarg);
  512. }
  513. _setbits (flags, PIB_MAC);
  514. break;
  515. case 'N':
  516. if (!hexencode (sample_pib.NMK, sizeof (sample_pib.NMK), synonym (optarg, nmks, SIZEOF (nmks))))
  517. {
  518. error (1, errno, PLC_BAD_NMK, optarg);
  519. }
  520. _setbits (flags, PIB_NMK);
  521. break;
  522. case 'S':
  523. for (sp = optarg; isprint (*sp); ++sp);
  524. if (*sp)
  525. {
  526. error (1, EINVAL, "Manufacturing string contains illegal characters");
  527. }
  528. if ((sp - optarg) > (signed)(sizeof (sample_pib.MFG) - 1))
  529. {
  530. error (1, 0, "Manufacturing string is at most %u chars", (unsigned)(sizeof (sample_pib.MFG) - 1));
  531. }
  532. memcpy (sample_pib.MFG, optarg, strlen (optarg));
  533. _setbits (flags, PIB_MFGSTRING);
  534. break;
  535. case 'T':
  536. for (sp = optarg; isprint (*sp); ++sp);
  537. if (*sp)
  538. {
  539. error (1, EINVAL, "NET string contains illegal characters");
  540. }
  541. if ((sp - optarg) > (signed)(sizeof (sample_pib.NET) - 1))
  542. {
  543. error (1, 0, "NET string is at most %u chars", (unsigned)(sizeof (sample_pib.NET) - 1));
  544. }
  545. memcpy (sample_pib.NET, optarg, strlen (optarg));
  546. _setbits (flags, PIB_NETWORK);
  547. break;
  548. case 'U':
  549. for (sp = optarg; isprint (*sp); ++sp);
  550. if (*sp)
  551. {
  552. error (1, EINVAL, "USR string contains illegal characters");
  553. }
  554. if ((sp - optarg) > (signed)(sizeof (sample_pib.USR) - 1))
  555. {
  556. error (1, 0, "USR string is at most %u chars", (unsigned)(sizeof (sample_pib.USR) - 1));
  557. }
  558. memcpy (sample_pib.USR, optarg, strlen (optarg));
  559. _setbits (flags, PIB_USER);
  560. break;
  561. case 'q':
  562. _setbits (flags, PIB_SILENCE);
  563. break;
  564. case 'v':
  565. _setbits (flags, PIB_VERBOSE);
  566. break;
  567. default:
  568. break;
  569. }
  570. }
  571. argc -= optind;
  572. argv += optind;
  573. while ((argc) && (* argv))
  574. {
  575. if (function (* argv, &sample_pib, level, flags))
  576. {
  577. state = 1;
  578. }
  579. argc--;
  580. argv++;
  581. }
  582. exit (state);
  583. }