util  Diff

Differences From Artifact [d671829e71]:

To Artifact [f1f30ece5b]:


     7      7    *      does this by attempting to execute a sequence
     8      8    *      of binaries, and then writing the password
     9      9    *      to STDIN of the binary that succeeds.
    10     10    *  ? generates passwords
    11     11    *  → mkpw is unlikely to be portable to non-POSIX
    12     12    *    systems, but should run fine on Linux as well
    13     13    *    as BSDs with getrandom() support.
           14  + *  ! for getrandom() to work with the version of
           15  + *    libc on my android phone, the getrandom() call
           16  + *    had to be converted to use the syscall()
           17  + *    interface. this is unlikely to cause problems,
           18  + *    but should be kept in mind.
    14     19    */
    15     20   
    16     21   #include <unistd.h>
    17     22   #include <sys/random.h>
    18     23   #include <sys/syscall.h>
    19     24   #include <stddef.h>
    20     25   #include <stdint.h>
................................................................................
    43     48   
    44     49   typedef enum bad {
    45     50   	ok = 0,
    46     51   	fail = 1,
    47     52   	bad_user,
    48     53   	bad_option,
    49     54   	bad_syntax,
           55  +	bad_entropy,
           56  +	bad_copy,
    50     57   	bad_usage = 64, /* fleabsd idiom */
    51     58   } bad;
    52     59   
    53     60   typedef _Bool bool;
    54     61   typedef unsigned long long iaia_word_type;
    55     62   typedef bad iaia_error_type;
    56     63   enum /* iaia errors */ {
................................................................................
   195    202   
   196    203   	char buf[len+1];
   197    204   	/* *buf = 0; */
   198    205   
   199    206   	unsigned char noise[len];
   200    207   	for (size_t i = 0; i<q; ++i) {
   201    208   		unsigned char* cur = noise;
   202         -		syscall(SYS_getrandom, noise, len, 0);
          209  +		if(syscall(SYS_getrandom, noise, len, 0) == -1)
          210  +			return bad_entropy;
   203    211   		/* getrandom(noise, len, 0); // android doesnt like it */
   204    212   
   205    213   		for(char* ptr = buf; ptr < buf + len; ++ptr, ++cur) {
   206    214   			*ptr = tbl[*cur % chars]; /* such a waste of entropy… :( */
   207    215   		}
   208    216   
   209    217   		buf[len] = '\n';
................................................................................
   219    227   			if (pipe(fds) != 0) return 63;
   220    228   			if (!fork()) {
   221    229   				close(fds[1]);
   222    230   				dup2(fds[0], 0);
   223    231   				if (clipboard_env != null) {
   224    232   					execvp(clipboard_env, (char* const[]){
   225    233   							clipboard_env, clipboard_env_arg, null});
   226         -					return -1;
          234  +					return bad_copy;
   227    235   				} else for(char* const** cmd = cbd_cmds; *cmd != null; ++cmd) {
   228    236   					execvp((*cmd)[0], *cmd);
   229    237   				}
   230         -				return -1; /* exec failed */
          238  +				return bad_copy; /* exec failed */
   231    239   			} else {
   232    240   				close(fds[0]);
   233    241   				write(fds[1], buf, len);
   234    242   				write(fds[1], "\n", 1);
   235    243   				close(fds[1]);
   236    244   			}
   237    245   		}
   238    246   #	endif
   239    247   	
   240    248   	return ok;
   241    249   }