1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of libpwmd.
8 Libpwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Libpwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
46 #ifdef HAVE_GETPWUID_R
47 char *_expand_homedir(char *str
, struct passwd
*pw
)
56 if (*p
!= '~' || *(p
+1) != '/')
57 return pwmd_strdup(p
);
62 pwbuf
= _getpwuid(&t
);
70 result
= pwmd_strdup_printf("%s/%s", pw
->pw_dir
, p
);
78 char *_getpwuid(struct passwd
*pwd
)
80 size_t size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
81 struct passwd
*result
;
88 buf
= pwmd_malloc(size
);
92 n
= getpwuid_r(getuid(), pwd
, buf
, size
, &result
);
108 char *_expand_homedir(char *str
, struct passwd
*pw
)
115 if (*p
!= '~' || *(p
+1) != '/')
116 return pwmd_strdup(p
);
119 pw
= getpwuid(getuid());
125 return pwmd_strdup_printf("%s/%s", pw
->pw_dir
, p
);
128 char *_getpwuid(struct passwd
*pwd
)
130 struct passwd
*pw
= getpwuid(getuid());
136 return pwmd_strdup("");
141 * Borrowed from libassuan.
143 char *_percent_escape(const char *atext
)
145 const unsigned char *s
;
152 len
= strlen(atext
) * 3 + 1;
153 buf
= (char *)pwmd_malloc(len
);
160 for (s
=(const unsigned char *)atext
; *s
; s
++) {
162 sprintf (p
, "%%%02X", *s
);
173 void update_pinentry_settings(pwm_t
*pwm
)
179 char *pwbuf
= _getpwuid(&pw
);
184 snprintf(buf
, sizeof(buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
186 if ((fp
= fopen(buf
, "r")) == NULL
) {
191 while ((p
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
192 char name
[32], val
[256];
194 if (sscanf(p
, " %32[a-zA-Z] = %256s", name
, val
) != 2)
197 if (strcasecmp(name
, "TTYNAME") == 0) {
198 pwmd_free(pwm
->pinentry_tty
);
199 pwm
->pinentry_tty
= pwmd_strdup(val
);
200 if (!pwm
->pinentry_tty
)
203 else if (strcasecmp(name
, "TTYTYPE") == 0) {
204 pwmd_free(pwm
->pinentry_term
);
205 pwm
->pinentry_term
= pwmd_strdup(val
);
206 if (!pwm
->pinentry_term
)
209 else if (strcasecmp(name
, "DISPLAY") == 0) {
210 pwmd_free(pwm
->pinentry_display
);
211 pwm
->pinentry_display
= pwmd_strdup(val
);
212 if (!pwm
->pinentry_display
)
215 else if (strcasecmp(name
, "PATH") == 0) {
216 pwmd_free(pwm
->pinentry_path
);
217 pwm
->pinentry_path
= _expand_homedir(val
, &pw
);
218 if (!pwm
->pinentry_path
)
221 else if (strcasecmp(name
, "LC_MESSAGES") == 0) {
222 pwmd_free(pwm
->pinentry_lcmessages
);
223 pwm
->pinentry_lcmessages
= pwmd_strdup(val
);
224 if (!pwm
->pinentry_lcmessages
)
227 else if (strcasecmp(name
, "LC_CTYPE") == 0) {
228 pwmd_free(pwm
->pinentry_lcctype
);
229 pwm
->pinentry_lcctype
= pwmd_strdup(val
);
230 if (!pwm
->pinentry_lcctype
)
240 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
241 * port specification.
243 gpg_error_t
parse_hostname_common(const char *str
, char **host
, int *port
)
249 /* IPv6 with optional port. */
258 /* Handle IPv6 without proper braces around the IP. */
259 for (x
= p
; *x
; x
++) {
269 len
= strlen(p
)-strlen(t
)+1;
270 *host
= pwmd_malloc(len
);
272 return gpg_error_from_errno(ENOMEM
);
274 snprintf(*host
, len
, "%s", p
);
281 *port
= strtol(t
, NULL
, 10);
286 while (*t
&& isdigit(*t
))
292 *host
= pwmd_strdup(str
);
294 return gpg_error_from_errno(ENOMEM
);