2 Copyright (C) 2006-2021 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
34 #include "pwmd-error.h"
36 #include "util-misc.h"
37 #include "util-string.h"
43 void log_write (const char *fmt
, ...);
46 valid_filename (const char *filename
)
50 if (!filename
|| !*filename
)
53 if (filename
[0] == '-' && filename
[1] == 0)
56 for (p
= filename
; *p
; p
++)
58 if (*p
== '/' || isspace (*p
))
65 /* If 'user' is null then lookup entry for 'uid'. Caller must free 'buf'. */
67 get_pwd_struct (const char *user
, uid_t uid
, struct passwd
*pw
, char **buf
,
70 struct passwd
*result
= NULL
;
71 #ifdef HAVE_GETPWNAM_R
75 len
= sysconf (_SC_GETPW_R_SIZE_MAX
);
84 err
= getpwnam_r (user
, pw
, *buf
, len
, &result
);
86 err
= getpwuid_r (uid
, pw
, *buf
, len
, &result
);
94 *rc
= gpg_error_from_errno (err
);
102 result
= getpwnam (user
);
104 result
= getpwuid (uid
);
107 *rc
= gpg_error_from_syserror ();
114 get_pwd_entry(uid_t uid
, int which
)
119 struct passwd
*result
= get_pwd_struct (NULL
, uid
, &pw
, &buf
, &rc
);
128 value
= str_dup (result
->pw_dir
);
130 value
= str_dup (result
->pw_name
);
138 get_username (uid_t uid
)
140 return get_pwd_entry (uid
, 1);
147 return home_directory
;
149 home_directory
= get_pwd_entry (getuid(), 0);
150 return home_directory
;
154 expand_homedir (char *str
)
160 char *dir
= get_pwd_entry(getuid(), 0);
166 s
= str_asprintf ("%s%s", dir
, p
);
171 return str_dup (str
);
174 /* The 'more' parameter lets the calling command process remaining
175 * non-option arguments. */
177 parse_options (char **line
, struct argv_s
* args
[], void *data
, int more
)
190 if (*p
== '-' && *(p
+ 1) == '-')
193 char opt
[255] = { 0 }, value
[255] =
199 if (!*p
|| *p
== ' ')
207 for (tp
= opt
, len
= 0; *p
; p
++, len
++)
210 return GPG_ERR_LINE_TOO_LONG
;
212 if (*p
== ' ' || *p
== '=')
222 for (tp
= value
, len
= 0; *p
; p
++, len
++)
225 return GPG_ERR_LINE_TOO_LONG
;
232 else if (*p
== ' ' && !inquote
)
250 for (int i
= 0; args
[i
]; i
++)
252 if (!strcmp (args
[i
]->opt
, opt
))
254 log_write2 ("param: name='%s' value='%s'", opt
, value
);
255 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
256 return GPG_ERR_SYNTAX
;
257 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
258 return GPG_ERR_SYNTAX
;
262 rc
= args
[i
]->func (data
, value
);
273 return GPG_ERR_UNKNOWN_OPTION
;
282 return GPG_ERR_SYNTAX
;
292 bin2hex (const unsigned char *data
, size_t len
)
294 size_t size
= len
* 2 + 1;
300 for (n
= 0; n
< len
; n
++)
304 sprintf (c
, "%02X", data
[n
]);
308 return str_dup (buf
);
312 html_escape (const char *line
, size_t len
, int space
)
314 struct string_s
*string
= string_new ("");
321 for (n
= 0; n
< len
; n
++)
326 string
= string_append (string
, "<");
329 string
= string_append (string
, ">");
334 string
= string_append (string
, "%20");
338 string
= string_append_printf (string
, "%c", line
[n
]);
344 string_free (string
, 0);
349 strip_texi_html (const char *line
)
354 struct string_s
*string
= string_new ("<html><body>");
359 /* The command usage text. */
360 p
= strchr (line
, '\n');
363 len
= strlen (line
)-strlen(p
);
364 tmp
= html_escape (line
, len
, 0);
365 string
= string_append_printf (string
, "<b>%s</b><p>", tmp
);
380 if (*p
== '@' && *(p
+ 1) != '@')
384 if (!strncasecmp (p
, "end ", 4))
387 if (!strncasecmp (p
, "table", 5))
389 string
= string_append (string
, "</p>");
391 else if (!strncasecmp (p
, "example", 7))
393 string
= string_append (string
, "</code>");
396 while (*p
++ != '\n');
400 else if (!strncasecmp (p
, "table ", 6))
402 while (*p
++ != '\n');
406 else if (!strncasecmp (p
, "example", 7))
408 string
= string_append (string
, "<code>");
409 while (*p
++ != '\n');
413 else if (!strncasecmp (p
, "sp ", 3))
415 while (*p
++ != '\n');
417 string
= string_append (string
, "<p>");
420 else if (!strncasecmp (p
, "item ", 5))
423 string
= string_append (string
, "<p><b>");
424 tmp
= strchr (p
, '\n');
425 len
= strlen (p
) - strlen (tmp
);
430 string
= string_append (string
, "<");
435 string
= string_append (string
, ">");
439 string
= string_append_printf (string
, "%c", *p
++);
442 string
= string_append (string
, "</b><br/>");
445 else if (!strncasecmp (p
, "pxref", 5)
446 || !strncasecmp (p
, "npxref", 6))
450 if (!strncasecmp (p
, "npxref", 6))
455 string
= string_append_printf (string
, "%s<a href=\"",
459 else if (!strncasecmp (p
, "xref", 4))
463 string
= string_append (string
, "See <a href=\"");
466 else if (!strncasecmp (p
, "url", 3))
470 string
= string_append (string
, "<a href=\"");
473 else if (!strncasecmp (p
, "key", 3))
478 else if (!strncasecmp (p
, "file", 4))
482 string
= string_append (string
, "<var>");
485 else if (!strncasecmp (p
, "var", 3))
489 string
= string_append (string
, "<var>");
492 else if (!strncasecmp (p
, "option", 6))
496 string
= string_append (string
, "<var>");
499 else if (!strncasecmp (p
, "code", 4))
503 string
= string_append (string
, "<b>");
506 else if (!strncasecmp (p
, "emph", 4))
510 string
= string_append (string
, "<em>");
513 else if (!strncasecmp (p
, "command", 7))
517 string
= string_append (string
, "<em>");
520 else if (!strncasecmp (p
, "cite", 4))
524 string
= string_append (string
, "<cite>");
527 else if (!strncasecmp (p
, "abbr", 4))
531 string
= string_append (string
, "<em>");
534 else if (!strncasecmp (p
, "*", 1))
536 string
= string_append (string
, "<br/>");
540 while (*p
&& *p
!= '{')
554 tmp
= strchr (p
, '}');
555 len
= strlen (p
) - strlen (tmp
);
567 tmp
= html_escape (buf
, strlen (buf
), 1);
568 string
= string_append_printf (string
, "%s\">%s</a>", tmp
, buf
);
572 string
= string_append (string
, buf
);
575 string
= string_append (string
, "</var>");
577 string
= string_append (string
, "</b>");
579 string
= string_append (string
, "</em>");
581 string
= string_append (string
, "</cite>");
584 else if (*p
== '@' && *(p
+ 1) == '@')
588 string
= string_append_printf (string
, "%c", *p
);
594 string
= string_append (string
, "</body></html>");
596 string_free (string
, 0);
601 strip_texi (const char *str
, int html
)
608 return strip_texi_html (str
);
614 for (p
= str
; *p
; p
++)
616 if (*p
== '@' && *(p
+ 1) != '@')
620 if (!strncasecmp (p
, "table", 5)
621 || !strncasecmp (p
, "end ", 4))
623 while (*p
++ != '\n');
627 else if (!strncasecmp (p
, "example", 7)
628 || !strncasecmp (p
, "end ", 4))
630 while (*p
++ != '\n');
634 else if (!strncasecmp (p
, "sp ", 3))
636 while (*p
++ != '\n');
640 else if (!strncasecmp (p
, "item", 4))
643 while (*p
&& *p
!= '\n')
649 else if (!strncasecmp (p
, "pxref", 5))
656 else if (!strncasecmp (p
, "xref", 4))
663 else if (!strncasecmp (p
, "*", 1))
670 while (*p
&& *p
!= '{')
684 while (*p
&& *p
!= '}')
694 else if (*p
== '@' && *(p
+ 1) == '@')
710 strip_texi_and_wrap (const char *str
, int html
)
712 char *tmp
= strip_texi (str
, html
);
713 char *help
= xcalloc (1, strlen (tmp
) * 2 + 1);
717 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++)
719 if (i
== 78 || *p
== '\n')
726 while (*(--t
) != ' ')
742 while (*p
!= '\n' && *p
== ' ')
758 free_key (void *data
)
764 create_thread (void *(*cb
) (void *), void *data
,
765 pthread_t
* tid
, int detached
)
770 pthread_attr_init (&attr
);
773 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
775 n
= pthread_create (tid
, &attr
, cb
, data
);
776 pthread_attr_destroy (&attr
);
777 return gpg_error_from_errno (n
);
781 release_mutex_cb (void *arg
)
783 pthread_mutex_t
*m
= (pthread_mutex_t
*) arg
;
789 close_fd_cb (void *arg
)
791 int fd
= *(int *)arg
;
798 get_checksum_memory (void *data
, size_t size
, unsigned char **r_crc
,
801 size_t len
= gcry_md_get_algo_dlen (GCRY_MD_CRC32
);
802 unsigned char *crc
= xmalloc (len
);
808 return GPG_ERR_ENOMEM
;
810 gcry_md_hash_buffer (GCRY_MD_CRC32
, crc
, data
, size
);
816 /* The advisory lock should be obtained before calling this function. */
818 get_checksum (const char *filename
, unsigned char **r_crc
, size_t * r_crclen
)
825 rc
= open_check_file (filename
, &fd
, &st
, 1);
829 pthread_cleanup_push (close_fd_cb
, &fd
);
830 buf
= xmalloc (st
.st_size
);
833 pthread_cleanup_push (xfree
, buf
);
835 size_t len
= read (fd
, buf
, st
.st_size
);
836 if (len
== st
.st_size
)
839 rc
= get_checksum_memory (buf
, st
.st_size
, r_crc
, r_crclen
);
844 rc
= GPG_ERR_TOO_SHORT
;
846 pthread_cleanup_pop (1);
851 pthread_cleanup_pop (1);
855 wchar_t *str_to_wchar (const char *str
)
862 memset (&ps
, 0, sizeof(mbstate_t));
863 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
867 wc
= xcalloc (len
+1, sizeof(wchar_t));
872 memset (&ps
, 0, sizeof(mbstate_t));
873 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
884 gnupg_escape (const char *str
)
889 char *buf
= xmalloc (strlen(str
)*4+1), *b
= buf
;
895 for (p
= str
; p
&& *p
; p
++)
921 /* From Beej's Guide to Network Programming. It's a good tutorial. */
923 get_in_addr (struct sockaddr
*sa
)
925 if (sa
->sa_family
== AF_INET
)
926 return &(((struct sockaddr_in
*) sa
)->sin_addr
);
928 return &(((struct sockaddr_in6
*) sa
)->sin6_addr
);
932 open_check_file (const char *filename
, int *r_fd
, struct stat
*r_st
, int reg
)
938 memset (r_st
, 9, sizeof (struct stat
));
944 if (lstat (filename
, &st
) == -1)
945 return gpg_err_code (gpg_error_from_syserror ());
947 if (!S_ISREG (st
.st_mode
))
948 return GPG_ERR_ENODEV
;
951 memcpy (r_st
, &st
, sizeof (struct stat
));
956 fd
= open (filename
, O_RDONLY
);
958 return gpg_err_code (gpg_error_from_syserror ());
960 if (r_st
&& fstat (fd
, r_st
) == -1)
962 rc
= gpg_err_code (gpg_error_from_syserror ());