Annotation of www/porting.html, Revision 1.29
1.20 rohee 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1.1 marc 2: <html>
3: <head>
4: <meta http-equiv="Content-Type"
1.15 espie 5: content="text/html; charset=iso-8859-1">
1.1 marc 6: <meta name="resource-type"
1.15 espie 7: content="document">
1.1 marc 8: <meta name="description"
1.15 espie 9: CONTENT="How to make an OpenBSD port">
1.1 marc 10: <meta name="keywords"
1.15 espie 11: content="openbsd,ports">
1.1 marc 12: <meta name="distribution"
1.15 espie 13: content="global">
1.1 marc 14: <meta name="copyright"
1.20 rohee 15: content="This document copyright 1997-1999 by the OpenBSD project">
1.1 marc 16: <title>Building an OpenBSD port</title>
17: <link rev="made" HREF="mailto:www@openbsd.org">
18: </head>
19: <body text="#000000" bgcolor="#FFFFFF" link="#23238E">
1.6 pauls 20: <img height=30 width=141 src=images/smalltitle.gif alt="[OpenBSD]" >
1.1 marc 21:
1.20 rohee 22: <h2><font color="#e00000">Building an OpenBSD port</font></h2>
1.1 marc 23:
24: So you've just compiled your favorite software package on your
25: OpenBSD machine and you want to share your effort by turning
26: it into a standard port. What to do?
27: <p>
1.25 espie 28: The most important thing you can is to <strong>communicate</strong>.
29: Ask people on <a href="mailto:ports@openbsd.org">ports@openbsd.org</a>
30: if they are working on the same port. <em>Tell the original software
31: author about it</em>, including problems you may find. If licensing
32: information appears incorrect tell him. If you had to jump through
33: loops to make the port build, tell him what he can fix. If they are
34: only developping on Linux and feel like ignoring the rest of the Unix
35: world, try to make them change their view.
36: <p>
37: <strong>COMMUNICATION</strong> makes the difference between a successful
38: port and a port that will slowly be abandonned by everyone.
39: <p>
1.9 marc 40: First look at the porting information on this page. Then check
41: out the referenced documents, especially the OpenBSD porting
1.26 espie 42: <a href="checklist.html">checklist</a>.
1.1 marc 43: <p>
1.9 marc 44: Test, then re-test, and finally test again!
45: <p>
46: Submit the port. Create a gzipped tarball of the port directory.
47: You can then either place it on a public FTP or HTTP server, sending
1.20 rohee 48: its address to <a href="mailto:ports@openbsd.org">ports@openbsd.org</a>
1.9 marc 49: or send the port mime encoded to the same address. Pick whichever
50: method works best for you.
1.1 marc 51: <p>
1.20 rohee 52: <h3><font color="#0000e0">Available Porting Information</font></h3>
1.1 marc 53: <ul>
54: <li>The file <code>/usr/share/mk/bsd.port.mk</code>. This is the
55: system ports makefile included at the end of each individual
56: port makefile. Read the comments at the start of the makefile.
57: They do a good job of describing the available make options.
58: <li>The
1.13 art 59: <a href="http://www.netbsd.org/Documentation/software/packages.html">
60: NetBSD Package System</a> documentation. This document describes
61: NetBSD's version of the FreeBSD ports system and is quite helpful.
1.8 marc 62: <li>Section 19.2.5 of the
1.1 marc 63: <a href="http://www.freebsd.org/handbook/porting.html">FreeBSD
64: Handbook</a>. This is the FreeBSD porting bible.
1.26 espie 65: <li>OpenBSD porting <a href="checklist.html">checklist</a>.
1.29 ! espie 66: <li>Some differences from other BSD port systems, mostly a summary
! 67: of <a href="porting/diffs.html">infrastructure differences</a>.
! 68: <li><a href="audio-port.html">Porting audio applications to OpenBSD</a>.
1.1 marc 69: <li>The OpenBSD ports mailing list,
70: <a href="mailto:ports@openbsd.org">ports@OpenBSD.ORG</a>.
71: </ul>
1.20 rohee 72: <h3><font color="#0000e0">OpenBSD Porting Policy</font></h3>
1.1 marc 73: <ul>
1.24 rohee 74: <li>OpenBSD does NOT use <code>/usr/local/etc/rc.d</code>.<br>
1.7 espie 75: <code>/usr/local</code> is often shared between several machines
1.15 espie 76: thanks to NFS. For this reason, configuration files that are specific
77: to a given machine can't be stored under <code>/usr/local</code>,
78: <code>/etc</code> is the central repository for per machine
79: configuration files. Moreover, OpenBSD policy is to never update
80: files under <code>/etc</code> automatically. Ports that need some
81: specific boot setup should advise the administrator about what to do
82: instead of blindly installing files.
1.1 marc 83: <li>OpenBSD does NOT compress man pages.
84: <li>OpenBSD does NOT require <code>-lcrypt</code>.<br>
85: DES encryption is part of the standard <code>libc</code>.
1.10 espie 86: <li>OpenBSD is strongly security-oriented. You should read and understand
87: this page's <a href="#security">security section</a>.
1.24 rohee 88: <li>Be sure to add the <code>$OpenBSD$</code> CVS tag to
1.10 espie 89: the Makefile. If importing a port from another system be sure to
90: leave their tag in the Makefile, too. However, replace the FreeBSD
1.24 rohee 91: <code>$Id$</code> tag with the
92: <code>$FreeBSD$</code> tag.
1.10 espie 93: <li>The goal is to get all ported applications to support OpenBSD. To
94: achieve this goal you <strong>must</strong> feed any OpenBSD patches
95: back to the application maintainer.
96: </ul>
1.20 rohee 97: <a name=security></a>
98: <h3><font color="#0000e0">Security recommendations</font></h3>
1.10 espie 99: There are many security problems to worry about. If
1.2 marc 100: you are not absolutely sure of what you are doing please request
1.1 marc 101: help from the <a href="mailto:ports@openbsd.org">ports</a> mailing
102: list.
1.10 espie 103:
104: <ul>
1.20 rohee 105: <li>Do <em>not</em> use alpha or beta code when preparing a port. Use the
1.10 espie 106: latest regular or patch release.
107:
1.1 marc 108: <li>Any software to be installed as a server should be scanned
109: for buffer overflows, especially unsafe use of
110: <code>strcat/strcpy/strcmp/sprintf</code>. In general,
111: <code>sprintf</code> should be replaced with <code>snprintf</code>.
1.10 espie 112:
1.17 espie 113: <li>Never use filenames instead of true security. There are numerous race
1.10 espie 114: conditions where you don't have proper control. For instance, an attacker
115: who already has user privileges on your machines may replace files in
116: <code>/tmp</code> with symbolic links to more strategic files, such as
1.19 rohee 117: <code>/etc/master.passwd</code>.
1.16 espie 118:
119: <li>For instance, both <code>fopen</code> and <code>freopen</code>
120: <strong>create a new file or open an existing file</strong> for
121: writing. An attacker may create a symbolic link from
1.19 rohee 122: <code>/etc/master.passwd</code> to <code>/tmp/addrpool_dump</code>. The
1.16 espie 123: instant you open it, your password file is hosed. Yes, even with
124: an <code>unlink</code> right before. You only narrow the window
125: of opportunity. Use <code>open</code> with
1.22 rohee 126: <code>O_CREAT|O_EXCL</code> and <code>fdopen</code> instead.
1.15 espie 127:
1.16 espie 128: <li>Another very common problem is the <code>mktemp</code>
1.17 espie 129: function. Heed the warnings of the bsd linker about its uses.
1.15 espie 130: <strong>These must be fixed</strong>.
131: This is not quite as simple as <code>s/mktemp/mkstemp/g</code>. <br>
132: Refer to the <code>mktemp(3)</code> man page of OpenBSD current
133: for more indications.
134: Correct code using <code>mkstemp</code> includes the source to
135: <code>ed</code> or <code>mail</code>.
136: A rare instance of code that uses <code>mktemp</code> correctly
137: can be found in the <code>rsync</code> port.
1.10 espie 138:
139: <li>Just because you can read it doesn't mean you should. A well-known hole
140: of this nature was the <code>startx</code> problem. As a setuid program,
141: you could launch startx with any file as a script. If the file was not
142: a valid shell script, a syntax error message would follow, along with the
143: first line of the offending file, without any further permission check.
144: Pretty handy to grab the first line of a shadow passwd file, considering
1.12 deraadt 145: these often start with root entry. Do not open your file, and then do
146: an <code>fstat</code> on the open descriptor to check if you should have
1.15 espie 147: been able to open it (or the attacker will play with /dev/rst0 and rewind
1.12 deraadt 148: your tape) -- open it with the correct uid/gid/grouplist set.
1.10 espie 149:
150: <li>Don't use anything that forks a shell in setuid programs before dropping
1.15 espie 151: your privileges. This includes <code>popen</code> and
152: <code>system</code>.
1.10 espie 153: Use <code>fork</code>, <code>pipe</code> and <code>execve</code> instead.
154:
1.15 espie 155: <li>Pass open descriptors instead of filenames to other programs to
156: avoid race conditions. Even if a program does not accept file
157: descriptors, you can still use <code>/dev/fd/0</code>.
1.10 espie 158:
1.15 espie 159: <li>Access rights are attached to file descriptors. If you need setuid rights
1.10 espie 160: to open a file, open that file, then drop your privileges. You can still
161: access the open descriptor, but you have less to worry about. This is
162: double-edged: even after dropping privileges, you should still watch out
163: for those descriptors.
164:
165: <li>Avoid root setuid as much as you can. Basically, root can do anything,
1.15 espie 166: but root rights are very rarely needed, except maybe to create
167: socket ports with a number under 1024. It is arguably better to
168: keep that under <code>inetd</code>
1.10 espie 169: control and just add the relevant entries to <code>inetd.conf</code>.
170: You must know the appropriate magic for writing daemons to achieve that.
1.15 espie 171: It could be argued that you have no business writing setuid programs
172: if you don't know how to do that.
1.10 espie 173:
1.15 espie 174: <li>Use setgid instead of setuid. Apart from those specific files needed
175: by setgid programs, most files are not group-writable. Hence, a
176: security problem in a setgid program won't compromise your system as
177: much: with only group rights, the worst an intruder will be able to
178: do is hack a games score table or some such.
1.10 espie 179: See the <code>xkobo</code> port for an instance of such a change.
180:
1.15 espie 181: <li>Don't trust group-writable files. Even though they are audited,
182: setgid programs are not perceived as important potential security
183: holes. Hence stuff they can tamper with shouldn't be considered
184: sensitive information.
185:
186: <li>Don't trust your environment ! This involves simple things such as
187: your <code>PATH</code> (never use <code>system</code> with an
188: unqualified name, avoid <code>execvp</code>), but also more subtle
189: items such as your locale, timezone, termcap, and so on.
190: Be aware of transitivity: even though you're taking full precautions,
191: programs you call directly won't necessarily. <strong>Never</strong>
192: use <code>system</code> in privileged programs, build your command
193: line, a controlled environment, and call <code>execve</code> directly.
194: The <code>perlsec</code> man page is a good tutorial on such problems.
195:
196: <li>Never used setuid shell-scripts. These are inherently insecure.
197: Wrap them into some C code that ensures a proper environment.
198: On the other hand, OpenBSD features secure perl scripts.
199:
200: <li>Beware the dynamic loader. If you are running setuid, it will only
201: use trusted libraries that were scanned with ldconfig.
202: Setgid is not enough.
203:
204: <li>Dynamic libraries are tricky. C++ code sets a similar problem.
205: Basically, libraries may take some action based upon your environment
206: before your main program even gets to check its setuid status.
207: OpenBSD <code>issetugid</code> addresses this problem, from the
208: library writer point of view. Don't try to port libraries unless you
209: understand this issue thoroughly.
1.10 espie 210: </ul>
1.20 rohee 211: <h3><font color="#0000e0">Generic porting hints</font></h3>
1.10 espie 212: <ul>
213: <li><code>__OpenBSD__</code> should be used sparingly, if at all.
214: Constructs that look like
215: <pre>
216: #if defined(__NetBSD__) || defined(__FreeBSD__)
217: </pre>
218: are often inappropriate. Don't add blindly <code>__OpenBSD__</code>
219: to it. Instead, try to figure out what's going on, and what actual
220: feature is needed. Manual pages are often useful, as they include
221: historic comments, stating when a particular feature was incorporated
222: into BSD. Checking the numeric value of <code>BSD</code> against known
223: releases is often the right way. See
1.17 espie 224: <a href="ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/Packages.txt">the NetBSD package guide</a>
1.10 espie 225: for more information.
226: <li>Defining <code>BSD</code> is a bad idea. Try to include <code>sys/param.h</code>.
227: This not only defines <code>BSD</code>, it also gives it a proper value.
228: The right code fragment should look like:
229: <pre>
1.23 rohee 230: #if (defined(__unix__) || defined(unix)) && !defined(USG)
1.10 espie 231: #include <sys/param.h>
232: #endif
233: </pre>
234: <li>Test for features, not for specific OSes. In the long run, it is much
1.15 espie 235: better to test whether <code>tcgetattr</code> works than whether
236: you're running under BSD 4.3 or later, or SystemVR4. These kind of
237: tests just confuse the issue. The way to go about it is, for instance,
238: to test for one particular system, set up a slew of
239: <code>HAVE_TCGETATTR</code> defines, then proceed to the next system.
240: This technique separates features tests from specific OSes.
241: In a hurry, another porter can just add the whole set of
242: <code>-DHAVE_XXX</code> defines to the Makefile. One may also write
243: or add to a configure script to check for that feature and add it
244: automatically. As an example not to follow, check nethack 3.2.2
245: source: it assumes loads of things based on the system type. Most
246: of these assumptions are obsolete and no longer reflect reality:
247: POSIX functions are more useful than older BSD versus SystemV
248: differences, to the point that some traditional bsd functions are
1.10 espie 249: now only supported through compatibility libraries.
250:
251: <li>Avoid include files that include other includes that... First, because
1.15 espie 252: this is inefficient. Your project will end up including a file that
253: includes everything. Also, because it makes some problems difficult
254: to fix. It becomes harder to <em>not</em> include one particular file
255: at a given point.
1.10 espie 256:
257: <li>Avoid bizarre macro tricks. Undefining a macro that was defined by a
258: header file is a bad idea. Defining macros to get some specific behavior
1.15 espie 259: from an include file is also a bad idea: compilation modes should be
260: global. If you want POSIX behavior, say so, and
261: <code>#define POSIX_C_SOURCE</code>
1.10 espie 262: throughout the whole project, not when you feel like it.
263:
264: <li>Don't ever write system function prototypes. All modern systems,
265: OpenBSD included, have a standard location for these prototypes. Likely
1.15 espie 266: places include <code>unistd.h</code>, <code>fcntl.h</code> or
267: <code>termios.h</code>.
268: The man page frequently states where the prototype can be found.
269: You might need another slew of <code>HAVE_XXX</code> macros to
270: procure the right file. Don't worry about including the same file
271: twice, include files have guards that prevent all kinds of nastiness.<br>
1.10 espie 272: If some broken system needs you to write the prototype, don't impose
1.15 espie 273: on all other systems. Roll-your-own prototypes will break because they
274: may use types that are equivalent on your system, but are not portable
275: to other systems (<code>unsigned long</code> instead of
276: <code>size_t</code>), or get some <code>const</code> status wrong.
277: Also, some compilers, such as OpenBSD's own gcc,
1.10 espie 278: are able to do a better job with some very frequent functions such as
279: <code>strlen</code> if you include the right header file.
280:
281: <li>Don't use the name of a standard system function for anything else.
1.15 espie 282: If you want to write your own function, give it its own name, and
283: call that function everywhere. If you wish to revert to the
284: default system function, you just need to comment your code out,
285: and define your own name to the system function. Don't do it the
286: other way round. Code should look like this
1.10 espie 287: <pre>
288: /* prototype part */
289: #ifdef USE_OWN_GCVT
290: char *foo_gcvt(double number, size_t ndigit, char *buf);
291: #else
292: /* include correct file */
293: #include <stdlib.h>
294: /* use system function */
295: #define foo_gcvt gcvt
296: #endif
297:
298: /* definition part */
299: #ifdef USE_OWN_GCVT
300: char *foo_gcvt(double number, size_t ndigit, char *buf)
301: {
1.15 espie 302: /* proper definition */
303: }
1.10 espie 304:
305: /* typical use */
306: s = foo_gcvt(n, 15, b);
307: </pre>
1.1 marc 308: </ul>
1.20 rohee 309: <h3><font color="#0000e0">Other Helpful Hints</font></h3>
1.1 marc 310: <ul>
1.15 espie 311: <li>Recent versions of <code>bsd.port.mk</code> set the installers
312: path. Specifically, they set <code>/usr/bin</code> and
313: <code>/bin</code> to be searched <em>before</em>
314: <code>/usr/local/bin</code> and <code>/usr/X11R6/bin</code>.
1.19 rohee 315: <li>Do <em>NOT</em> generate shared libraries if
1.21 rohee 316: <code>${NO_SHARED_LIBS}</code> is defined (beware, it can be defined
317: only after inclusion of <code>bsd.port.mk</code>). If your port has
318: a GNU configure simply add the line
319: <code>CONFIGURE_ARGS += ${CONFIGURE_SHARED}</code> to the Makefile.
1.19 rohee 320: <li>If you rely on a feature that appeared in a recent version of
321: <code>bsd.port.mk</code> don't forget to add a line
322: <code>NEED_VERSION = x.yy</code> in the Makefile.
1.1 marc 323: <li>In OpenBSD <code>curses.h/libcurses/libtermlib</code> are the
324: ``new curses''. Change:<br>
1.15 espie 325: <code>ncurses.h ==> curses.h</code><br>
326: <code>-lncurses ==> -lcurses</code><br>
327: ``old (BSD) curses'' is available by defining
328: <code>_USE_OLD_CURSES_</code>
1.11 millert 329: before including <code>curses.h</code> (usually in a Makefile) and
1.15 espie 330: linking with <code>-locurses</code>.
1.11 millert 331: <li>In OpenBSD, terminal discipline has been upgraded from the older BSD
332: <code>sgtty</code> to the newer POSIX <code>tcgetattr</code> family.
1.15 espie 333: Avoid the older style in new code. Some code may define
334: <code>tcgetattr</code> to be a synonym for the older
335: <code>sgtty</code>, but this is at best a stopgap measure on OpenBSD.
336: The <code>xterm</code> source code is a very good example of
337: what not to do. Try to get your system functionality right: you
338: want a type that remembers the state of your terminal
339: (possible typedef), you want a function that extracts the current
340: state, and a function that sets the new state.
341: Functions that modify this state are more difficult, as they tend
342: to vary depending upon the system. Also, don't forget that you will
343: have to handle cases where you are not connected to a terminal,
344: and that you need to handle signals: not only termination, but
345: also background (<code>SIGTSTP</code>). You should always leave
346: the terminal in a sane state. Do your tests under an older shell,
347: such as sh, which does not reset the terminal in any way at
1.10 espie 348: program's termination.
1.15 espie 349: <li>The newer termcap/terminfo and curses, as included with OpenBSD,
350: include standard sequences for controlling color terminals. You
351: should use these if possible, reverting to standard ANSI color
352: sequences on other systems. However, some terminal descriptions
353: have not been updated yet, and you may need to be able to specify
354: these sequences manually. This is the way vim handles it. This is
355: not strictly necessary. Except for privileged programs, it is
356: generally possible to override a termcap definition through the
1.10 espie 357: <code>TERMCAP</code> variable and get it to work properly.
1.15 espie 358: <li>Signal semantics are tricky, and vary from one system to another.
359: Use <code>sigaction</code> to ensure a specific semantics, along
360: with other system calls referenced in the corresponding manpage.
1.1 marc 361: </ul>
362: <hr>
1.6 pauls 363: <a href="index.html"><img height=24 width=24 src=back.gif border=0 alt=OpenBSD></a>
1.20 rohee 364: <a href="mailto:www@openbsd.org">www@openbsd.org</a>
1.29 ! espie 365: <br><small>$OpenBSD: porting.html,v 1.28 2000/03/25 02:29:50 wvdputte Exp $</small>
1.1 marc 366: </body>
367: </html>