ident: loosen getpwuid error in non-strict mode
[git/git.git] / ident.c
1 /*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
9
10 static struct strbuf git_default_name = STRBUF_INIT;
11 static struct strbuf git_default_email = STRBUF_INIT;
12 static struct strbuf git_default_date = STRBUF_INIT;
13 static int default_email_is_bogus;
14 static int default_name_is_bogus;
15
16 #define IDENT_NAME_GIVEN 01
17 #define IDENT_MAIL_GIVEN 02
18 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
19 static int committer_ident_explicitly_given;
20 static int author_ident_explicitly_given;
21
22 #ifdef NO_GECOS_IN_PWENT
23 #define get_gecos(ignored) "&"
24 #else
25 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
26 #endif
27
28 static struct passwd *xgetpwuid_self(int *is_bogus)
29 {
30 struct passwd *pw;
31
32 errno = 0;
33 pw = getpwuid(getuid());
34 if (!pw) {
35 static struct passwd fallback;
36 fallback.pw_name = "unknown";
37 #ifndef NO_GECOS_IN_PWENT
38 fallback.pw_gecos = "Unknown";
39 #endif
40 pw = &fallback;
41 if (is_bogus)
42 *is_bogus = 1;
43 }
44 return pw;
45 }
46
47 static void copy_gecos(const struct passwd *w, struct strbuf *name)
48 {
49 char *src;
50
51 /* Traditionally GECOS field had office phone numbers etc, separated
52 * with commas. Also & stands for capitalized form of the login name.
53 */
54
55 for (src = get_gecos(w); *src && *src != ','; src++) {
56 int ch = *src;
57 if (ch != '&')
58 strbuf_addch(name, ch);
59 else {
60 /* Sorry, Mr. McDonald... */
61 strbuf_addch(name, toupper(*w->pw_name));
62 strbuf_addstr(name, w->pw_name + 1);
63 }
64 }
65 }
66
67 static int add_mailname_host(struct strbuf *buf)
68 {
69 FILE *mailname;
70 struct strbuf mailnamebuf = STRBUF_INIT;
71
72 mailname = fopen("/etc/mailname", "r");
73 if (!mailname) {
74 if (errno != ENOENT)
75 warning("cannot open /etc/mailname: %s",
76 strerror(errno));
77 return -1;
78 }
79 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
80 if (ferror(mailname))
81 warning("cannot read /etc/mailname: %s",
82 strerror(errno));
83 strbuf_release(&mailnamebuf);
84 fclose(mailname);
85 return -1;
86 }
87 /* success! */
88 strbuf_addbuf(buf, &mailnamebuf);
89 strbuf_release(&mailnamebuf);
90 fclose(mailname);
91 return 0;
92 }
93
94 static void add_domainname(struct strbuf *out, int *is_bogus)
95 {
96 char buf[1024];
97 struct hostent *he;
98
99 if (gethostname(buf, sizeof(buf))) {
100 warning("cannot get host name: %s", strerror(errno));
101 strbuf_addstr(out, "(none)");
102 *is_bogus = 1;
103 return;
104 }
105 if (strchr(buf, '.'))
106 strbuf_addstr(out, buf);
107 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
108 strbuf_addstr(out, he->h_name);
109 else {
110 strbuf_addf(out, "%s.(none)", buf);
111 *is_bogus = 1;
112 }
113 }
114
115 static void copy_email(const struct passwd *pw, struct strbuf *email,
116 int *is_bogus)
117 {
118 /*
119 * Make up a fake email address
120 * (name + '@' + hostname [+ '.' + domainname])
121 */
122 strbuf_addstr(email, pw->pw_name);
123 strbuf_addch(email, '@');
124
125 if (!add_mailname_host(email))
126 return; /* read from "/etc/mailname" (Debian) */
127 add_domainname(email, is_bogus);
128 }
129
130 const char *ident_default_name(void)
131 {
132 if (!git_default_name.len) {
133 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
134 strbuf_trim(&git_default_name);
135 }
136 return git_default_name.buf;
137 }
138
139 const char *ident_default_email(void)
140 {
141 if (!git_default_email.len) {
142 const char *email = getenv("EMAIL");
143
144 if (email && email[0]) {
145 strbuf_addstr(&git_default_email, email);
146 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
147 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
148 } else
149 copy_email(xgetpwuid_self(&default_email_is_bogus),
150 &git_default_email, &default_email_is_bogus);
151 strbuf_trim(&git_default_email);
152 }
153 return git_default_email.buf;
154 }
155
156 static const char *ident_default_date(void)
157 {
158 if (!git_default_date.len)
159 datestamp(&git_default_date);
160 return git_default_date.buf;
161 }
162
163 static int crud(unsigned char c)
164 {
165 return c <= 32 ||
166 c == '.' ||
167 c == ',' ||
168 c == ':' ||
169 c == ';' ||
170 c == '<' ||
171 c == '>' ||
172 c == '"' ||
173 c == '\\' ||
174 c == '\'';
175 }
176
177 /*
178 * Copy over a string to the destination, but avoid special
179 * characters ('\n', '<' and '>') and remove crud at the end
180 */
181 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
182 {
183 size_t i, len;
184 unsigned char c;
185
186 /* Remove crud from the beginning.. */
187 while ((c = *src) != 0) {
188 if (!crud(c))
189 break;
190 src++;
191 }
192
193 /* Remove crud from the end.. */
194 len = strlen(src);
195 while (len > 0) {
196 c = src[len-1];
197 if (!crud(c))
198 break;
199 --len;
200 }
201
202 /*
203 * Copy the rest to the buffer, but avoid the special
204 * characters '\n' '<' and '>' that act as delimiters on
205 * an identification line. We can only remove crud, never add it,
206 * so 'len' is our maximum.
207 */
208 strbuf_grow(sb, len);
209 for (i = 0; i < len; i++) {
210 c = *src++;
211 switch (c) {
212 case '\n': case '<': case '>':
213 continue;
214 }
215 sb->buf[sb->len++] = c;
216 }
217 sb->buf[sb->len] = '\0';
218 }
219
220 /*
221 * Reverse of fmt_ident(); given an ident line, split the fields
222 * to allow the caller to parse it.
223 * Signal a success by returning 0, but date/tz fields of the result
224 * can still be NULL if the input line only has the name/email part
225 * (e.g. reading from a reflog entry).
226 */
227 int split_ident_line(struct ident_split *split, const char *line, int len)
228 {
229 const char *cp;
230 size_t span;
231 int status = -1;
232
233 memset(split, 0, sizeof(*split));
234
235 split->name_begin = line;
236 for (cp = line; *cp && cp < line + len; cp++)
237 if (*cp == '<') {
238 split->mail_begin = cp + 1;
239 break;
240 }
241 if (!split->mail_begin)
242 return status;
243
244 for (cp = split->mail_begin - 2; line <= cp; cp--)
245 if (!isspace(*cp)) {
246 split->name_end = cp + 1;
247 break;
248 }
249 if (!split->name_end) {
250 /* no human readable name */
251 split->name_end = split->name_begin;
252 }
253
254 for (cp = split->mail_begin; cp < line + len; cp++)
255 if (*cp == '>') {
256 split->mail_end = cp;
257 break;
258 }
259 if (!split->mail_end)
260 return status;
261
262 /*
263 * Look from the end-of-line to find the trailing ">" of the mail
264 * address, even though we should already know it as split->mail_end.
265 * This can help in cases of broken idents with an extra ">" somewhere
266 * in the email address. Note that we are assuming the timestamp will
267 * never have a ">" in it.
268 *
269 * Note that we will always find some ">" before going off the front of
270 * the string, because will always hit the split->mail_end closing
271 * bracket.
272 */
273 for (cp = line + len - 1; *cp != '>'; cp--)
274 ;
275
276 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
277 ;
278 if (line + len <= cp)
279 goto person_only;
280 split->date_begin = cp;
281 span = strspn(cp, "0123456789");
282 if (!span)
283 goto person_only;
284 split->date_end = split->date_begin + span;
285 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
286 ;
287 if (line + len <= cp || (*cp != '+' && *cp != '-'))
288 goto person_only;
289 split->tz_begin = cp;
290 span = strspn(cp + 1, "0123456789");
291 if (!span)
292 goto person_only;
293 split->tz_end = split->tz_begin + 1 + span;
294 return 0;
295
296 person_only:
297 split->date_begin = NULL;
298 split->date_end = NULL;
299 split->tz_begin = NULL;
300 split->tz_end = NULL;
301 return 0;
302 }
303
304 static const char *env_hint =
305 "\n"
306 "*** Please tell me who you are.\n"
307 "\n"
308 "Run\n"
309 "\n"
310 " git config --global user.email \"you@example.com\"\n"
311 " git config --global user.name \"Your Name\"\n"
312 "\n"
313 "to set your account\'s default identity.\n"
314 "Omit --global to set the identity only in this repository.\n"
315 "\n";
316
317 const char *fmt_ident(const char *name, const char *email,
318 const char *date_str, int flag)
319 {
320 static struct strbuf ident = STRBUF_INIT;
321 int strict = (flag & IDENT_STRICT);
322 int want_date = !(flag & IDENT_NO_DATE);
323 int want_name = !(flag & IDENT_NO_NAME);
324
325 if (want_name && !name)
326 name = ident_default_name();
327 if (!email)
328 email = ident_default_email();
329
330 if (want_name && !*name) {
331 struct passwd *pw;
332
333 if (strict) {
334 if (name == git_default_name.buf)
335 fputs(env_hint, stderr);
336 die("empty ident name (for <%s>) not allowed", email);
337 }
338 pw = xgetpwuid_self(NULL);
339 name = pw->pw_name;
340 }
341
342 if (want_name && strict &&
343 name == git_default_name.buf && default_name_is_bogus) {
344 fputs(env_hint, stderr);
345 die("unable to auto-detect name (got '%s')", name);
346 }
347
348 if (strict && email == git_default_email.buf && default_email_is_bogus) {
349 fputs(env_hint, stderr);
350 die("unable to auto-detect email address (got '%s')", email);
351 }
352
353 strbuf_reset(&ident);
354 if (want_name) {
355 strbuf_addstr_without_crud(&ident, name);
356 strbuf_addstr(&ident, " <");
357 }
358 strbuf_addstr_without_crud(&ident, email);
359 if (want_name)
360 strbuf_addch(&ident, '>');
361 if (want_date) {
362 strbuf_addch(&ident, ' ');
363 if (date_str && date_str[0]) {
364 if (parse_date(date_str, &ident) < 0)
365 die("invalid date format: %s", date_str);
366 }
367 else
368 strbuf_addstr(&ident, ident_default_date());
369 }
370
371 return ident.buf;
372 }
373
374 const char *fmt_name(const char *name, const char *email)
375 {
376 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
377 }
378
379 const char *git_author_info(int flag)
380 {
381 if (getenv("GIT_AUTHOR_NAME"))
382 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
383 if (getenv("GIT_AUTHOR_EMAIL"))
384 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
385 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
386 getenv("GIT_AUTHOR_EMAIL"),
387 getenv("GIT_AUTHOR_DATE"),
388 flag);
389 }
390
391 const char *git_committer_info(int flag)
392 {
393 if (getenv("GIT_COMMITTER_NAME"))
394 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
395 if (getenv("GIT_COMMITTER_EMAIL"))
396 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
397 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
398 getenv("GIT_COMMITTER_EMAIL"),
399 getenv("GIT_COMMITTER_DATE"),
400 flag);
401 }
402
403 static int ident_is_sufficient(int user_ident_explicitly_given)
404 {
405 #ifndef WINDOWS
406 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
407 #else
408 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
409 #endif
410 }
411
412 int committer_ident_sufficiently_given(void)
413 {
414 return ident_is_sufficient(committer_ident_explicitly_given);
415 }
416
417 int author_ident_sufficiently_given(void)
418 {
419 return ident_is_sufficient(author_ident_explicitly_given);
420 }
421
422 int git_ident_config(const char *var, const char *value, void *data)
423 {
424 if (!strcmp(var, "user.name")) {
425 if (!value)
426 return config_error_nonbool(var);
427 strbuf_reset(&git_default_name);
428 strbuf_addstr(&git_default_name, value);
429 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
430 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
431 return 0;
432 }
433
434 if (!strcmp(var, "user.email")) {
435 if (!value)
436 return config_error_nonbool(var);
437 strbuf_reset(&git_default_email);
438 strbuf_addstr(&git_default_email, value);
439 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
440 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
441 return 0;
442 }
443
444 return 0;
445 }
446
447 static int buf_cmp(const char *a_begin, const char *a_end,
448 const char *b_begin, const char *b_end)
449 {
450 int a_len = a_end - a_begin;
451 int b_len = b_end - b_begin;
452 int min = a_len < b_len ? a_len : b_len;
453 int cmp;
454
455 cmp = memcmp(a_begin, b_begin, min);
456 if (cmp)
457 return cmp;
458
459 return a_len - b_len;
460 }
461
462 int ident_cmp(const struct ident_split *a,
463 const struct ident_split *b)
464 {
465 int cmp;
466
467 cmp = buf_cmp(a->mail_begin, a->mail_end,
468 b->mail_begin, b->mail_end);
469 if (cmp)
470 return cmp;
471
472 return buf_cmp(a->name_begin, a->name_end,
473 b->name_begin, b->name_end);
474 }